Animation.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * @author mikael emtinger / http://gomo.se/
  3. */
  4. THREE.Animation = function( root, data ) {
  5. this.root = root;
  6. this.data = data;
  7. this.hierarchy = [];
  8. this.startTime = 0;
  9. this.isPlaying = false;
  10. this.loop = true;
  11. this.offset = 0;
  12. // need to initialize data?
  13. if( !this.data.initialized )
  14. THREE.AnimationHandler.initData( this.data );
  15. // setup hierarchy
  16. if( root instanceof THREE.SkinnedMesh ) {
  17. for( var b = 0; b < this.root.bones.length; b++ )
  18. this.hierarchy.push( this.root.bones[ b ] );
  19. } else {
  20. // parse hierarchy and match against animation (somehow)
  21. }
  22. }
  23. /*
  24. * Play
  25. */
  26. THREE.Animation.prototype.play = function( loop ) {
  27. if( !this.isPlaying ) {
  28. this.isPlaying = true;
  29. this.startTime = new Date().getTime() * 0.001;
  30. // reset key cache
  31. for( var h = 0; h < this.hierarchy.length; h++ ) {
  32. this.hierarchy[ h ].useQuaternion = true;
  33. this.hierarchy[ h ].matrixAutoUpdate = true;
  34. if( this.hierarchy[ h ].prevKey === undefined ) {
  35. this.hierarchy[ h ].prevKey = { pos: 0, rot: 0, scl: 0 };
  36. this.hierarchy[ h ].nextKey = { pos: 0, rot: 0, scl: 0 };
  37. }
  38. this.hierarchy[ h ].prevKey.pos = this.data.hierarchy[ h ].keys[ 0 ];
  39. this.hierarchy[ h ].prevKey.rot = this.data.hierarchy[ h ].keys[ 0 ];
  40. this.hierarchy[ h ].prevKey.scl = this.data.hierarchy[ h ].keys[ 0 ];
  41. this.hierarchy[ h ].nextKey.pos = this.getNextKeyWith( "pos", h, 1 );
  42. this.hierarchy[ h ].nextKey.rot = this.getNextKeyWith( "rot", h, 1 );
  43. this.hierarchy[ h ].nextKey.scl = this.getNextKeyWith( "scl", h, 1 );
  44. }
  45. this.update();
  46. THREE.AnimationHandler.add( this );
  47. }
  48. };
  49. /*
  50. * Pause
  51. */
  52. THREE.Animation.prototype.pause = function() {
  53. THREE.AnimationHandler.remove( this );
  54. // todo
  55. }
  56. /*
  57. * Stop
  58. */
  59. THREE.Animation.prototype.stop = function() {
  60. this.isPlaying = false;
  61. THREE.AnimationHandler.remove( this );
  62. }
  63. /*
  64. * Update
  65. */
  66. THREE.Animation.prototype.update = function( time ) {
  67. // todo: add input time
  68. // early out
  69. if( !this.isPlaying ) return;
  70. // vars
  71. var types = [ "pos", "rot", "scl" ];
  72. var scale;
  73. var relative;
  74. var vector;
  75. var prevXYZ, nextXYZ;
  76. var object;
  77. var frame;
  78. var JIThierarchy = this.data.JIT.hierarchy;
  79. // update
  80. var currentTime = new Date().getTime() * 0.001 - this.startTime + this.offset;
  81. var unloopedCurrentTime = currentTime;
  82. // looped?
  83. if( currentTime > this.data.length ) {
  84. while( currentTime > this.data.length )
  85. currentTime -= this.data.length;
  86. this.startTime = new Date().getTime() * 0.001 - currentTime;
  87. currentTime = new Date().getTime() * 0.001 - this.startTime;
  88. }
  89. frame = Math.min( parseInt( currentTime * this.data.fps ), parseInt( this.data.length * this.data.fps ) );
  90. // update
  91. for( var h = 0, hl = this.hierarchy.length; h < hl; h++ ) {
  92. object = this.hierarchy[ h ];
  93. if( JIThierarchy[ h ][ frame ] !== undefined ) {
  94. object.skinMatrix = JIThierarchy[ h ][ frame ];
  95. object.matrixAutoUpdate = false;
  96. object.matrixNeedsUpdate = false;
  97. object.skinMatrix.flattenToArrayOffset( this.root.boneMatrices, h * 16 );
  98. }
  99. else {
  100. for( var t = 0; t < 3; t++ ) {
  101. // get keys
  102. var type = types[ t ];
  103. var prevKey = object.prevKey[ type ];
  104. var nextKey = object.nextKey[ type ];
  105. // switch keys?
  106. if( nextKey.time < unloopedCurrentTime ) {
  107. // did we loop?
  108. if( currentTime < unloopedCurrentTime ) {
  109. if( this.loop ) {
  110. prevKey = this.data.hierarchy[ h ].keys[ 0 ];
  111. nextKey = this.getNextKeyWith( type, h, 1 );
  112. } else {
  113. this.stop();
  114. return;
  115. }
  116. } else {
  117. do {
  118. prevKey = nextKey;
  119. nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 );
  120. }
  121. while( nextKey.time < currentTime )
  122. }
  123. object.prevKey[ type ] = prevKey;
  124. object.nextKey[ type ] = nextKey;
  125. }
  126. // interpolate rot (quaternion slerp)
  127. object.matrixAutoUpdate = true;
  128. object.matrixNeedsUpdate = true;
  129. scale = ( currentTime - prevKey.time ) / ( nextKey.time - prevKey.time );
  130. prevXYZ = prevKey[ type ];
  131. nextXYZ = nextKey[ type ];
  132. if( type === "rot" ) {
  133. if( scale < 0 || scale > 1 ) {
  134. console.log( "Scale out of bounds:" + scale );
  135. scale = scale < 0 ? 0 : 1;
  136. }
  137. THREE.Quaternion.slerp( prevXYZ, nextXYZ, object.quaternion, scale );
  138. }
  139. // lerp pos/scl
  140. else {
  141. vector = type === "pos" ? object.position : object.scale;
  142. vector.x = prevXYZ[ 0 ] + ( nextXYZ[ 0 ] - prevXYZ[ 0 ] ) * scale;
  143. vector.y = prevXYZ[ 1 ] + ( nextXYZ[ 1 ] - prevXYZ[ 1 ] ) * scale;
  144. vector.z = prevXYZ[ 2 ] + ( nextXYZ[ 2 ] - prevXYZ[ 2 ] ) * scale;
  145. }
  146. }
  147. }
  148. }
  149. // update JIT?
  150. if( JIThierarchy[ 0 ][ frame ] === undefined ) {
  151. this.hierarchy[ 0 ].update( undefined, true );
  152. for( var h = 0; h < this.hierarchy.length; h++ )
  153. JIThierarchy[ h ][ frame ] = this.hierarchy[ h ].skinMatrix.clone();
  154. }
  155. };
  156. /*
  157. * Update Object
  158. */
  159. THREE.Animation.prototype.updateObject = function( h, currentTime, unloopedCurrentTime ) {
  160. }
  161. THREE.Animation.prototype.getNextKeyWith = function( type, h, key ) {
  162. var keys = this.data.hierarchy[ h ].keys;
  163. for( ; key < keys.length; key++ ) {
  164. if( keys[ key ][ type ] !== undefined )
  165. return keys[ key ];
  166. }
  167. return this.data.hierarchy[ h ].keys[ 0 ];
  168. }
粤ICP备19079148号