MorphBlendMesh.js 7.9 KB

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