MorphBlendMesh.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. import {
  2. MathUtils,
  3. Mesh
  4. } from 'three';
  5. /**
  6. * A special type of an animated mesh with a more advanced interface
  7. * for animation playback. Unlike {@link MorphAnimMesh}. It allows to
  8. * playback more than one morph animation at the same time but without
  9. * fading options.
  10. *
  11. * @augments Mesh
  12. */
  13. class MorphBlendMesh extends Mesh {
  14. /**
  15. * Constructs a new morph blend mesh.
  16. *
  17. * @param {BufferGeometry} [geometry] - The mesh geometry.
  18. * @param {Material|Array<Material>} [material] - The mesh material.
  19. */
  20. constructor( geometry, material ) {
  21. super( geometry, material );
  22. /**
  23. * A dictionary of animations.
  24. *
  25. * @type {Object<string,Object>}
  26. */
  27. this.animationsMap = {};
  28. /**
  29. * A list of animations.
  30. *
  31. * @type {Array<Object>}
  32. */
  33. this.animationsList = [];
  34. // prepare default animation
  35. // (all frames played together in 1 second)
  36. const numFrames = Object.keys( this.morphTargetDictionary ).length;
  37. const name = '__default';
  38. const startFrame = 0;
  39. const endFrame = numFrames - 1;
  40. const fps = numFrames / 1;
  41. this.createAnimation( name, startFrame, endFrame, fps );
  42. this.setAnimationWeight( name, 1 );
  43. }
  44. /**
  45. * Creates a new animation.
  46. *
  47. * @param {string} name - The animation name.
  48. * @param {number} start - The start time.
  49. * @param {number} end - The end time.
  50. * @param {number} fps - The FPS.
  51. */
  52. createAnimation( name, start, end, fps ) {
  53. const animation = {
  54. start: start,
  55. end: end,
  56. length: end - start + 1,
  57. fps: fps,
  58. duration: ( end - start ) / fps,
  59. lastFrame: 0,
  60. currentFrame: 0,
  61. active: false,
  62. time: 0,
  63. direction: 1,
  64. weight: 1,
  65. directionBackwards: false,
  66. mirroredLoop: false
  67. };
  68. this.animationsMap[ name ] = animation;
  69. this.animationsList.push( animation );
  70. }
  71. /**
  72. * Automatically creates animations based on the values in
  73. * {@link Mesh#morphTargetDictionary}.
  74. *
  75. * @param {number} fps - The FPS of all animations.
  76. */
  77. autoCreateAnimations( fps ) {
  78. const pattern = /([a-z]+)_?(\d+)/i;
  79. let firstAnimation;
  80. const frameRanges = {};
  81. let i = 0;
  82. for ( const key in this.morphTargetDictionary ) {
  83. const chunks = key.match( pattern );
  84. if ( chunks && chunks.length > 1 ) {
  85. const name = chunks[ 1 ];
  86. if ( ! frameRanges[ name ] ) frameRanges[ name ] = { start: Infinity, end: - Infinity };
  87. const range = frameRanges[ name ];
  88. if ( i < range.start ) range.start = i;
  89. if ( i > range.end ) range.end = i;
  90. if ( ! firstAnimation ) firstAnimation = name;
  91. }
  92. i ++;
  93. }
  94. for ( const name in frameRanges ) {
  95. const range = frameRanges[ name ];
  96. this.createAnimation( name, range.start, range.end, fps );
  97. }
  98. this.firstAnimation = firstAnimation;
  99. }
  100. /**
  101. * Sets the animation playback direction to "forward" for the
  102. * defined animation.
  103. *
  104. * @param {string} name - The animation name.
  105. */
  106. setAnimationDirectionForward( name ) {
  107. const animation = this.animationsMap[ name ];
  108. if ( animation ) {
  109. animation.direction = 1;
  110. animation.directionBackwards = false;
  111. }
  112. }
  113. /**
  114. * Sets the animation playback direction to "backward" for the
  115. * defined animation.
  116. *
  117. * @param {string} name - The animation name.
  118. */
  119. setAnimationDirectionBackward( name ) {
  120. const animation = this.animationsMap[ name ];
  121. if ( animation ) {
  122. animation.direction = - 1;
  123. animation.directionBackwards = true;
  124. }
  125. }
  126. /**
  127. * Sets the FPS to the given value for the defined animation.
  128. *
  129. * @param {string} name - The animation name.
  130. * @param {number} fps - The FPS to set.
  131. */
  132. setAnimationFPS( name, fps ) {
  133. const animation = this.animationsMap[ name ];
  134. if ( animation ) {
  135. animation.fps = fps;
  136. animation.duration = ( animation.end - animation.start ) / animation.fps;
  137. }
  138. }
  139. /**
  140. * Sets the duration to the given value for the defined animation.
  141. *
  142. * @param {string} name - The animation name.
  143. * @param {number} duration - The duration to set.
  144. */
  145. setAnimationDuration( name, duration ) {
  146. const animation = this.animationsMap[ name ];
  147. if ( animation ) {
  148. animation.duration = duration;
  149. animation.fps = ( animation.end - animation.start ) / animation.duration;
  150. }
  151. }
  152. /**
  153. * Sets the weight to the given value for the defined animation.
  154. *
  155. * @param {string} name - The animation name.
  156. * @param {number} weight - The weight to set.
  157. */
  158. setAnimationWeight( name, weight ) {
  159. const animation = this.animationsMap[ name ];
  160. if ( animation ) {
  161. animation.weight = weight;
  162. }
  163. }
  164. /**
  165. * Sets the time to the given value for the defined animation.
  166. *
  167. * @param {string} name - The animation name.
  168. * @param {number} time - The time to set.
  169. */
  170. setAnimationTime( name, time ) {
  171. const animation = this.animationsMap[ name ];
  172. if ( animation ) {
  173. animation.time = time;
  174. }
  175. }
  176. /**
  177. * Returns the time for the defined animation.
  178. *
  179. * @param {string} name - The animation name.
  180. * @return {number} The time.
  181. */
  182. getAnimationTime( name ) {
  183. let time = 0;
  184. const animation = this.animationsMap[ name ];
  185. if ( animation ) {
  186. time = animation.time;
  187. }
  188. return time;
  189. }
  190. /**
  191. * Returns the duration for the defined animation.
  192. *
  193. * @param {string} name - The animation name.
  194. * @return {number} The duration.
  195. */
  196. getAnimationDuration( name ) {
  197. let duration = - 1;
  198. const animation = this.animationsMap[ name ];
  199. if ( animation ) {
  200. duration = animation.duration;
  201. }
  202. return duration;
  203. }
  204. /**
  205. * Plays the defined animation.
  206. *
  207. * @param {string} name - The animation name.
  208. */
  209. playAnimation( name ) {
  210. const animation = this.animationsMap[ name ];
  211. if ( animation ) {
  212. animation.time = 0;
  213. animation.active = true;
  214. } else {
  215. console.warn( 'THREE.MorphBlendMesh: animation[' + name + '] undefined in .playAnimation()' );
  216. }
  217. }
  218. /**
  219. * Stops the defined animation.
  220. *
  221. * @param {string} name - The animation name.
  222. */
  223. stopAnimation( name ) {
  224. const animation = this.animationsMap[ name ];
  225. if ( animation ) {
  226. animation.active = false;
  227. }
  228. }
  229. /**
  230. * Updates the animations of the mesh.
  231. *
  232. * @param {number} delta - The delta time in seconds.
  233. */
  234. update( delta ) {
  235. for ( let i = 0, il = this.animationsList.length; i < il; i ++ ) {
  236. const animation = this.animationsList[ i ];
  237. if ( ! animation.active ) continue;
  238. const frameTime = animation.duration / animation.length;
  239. animation.time += animation.direction * delta;
  240. if ( animation.mirroredLoop ) {
  241. if ( animation.time > animation.duration || animation.time < 0 ) {
  242. animation.direction *= - 1;
  243. if ( animation.time > animation.duration ) {
  244. animation.time = animation.duration;
  245. animation.directionBackwards = true;
  246. }
  247. if ( animation.time < 0 ) {
  248. animation.time = 0;
  249. animation.directionBackwards = false;
  250. }
  251. }
  252. } else {
  253. animation.time = animation.time % animation.duration;
  254. if ( animation.time < 0 ) animation.time += animation.duration;
  255. }
  256. const keyframe = animation.start + MathUtils.clamp( Math.floor( animation.time / frameTime ), 0, animation.length - 1 );
  257. const weight = animation.weight;
  258. if ( keyframe !== animation.currentFrame ) {
  259. this.morphTargetInfluences[ animation.lastFrame ] = 0;
  260. this.morphTargetInfluences[ animation.currentFrame ] = 1 * weight;
  261. this.morphTargetInfluences[ keyframe ] = 0;
  262. animation.lastFrame = animation.currentFrame;
  263. animation.currentFrame = keyframe;
  264. }
  265. let mix = ( animation.time % frameTime ) / frameTime;
  266. if ( animation.directionBackwards ) mix = 1 - mix;
  267. if ( animation.currentFrame !== animation.lastFrame ) {
  268. this.morphTargetInfluences[ animation.currentFrame ] = mix * weight;
  269. this.morphTargetInfluences[ animation.lastFrame ] = ( 1 - mix ) * weight;
  270. } else {
  271. this.morphTargetInfluences[ animation.currentFrame ] = weight;
  272. }
  273. }
  274. }
  275. }
  276. export { MorphBlendMesh };
粤ICP备19079148号