Browse Source

Mergable:
Animation.js - fixed scale bugs and removed flattenToArrayOffset
AnimationHandler.js - fixed so keyframes at same time is removed
Matrix4.lookAt - fixed so it handles situations where z === up and where eye === target
SkinnedMesh.update - added flattenToArrayOffset in end of update

Mikael Emtinger 14 years ago
parent
commit
2ab6fb41ad
4 changed files with 80 additions and 18 deletions
  1. 22 12
      src/animation/Animation.js
  2. 24 4
      src/animation/AnimationHandler.js
  3. 21 2
      src/core/Matrix4.js
  4. 13 0
      src/objects/SkinnedMesh.js

+ 22 - 12
src/animation/Animation.js

@@ -162,12 +162,11 @@ THREE.Animation.prototype.update = function( deltaTimeMS ) {
 	
 		// use JIT?
 	
-		if ( JIThierarchy[ h ][ frame ] !== undefined ) {
+		if ( this.JITCompile && JIThierarchy[ h ][ frame ] !== undefined ) {
 
 			if( object instanceof THREE.Bone ) {
 				
 				object.skinMatrix = JIThierarchy[ h ][ frame ];
-				object.skinMatrix.flattenToArrayOffset( this.root.boneMatrices, h * 16 );
 				
 				object.matrixAutoUpdate = false;
 				object.matrixNeedsUpdate = false;
@@ -186,14 +185,18 @@ THREE.Animation.prototype.update = function( deltaTimeMS ) {
 
 			// make sure so original matrix and not JIT matrix is set
 
-			if( object instanceof THREE.Bone ) {
-				
-				object.skinMatrix = object.animationCache.originalMatrix;
-				
-			} else {
-				
-				object.matrix = object.animationCache.originalMatrix;
+			if ( this.JITCompile ) {
 				
+				if( object instanceof THREE.Bone ) {
+					
+					object.skinMatrix = object.animationCache.originalMatrix;
+					
+				} else {
+					
+					object.matrix = object.animationCache.originalMatrix;
+					
+				}
+
 			}
 
 
@@ -209,17 +212,24 @@ THREE.Animation.prototype.update = function( deltaTimeMS ) {
 
 				// switch keys?
 
-				if ( nextKey.time < unloopedCurrentTime ) {
+				if ( nextKey.time <= unloopedCurrentTime ) {
 
 					// did we loop?
 
-					if ( currentTime < unloopedCurrentTime ) {
+					if ( currentTime <= unloopedCurrentTime ) {
 
 						if ( this.loop ) {
 
 							prevKey = this.data.hierarchy[ h ].keys[ 0 ];
 							nextKey = this.getNextKeyWith( type, h, 1 );
 
+							while( nextKey.time < currentTime ) {
+	
+								prevKey = nextKey;
+								nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 );
+	
+							}
+
 						} else {
 
 							this.stop();
@@ -256,7 +266,7 @@ THREE.Animation.prototype.update = function( deltaTimeMS ) {
 
 				if ( scale < 0 || scale > 1 ) {
 
-					console.log( "THREE.Animation.update: Warning! Scale out of bounds:" + scale ); 
+					console.log( "THREE.Animation.update: Warning! Scale out of bounds:" + scale + " on bone " + h ); 
 					scale = scale < 0 ? 0 : 1;
 
 				}

+ 24 - 4
src/animation/AnimationHandler.js

@@ -137,10 +137,6 @@ THREE.AnimationHandler = (function() {
 				// THIS SHOULD BE REMOVED WHEN LENGTH IS UPDATED TO MS IN EXPORT FORMAT!
 				data.hierarchy[ h ].keys[ k ].time = parseInt( data.hierarchy[ h ].keys[ k ].time * 1000, 10 );
 
-				// set index
-
-				data.hierarchy[ h ].keys[ k ].index = k;
-
 
 				// create quaternions
 
@@ -151,6 +147,30 @@ THREE.AnimationHandler = (function() {
 					data.hierarchy[ h ].keys[ k ].rot = new THREE.Quaternion( quat[0], quat[1], quat[2], quat[3] );
 
 				}
+			
+			}
+			
+			
+			// remove all keys that are on the same time
+			
+			for( var k = 1; k < data.hierarchy[ h ].keys.length; k++ ) {
+				
+				if( data.hierarchy[ h ].keys[ k ].time === data.hierarchy[ h ].keys[ k - 1 ].time ) {
+					
+					data.hierarchy[ h ].keys.splice( k, 1 );
+					k--;
+				
+				}
+				
+			}
+
+
+			// set index
+			
+			for( var k = 1; k < data.hierarchy[ h ].keys.length; k++ ) {
+				
+				data.hierarchy[ h ].keys[ k ].index = k;
+				
 			}
 		}
 

+ 21 - 2
src/core/Matrix4.js

@@ -72,9 +72,26 @@ THREE.Matrix4.prototype = {
 		var x = THREE.Matrix4.__tmpVec1, y = THREE.Matrix4.__tmpVec2, z = THREE.Matrix4.__tmpVec3;
 
 		z.sub( eye, center ).normalize();
+		
+		if ( z.length() === 0 ) {
+			
+			z.z = 1;
+		}
+		
 		x.cross( up, z ).normalize();
+
+		if ( x.length() === 0 ) {
+			
+			z.x += 0.0001;
+			x.cross( up, z ).normalize();
+			
+		}
+
 		y.cross( z, x ).normalize();
 
+//		if ( x.length() === 0 ) x.set( 1, 0, 0 );
+//		if ( y.length() === 0 ) y.set( 0, 1, 0 );
+
 /*		this.n11 = x.x; this.n12 = x.y; this.n13 = x.z; this.n14 = - x.dot( eye );
 		this.n21 = y.x; this.n22 = y.y; this.n23 = y.z; this.n24 = - y.dot( eye );
 		this.n31 = z.x; this.n32 = z.y; this.n33 = z.z; this.n34 = - z.dot( eye );
@@ -751,8 +768,10 @@ THREE.Matrix4.makeInvert3x3 = function ( m1 ) {
 	idet;
 
 	// no inverse
-	if (det == 0) throw "matrix not invertible";
-
+	if (det == 0) {
+		throw "matrix not invertible";
+	}
+	
 	idet = 1.0 / det;
 
 	m33m[ 0 ] = idet * a11; m33m[ 1 ] = idet * a21; m33m[ 2 ] = idet * a31;

+ 13 - 0
src/objects/SkinnedMesh.js

@@ -139,6 +139,19 @@ THREE.SkinnedMesh.prototype.update = function ( parentMatrixWorld, forceUpdate,
 
 		}
 
+
+		// flatten to array
+
+		var b, bl = this.bones.length;
+			ba = this.bones;
+			bm = this.boneMatrices;
+
+		for ( b = 0; b < bl; b++ ) {
+			
+			ba[ b ].skinMatrix.flattenToArrayOffset( bm, b * 16 );
+			
+		}
+
 	}
 
 };