MorphBlendMesh.js 5.4 KB

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