Browse Source

Update shared MMDPhysics

Takahiro 8 years ago
parent
commit
0c5e84eac5
2 changed files with 80 additions and 13 deletions
  1. 8 2
      examples/js/animation/MMDPhysics.js
  2. 72 11
      examples/js/loaders/MMDLoader.js

+ 8 - 2
examples/js/animation/MMDPhysics.js

@@ -131,6 +131,14 @@ THREE.MMDPhysics.prototype = {
 
 	update: function ( delta ) {
 
+		this.updateRigidBodies();
+		this.stepSimulation( delta );
+		this.updateBones();
+
+	},
+
+	stepSimulation: function ( delta ) {
+
 		var unitStep = this.unitStep;
 		var stepTime = delta;
 		var maxStepNum = ( ( delta / unitStep ) | 0 ) + 1;
@@ -148,9 +156,7 @@ THREE.MMDPhysics.prototype = {
 
 		}
 
-		this.updateRigidBodies();
 		this.world.stepSimulation( stepTime, maxStepNum, unitStep );
-		this.updateBones();
 
 	},
 

+ 72 - 11
examples/js/loaders/MMDLoader.js

@@ -2223,6 +2223,9 @@ THREE.MMDHelper = function ( renderer ) {
 	this.doOutlineDrawing = true;
 	this.doCameraAnimation = true;
 
+	this.sharedPhysics = false;
+	this.masterPhysics = null;
+
 	this.audioManager = null;
 	this.camera = null;
 
@@ -2296,31 +2299,31 @@ THREE.MMDHelper.prototype = {
 
 	setPhysicses: function ( params ) {
 
-		params = ( params === undefined ) ? {} : Object.assign( {}, params );
-
 		for ( var i = 0; i < this.meshes.length; i++ ) {
 
 			this.setPhysics( this.meshes[ i ], params );
 
-			if ( i === 0 && params.sharePhysicsWorld === true ) {
-
-				params.world = this.meshes[ 0 ].physics.world;
-
-			}
-
 		}
 
 	},
 
 	setPhysics: function ( mesh, params ) {
 
-		if ( params === undefined ) params = {};
+		params = ( params === undefined ) ? {} : Object.assign( {}, params );
+
+		if ( params.world === undefined && this.sharedPhysics ) {
+
+			var masterPhysics = this.getMasterPhysics();
+
+			if ( masterPhysics !== null ) params.world = masterPhysics.world;
+
+		}
 
 		var warmup = params.warmup !== undefined ? params.warmup : 60;
 
 		var physics = new THREE.MMDPhysics( mesh, params );
 
-		if ( mesh.mixer !== null && mesh.mixer !== undefined && this.doAnimation === true && params.preventAnimationWarmup !== false ) {
+		if ( mesh.mixer !== null && mesh.mixer !== undefined && params.preventAnimationWarmup !== false ) {
 
 			this.animateOneMesh( 0, mesh );
 			physics.reset();
@@ -2335,6 +2338,26 @@ THREE.MMDHelper.prototype = {
 
 	},
 
+	getMasterPhysics: function () {
+
+		if ( this.masterPhysics !== null ) return this.masterPhysics;
+
+		for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
+
+			var physics = this.meshes[ i ].physics;
+
+			if ( physics !== undefined && physics !== null ) {
+
+				this.masterPhysics = physics;
+				return this.masterPhysics;
+
+			}
+		}
+
+		return null;
+
+	},
+
 	enablePhysics: function ( enabled ) {
 
 		if ( enabled === true ) {
@@ -2597,6 +2620,8 @@ THREE.MMDHelper.prototype = {
 
 		}
 
+		if ( this.sharedPhysics ) this.updateSharedPhysics( delta );
+
 		this.animateCamera( delta );
 
 	},
@@ -2640,7 +2665,7 @@ THREE.MMDHelper.prototype = {
 
 		}
 
-		if ( physics !== null && this.doPhysics === true ) {
+		if ( physics !== null && this.doPhysics && ! this.sharedPhysics) {
 
 			physics.update( delta );
 
@@ -2648,6 +2673,42 @@ THREE.MMDHelper.prototype = {
 
 	},
 
+	updateSharedPhysics: function ( delta ) {
+
+		if ( this.meshes.length === 0 || ! this.doPhysics || ! this.sharedPhysics ) return;
+
+		var physics = this.getMasterPhysics();
+
+		if ( physics === null ) return;
+
+		for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
+
+			var p = this.meshes[ i ].physics;
+
+			if ( p !== null && p !== undefined ) {
+
+				p.updateRigidBodies();
+
+			}
+
+		}
+
+		physics.stepSimulation( delta );
+
+		for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
+
+			var p = this.meshes[ i ].physics;
+
+			if ( p !== null && p !== undefined ) {
+
+				p.updateBones();
+
+			}
+
+		}
+
+	},
+
 	animateCamera: function ( delta ) {
 
 		if ( this.camera === null ) {