2
0

MorphBlendMesh.js 5.8 KB

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