MorphBlendMesh.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. ( function () {
  2. class MorphBlendMesh extends THREE.Mesh {
  3. constructor( geometry, material ) {
  4. super( geometry, material );
  5. this.animationsMap = {};
  6. this.animationsList = []; // prepare default animation
  7. // (all frames played together in 1 second)
  8. const numFrames = Object.keys( this.morphTargetDictionary ).length;
  9. const name = '__default';
  10. const startFrame = 0;
  11. const endFrame = numFrames - 1;
  12. const fps = numFrames / 1;
  13. this.createAnimation( name, startFrame, endFrame, fps );
  14. this.setAnimationWeight( name, 1 );
  15. }
  16. createAnimation( name, start, end, fps ) {
  17. const animation = {
  18. start: start,
  19. end: end,
  20. length: end - start + 1,
  21. fps: fps,
  22. duration: ( end - start ) / fps,
  23. lastFrame: 0,
  24. currentFrame: 0,
  25. active: false,
  26. time: 0,
  27. direction: 1,
  28. weight: 1,
  29. directionBackwards: false,
  30. mirroredLoop: false
  31. };
  32. this.animationsMap[ name ] = animation;
  33. this.animationsList.push( animation );
  34. }
  35. autoCreateAnimations( fps ) {
  36. const pattern = /([a-z]+)_?(\d+)/i;
  37. let firstAnimation;
  38. const frameRanges = {};
  39. let i = 0;
  40. for ( const key in this.morphTargetDictionary ) {
  41. const chunks = key.match( pattern );
  42. if ( chunks && chunks.length > 1 ) {
  43. const name = chunks[ 1 ];
  44. if ( ! frameRanges[ name ] ) frameRanges[ name ] = {
  45. start: Infinity,
  46. end: - Infinity
  47. };
  48. const range = frameRanges[ name ];
  49. if ( i < range.start ) range.start = i;
  50. if ( i > range.end ) range.end = i;
  51. if ( ! firstAnimation ) firstAnimation = name;
  52. }
  53. i ++;
  54. }
  55. for ( const name in frameRanges ) {
  56. const range = frameRanges[ name ];
  57. this.createAnimation( name, range.start, range.end, fps );
  58. }
  59. this.firstAnimation = firstAnimation;
  60. }
  61. setAnimationDirectionForward( name ) {
  62. const animation = this.animationsMap[ name ];
  63. if ( animation ) {
  64. animation.direction = 1;
  65. animation.directionBackwards = false;
  66. }
  67. }
  68. setAnimationDirectionBackward( name ) {
  69. const animation = this.animationsMap[ name ];
  70. if ( animation ) {
  71. animation.direction = - 1;
  72. animation.directionBackwards = true;
  73. }
  74. }
  75. setAnimationFPS( name, fps ) {
  76. const animation = this.animationsMap[ name ];
  77. if ( animation ) {
  78. animation.fps = fps;
  79. animation.duration = ( animation.end - animation.start ) / animation.fps;
  80. }
  81. }
  82. setAnimationDuration( name, duration ) {
  83. const animation = this.animationsMap[ name ];
  84. if ( animation ) {
  85. animation.duration = duration;
  86. animation.fps = ( animation.end - animation.start ) / animation.duration;
  87. }
  88. }
  89. setAnimationWeight( name, weight ) {
  90. const animation = this.animationsMap[ name ];
  91. if ( animation ) {
  92. animation.weight = weight;
  93. }
  94. }
  95. setAnimationTime( name, time ) {
  96. const animation = this.animationsMap[ name ];
  97. if ( animation ) {
  98. animation.time = time;
  99. }
  100. }
  101. getAnimationTime( name ) {
  102. let time = 0;
  103. const animation = this.animationsMap[ name ];
  104. if ( animation ) {
  105. time = animation.time;
  106. }
  107. return time;
  108. }
  109. getAnimationDuration( name ) {
  110. let duration = - 1;
  111. const animation = this.animationsMap[ name ];
  112. if ( animation ) {
  113. duration = animation.duration;
  114. }
  115. return duration;
  116. }
  117. playAnimation( name ) {
  118. const animation = this.animationsMap[ name ];
  119. if ( animation ) {
  120. animation.time = 0;
  121. animation.active = true;
  122. } else {
  123. console.warn( 'THREE.MorphBlendMesh: animation[' + name + '] undefined in .playAnimation()' );
  124. }
  125. }
  126. stopAnimation( name ) {
  127. const animation = this.animationsMap[ name ];
  128. if ( animation ) {
  129. animation.active = false;
  130. }
  131. }
  132. update( delta ) {
  133. for ( let i = 0, il = this.animationsList.length; i < il; i ++ ) {
  134. const animation = this.animationsList[ i ];
  135. if ( ! animation.active ) continue;
  136. const frameTime = animation.duration / animation.length;
  137. animation.time += animation.direction * delta;
  138. if ( animation.mirroredLoop ) {
  139. if ( animation.time > animation.duration || animation.time < 0 ) {
  140. animation.direction *= - 1;
  141. if ( animation.time > animation.duration ) {
  142. animation.time = animation.duration;
  143. animation.directionBackwards = true;
  144. }
  145. if ( animation.time < 0 ) {
  146. animation.time = 0;
  147. animation.directionBackwards = false;
  148. }
  149. }
  150. } else {
  151. animation.time = animation.time % animation.duration;
  152. if ( animation.time < 0 ) animation.time += animation.duration;
  153. }
  154. const keyframe = animation.start + THREE.MathUtils.clamp( Math.floor( animation.time / frameTime ), 0, animation.length - 1 );
  155. const weight = animation.weight;
  156. if ( keyframe !== animation.currentFrame ) {
  157. this.morphTargetInfluences[ animation.lastFrame ] = 0;
  158. this.morphTargetInfluences[ animation.currentFrame ] = 1 * weight;
  159. this.morphTargetInfluences[ keyframe ] = 0;
  160. animation.lastFrame = animation.currentFrame;
  161. animation.currentFrame = keyframe;
  162. }
  163. let mix = animation.time % frameTime / frameTime;
  164. if ( animation.directionBackwards ) mix = 1 - mix;
  165. if ( animation.currentFrame !== animation.lastFrame ) {
  166. this.morphTargetInfluences[ animation.currentFrame ] = mix * weight;
  167. this.morphTargetInfluences[ animation.lastFrame ] = ( 1 - mix ) * weight;
  168. } else {
  169. this.morphTargetInfluences[ animation.currentFrame ] = weight;
  170. }
  171. }
  172. }
  173. }
  174. THREE.MorphBlendMesh = MorphBlendMesh;
  175. } )();