Browse Source

BatchedMesh - better error handling 2 (#29790)

* BatchedMesh - better error handling 2

* Update BatchedMesh.js

Add `THREE` namespace to messages.

---------

Co-authored-by: Michael Herzog <michael.herzog@human-interactive.org>
Makio64 1 year ago
parent
commit
f421bffab5
1 changed files with 54 additions and 95 deletions
  1. 54 95
      src/objects/BatchedMesh.js

+ 54 - 95
src/objects/BatchedMesh.js

@@ -306,7 +306,7 @@ 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".' );
 
 		}
 
@@ -314,7 +314,7 @@ class BatchedMesh extends Mesh {
 
 			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.` );
 
 			}
 
@@ -322,7 +322,7 @@ class BatchedMesh extends Mesh {
 			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.' );
 
 			}
 
@@ -330,6 +330,29 @@ class BatchedMesh extends Mesh {
 
 	}
 
+	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;
@@ -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.' );
 
 		}
 
@@ -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.' );
 
 		}
 
@@ -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.' );
 
 		}
 
@@ -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.' );
 
 		}
 
@@ -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;
 
@@ -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;
 
@@ -861,37 +874,23 @@ 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;
 
@@ -899,34 +898,22 @@ class BatchedMesh extends Mesh {
 
 	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;
@@ -935,37 +922,18 @@ 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;
 
@@ -973,24 +941,15 @@ class BatchedMesh extends Mesh {
 
 	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;

粤ICP备19079148号