MorphBlendMesh.js 5.9 KB

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