MorphAnimMesh.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {
  2. AnimationClip,
  3. AnimationMixer,
  4. Mesh
  5. } from 'three';
  6. /**
  7. * A special type of an animated mesh with a simple interface
  8. * for animation playback. It allows to playback just one animation
  9. * without any transitions or fading between animation changes.
  10. *
  11. * @augments Mesh
  12. * @three_import import { MorphAnimMesh } from 'three/addons/misc/MorphAnimMesh.js';
  13. */
  14. class MorphAnimMesh extends Mesh {
  15. /**
  16. * Constructs a new morph anim 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. this.type = 'MorphAnimMesh';
  24. /**
  25. * The internal animation mixer.
  26. *
  27. * @type {AnimationMixer}
  28. */
  29. this.mixer = new AnimationMixer( this );
  30. /**
  31. * The current active animation action.
  32. *
  33. * @type {?AnimationAction}
  34. * @default null
  35. */
  36. this.activeAction = null;
  37. }
  38. /**
  39. * Sets the animation playback direction to "forward".
  40. */
  41. setDirectionForward() {
  42. this.mixer.timeScale = 1.0;
  43. }
  44. /**
  45. * Sets the animation playback direction to "backward".
  46. */
  47. setDirectionBackward() {
  48. this.mixer.timeScale = - 1.0;
  49. }
  50. /**
  51. * Plays the defined animation clip. The implementation assumes the animation
  52. * clips are stored in {@link Object3D#animations} or the geometry.
  53. *
  54. * @param {string} label - The name of the animation clip.
  55. * @param {number} fps - The FPS of the animation clip.
  56. */
  57. playAnimation( label, fps ) {
  58. if ( this.activeAction ) {
  59. this.activeAction.stop();
  60. this.activeAction = null;
  61. }
  62. const clip = AnimationClip.findByName( this, label );
  63. if ( clip ) {
  64. const action = this.mixer.clipAction( clip );
  65. action.timeScale = ( clip.tracks.length * fps ) / clip.duration;
  66. this.activeAction = action.play();
  67. } else {
  68. throw new Error( 'THREE.MorphAnimMesh: animations[' + label + '] undefined in .playAnimation()' );
  69. }
  70. }
  71. /**
  72. * Updates the animations of the mesh. Must be called inside the animation loop.
  73. *
  74. * @param {number} delta - The delta time in seconds.
  75. */
  76. updateAnimation( delta ) {
  77. this.mixer.update( delta );
  78. }
  79. copy( source, recursive ) {
  80. super.copy( source, recursive );
  81. this.mixer = new AnimationMixer( this );
  82. return this;
  83. }
  84. }
  85. export { MorphAnimMesh };