Browse Source

GLTFLoader: Sets combined indices in .loagGeometries()

Takahiro 7 years ago
parent
commit
0b49b7d387
1 changed files with 36 additions and 39 deletions
  1. 36 39
      examples/js/loaders/GLTFLoader.js

+ 36 - 39
examples/js/loaders/GLTFLoader.js

@@ -2059,31 +2059,7 @@ THREE.GLTFLoader = ( function () {
 
 		}
 
-		// .indicesArray isn't in glTF spec. See .loadGeometries()
-		if ( primitiveDef.indicesArray !== undefined && ! geometry.index ) {
-
-			var indices = [];
-			var offset = 0;
-
-			for ( var i = 0, il = primitiveDef.indicesArray.length; i < il; i ++ ) {
-
-				var accessor = accessors[ primitiveDef.indicesArray[ i ] ];
-
-				for ( var j = 0, jl = accessor.count; j < jl; j ++ ) {
-
-					indices.push( accessor.array[ j ] );
-
-				}
-
-				geometry.addGroup( offset, accessor.count, i );
-
-				offset += accessor.count;
-
-			}
-
-			geometry.setIndex( indices );
-
-		} else if ( primitiveDef.indices !== undefined && ! geometry.index ) {
+		if ( primitiveDef.indices !== undefined && ! geometry.index ) {
 
 			geometry.setIndex( accessors[ primitiveDef.indices ] );
 
@@ -2107,24 +2083,21 @@ THREE.GLTFLoader = ( function () {
 		var extensions = this.extensions;
 		var cache = this.primitiveCache;
 
-		if ( isMultiPassGeometry( primitives ) ) {
+		var isCombinable = isMultiPassGeometry( primitives );
+		var originalPrimitives;
+
+		if ( isCombinable ) {
+
+			originalPrimitives = primitives; // save original primitives and use later
 
 			// We builds a single BufferGeometry with .groups from multiple primitives
 			// because all primitives share the same attributes/morph/mode and have indices.
 
 			var primitive = Object.assign( {}, primitives[ 0 ] );
-			primitive.indicesArray = [];
-
-			// combines indices in addPrimitiveAttributes() later
-
-			for ( var i = 0, il = primitives.length; i < il; i ++ ) {
-
-				primitive.indicesArray[ i ] = primitives[ i ].indices;
-
-			}
-
 			primitives = [ primitive ];
 
+			// Sets .groups and combined indices to a geometry later in this method.
+
 		}
 
 		return this.getDependencies( 'accessor' ).then( function ( accessors ) {
@@ -2180,13 +2153,37 @@ THREE.GLTFLoader = ( function () {
 
 			return Promise.all( pending ).then( function ( geometries ) {
 
-				// Try merge geometries with BufferGeometryUtils if possible
+				if ( isCombinable ) {
+
+					var geometry = geometries[ 0 ];
+					var indices = [];
+					var offset = 0;
+
+					for ( var i = 0, il = originalPrimitives.length; i < il; i ++ ) {
+
+						var accessor = accessors[ originalPrimitives[ i ].indices ];
+
+						for ( var j = 0, jl = accessor.count; j < jl; j ++ ) {
+
+							indices.push( accessor.array[ j ] );
+
+						}
+
+						geometry.addGroup( offset, accessor.count, i );
+
+						offset += accessor.count;
+
+					}
+
+					geometry.setIndex( indices );
+
+				} else if ( geometries.length > 1 && THREE.BufferGeometryUtils !== undefined ) {
 
-				if ( geometries.length > 1 && THREE.BufferGeometryUtils !== undefined ) {
+					// Tries to merge geometries with BufferGeometryUtils if possible
 
 					for ( var i = 1, il = primitives.length; i < il; i ++ ) {
 
-						// can't merge if draw mode is differenct
+						// can't merge if draw mode is different
 						if ( primitives[ 0 ].mode !== primitives[ i ].mode ) return geometries;
 
 					}