MorphBlendMesh.js 5.6 KB

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