addInstance method

dynamic addInstance(
  1. dynamic geometryId
)
  • Adds a new instance to the batch using the geometry of the given ID and returns
    • a new id referring to the new instance to be used by other functions. *
      • @param {number} geometryId - The ID of a previously added geometry via {@link BatchedMesh#addGeometry}.
      • @return {number} The instance ID.

Implementation

addInstance( geometryId ) {
	final atCapacity = this._instanceInfo.length >= this.maxInstanceCount;

	// ensure we're not over geometry
	if ( atCapacity && this._availableInstanceIds.length == 0 ) {
		throw( 'THREE.BatchedMesh: Maximum item count reached.' );
	}

	final instanceInfo = {
		'visible': true,
		'active': true,
		'geometryIndex': geometryId,
	};

	int? drawId;

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

		drawId = this._availableInstanceIds.removeAt(0);
		this._instanceInfo[ drawId! ] = instanceInfo;
	}
    else {
		drawId = this._instanceInfo.length;
		this._instanceInfo.add( instanceInfo );
	}

	final matricesTexture = this.matricesTexture;
	_matrix.identity().copyIntoArray( matricesTexture!.image.data, drawId * 16 );
	matricesTexture.needsUpdate = true;

	final colorsTexture = this.colorsTexture;
	if ( colorsTexture != null) {
		_whiteColor.copyIntoArray( colorsTexture.image.data, drawId * 4 );
		colorsTexture.needsUpdate = true;
	}

	this._visibilityChanged = true;
	return drawId;
}