Skip to content

Commit f421bff

Browse files
Makio64Mugen87
andauthored
BatchedMesh - better error handling 2 (#29790)
* BatchedMesh - better error handling 2 * Update BatchedMesh.js Add `THREE` namespace to messages. --------- Co-authored-by: Michael Herzog <[email protected]>
1 parent 6933414 commit f421bff

File tree

1 file changed

+54
-95
lines changed

1 file changed

+54
-95
lines changed

src/objects/BatchedMesh.js

Lines changed: 54 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -306,30 +306,53 @@ class BatchedMesh extends Mesh {
306306
const batchGeometry = this.geometry;
307307
if ( Boolean( geometry.getIndex() ) !== Boolean( batchGeometry.getIndex() ) ) {
308308

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

311311
}
312312

313313
for ( const attributeName in batchGeometry.attributes ) {
314314

315315
if ( ! geometry.hasAttribute( attributeName ) ) {
316316

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

319319
}
320320

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

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

327327
}
328328

329329
}
330330

331331
}
332332

333+
validateInstanceId( instanceId ) {
334+
335+
const instanceInfo = this._instanceInfo;
336+
if ( instanceId < 0 || instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {
337+
338+
throw new Error( `THREE.BatchedMesh: Invalid instanceId ${instanceId}. Instance is either out of range or has been deleted.` );
339+
340+
}
341+
342+
}
343+
344+
validateGeometryId( geometryId ) {
345+
346+
const geometryInfoList = this._geometryInfo;
347+
if ( geometryId < 0 || geometryId >= geometryInfoList.length || geometryInfoList[ geometryId ].active === false ) {
348+
349+
throw new Error( `THREE.BatchedMesh: Invalid geometryId ${geometryId}. Geometry is either out of range or has been deleted.` );
350+
351+
}
352+
353+
}
354+
355+
333356
setCustomSort( func ) {
334357

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

397-
throw new Error( 'BatchedMesh: Maximum item count reached.' );
420+
throw new Error( 'THREE.BatchedMesh: Maximum item count reached.' );
398421

399422
}
400423

@@ -483,7 +506,7 @@ class BatchedMesh extends Mesh {
483506
geometryInfo.vertexStart + geometryInfo.reservedVertexCount > this._maxVertexCount
484507
) {
485508

486-
throw new Error( 'BatchedMesh: Reserved space request exceeds the maximum buffer size.' );
509+
throw new Error( 'THREE.BatchedMesh: Reserved space request exceeds the maximum buffer size.' );
487510

488511
}
489512

@@ -520,7 +543,7 @@ class BatchedMesh extends Mesh {
520543

521544
if ( geometryId >= this._geometryCount ) {
522545

523-
throw new Error( 'BatchedMesh: Maximum geometry count reached.' );
546+
throw new Error( 'THREE.BatchedMesh: Maximum geometry count reached.' );
524547

525548
}
526549

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

540-
throw new Error( 'BatchedMesh: Reserved space not large enough for provided geometry.' );
563+
throw new Error( 'THREE.BatchedMesh: Reserved space not large enough for provided geometry.' );
541564

542565
}
543566

@@ -652,14 +675,9 @@ class BatchedMesh extends Mesh {
652675

653676
deleteInstance( instanceId ) {
654677

655-
const instanceInfo = this._instanceInfo;
656-
if ( instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {
657-
658-
return this;
678+
this.validateInstanceId( instanceId );
659679

660-
}
661-
662-
instanceInfo[ instanceId ].active = false;
680+
this._instanceInfo[ instanceId ].active = false;
663681
this._availableInstanceIds.push( instanceId );
664682
this._visibilityChanged = true;
665683

@@ -843,15 +861,10 @@ class BatchedMesh extends Mesh {
843861

844862
setMatrixAt( instanceId, matrix ) {
845863

846-
const instanceInfo = this._instanceInfo;
864+
this.validateInstanceId( instanceId );
865+
847866
const matricesTexture = this._matricesTexture;
848867
const matricesArray = this._matricesTexture.image.data;
849-
if ( instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {
850-
851-
return this;
852-
853-
}
854-
855868
matrix.toArray( matricesArray, instanceId * 16 );
856869
matricesTexture.needsUpdate = true;
857870

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

862875
getMatrixAt( instanceId, matrix ) {
863876

864-
const instanceInfo = this._instanceInfo;
865-
const matricesArray = this._matricesTexture.image.data;
866-
if ( instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {
867-
868-
return null;
869-
870-
}
871-
872-
return matrix.fromArray( matricesArray, instanceId * 16 );
877+
this.validateInstanceId( instanceId );
878+
return matrix.fromArray( this._matricesTexture.image.data, instanceId * 16 );
873879

874880
}
875881

876882
setColorAt( instanceId, color ) {
877883

884+
this.validateInstanceId( instanceId );
885+
878886
if ( this._colorsTexture === null ) {
879887

880888
this._initColorsTexture();
881889

882890
}
883891

884-
const colorsTexture = this._colorsTexture;
885-
const colorsArray = this._colorsTexture.image.data;
886-
const instanceInfo = this._instanceInfo;
887-
if ( instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {
888-
889-
return this;
890-
891-
}
892-
893-
color.toArray( colorsArray, instanceId * 4 );
894-
colorsTexture.needsUpdate = true;
892+
color.toArray( this._colorsTexture.image.data, instanceId * 4 );
893+
this._colorsTexture.needsUpdate = true;
895894

896895
return this;
897896

898897
}
899898

900899
getColorAt( instanceId, color ) {
901900

902-
const colorsArray = this._colorsTexture.image.data;
903-
const instanceInfo = this._instanceInfo;
904-
if ( instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {
905-
906-
return null;
907-
908-
}
909-
910-
return color.fromArray( colorsArray, instanceId * 4 );
901+
this.validateInstanceId( instanceId );
902+
return color.fromArray( this._colorsTexture.image.data, instanceId * 4 );
911903

912904
}
913905

914906
setVisibleAt( instanceId, value ) {
915907

916-
// if the geometry is out of range, not active, or visibility state
917-
// does not change then return early
918-
const instanceInfo = this._instanceInfo;
919-
if (
920-
instanceId >= instanceInfo.length ||
921-
instanceInfo[ instanceId ].active === false ||
922-
instanceInfo[ instanceId ].visible === value
923-
) {
908+
this.validateInstanceId( instanceId );
909+
910+
if ( this._instanceInfo[ instanceId ].visible === value ) {
924911

925912
return this;
926913

927914
}
928915

929-
instanceInfo[ instanceId ].visible = value;
916+
this._instanceInfo[ instanceId ].visible = value;
930917
this._visibilityChanged = true;
931918

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

936923
getVisibleAt( instanceId ) {
937924

938-
// return early if the geometry is out of range or not active
939-
const instanceInfo = this._instanceInfo;
940-
if ( instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {
941-
942-
return false;
925+
this.validateInstanceId( instanceId );
943926

944-
}
945-
946-
return instanceInfo[ instanceId ].visible;
927+
return this._instanceInfo[ instanceId ].visible;
947928

948929
}
949930

950931
setGeometryIdAt( instanceId, geometryId ) {
951932

952-
// return early if the geometry is out of range or not active
953-
const instanceInfo = this._instanceInfo;
954-
const geometryInfoList = this._geometryInfo;
955-
if ( instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {
956-
957-
return null;
958-
959-
}
933+
this.validateInstanceId( instanceId );
934+
this.validateGeometryId( geometryId );
960935

961-
// check if the provided geometryId is within the valid range
962-
if ( geometryId >= geometryInfoList.length || geometryInfoList[ geometryId ].active === false ) {
963-
964-
return null;
965-
966-
}
967-
968-
instanceInfo[ instanceId ].geometryIndex = geometryId;
936+
this._instanceInfo[ instanceId ].geometryIndex = geometryId;
969937

970938
return this;
971939

972940
}
973941

974942
getGeometryIdAt( instanceId ) {
975943

976-
const instanceInfo = this._instanceInfo;
977-
if ( instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {
978-
979-
return - 1;
980-
981-
}
944+
this.validateInstanceId( instanceId );
982945

983-
return instanceInfo[ instanceId ].geometryIndex;
946+
return this._instanceInfo[ instanceId ].geometryIndex;
984947

985948
}
986949

987950
getGeometryRangeAt( geometryId, target = {} ) {
988951

989-
if ( geometryId < 0 || geometryId >= this._geometryCount ) {
990-
991-
return null;
992-
993-
}
952+
this.validateGeometryId( geometryId );
994953

995954
const geometryInfo = this._geometryInfo[ geometryId ];
996955
target.vertexStart = geometryInfo.vertexStart;

0 commit comments

Comments
 (0)