MorphAnimMesh.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. */
  13. class MorphAnimMesh extends Mesh {
  14. /**
  15. * Constructs a new morph anim 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. this.type = 'MorphAnimMesh';
  23. /**
  24. * The internal animation mixer.
  25. *
  26. * @type {AnimationMixer}
  27. */
  28. this.mixer = new AnimationMixer( this );
  29. /**
  30. * The current active animation action.
  31. *
  32. * @type {?AnimationAction}
  33. * @default null
  34. */
  35. this.activeAction = null;
  36. }
  37. /**
  38. * Sets the animation playback direction to "forward".
  39. */
  40. setDirectionForward() {
  41. this.mixer.timeScale = 1.0;
  42. }
  43. /**
  44. * Sets the animation playback direction to "backward".
  45. */
  46. setDirectionBackward() {
  47. this.mixer.timeScale = - 1.0;
  48. }
  49. /**
  50. * Plays the defined animation clip. The implementation assumes the animation
  51. * clips are stored in {@link Object3D#animations} or the geometry.
  52. *
  53. * @param {string} label - The name of the animation clip.
  54. * @param {number} fps - The FPS of the animation clip.
  55. */
  56. playAnimation( label, fps ) {
  57. if ( this.activeAction ) {
  58. this.activeAction.stop();
  59. this.activeAction = null;
  60. }
  61. const clip = AnimationClip.findByName( this, label );
  62. if ( clip ) {
  63. const action = this.mixer.clipAction( clip );
  64. action.timeScale = ( clip.tracks.length * fps ) / clip.duration;
  65. this.activeAction = action.play();
  66. } else {
  67. throw new Error( 'THREE.MorphAnimMesh: animations[' + label + '] undefined in .playAnimation()' );
  68. }
  69. }
  70. /**
  71. * Updates the animations of the mesh. Must be called inside the animation loop.
  72. *
  73. * @param {number} delta - The delta time in seconds.
  74. */
  75. updateAnimation( delta ) {
  76. this.mixer.update( delta );
  77. }
  78. copy( source, recursive ) {
  79. super.copy( source, recursive );
  80. this.mixer = new AnimationMixer( this );
  81. return this;
  82. }
  83. }
  84. export { MorphAnimMesh };
粤ICP备19079148号