MorphBlendMesh.js 6.1 KB

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