Sfoglia il codice sorgente

GLTFLoader: Clean up loadSkin (#25007)

Takahiro 2 anni fa
parent
commit
7f2bf89b3b
1 ha cambiato i file con 50 aggiunte e 55 eliminazioni
  1. 50 55
      examples/jsm/loaders/GLTFLoader.js

+ 50 - 55
examples/jsm/loaders/GLTFLoader.js

@@ -3591,25 +3591,65 @@ class GLTFParser {
 	/**
 	 * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins
 	 * @param {number} skinIndex
-	 * @return {Promise<Object>}
+	 * @return {Promise<Skeleton>}
 	 */
 	loadSkin( skinIndex ) {
 
 		const skinDef = this.json.skins[ skinIndex ];
 
-		const skinEntry = { joints: skinDef.joints };
+		const pending = [];
+
+		for ( let i = 0, il = skinDef.joints.length; i < il; i ++ ) {
+
+			pending.push( this.getDependency( 'node', skinDef.joints[ i ] ) );
+
+		}
+
+		if ( skinDef.inverseBindMatrices !== undefined ) {
+
+			pending.push( this.getDependency( 'accessor', skinDef.inverseBindMatrices ) );
 
-		if ( skinDef.inverseBindMatrices === undefined ) {
+		} else {
 
-			return Promise.resolve( skinEntry );
+			pending.push( null );
 
 		}
 
-		return this.getDependency( 'accessor', skinDef.inverseBindMatrices ).then( function ( accessor ) {
+		return Promise.all( pending ).then( function ( results ) {
+
+			const inverseBindMatrices = results.pop();
+			const jointNodes = results;
+
+			const bones = [];
+			const boneInverses = [];
+
+			for ( let i = 0, il = jointNodes.length; i < il; i ++ ) {
+
+				const jointNode = jointNodes[ i ];
+
+				if ( jointNode ) {
 
-			skinEntry.inverseBindMatrices = accessor;
+					bones.push( jointNode );
+
+					const mat = new Matrix4();
+
+					if ( inverseBindMatrices !== null ) {
+
+						mat.fromArray( inverseBindMatrices.array, i * 16 );
+
+					}
+
+					boneInverses.push( mat );
+
+				} else {
+
+					console.warn( 'THREE.GLTFLoader: Joint "%s" could not be found.', skinDef.joints[ i ] );
+
+				}
+
+			}
 
-			return skinEntry;
+			return new Skeleton( bones, boneInverses );
 
 		} );
 
@@ -4046,58 +4086,13 @@ function buildNodeHierarchy( nodeId, parentObject, json, parser ) {
 
 		// build skeleton here as well
 
-		let skinEntry;
-
-		return parser.getDependency( 'skin', nodeDef.skin ).then( function ( skin ) {
-
-			skinEntry = skin;
-
-			const pendingJoints = [];
-
-			for ( let i = 0, il = skinEntry.joints.length; i < il; i ++ ) {
-
-				pendingJoints.push( parser.getDependency( 'node', skinEntry.joints[ i ] ) );
-
-			}
-
-			return Promise.all( pendingJoints );
-
-		} ).then( function ( jointNodes ) {
+		return parser.getDependency( 'skin', nodeDef.skin ).then( function ( skeleton ) {
 
 			node.traverse( function ( mesh ) {
 
-				if ( ! mesh.isMesh ) return;
-
-				const bones = [];
-				const boneInverses = [];
-
-				for ( let j = 0, jl = jointNodes.length; j < jl; j ++ ) {
-
-					const jointNode = jointNodes[ j ];
-
-					if ( jointNode ) {
-
-						bones.push( jointNode );
-
-						const mat = new Matrix4();
-
-						if ( skinEntry.inverseBindMatrices !== undefined ) {
-
-							mat.fromArray( skinEntry.inverseBindMatrices.array, j * 16 );
-
-						}
-
-						boneInverses.push( mat );
-
-					} else {
-
-						console.warn( 'THREE.GLTFLoader: Joint "%s" could not be found.', skinEntry.joints[ j ] );
-
-					}
-
-				}
+				if ( ! mesh.isSkinnedMesh ) return;
 
-				mesh.bind( new Skeleton( bones, boneInverses ), mesh.matrixWorld );
+				mesh.bind( skeleton, mesh.matrixWorld );
 
 			} );