|
@@ -32,36 +32,7 @@ THREE.Animation.prototype.play = function ( startTime ) {
|
|
|
|
|
|
this.isPlaying = true;
|
|
|
|
|
|
- // reset key cache
|
|
|
-
|
|
|
- for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
|
|
|
-
|
|
|
- var object = this.hierarchy[ h ];
|
|
|
-
|
|
|
- object.matrixAutoUpdate = true;
|
|
|
-
|
|
|
- if ( object.animationCache === undefined ) {
|
|
|
-
|
|
|
- object.animationCache = {};
|
|
|
- object.animationCache.prevKey = { pos: 0, rot: 0, scl: 0 };
|
|
|
- object.animationCache.nextKey = { pos: 0, rot: 0, scl: 0 };
|
|
|
- object.animationCache.originalMatrix = object instanceof THREE.Bone ? object.skinMatrix : object.matrix;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- var prevKey = object.animationCache.prevKey;
|
|
|
- var nextKey = object.animationCache.nextKey;
|
|
|
-
|
|
|
- prevKey.pos = this.data.hierarchy[ h ].keys[ 0 ];
|
|
|
- prevKey.rot = this.data.hierarchy[ h ].keys[ 0 ];
|
|
|
- prevKey.scl = this.data.hierarchy[ h ].keys[ 0 ];
|
|
|
-
|
|
|
- nextKey.pos = this.getNextKeyWith( "pos", h, 1 );
|
|
|
- nextKey.rot = this.getNextKeyWith( "rot", h, 1 );
|
|
|
- nextKey.scl = this.getNextKeyWith( "scl", h, 1 );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
+ this.reset();
|
|
|
this.update( 0 );
|
|
|
|
|
|
}
|
|
@@ -98,100 +69,85 @@ THREE.Animation.prototype.stop = function() {
|
|
|
|
|
|
};
|
|
|
|
|
|
+THREE.Animation.prototype.reset = function () {
|
|
|
|
|
|
-THREE.Animation.prototype.update = function ( deltaTimeMS ) {
|
|
|
-
|
|
|
- // early out
|
|
|
-
|
|
|
- if ( this.isPlaying === false ) return;
|
|
|
+ for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
|
|
|
|
|
|
+ var object = this.hierarchy[ h ];
|
|
|
|
|
|
- // vars
|
|
|
+ object.matrixAutoUpdate = true;
|
|
|
|
|
|
- var types = [ "pos", "rot", "scl" ];
|
|
|
- var type;
|
|
|
- var scale;
|
|
|
- var vector;
|
|
|
- var prevXYZ, nextXYZ;
|
|
|
- var prevKey, nextKey;
|
|
|
- var object;
|
|
|
- var animationCache;
|
|
|
- var frame;
|
|
|
- var JIThierarchy = this.data.JIT.hierarchy;
|
|
|
- var currentTime, unloopedCurrentTime;
|
|
|
- var currentPoint, forwardPoint, angle;
|
|
|
+ if ( object.animationCache === undefined ) {
|
|
|
|
|
|
+ object.animationCache = {};
|
|
|
+ object.animationCache.prevKey = { pos: 0, rot: 0, scl: 0 };
|
|
|
+ object.animationCache.nextKey = { pos: 0, rot: 0, scl: 0 };
|
|
|
+ object.animationCache.originalMatrix = object instanceof THREE.Bone ? object.skinMatrix : object.matrix;
|
|
|
|
|
|
- this.currentTime += deltaTimeMS * this.timeScale;
|
|
|
+ }
|
|
|
|
|
|
- unloopedCurrentTime = this.currentTime;
|
|
|
+ var prevKey = object.animationCache.prevKey;
|
|
|
+ var nextKey = object.animationCache.nextKey;
|
|
|
|
|
|
- // Mod operation fails on floats
|
|
|
- // was this supposed to be in frames?
|
|
|
- while ( this.currentTime > this.data.length ) {
|
|
|
+ prevKey.pos = this.data.hierarchy[ h ].keys[ 0 ];
|
|
|
+ prevKey.rot = this.data.hierarchy[ h ].keys[ 0 ];
|
|
|
+ prevKey.scl = this.data.hierarchy[ h ].keys[ 0 ];
|
|
|
|
|
|
- this.currentTime -= this.data.length;
|
|
|
+ nextKey.pos = this.getNextKeyWith( "pos", h, 1 );
|
|
|
+ nextKey.rot = this.getNextKeyWith( "rot", h, 1 );
|
|
|
+ nextKey.scl = this.getNextKeyWith( "scl", h, 1 );
|
|
|
|
|
|
}
|
|
|
|
|
|
- currentTime = this.currentTime = this.currentTime % this.data.length;
|
|
|
-
|
|
|
-
|
|
|
- frame = parseInt( Math.min( currentTime * this.data.fps, this.data.length * this.data.fps ), 10 );
|
|
|
-
|
|
|
+};
|
|
|
|
|
|
- for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
|
|
|
|
|
|
- object = this.hierarchy[ h ];
|
|
|
- animationCache = object.animationCache;
|
|
|
+THREE.Animation.prototype.update = function ( delta ) {
|
|
|
|
|
|
- // loop through pos/rot/scl
|
|
|
+ if ( this.isPlaying === false ) return;
|
|
|
|
|
|
- for ( var t = 0; t < 3; t ++ ) {
|
|
|
+ this.currentTime += delta * this.timeScale;
|
|
|
|
|
|
- // get keys
|
|
|
+ //
|
|
|
|
|
|
- type = types[ t ];
|
|
|
- prevKey = animationCache.prevKey[ type ];
|
|
|
- nextKey = animationCache.nextKey[ type ];
|
|
|
+ var vector;
|
|
|
+ var types = [ "pos", "rot", "scl" ];
|
|
|
|
|
|
- // switch keys?
|
|
|
-
|
|
|
- if ( nextKey.time <= unloopedCurrentTime ) {
|
|
|
+ var duration = this.data.length;
|
|
|
+ var currentTime = this.currentTime;
|
|
|
|
|
|
- // did we loop?
|
|
|
+ if ( this.loop === true ) {
|
|
|
|
|
|
- if ( currentTime <= unloopedCurrentTime ) {
|
|
|
+ currentTime %= duration;
|
|
|
|
|
|
- if ( this.loop ) {
|
|
|
+ }
|
|
|
|
|
|
- prevKey = this.data.hierarchy[ h ].keys[ 0 ];
|
|
|
- nextKey = this.getNextKeyWith( type, h, 1 );
|
|
|
+ currentTime = Math.min( currentTime, duration );
|
|
|
|
|
|
- // if ( nextKey.index < prevKey.index ) then we have wrapped over the end, and nextKey.time < currentTime will loop forever
|
|
|
- while ( nextKey !== null && nextKey.time < currentTime && nextKey.index > prevKey.index) {
|
|
|
+ for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
|
|
|
|
|
|
- prevKey = nextKey;
|
|
|
- nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 );
|
|
|
+ var object = this.hierarchy[ h ];
|
|
|
+ var animationCache = object.animationCache;
|
|
|
|
|
|
- }
|
|
|
+ // loop through pos/rot/scl
|
|
|
|
|
|
- } else {
|
|
|
+ for ( var t = 0; t < 3; t ++ ) {
|
|
|
|
|
|
- this.stop();
|
|
|
- return;
|
|
|
+ // get keys
|
|
|
|
|
|
- }
|
|
|
+ var type = types[ t ];
|
|
|
+ var prevKey = animationCache.prevKey[ type ];
|
|
|
+ var nextKey = animationCache.nextKey[ type ];
|
|
|
|
|
|
- } else {
|
|
|
+ if ( nextKey.time <= currentTime ) {
|
|
|
|
|
|
- do {
|
|
|
+ prevKey = this.data.hierarchy[ h ].keys[ 0 ];
|
|
|
+ nextKey = this.getNextKeyWith( type, h, 1 );
|
|
|
|
|
|
- prevKey = nextKey;
|
|
|
- nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 );
|
|
|
+ while ( nextKey.time < currentTime && nextKey.index > prevKey.index ) {
|
|
|
|
|
|
- } while ( nextKey !== null && nextKey.time < currentTime && nextKey.index > prevKey.index )
|
|
|
- // if ( nextKey.index < prevKey.index ) then we have wrapped over the end, and nextKey.time < currentTime will loop forever
|
|
|
+ prevKey = nextKey;
|
|
|
+ nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 );
|
|
|
|
|
|
}
|
|
|
|
|
@@ -200,23 +156,16 @@ THREE.Animation.prototype.update = function ( deltaTimeMS ) {
|
|
|
|
|
|
}
|
|
|
|
|
|
-
|
|
|
object.matrixAutoUpdate = true;
|
|
|
object.matrixWorldNeedsUpdate = true;
|
|
|
|
|
|
- scale = ( currentTime - prevKey.time ) / ( nextKey.time - prevKey.time );
|
|
|
- prevXYZ = prevKey[ type ];
|
|
|
- nextXYZ = nextKey[ type ];
|
|
|
-
|
|
|
+ var scale = ( currentTime - prevKey.time ) / ( nextKey.time - prevKey.time );
|
|
|
|
|
|
- // check scale error
|
|
|
+ var prevXYZ = prevKey[ type ];
|
|
|
+ var nextXYZ = nextKey[ type ];
|
|
|
|
|
|
- if ( scale < 0 || scale > 1 ) {
|
|
|
-
|
|
|
- console.log( "THREE.Animation.update: Warning! Scale out of bounds:" + scale + " on bone " + h );
|
|
|
- scale = scale < 0 ? 0 : 1;
|
|
|
-
|
|
|
- }
|
|
|
+ if ( scale < 0 ) scale = 0;
|
|
|
+ if ( scale > 1 ) scale = 1;
|
|
|
|
|
|
// interpolate
|
|
|
|
|
@@ -231,7 +180,7 @@ THREE.Animation.prototype.update = function ( deltaTimeMS ) {
|
|
|
vector.z = prevXYZ[ 2 ] + ( nextXYZ[ 2 ] - prevXYZ[ 2 ] ) * scale;
|
|
|
|
|
|
} else if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM ||
|
|
|
- this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
|
|
|
+ this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
|
|
|
|
|
|
this.points[ 0 ] = this.getPrevKeyWith( "pos", h, prevKey.index - 1 )[ "pos" ];
|
|
|
this.points[ 1 ] = prevXYZ;
|
|
@@ -240,7 +189,7 @@ THREE.Animation.prototype.update = function ( deltaTimeMS ) {
|
|
|
|
|
|
scale = scale * 0.33 + 0.33;
|
|
|
|
|
|
- currentPoint = this.interpolateCatmullRom( this.points, scale );
|
|
|
+ var currentPoint = this.interpolateCatmullRom( this.points, scale );
|
|
|
|
|
|
vector.x = currentPoint[ 0 ];
|
|
|
vector.y = currentPoint[ 1 ];
|
|
@@ -248,14 +197,14 @@ THREE.Animation.prototype.update = function ( deltaTimeMS ) {
|
|
|
|
|
|
if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
|
|
|
|
|
|
- forwardPoint = this.interpolateCatmullRom( this.points, scale * 1.01 );
|
|
|
+ var forwardPoint = this.interpolateCatmullRom( this.points, scale * 1.01 );
|
|
|
|
|
|
this.target.set( forwardPoint[ 0 ], forwardPoint[ 1 ], forwardPoint[ 2 ] );
|
|
|
this.target.sub( vector );
|
|
|
this.target.y = 0;
|
|
|
this.target.normalize();
|
|
|
|
|
|
- angle = Math.atan2( this.target.x, this.target.z );
|
|
|
+ var angle = Math.atan2( this.target.x, this.target.z );
|
|
|
object.rotation.set( 0, angle, 0 );
|
|
|
|
|
|
}
|
|
@@ -280,6 +229,18 @@ THREE.Animation.prototype.update = function ( deltaTimeMS ) {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ if ( this.currentTime > duration ) {
|
|
|
+
|
|
|
+ this.reset();
|
|
|
+
|
|
|
+ if ( this.loop === false ) {
|
|
|
+
|
|
|
+ this.stop();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
};
|
|
|
|
|
|
// Catmull-Rom spline
|
|
@@ -364,7 +325,7 @@ THREE.Animation.prototype.getPrevKeyWith = function ( type, h, key ) {
|
|
|
var keys = this.data.hierarchy[ h ].keys;
|
|
|
|
|
|
if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM ||
|
|
|
- this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
|
|
|
+ this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
|
|
|
|
|
|
key = key > 0 ? key : 0;
|
|
|
|