Animation.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. * @author mikael emtinger / http://gomo.se/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author alteredq / http://alteredqualia.com/
  5. */
  6. THREE.Animation = function ( root, name, interpolationType ) {
  7. this.root = root;
  8. this.data = THREE.AnimationHandler.get( name );
  9. this.hierarchy = THREE.AnimationHandler.parse( root );
  10. this.currentTime = 0;
  11. this.timeScale = 1;
  12. this.isPlaying = false;
  13. this.isPaused = true;
  14. this.loop = true;
  15. this.interpolationType = interpolationType !== undefined ? interpolationType : THREE.AnimationHandler.LINEAR;
  16. this.points = [];
  17. this.target = new THREE.Vector3();
  18. };
  19. THREE.Animation.prototype.play = function ( loop, startTimeMS ) {
  20. if ( this.isPlaying === false ) {
  21. this.isPlaying = true;
  22. this.loop = loop !== undefined ? loop : true;
  23. this.currentTime = startTimeMS !== undefined ? startTimeMS : 0;
  24. // reset key cache
  25. var h, hl = this.hierarchy.length,
  26. object;
  27. for ( h = 0; h < hl; h ++ ) {
  28. object = this.hierarchy[ h ];
  29. if ( this.interpolationType !== THREE.AnimationHandler.CATMULLROM_FORWARD ) {
  30. object.useQuaternion = true;
  31. }
  32. object.matrixAutoUpdate = true;
  33. if ( object.animationCache === undefined ) {
  34. object.animationCache = {};
  35. object.animationCache.prevKey = { pos: 0, rot: 0, scl: 0 };
  36. object.animationCache.nextKey = { pos: 0, rot: 0, scl: 0 };
  37. object.animationCache.originalMatrix = object instanceof THREE.Bone ? object.skinMatrix : object.matrix;
  38. }
  39. var prevKey = object.animationCache.prevKey;
  40. var nextKey = object.animationCache.nextKey;
  41. prevKey.pos = this.data.hierarchy[ h ].keys[ 0 ];
  42. prevKey.rot = this.data.hierarchy[ h ].keys[ 0 ];
  43. prevKey.scl = this.data.hierarchy[ h ].keys[ 0 ];
  44. nextKey.pos = this.getNextKeyWith( "pos", h, 1 );
  45. nextKey.rot = this.getNextKeyWith( "rot", h, 1 );
  46. nextKey.scl = this.getNextKeyWith( "scl", h, 1 );
  47. }
  48. this.update( 0 );
  49. }
  50. this.isPaused = false;
  51. THREE.AnimationHandler.addToUpdate( this );
  52. };
  53. THREE.Animation.prototype.pause = function() {
  54. if ( this.isPaused === true ) {
  55. THREE.AnimationHandler.addToUpdate( this );
  56. } else {
  57. THREE.AnimationHandler.removeFromUpdate( this );
  58. }
  59. this.isPaused = !this.isPaused;
  60. };
  61. THREE.Animation.prototype.stop = function() {
  62. this.isPlaying = false;
  63. this.isPaused = false;
  64. THREE.AnimationHandler.removeFromUpdate( this );
  65. };
  66. THREE.Animation.prototype.update = function ( deltaTimeMS ) {
  67. // early out
  68. if ( this.isPlaying === false ) return;
  69. // vars
  70. var types = [ "pos", "rot", "scl" ];
  71. var type;
  72. var scale;
  73. var vector;
  74. var prevXYZ, nextXYZ;
  75. var prevKey, nextKey;
  76. var object;
  77. var animationCache;
  78. var frame;
  79. var JIThierarchy = this.data.JIT.hierarchy;
  80. var currentTime, unloopedCurrentTime;
  81. var currentPoint, forwardPoint, angle;
  82. this.currentTime += deltaTimeMS * this.timeScale;
  83. unloopedCurrentTime = this.currentTime;
  84. currentTime = this.currentTime = this.currentTime % this.data.length;
  85. frame = parseInt( Math.min( currentTime * this.data.fps, this.data.length * this.data.fps ), 10 );
  86. for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
  87. object = this.hierarchy[ h ];
  88. animationCache = object.animationCache;
  89. // loop through pos/rot/scl
  90. for ( var t = 0; t < 3; t ++ ) {
  91. // get keys
  92. type = types[ t ];
  93. prevKey = animationCache.prevKey[ type ];
  94. nextKey = animationCache.nextKey[ type ];
  95. // switch keys?
  96. if ( nextKey.time <= unloopedCurrentTime ) {
  97. // did we loop?
  98. if ( currentTime < unloopedCurrentTime ) {
  99. if ( this.loop ) {
  100. prevKey = this.data.hierarchy[ h ].keys[ 0 ];
  101. nextKey = this.getNextKeyWith( type, h, 1 );
  102. while( nextKey.time < currentTime ) {
  103. prevKey = nextKey;
  104. nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 );
  105. }
  106. } else {
  107. this.stop();
  108. return;
  109. }
  110. } else {
  111. do {
  112. prevKey = nextKey;
  113. nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 );
  114. } while( nextKey.time < currentTime )
  115. }
  116. animationCache.prevKey[ type ] = prevKey;
  117. animationCache.nextKey[ type ] = nextKey;
  118. }
  119. object.matrixAutoUpdate = true;
  120. object.matrixWorldNeedsUpdate = true;
  121. scale = ( currentTime - prevKey.time ) / ( nextKey.time - prevKey.time );
  122. prevXYZ = prevKey[ type ];
  123. nextXYZ = nextKey[ type ];
  124. // check scale error
  125. if ( scale < 0 || scale > 1 ) {
  126. console.log( "THREE.Animation.update: Warning! Scale out of bounds:" + scale + " on bone " + h );
  127. scale = scale < 0 ? 0 : 1;
  128. }
  129. // interpolate
  130. if ( type === "pos" ) {
  131. vector = object.position;
  132. if ( this.interpolationType === THREE.AnimationHandler.LINEAR ) {
  133. vector.x = prevXYZ[ 0 ] + ( nextXYZ[ 0 ] - prevXYZ[ 0 ] ) * scale;
  134. vector.y = prevXYZ[ 1 ] + ( nextXYZ[ 1 ] - prevXYZ[ 1 ] ) * scale;
  135. vector.z = prevXYZ[ 2 ] + ( nextXYZ[ 2 ] - prevXYZ[ 2 ] ) * scale;
  136. } else if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM ||
  137. this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
  138. this.points[ 0 ] = this.getPrevKeyWith( "pos", h, prevKey.index - 1 )[ "pos" ];
  139. this.points[ 1 ] = prevXYZ;
  140. this.points[ 2 ] = nextXYZ;
  141. this.points[ 3 ] = this.getNextKeyWith( "pos", h, nextKey.index + 1 )[ "pos" ];
  142. scale = scale * 0.33 + 0.33;
  143. currentPoint = this.interpolateCatmullRom( this.points, scale );
  144. vector.x = currentPoint[ 0 ];
  145. vector.y = currentPoint[ 1 ];
  146. vector.z = currentPoint[ 2 ];
  147. if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
  148. forwardPoint = this.interpolateCatmullRom( this.points, scale * 1.01 );
  149. this.target.set( forwardPoint[ 0 ], forwardPoint[ 1 ], forwardPoint[ 2 ] );
  150. this.target.sub( vector );
  151. this.target.y = 0;
  152. this.target.normalize();
  153. angle = Math.atan2( this.target.x, this.target.z );
  154. object.rotation.set( 0, angle, 0 );
  155. }
  156. }
  157. } else if ( type === "rot" ) {
  158. THREE.Quaternion.slerp( prevXYZ, nextXYZ, object.quaternion, scale );
  159. } else if ( type === "scl" ) {
  160. vector = object.scale;
  161. vector.x = prevXYZ[ 0 ] + ( nextXYZ[ 0 ] - prevXYZ[ 0 ] ) * scale;
  162. vector.y = prevXYZ[ 1 ] + ( nextXYZ[ 1 ] - prevXYZ[ 1 ] ) * scale;
  163. vector.z = prevXYZ[ 2 ] + ( nextXYZ[ 2 ] - prevXYZ[ 2 ] ) * scale;
  164. }
  165. }
  166. }
  167. };
  168. // Catmull-Rom spline
  169. THREE.Animation.prototype.interpolateCatmullRom = function ( points, scale ) {
  170. var c = [], v3 = [],
  171. point, intPoint, weight, w2, w3,
  172. pa, pb, pc, pd;
  173. point = ( points.length - 1 ) * scale;
  174. intPoint = Math.floor( point );
  175. weight = point - intPoint;
  176. c[ 0 ] = intPoint === 0 ? intPoint : intPoint - 1;
  177. c[ 1 ] = intPoint;
  178. c[ 2 ] = intPoint > points.length - 2 ? intPoint : intPoint + 1;
  179. c[ 3 ] = intPoint > points.length - 3 ? intPoint : intPoint + 2;
  180. pa = points[ c[ 0 ] ];
  181. pb = points[ c[ 1 ] ];
  182. pc = points[ c[ 2 ] ];
  183. pd = points[ c[ 3 ] ];
  184. w2 = weight * weight;
  185. w3 = weight * w2;
  186. v3[ 0 ] = this.interpolate( pa[ 0 ], pb[ 0 ], pc[ 0 ], pd[ 0 ], weight, w2, w3 );
  187. v3[ 1 ] = this.interpolate( pa[ 1 ], pb[ 1 ], pc[ 1 ], pd[ 1 ], weight, w2, w3 );
  188. v3[ 2 ] = this.interpolate( pa[ 2 ], pb[ 2 ], pc[ 2 ], pd[ 2 ], weight, w2, w3 );
  189. return v3;
  190. };
  191. THREE.Animation.prototype.interpolate = function ( p0, p1, p2, p3, t, t2, t3 ) {
  192. var v0 = ( p2 - p0 ) * 0.5,
  193. v1 = ( p3 - p1 ) * 0.5;
  194. return ( 2 * ( p1 - p2 ) + v0 + v1 ) * t3 + ( - 3 * ( p1 - p2 ) - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  195. };
  196. // Get next key with
  197. THREE.Animation.prototype.getNextKeyWith = function ( type, h, key ) {
  198. var keys = this.data.hierarchy[ h ].keys;
  199. if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM ||
  200. this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
  201. key = key < keys.length - 1 ? key : keys.length - 1;
  202. } else {
  203. key = key % keys.length;
  204. }
  205. for ( ; key < keys.length; key++ ) {
  206. if ( keys[ key ][ type ] !== undefined ) {
  207. return keys[ key ];
  208. }
  209. }
  210. return this.data.hierarchy[ h ].keys[ 0 ];
  211. };
  212. // Get previous key with
  213. THREE.Animation.prototype.getPrevKeyWith = function ( type, h, key ) {
  214. var keys = this.data.hierarchy[ h ].keys;
  215. if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM ||
  216. this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
  217. key = key > 0 ? key : 0;
  218. } else {
  219. key = key >= 0 ? key : key + keys.length;
  220. }
  221. for ( ; key >= 0; key -- ) {
  222. if ( keys[ key ][ type ] !== undefined ) {
  223. return keys[ key ];
  224. }
  225. }
  226. return this.data.hierarchy[ h ].keys[ keys.length - 1 ];
  227. };