Created
March 16, 2022 15:27
-
-
Save jimib/b7b29a571e01b458312f8d15dbb35ce8 to your computer and use it in GitHub Desktop.
ThreeJS Animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var loader = new THREE.GLTFLoader(); | |
| var mixer; | |
| loader.load( SRC_GLB, | |
| ( gltf ) => { | |
| //add the scene to a container (or directly to our THREE scene) | |
| container.add( gltf.scene ) | |
| //configure the animation | |
| mixer = new THREE.AnimationMixer( gltf.scene ); | |
| var action = mixer.clipAction( THREE.AnimationClip.findByName( gltf.animations, 'ANIMATION_NAME' ) ); | |
| action.loop = THREE.LoopOnce; //it can loop forever or play through one time | |
| action.clampWhenFinished = true; //when it finishes stay on the last frame instead of skipping back to the first frame | |
| action.stop(); //pause the animation until you need it | |
| //add listener for when this animation has finished | |
| mixer.addEventListener( 'finished', ( e ) => { | |
| // show a message or play the next animation | |
| } ); | |
| } | |
| // during the animation loop you update the mixer using the delta (time since the last frame) | |
| var timeLastLoop = Date.now(); | |
| function animate(){ | |
| timeNow = Date.now(); | |
| var delta = timeNow - timeLastLoop; | |
| //update the mixer | |
| mixer.update( delta ); | |
| //finish up | |
| timeLastLoop = timeNow; | |
| //request the next animation loop | |
| requestAnimationFrame( animate ); | |
| } | |
| //start animating | |
| animate(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment