Skip to content
Merged
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
149 changes: 54 additions & 95 deletions src/objects/BatchedMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,30 +306,53 @@ class BatchedMesh extends Mesh {
const batchGeometry = this.geometry;
if ( Boolean( geometry.getIndex() ) !== Boolean( batchGeometry.getIndex() ) ) {

throw new Error( 'BatchedMesh: All geometries must consistently have "index".' );
throw new Error( 'THREE.BatchedMesh: All geometries must consistently have "index".' );

}

for ( const attributeName in batchGeometry.attributes ) {

if ( ! geometry.hasAttribute( attributeName ) ) {

throw new Error( `BatchedMesh: Added geometry missing "${ attributeName }". All geometries must have consistent attributes.` );
throw new Error( `THREE.BatchedMesh: Added geometry missing "${ attributeName }". All geometries must have consistent attributes.` );

}

const srcAttribute = geometry.getAttribute( attributeName );
const dstAttribute = batchGeometry.getAttribute( attributeName );
if ( srcAttribute.itemSize !== dstAttribute.itemSize || srcAttribute.normalized !== dstAttribute.normalized ) {

throw new Error( 'BatchedMesh: All attributes must have a consistent itemSize and normalized value.' );
throw new Error( 'THREE.BatchedMesh: All attributes must have a consistent itemSize and normalized value.' );

}

}

}

validateInstanceId( instanceId ) {

const instanceInfo = this._instanceInfo;
if ( instanceId < 0 || instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {

throw new Error( `THREE.BatchedMesh: Invalid instanceId ${instanceId}. Instance is either out of range or has been deleted.` );

}

}

validateGeometryId( geometryId ) {

const geometryInfoList = this._geometryInfo;
if ( geometryId < 0 || geometryId >= geometryInfoList.length || geometryInfoList[ geometryId ].active === false ) {

throw new Error( `THREE.BatchedMesh: Invalid geometryId ${geometryId}. Geometry is either out of range or has been deleted.` );

}

}


setCustomSort( func ) {

this.customSort = func;
Expand Down Expand Up @@ -394,7 +417,7 @@ class BatchedMesh extends Mesh {
// ensure we're not over geometry
if ( atCapacity && this._availableInstanceIds.length === 0 ) {

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

}

Expand Down Expand Up @@ -483,7 +506,7 @@ class BatchedMesh extends Mesh {
geometryInfo.vertexStart + geometryInfo.reservedVertexCount > this._maxVertexCount
) {

throw new Error( 'BatchedMesh: Reserved space request exceeds the maximum buffer size.' );
throw new Error( 'THREE.BatchedMesh: Reserved space request exceeds the maximum buffer size.' );

}

Expand Down Expand Up @@ -520,7 +543,7 @@ class BatchedMesh extends Mesh {

if ( geometryId >= this._geometryCount ) {

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

}

Expand All @@ -537,7 +560,7 @@ class BatchedMesh extends Mesh {
geometry.attributes.position.count > geometryInfo.reservedVertexCount
) {

throw new Error( 'BatchedMesh: Reserved space not large enough for provided geometry.' );
throw new Error( 'THREE.BatchedMesh: Reserved space not large enough for provided geometry.' );

}

Expand Down Expand Up @@ -652,14 +675,9 @@ class BatchedMesh extends Mesh {

deleteInstance( instanceId ) {

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

return this;
this.validateInstanceId( instanceId );

}

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

Expand Down Expand Up @@ -843,15 +861,10 @@ class BatchedMesh extends Mesh {

setMatrixAt( instanceId, matrix ) {

const instanceInfo = this._instanceInfo;
this.validateInstanceId( instanceId );

const matricesTexture = this._matricesTexture;
const matricesArray = this._matricesTexture.image.data;
if ( instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {

return this;

}

matrix.toArray( matricesArray, instanceId * 16 );
matricesTexture.needsUpdate = true;

Expand All @@ -861,72 +874,46 @@ class BatchedMesh extends Mesh {

getMatrixAt( instanceId, matrix ) {

const instanceInfo = this._instanceInfo;
const matricesArray = this._matricesTexture.image.data;
if ( instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {

return null;

}

return matrix.fromArray( matricesArray, instanceId * 16 );
this.validateInstanceId( instanceId );
return matrix.fromArray( this._matricesTexture.image.data, instanceId * 16 );

}

setColorAt( instanceId, color ) {

this.validateInstanceId( instanceId );

if ( this._colorsTexture === null ) {

this._initColorsTexture();

}

const colorsTexture = this._colorsTexture;
const colorsArray = this._colorsTexture.image.data;
const instanceInfo = this._instanceInfo;
if ( instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {

return this;

}

color.toArray( colorsArray, instanceId * 4 );
colorsTexture.needsUpdate = true;
color.toArray( this._colorsTexture.image.data, instanceId * 4 );
this._colorsTexture.needsUpdate = true;

return this;

}

getColorAt( instanceId, color ) {

const colorsArray = this._colorsTexture.image.data;
const instanceInfo = this._instanceInfo;
if ( instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {

return null;

}

return color.fromArray( colorsArray, instanceId * 4 );
this.validateInstanceId( instanceId );
return color.fromArray( this._colorsTexture.image.data, instanceId * 4 );

}

setVisibleAt( instanceId, value ) {

// if the geometry is out of range, not active, or visibility state
// does not change then return early
const instanceInfo = this._instanceInfo;
if (
instanceId >= instanceInfo.length ||
instanceInfo[ instanceId ].active === false ||
instanceInfo[ instanceId ].visible === value
) {
this.validateInstanceId( instanceId );

if ( this._instanceInfo[ instanceId ].visible === value ) {

return this;

}

instanceInfo[ instanceId ].visible = value;
this._instanceInfo[ instanceId ].visible = value;
this._visibilityChanged = true;

return this;
Expand All @@ -935,62 +922,34 @@ class BatchedMesh extends Mesh {

getVisibleAt( instanceId ) {

// return early if the geometry is out of range or not active
const instanceInfo = this._instanceInfo;
if ( instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {

return false;
this.validateInstanceId( instanceId );

}

return instanceInfo[ instanceId ].visible;
return this._instanceInfo[ instanceId ].visible;

}

setGeometryIdAt( instanceId, geometryId ) {

// return early if the geometry is out of range or not active
const instanceInfo = this._instanceInfo;
const geometryInfoList = this._geometryInfo;
if ( instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {

return null;

}
this.validateInstanceId( instanceId );
this.validateGeometryId( geometryId );

// check if the provided geometryId is within the valid range
if ( geometryId >= geometryInfoList.length || geometryInfoList[ geometryId ].active === false ) {

return null;

}

instanceInfo[ instanceId ].geometryIndex = geometryId;
this._instanceInfo[ instanceId ].geometryIndex = geometryId;

return this;

}

getGeometryIdAt( instanceId ) {

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

return - 1;

}
this.validateInstanceId( instanceId );

return instanceInfo[ instanceId ].geometryIndex;
return this._instanceInfo[ instanceId ].geometryIndex;

}

getGeometryRangeAt( geometryId, target = {} ) {

if ( geometryId < 0 || geometryId >= this._geometryCount ) {

return null;

}
this.validateGeometryId( geometryId );

const geometryInfo = this._geometryInfo[ geometryId ];
target.vertexStart = geometryInfo.vertexStart;
Expand Down