MorphAnimMesh.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import {
  2. AnimationClip,
  3. AnimationMixer,
  4. Mesh
  5. } from 'three';
  6. class MorphAnimMesh extends Mesh {
  7. constructor( geometry, material ) {
  8. super( geometry, material );
  9. this.type = 'MorphAnimMesh';
  10. this.mixer = new AnimationMixer( this );
  11. this.activeAction = null;
  12. }
  13. setDirectionForward() {
  14. this.mixer.timeScale = 1.0;
  15. }
  16. setDirectionBackward() {
  17. this.mixer.timeScale = - 1.0;
  18. }
  19. playAnimation( label, fps ) {
  20. if ( this.activeAction ) {
  21. this.activeAction.stop();
  22. this.activeAction = null;
  23. }
  24. const clip = AnimationClip.findByName( this, label );
  25. if ( clip ) {
  26. const action = this.mixer.clipAction( clip );
  27. action.timeScale = ( clip.tracks.length * fps ) / clip.duration;
  28. this.activeAction = action.play();
  29. } else {
  30. throw new Error( 'THREE.MorphAnimMesh: animations[' + label + '] undefined in .playAnimation()' );
  31. }
  32. }
  33. updateAnimation( delta ) {
  34. this.mixer.update( delta );
  35. }
  36. copy( source, recursive ) {
  37. super.copy( source, recursive );
  38. this.mixer = new AnimationMixer( this );
  39. return this;
  40. }
  41. }
  42. export { MorphAnimMesh };