diff --git a/docs/api/en/objects/BatchedMesh.html b/docs/api/en/objects/BatchedMesh.html index 1b59384361dc47..5450edc43c1c06 100644 --- a/docs/api/en/objects/BatchedMesh.html +++ b/docs/api/en/objects/BatchedMesh.html @@ -260,7 +260,13 @@

Adds the given geometry to the [name] and returns the associated geometry id referring to it to be used in other functions.

- +

+ [method:Integer deleteGeometry]( [param:Integer geometryId] ) +

+

+ [page:Integer geometryId]: The id of a geometry to remove from the [name] that was previously added via "addGeometry". Any instances referencing + this geometry will also be removed as a side effect. +

[method:Integer addInstance]( [param:Integer geometryId] )

diff --git a/src/objects/BatchedMesh.js b/src/objects/BatchedMesh.js index ee9ec865a8e00b..55dd22adf8ce34 100644 --- a/src/objects/BatchedMesh.js +++ b/src/objects/BatchedMesh.js @@ -145,8 +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 + // instance, geometry ids that have been set as inactive, and are available to be overwritten this._availableInstanceIds = []; + this._availableGeometryIds = []; // geometry information this._drawRanges = []; @@ -400,13 +401,6 @@ class BatchedMesh extends Mesh { this._validateGeometry( geometry ); - // ensure we're not over geometry - if ( this._drawInfo.length >= this._maxInstanceCount ) { - - throw new Error( 'BatchedMesh: Maximum item count reached.' ); - - } - // get the necessary range fo the geometry const reservedRange = { vertexStart: - 1, @@ -481,23 +475,40 @@ class BatchedMesh extends Mesh { } - // update id - const geometryId = this._geometryCount; - this._geometryCount ++; - // add the reserved range and draw range objects - reservedRanges.push( reservedRange ); - drawRanges.push( { + const drawRange = { start: hasIndex ? reservedRange.indexStart : reservedRange.vertexStart, - count: - 1 - } ); - bounds.push( { + count: - 1, + active: true, + }; + + const boundsInfo = { boxInitialized: false, box: new Box3(), sphereInitialized: false, sphere: new Sphere() - } ); + }; + + // update id + let geometryId; + if ( this._availableGeometryIds.length > 0 ) { + + geometryId = this._availableGeometryIds.pop(); + reservedRanges[ geometryId ] = reservedRange; + drawRanges[ geometryId ] = drawRange; + bounds[ geometryId ] = boundsInfo; + + + } else { + + geometryId = this._geometryCount; + this._geometryCount ++; + reservedRanges.push( reservedRange ); + drawRanges.push( drawRange ); + bounds.push( boundsInfo ); + + } // update the geometry this.setGeometryAt( geometryId, geometry ); @@ -617,13 +628,34 @@ class BatchedMesh extends Mesh { } - /* deleteGeometry( geometryId ) { - // TODO: delete geometry and associated instances + const drawRanges = this._drawRanges; + if ( geometryId >= drawRanges.length || drawRanges[ geometryId ].active === false ) { + + return this; + + } + + // delete any instances associated with this geometry + const drawInfo = this._drawInfo; + for ( let i = 0, l = drawInfo.length; i < l; i ++ ) { + + if ( drawInfo[ i ].geometryIndex === geometryId ) { + + this.deleteInstance( i ); + + } + + } + + drawRanges[ geometryId ].active = false; + this._availableGeometryIds.push( geometryId ); + this._visibilityChanged = true; + + return this; } - */ deleteInstance( instanceId ) {