Pārlūkot izejas kodu

BatchedMesh: Fix `setInstanceCount()`. (#31458)

* fix BatchedMesh.setInstanceCount

* Cleanup
andreas-hilti 5 mēneši atpakaļ
vecāks
revīzija
6a0dab07a3

+ 1 - 1
src/objects/BatchedMesh.js

@@ -1251,7 +1251,7 @@ class BatchedMesh extends Mesh {
 		const availableInstanceIds = this._availableInstanceIds;
 		const instanceInfo = this._instanceInfo;
 		availableInstanceIds.sort( ascIdSort );
-		while ( availableInstanceIds[ availableInstanceIds.length - 1 ] === instanceInfo.length ) {
+		while ( availableInstanceIds[ availableInstanceIds.length - 1 ] === instanceInfo.length - 1 ) {
 
 			instanceInfo.pop();
 			availableInstanceIds.pop();

+ 40 - 0
test/unit/src/objects/BatchedMesh.tests.js

@@ -0,0 +1,40 @@
+/* global QUnit */
+
+import { BatchedMesh } from '../../../../src/objects/BatchedMesh.js';
+import { BoxGeometry } from '../../../../src/geometries/BoxGeometry.js';
+import { MeshBasicMaterial } from '../../../../src/materials/MeshBasicMaterial.js';
+
+export default QUnit.module( 'Objects', () => {
+
+	QUnit.module( 'BatchedMesh', () => {
+
+		// PUBLIC
+
+		QUnit.test( 'setInstanceCount', ( assert ) => {
+
+			const box = new BoxGeometry( 1, 1, 1 );
+			const material = new MeshBasicMaterial( { color: 0x00ff00 } );
+			
+			// initialize and add a geometry into the batched mesh
+			const batchedMesh = new BatchedMesh( 4, 5000, 10000, material );
+			const boxGeometryId = batchedMesh.addGeometry( box );
+			
+			// create instances of this geometry
+			let boxInstanceIds = [];
+			for (let i = 0; i < 4; i++){
+				boxInstanceIds.push( batchedMesh.addInstance( boxGeometryId ) );
+			}
+			
+			batchedMesh.deleteInstance( boxInstanceIds[2] );
+			batchedMesh.deleteInstance( boxInstanceIds[3] );
+
+			// shrink the instance count
+			batchedMesh.setInstanceCount(2);
+
+			assert.ok( batchedMesh.instanceCount === 2, 'instance count unequal 2' );
+
+		} );
+
+	} );
+
+} );

+ 1 - 0
test/unit/three.source.unit.js

@@ -215,6 +215,7 @@ import './src/math/interpolants/QuaternionLinearInterpolant.tests.js';
 
 //src/objects
 import './src/objects/Bone.tests.js';
+import './src/objects/BatchedMesh.tests.js';
 import './src/objects/Group.tests.js';
 import './src/objects/InstancedMesh.tests.js';
 import './src/objects/Line.tests.js';

粤ICP备19079148号