Browse Source

GLTFLoader: Clean up node hierarchy build (#25058)

Takahiro 2 years ago
parent
commit
9184915a84
1 changed files with 33 additions and 34 deletions
  1. 33 34
      examples/jsm/loaders/GLTFLoader.js

+ 33 - 34
examples/jsm/loaders/GLTFLoader.js

@@ -4017,6 +4017,31 @@ class GLTFParser {
 
 
 			} );
 			} );
 
 
+		} ).then( function ( node ) {
+
+			if ( nodeDef.children === undefined ) return node;
+
+			const pending = [];
+			const childrenDef = nodeDef.children;
+
+			for ( let i = 0, il = childrenDef.length; i < il; i ++ ) {
+
+				pending.push( parser.getDependency( 'node', childrenDef[ i ] ) );
+
+			}
+
+			return Promise.all( pending ).then( function ( children ) {
+
+				for ( let i = 0, il = children.length; i < il; i ++ ) {
+
+					node.add( children[ i ] );
+
+				}
+
+				return node;
+
+			} );
+
 		} );
 		} );
 
 
 	}
 	}
@@ -4028,7 +4053,6 @@ class GLTFParser {
 	 */
 	 */
 	loadScene( sceneIndex ) {
 	loadScene( sceneIndex ) {
 
 
-		const json = this.json;
 		const extensions = this.extensions;
 		const extensions = this.extensions;
 		const sceneDef = this.json.scenes[ sceneIndex ];
 		const sceneDef = this.json.scenes[ sceneIndex ];
 		const parser = this;
 		const parser = this;
@@ -4048,11 +4072,17 @@ class GLTFParser {
 
 
 		for ( let i = 0, il = nodeIds.length; i < il; i ++ ) {
 		for ( let i = 0, il = nodeIds.length; i < il; i ++ ) {
 
 
-			pending.push( buildNodeHierarchy( nodeIds[ i ], scene, json, parser ) );
+			pending.push( parser.getDependency( 'node', nodeIds[ i ] ) );
 
 
 		}
 		}
 
 
-		return Promise.all( pending ).then( function () {
+		return Promise.all( pending ).then( function ( nodes ) {
+
+			for ( let i = 0, il = nodes.length; i < il; i ++ ) {
+
+				scene.add( nodes[ i ] );
+
+			}
 
 
 			// Removes dangling associations, associations that reference a node that
 			// Removes dangling associations, associations that reference a node that
 			// didn't make it into the scene.
 			// didn't make it into the scene.
@@ -4096,37 +4126,6 @@ class GLTFParser {
 
 
 }
 }
 
 
-function buildNodeHierarchy( nodeId, parentObject, json, parser ) {
-
-	const nodeDef = json.nodes[ nodeId ];
-
-	return parser.getDependency( 'node', nodeId ).then( function ( node ) {
-
-		// build node hierachy
-
-		parentObject.add( node );
-
-		const pending = [];
-
-		if ( nodeDef.children ) {
-
-			const children = nodeDef.children;
-
-			for ( let i = 0, il = children.length; i < il; i ++ ) {
-
-				const child = children[ i ];
-				pending.push( buildNodeHierarchy( child, node, json, parser ) );
-
-			}
-
-		}
-
-		return Promise.all( pending );
-
-	} );
-
-}
-
 /**
 /**
  * @param {BufferGeometry} geometry
  * @param {BufferGeometry} geometry
  * @param {GLTF.Primitive} primitiveDef
  * @param {GLTF.Primitive} primitiveDef