Skip to content

Commit

Permalink
BatchedMesh: add deleteInstance()
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanLane-Veerum committed Sep 19, 2024
1 parent 9cfaaa8 commit 3f5f814
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
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
31 changes: 21 additions & 10 deletions src/objects/BatchedMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class BatchedMesh extends Mesh {

// stores visible, active, and geometry id per object
this._drawInfo = [];
this._fragInfo = [];

// geometry information
this._drawRanges = [];
Expand Down Expand Up @@ -344,23 +345,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._fragInfo.length === 0 ) {

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

}

this._drawInfo.push( {

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

} );
let drawId = null;

// If at capacity, rewrite over an inactive block
if ( atCapacity ) {

drawId = this._fragInfo.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 +623,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 +633,12 @@ class BatchedMesh extends Mesh {
}

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

return this;

}
*/

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

0 comments on commit 3f5f814

Please sign in to comment.