Browse Source

MMDLoader: Fixed breakage after removing Geometry support in SkinnedMesh.

Mr.doob 6 years ago
parent
commit
803bb9fece
1 changed files with 67 additions and 0 deletions
  1. 67 0
      examples/js/loaders/MMDLoader.js

+ 67 - 0
examples/js/loaders/MMDLoader.js

@@ -355,6 +355,9 @@ THREE.MMDLoader = ( function () {
 
 			var mesh = new THREE.SkinnedMesh( geometry, material );
 
+			var skeleton = new THREE.Skeleton( initBones( mesh ) );
+			mesh.bind( skeleton );
+
 			// console.log( mesh ); // for console debug
 
 			return mesh;
@@ -363,6 +366,70 @@ THREE.MMDLoader = ( function () {
 
 	};
 
+	// TODO: Try to remove this function
+
+	function initBones( mesh ) {
+
+		var geometry = mesh.geometry;
+
+		var bones = [], bone, gbone;
+		var i, il;
+
+		if ( geometry && geometry.bones !== undefined ) {
+
+			// first, create array of 'Bone' objects from geometry data
+
+			for ( i = 0, il = geometry.bones.length; i < il; i ++ ) {
+
+				gbone = geometry.bones[ i ];
+
+				// create new 'Bone' object
+
+				bone = new THREE.Bone();
+				bones.push( bone );
+
+				// apply values
+
+				bone.name = gbone.name;
+				bone.position.fromArray( gbone.pos );
+				bone.quaternion.fromArray( gbone.rotq );
+				if ( gbone.scl !== undefined ) bone.scale.fromArray( gbone.scl );
+
+			}
+
+			// second, create bone hierarchy
+
+			for ( i = 0, il = geometry.bones.length; i < il; i ++ ) {
+
+				gbone = geometry.bones[ i ];
+
+				if ( ( gbone.parent !== - 1 ) && ( gbone.parent !== null ) && ( bones[ gbone.parent ] !== undefined ) ) {
+
+					// subsequent bones in the hierarchy
+
+					bones[ gbone.parent ].add( bones[ i ] );
+
+				} else {
+
+					// topmost bone, immediate child of the skinned mesh
+
+					mesh.add( bones[ i ] );
+
+				}
+
+			}
+
+		}
+
+		// now the bones are part of the scene graph and children of the skinned mesh.
+		// let's update the corresponding matrices
+
+		mesh.updateMatrixWorld( true );
+
+		return bones;
+
+	}
+
 	//
 
 	function GeometryBuilder() {