Răsfoiți Sursa

BufferGeometryUtils: Fix groups on indexed geometry.

Don McCurdy 7 ani în urmă
părinte
comite
eb1ff2ce4b

+ 3 - 2
docs/api/core/BufferGeometry.html

@@ -134,8 +134,9 @@
 
 			Each group is an object of the form:
 			<code>{ start: Integer, count: Integer, materialIndex: Integer }</code>
-			where start specifies the index of the first vertex in this draw call, count specifies
-			how many vertices are included, and materialIndex specifies the material array index to use.<br /><br />
+			where start specifies the first element in this draw call – the first vertex for non-indexed geometry,
+			otherwise the first triangle index. Count specifies how many vertices (or indices) are included, and
+			materialIndex specifies the material array index to use.<br /><br />
 
 			Use [page:.addGroup] to add groups, rather than modifying this array directly.
 		</div>

+ 11 - 2
examples/js/BufferGeometryUtils.js

@@ -245,8 +245,17 @@ THREE.BufferGeometryUtils = {
 
 			// create new group for this geometry
 
-			mergedGeometry.addGroup( offset, geometry.attributes.position.count, i );
-			offset += geometry.attributes.position.count;
+			if ( isIndexed ) {
+
+				mergedGeometry.addGroup( offset, geometry.index.count, i );
+				offset += geometry.index.count;
+
+			} else {
+
+				mergedGeometry.addGroup( offset, geometry.attributes.position.count, i );
+				offset += geometry.attributes.position.count;
+
+			}
 
 		}
 

+ 3 - 2
test/unit/example/BufferGeometryUtils.tests.js

@@ -64,7 +64,7 @@ export default QUnit.module( 'BufferGeometryUtils', () => {
 
     var geometry1 = new THREE.BufferGeometry();
     geometry1.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( [ 1, 2, 3 ] ), 1, false ) );
-    geometry1.setIndex( new THREE.BufferAttribute( new Uint16Array( [ 0, 1, 2 ] ), 1, false ) );
+    geometry1.setIndex( new THREE.BufferAttribute( new Uint16Array( [ 0, 1, 2, 2, 1, 0 ] ), 1, false ) );
 
     var geometry2 = new THREE.BufferGeometry();
     geometry2.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( [ 4, 5, 6 ] ), 1, false ) );
@@ -74,7 +74,8 @@ export default QUnit.module( 'BufferGeometryUtils', () => {
 
     assert.ok( mergedGeometry, 'merge succeeds' );
     assert.smartEqual( Array.from( mergedGeometry.attributes.position.array ), [ 1, 2, 3, 4, 5, 6 ], 'merges elements' );
-    assert.smartEqual( Array.from( mergedGeometry.index.array ), [ 0, 1, 2, 3, 4, 5 ], 'merges indices' );
+    assert.smartEqual( Array.from( mergedGeometry.index.array ), [ 0, 1, 2, 2, 1, 0, 3, 4, 5 ], 'merges indices' );
+    assert.smartEqual( [ { start: 0, count: 6, materialIndex: 0 }, { start: 6, count: 3, materialIndex: 1 } ], mergedGeometry.groups, 'creates groups' );
     assert.equal( mergedGeometry.attributes.position.itemSize, 1, 'retains .itemSize' );
 
   } );