Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BatchedMesh: add deleteInstance() #29449

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/api/en/objects/BatchedMesh.html
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ <h3>
Adds a new instance to the [name] using the geometry of the given geometryId and returns a new id referring to the new instance to be used
by other functions.
</p>
<h3>
[method:Integer deleteInstance]( [param:Integer instanceId] )
</h3>
<p>
[page:Integer instanceId]: The id of an instance to remove from the [name] that was previously added via "addInstance".
</p>
<p>
Removes an existing instance from the [name] using the given instanceId.
</p>

<h3>
[method:Integer setGeometryAt]( [param:Integer geometryId], [param:BufferGeometry geometry] )
Expand Down
33 changes: 23 additions & 10 deletions src/objects/BatchedMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ class BatchedMesh extends Mesh {
// stores visible, active, and geometry id per object
this._drawInfo = [];

// instance ids that have been set as inactive, and are available to be overwritten
this._availableInstanceIds = [];

// geometry information
this._drawRanges = [];
this._reservedRanges = [];
Expand Down Expand Up @@ -344,23 +347,36 @@ class BatchedMesh extends Mesh {

addInstance( geometryId ) {

const atCapacity = this._drawInfo.length >= this.maxInstanceCount;

// ensure we're not over geometry
if ( this._drawInfo.length >= this._maxInstanceCount ) {
if ( atCapacity && this._availableInstanceIds.length === 0 ) {

throw new Error( 'BatchedMesh: Maximum item count reached.' );

}

this._drawInfo.push( {

const instanceDrawInfo = {
visible: true,
active: true,
geometryIndex: geometryId,
};

} );
let drawId = null;

// Prioritize using previously freed instance ids
if ( this._availableInstanceIds.length > 0 ) {

drawId = this._availableInstanceIds.pop();
this._drawInfo[ drawId ] = instanceDrawInfo;

} else {

drawId = this._drawInfo.length;
this._drawInfo.push( instanceDrawInfo );

}

// initialize the matrix
const drawId = this._drawInfo.length - 1;
const matricesTexture = this._matricesTexture;
const matricesArray = matricesTexture.image.data;
_identityMatrix.toArray( matricesArray, drawId * 16 );
Expand Down Expand Up @@ -609,11 +625,8 @@ class BatchedMesh extends Mesh {
}
*/

/*
deleteInstance( instanceId ) {

// Note: User needs to call optimize() afterward to pack the data.

const drawInfo = this._drawInfo;
if ( instanceId >= drawInfo.length || drawInfo[ instanceId ].active === false ) {

Expand All @@ -622,12 +635,12 @@ class BatchedMesh extends Mesh {
}

drawInfo[ instanceId ].active = false;
this._availableInstanceIds.push( instanceId );
this._visibilityChanged = true;

return this;

}
*/

// get bounding box and compute it if it doesn't exist
getBoundingBoxAt( geometryId, target ) {
Expand Down