Animation.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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, data ) {
  7. this.root = root;
  8. this.data = THREE.AnimationHandler.init( data );
  9. this.hierarchy = THREE.AnimationHandler.parse( root );
  10. this.currentTime = 0;
  11. this.timeScale = 1;
  12. this.isPlaying = false;
  13. this.loop = true;
  14. this.weight = 0;
  15. this.interpolationType = THREE.AnimationHandler.LINEAR;
  16. };
  17. THREE.Animation.prototype.keyTypes = [ "pos", "rot", "scl" ];
  18. THREE.Animation.prototype.play = function ( startTime, weight ) {
  19. this.currentTime = startTime !== undefined ? startTime : 0;
  20. this.weight = weight !== undefined ? weight: 1;
  21. this.isPlaying = true;
  22. this.reset();
  23. THREE.AnimationHandler.play( this );
  24. };
  25. THREE.Animation.prototype.stop = function() {
  26. this.isPlaying = false;
  27. THREE.AnimationHandler.stop( this );
  28. };
  29. THREE.Animation.prototype.reset = function () {
  30. for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
  31. var object = this.hierarchy[ h ];
  32. if ( object.animationCache === undefined ) {
  33. object.animationCache = {
  34. animations: {},
  35. blending: {
  36. positionWeight: 0.0,
  37. quaternionWeight: 0.0,
  38. scaleWeight: 0.0
  39. }
  40. };
  41. }
  42. if ( object.animationCache.animations[this.data.name] === undefined ) {
  43. object.animationCache.animations[this.data.name] = {};
  44. object.animationCache.animations[this.data.name].prevKey = { pos: 0, rot: 0, scl: 0 };
  45. object.animationCache.animations[this.data.name].nextKey = { pos: 0, rot: 0, scl: 0 };
  46. object.animationCache.animations[this.data.name].originalMatrix = object.matrix;
  47. }
  48. var animationCache = object.animationCache.animations[this.data.name];
  49. // Get keys to match our current time
  50. for ( var t = 0; t < 3; t ++ ) {
  51. var type = this.keyTypes[ t ];
  52. var prevKey = this.data.hierarchy[ h ].keys[ 0 ];
  53. var nextKey = this.getNextKeyWith( type, h, 1 );
  54. while ( nextKey.time < this.currentTime && nextKey.index > prevKey.index ) {
  55. prevKey = nextKey;
  56. nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 );
  57. }
  58. animationCache.prevKey[ type ] = prevKey;
  59. animationCache.nextKey[ type ] = nextKey;
  60. }
  61. }
  62. };
  63. THREE.Animation.prototype.resetBlendWeights = function () {
  64. for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
  65. var object = this.hierarchy[ h ];
  66. if ( object.animationCache !== undefined ) {
  67. object.animationCache.blending.positionWeight = 0.0;
  68. object.animationCache.blending.quaternionWeight = 0.0;
  69. object.animationCache.blending.scaleWeight = 0.0;
  70. }
  71. }
  72. };
  73. THREE.Animation.prototype.update = (function(){
  74. var points = [];
  75. var target = new THREE.Vector3();
  76. var newVector = new THREE.Vector3();
  77. var newQuat = new THREE.Quaternion();
  78. // Catmull-Rom spline
  79. var interpolateCatmullRom = function ( points, scale ) {
  80. var c = [], v3 = [],
  81. point, intPoint, weight, w2, w3,
  82. pa, pb, pc, pd;
  83. point = ( points.length - 1 ) * scale;
  84. intPoint = Math.floor( point );
  85. weight = point - intPoint;
  86. c[ 0 ] = intPoint === 0 ? intPoint : intPoint - 1;
  87. c[ 1 ] = intPoint;
  88. c[ 2 ] = intPoint > points.length - 2 ? intPoint : intPoint + 1;
  89. c[ 3 ] = intPoint > points.length - 3 ? intPoint : intPoint + 2;
  90. pa = points[ c[ 0 ] ];
  91. pb = points[ c[ 1 ] ];
  92. pc = points[ c[ 2 ] ];
  93. pd = points[ c[ 3 ] ];
  94. w2 = weight * weight;
  95. w3 = weight * w2;
  96. v3[ 0 ] = interpolate( pa[ 0 ], pb[ 0 ], pc[ 0 ], pd[ 0 ], weight, w2, w3 );
  97. v3[ 1 ] = interpolate( pa[ 1 ], pb[ 1 ], pc[ 1 ], pd[ 1 ], weight, w2, w3 );
  98. v3[ 2 ] = interpolate( pa[ 2 ], pb[ 2 ], pc[ 2 ], pd[ 2 ], weight, w2, w3 );
  99. return v3;
  100. };
  101. var interpolate = function ( p0, p1, p2, p3, t, t2, t3 ) {
  102. var v0 = ( p2 - p0 ) * 0.5,
  103. v1 = ( p3 - p1 ) * 0.5;
  104. return ( 2 * ( p1 - p2 ) + v0 + v1 ) * t3 + ( - 3 * ( p1 - p2 ) - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  105. };
  106. return function ( delta ) {
  107. if ( this.isPlaying === false ) return;
  108. this.currentTime += delta * this.timeScale;
  109. if ( this.weight === 0 )
  110. return;
  111. //
  112. var duration = this.data.length;
  113. if ( this.currentTime > duration || this.currentTime < 0 ) {
  114. if ( this.loop ) {
  115. this.currentTime %= duration;
  116. if ( this.currentTime < 0 )
  117. this.currentTime += duration;
  118. this.reset();
  119. } else {
  120. this.stop();
  121. }
  122. }
  123. for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
  124. var object = this.hierarchy[ h ];
  125. var animationCache = object.animationCache.animations[this.data.name];
  126. var blending = object.animationCache.blending;
  127. // loop through pos/rot/scl
  128. for ( var t = 0; t < 3; t ++ ) {
  129. // get keys
  130. var type = this.keyTypes[ t ];
  131. var prevKey = animationCache.prevKey[ type ];
  132. var nextKey = animationCache.nextKey[ type ];
  133. if ( ( this.timeScale > 0 && nextKey.time <= this.currentTime ) ||
  134. ( this.timeScale < 0 && prevKey.time >= this.currentTime ) ) {
  135. prevKey = this.data.hierarchy[ h ].keys[ 0 ];
  136. nextKey = this.getNextKeyWith( type, h, 1 );
  137. while ( nextKey.time < this.currentTime && nextKey.index > prevKey.index ) {
  138. prevKey = nextKey;
  139. nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 );
  140. }
  141. animationCache.prevKey[ type ] = prevKey;
  142. animationCache.nextKey[ type ] = nextKey;
  143. }
  144. var scale = ( this.currentTime - prevKey.time ) / ( nextKey.time - prevKey.time );
  145. var prevXYZ = prevKey[ type ];
  146. var nextXYZ = nextKey[ type ];
  147. if ( scale < 0 ) scale = 0;
  148. if ( scale > 1 ) scale = 1;
  149. // interpolate
  150. if ( type === "pos" ) {
  151. if ( this.interpolationType === THREE.AnimationHandler.LINEAR ) {
  152. newVector.x = prevXYZ[ 0 ] + ( nextXYZ[ 0 ] - prevXYZ[ 0 ] ) * scale;
  153. newVector.y = prevXYZ[ 1 ] + ( nextXYZ[ 1 ] - prevXYZ[ 1 ] ) * scale;
  154. newVector.z = prevXYZ[ 2 ] + ( nextXYZ[ 2 ] - prevXYZ[ 2 ] ) * scale;
  155. // blend
  156. var proportionalWeight = this.weight / ( this.weight + blending.positionWeight );
  157. object.position.lerp( newVector, proportionalWeight );
  158. blending.positionWeight += this.weight;
  159. } else if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM ||
  160. this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
  161. points[ 0 ] = this.getPrevKeyWith( "pos", h, prevKey.index - 1 )[ "pos" ];
  162. points[ 1 ] = prevXYZ;
  163. points[ 2 ] = nextXYZ;
  164. points[ 3 ] = this.getNextKeyWith( "pos", h, nextKey.index + 1 )[ "pos" ];
  165. scale = scale * 0.33 + 0.33;
  166. var currentPoint = interpolateCatmullRom( points, scale );
  167. var proportionalWeight = this.weight / ( this.weight + blending.positionWeight );
  168. blending.positionWeight += this.weight;
  169. // blend
  170. var vector = object.position;
  171. vector.x = vector.x + ( currentPoint[ 0 ] - vector.x ) * proportionalWeight;
  172. vector.y = vector.y + ( currentPoint[ 1 ] - vector.y ) * proportionalWeight;
  173. vector.z = vector.z + ( currentPoint[ 2 ] - vector.z ) * proportionalWeight;
  174. if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
  175. var forwardPoint = interpolateCatmullRom( points, scale * 1.01 );
  176. target.set( forwardPoint[ 0 ], forwardPoint[ 1 ], forwardPoint[ 2 ] );
  177. target.sub( vector );
  178. target.y = 0;
  179. target.normalize();
  180. var angle = Math.atan2( target.x, target.z );
  181. object.rotation.set( 0, angle, 0 );
  182. }
  183. }
  184. } else if ( type === "rot" ) {
  185. THREE.Quaternion.slerp( prevXYZ, nextXYZ, newQuat, scale );
  186. // Avoid paying the cost of an additional slerp if we don't have to
  187. if ( blending.quaternionWeight === 0 ) {
  188. object.quaternion.copy(newQuat);
  189. blending.quaternionWeight = this.weight;
  190. } else {
  191. var proportionalWeight = this.weight / ( this.weight + blending.quaternionWeight );
  192. THREE.Quaternion.slerp( object.quaternion, newQuat, object.quaternion, proportionalWeight );
  193. blending.quaternionWeight += this.weight;
  194. }
  195. } else if ( type === "scl" ) {
  196. newVector.x = prevXYZ[ 0 ] + ( nextXYZ[ 0 ] - prevXYZ[ 0 ] ) * scale;
  197. newVector.y = prevXYZ[ 1 ] + ( nextXYZ[ 1 ] - prevXYZ[ 1 ] ) * scale;
  198. newVector.z = prevXYZ[ 2 ] + ( nextXYZ[ 2 ] - prevXYZ[ 2 ] ) * scale;
  199. var proportionalWeight = this.weight / ( this.weight + blending.scaleWeight );
  200. object.scale.lerp( newVector, proportionalWeight );
  201. blending.scaleWeight += this.weight;
  202. }
  203. }
  204. }
  205. return true;
  206. };
  207. })();
  208. // Get next key with
  209. THREE.Animation.prototype.getNextKeyWith = function ( type, h, key ) {
  210. var keys = this.data.hierarchy[ h ].keys;
  211. if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM ||
  212. this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
  213. key = key < keys.length - 1 ? key : keys.length - 1;
  214. } else {
  215. key = key % keys.length;
  216. }
  217. for ( ; key < keys.length; key ++ ) {
  218. if ( keys[ key ][ type ] !== undefined ) {
  219. return keys[ key ];
  220. }
  221. }
  222. return this.data.hierarchy[ h ].keys[ 0 ];
  223. };
  224. // Get previous key with
  225. THREE.Animation.prototype.getPrevKeyWith = function ( type, h, key ) {
  226. var keys = this.data.hierarchy[ h ].keys;
  227. if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM ||
  228. this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
  229. key = key > 0 ? key : 0;
  230. } else {
  231. key = key >= 0 ? key : key + keys.length;
  232. }
  233. for ( ; key >= 0; key -- ) {
  234. if ( keys[ key ][ type ] !== undefined ) {
  235. return keys[ key ];
  236. }
  237. }
  238. return this.data.hierarchy[ h ].keys[ keys.length - 1 ];
  239. };