2
0
Эх сурвалжийг харах

Merge pull request #14007 from takahirox/MMDLoaderUpdate2

MMDLoader update part3
Mr.doob 7 жил өмнө
parent
commit
2a7da72dfc

+ 2 - 0
docs/examples/animations/CCDIKSolver.html

@@ -61,6 +61,8 @@
 			<ul>
 			<ul>
 				<li>[page:Integer index] — Link bone.</li>
 				<li>[page:Integer index] — Link bone.</li>
 				<li>[page:Vector3 limitation] — (optional) Rotation axis. Default is undefined.</li>
 				<li>[page:Vector3 limitation] — (optional) Rotation axis. Default is undefined.</li>
+				<li>[page:Vector3 rotationMin] — (optional) Rotation minimum limit. Default is undefined.</li>
+				<li>[page:Vector3 rotationMax] — (optional) Rotation maximum limit. Default is undefined.</li>
 				<li>[page:Boolean enabled] — (optional) Default is true.</li>
 				<li>[page:Boolean enabled] — (optional) Default is true.</li>
 			</ul>
 			</ul>
 			</li>
 			</li>

+ 21 - 0
examples/js/animation/CCDIKSolver.js

@@ -55,6 +55,7 @@ THREE.CCDIKSolver = ( function () {
 			var invLinkQ = new THREE.Quaternion();
 			var invLinkQ = new THREE.Quaternion();
 			var linkScale = new THREE.Vector3();
 			var linkScale = new THREE.Vector3();
 			var axis = new THREE.Vector3();
 			var axis = new THREE.Vector3();
+			var vector = new THREE.Vector3();
 
 
 			return function update() {
 			return function update() {
 
 
@@ -90,6 +91,8 @@ THREE.CCDIKSolver = ( function () {
 							if ( links[ k ].enabled === false ) break;
 							if ( links[ k ].enabled === false ) break;
 
 
 							var limitation = links[ k ].limitation;
 							var limitation = links[ k ].limitation;
+							var rotationMin = links[ k ].rotationMin;
+							var rotationMax = links[ k ].rotationMax;
 
 
 							// don't use getWorldPosition/Quaternion() here for the performance
 							// don't use getWorldPosition/Quaternion() here for the performance
 							// because they call updateMatrixWorld( true ) inside.
 							// because they call updateMatrixWorld( true ) inside.
@@ -157,6 +160,24 @@ THREE.CCDIKSolver = ( function () {
 
 
 							}
 							}
 
 
+							if ( rotationMin !== undefined ) {
+
+								link.rotation.setFromVector3(
+									link.rotation
+										.toVector3( vector )
+										.max( rotationMin ) );
+
+							}
+
+							if ( rotationMax !== undefined ) {
+
+								link.rotation.setFromVector3(
+									link.rotation
+										.toVector3( vector )
+										.min( rotationMax ) );
+
+							}
+
 							link.updateMatrixWorld( true );
 							link.updateMatrixWorld( true );
 
 
 							rotated = true;
 							rotated = true;

+ 23 - 23
examples/js/animation/MMDAnimationHelper.js

@@ -293,11 +293,7 @@ THREE.MMDAnimationHelper = ( function () {
 			this.meshes.push( mesh );
 			this.meshes.push( mesh );
 			this.objects.set( mesh, { looped: false } );
 			this.objects.set( mesh, { looped: false } );
 
 
-			if ( params.animation !== undefined ) {
-
-				this._setupMeshAnimation( mesh, params.animation );
-
-			}
+			this._setupMeshAnimation( mesh, params.animation );
 
 
 			if ( params.physics !== false ) {
 			if ( params.physics !== false ) {
 
 
@@ -429,30 +425,34 @@ THREE.MMDAnimationHelper = ( function () {
 
 
 		_setupMeshAnimation: function ( mesh, animation ) {
 		_setupMeshAnimation: function ( mesh, animation ) {
 
 
-			var animations = Array.isArray( animation )
-				? animation : [ animation ];
-
 			var objects = this.objects.get( mesh );
 			var objects = this.objects.get( mesh );
 
 
-			objects.mixer = new THREE.AnimationMixer( mesh );
+			if ( animation !== undefined ) {
 
 
-			for ( var i = 0, il = animations.length; i < il; i ++ ) {
+				var animations = Array.isArray( animation )
+					? animation : [ animation ];
 
 
-				objects.mixer.clipAction( animations[ i ] ).play();
+				objects.mixer = new THREE.AnimationMixer( mesh );
 
 
-			}
+				for ( var i = 0, il = animations.length; i < il; i ++ ) {
 
 
-			// TODO: find a workaround not to access ._clip looking like a private property
-			objects.mixer.addEventListener( 'loop', function ( event ) {
+					objects.mixer.clipAction( animations[ i ] ).play();
 
 
-				var tracks = event.action._clip.tracks;
+				}
 
 
-				if ( tracks.length > 0 &&
-				     tracks[ 0 ].name.slice( 0, 6 ) !== '.bones' ) return;
+				// TODO: find a workaround not to access ._clip looking like a private property
+				objects.mixer.addEventListener( 'loop', function ( event ) {
 
 
-				objects.looped = true;
+					var tracks = event.action._clip.tracks;
 
 
-			} );
+					if ( tracks.length > 0 &&
+					     tracks[ 0 ].name.slice( 0, 6 ) !== '.bones' ) return;
+
+					objects.looped = true;
+
+				} );
+
+			}
 
 
 			objects.ikSolver = this._createCCDIKSolver( mesh );
 			objects.ikSolver = this._createCCDIKSolver( mesh );
 			objects.grantSolver = this.createGrantSolver( mesh );
 			objects.grantSolver = this.createGrantSolver( mesh );
@@ -517,14 +517,14 @@ THREE.MMDAnimationHelper = ( function () {
 			var physics = objects.physics;
 			var physics = objects.physics;
 			var looped = objects.looped;
 			var looped = objects.looped;
 
 
+			// alternate solution to save/restore bones but less performant?
+			//mesh.pose();
+			//this._updatePropertyMixersBuffer( mesh );
+
 			if ( mixer && this.enabled.animation ) {
 			if ( mixer && this.enabled.animation ) {
 
 
 				this._restoreBones( mesh );
 				this._restoreBones( mesh );
 
 
-				// another solution but less performant?
-				//mesh.pose();
-				//this._updatePropertyMixersBuffer( mesh );
-
 				mixer.update( delta );
 				mixer.update( delta );
 
 
 				this._saveBones( mesh );
 				this._saveBones( mesh );

+ 19 - 5
examples/js/loaders/MMDLoader.js

@@ -576,10 +576,24 @@ THREE.MMDLoader = ( function () {
 
 
 						if ( ik.links[ j ].angleLimitation === 1 ) {
 						if ( ik.links[ j ].angleLimitation === 1 ) {
 
 
-							link.limitation = new THREE.Vector3( 1.0, 0.0, 0.0 );
-							// TODO: use limitation angles
-							// link.lowerLimitationAngle;
-							// link.upperLimitationAngle;
+							// Revert if rotationMin/Max doesn't work well
+							// link.limitation = new THREE.Vector3( 1.0, 0.0, 0.0 );
+
+							var rotationMin = ik.links[ j ].lowerLimitationAngle;
+							var rotationMax = ik.links[ j ].upperLimitationAngle;
+
+							// Convert Left to Right coordinate by myself because
+							// MMDParser doesn't convert. It's a MMDParser's bug
+
+							var tmp1 = - rotationMax[ 0 ];
+							var tmp2 = - rotationMax[ 1 ];
+							rotationMax[ 0 ] = - rotationMin[ 0 ];
+							rotationMax[ 1 ] = - rotationMin[ 1 ];
+							rotationMin[ 0 ] = tmp1;
+							rotationMin[ 1 ] = tmp2;
+
+							link.rotationMin = new THREE.Vector3().fromArray( rotationMin );
+							link.rotationMax = new THREE.Vector3().fromArray( rotationMax );
 
 
 						}
 						}
 
 
@@ -1123,7 +1137,7 @@ THREE.MMDLoader = ( function () {
 
 
 						for ( var j = 0, jl = elements.length; j < jl; j ++ ) {
 						for ( var j = 0, jl = elements.length; j < jl; j ++ ) {
 
 
-							var morph2 = model.morphs[ elements[ j ].index ];
+							var morph2 = data.morphs[ elements[ j ].index ];
 
 
 							if ( morph2.type !== 8 ) continue;
 							if ( morph2.type !== 8 ) continue;