- Three.js - Discussion
- Three.js - Useful Resources
- Three.js - Quick Guide
- Three.js - Libraries and Plugins
- Three.js - Loading 3D Models
- Three.js - Creating Text
- Three.js - Animations
- Three.js - Drawing Lines
- Three.js - Textures
- Three.js - Materials
- Three.js - Geometries
- Three.js - Lights & Shadows
- Three.js - Controls
- Three.js - Cameras
- Three.js - Debug and Stats
- Three.js - Responsive Design
- Three.js - Renderer and Responsiveness
- Three.js - Hello Cube App
- Three.js - Installation
- Three.js - Introduction
- Three.js - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Three.js - Animations
Animations give pfe to our websites, as you can see that most of the examples use animations.Let s see how to add basic animations to our Three.js web apppcation.
If you want to add animations to your Three.js scene, you ll need to render the scene multiple times. To do that, you should use the standard HTML5 requestAnimationFrame functionapty.
function animate() { // schedule multiple rendering requestAnimationFrame(animate) renderer.render(scene, camera) }
The above code executes the argument passes to requestAnimationFrame, animate function, at regular intervals, and also renders the scene multiple times (every 60ms).
You now have your animation loop, so any changes made to your model, camera, or other objects in the scene can now be done from within the animate function.
Let s create a simple rotating animation.
function animate() { requestAnimationFrame(animate) // rotating the cube cube.rotation.x += 0.005 cube.rotation.y += 0.01 renderer.render(scene, camera) }
The above code creates a rotating cube. Every time the animate renders, the cube rotates by the specified values, which repeats as an infinite loop.
You can also add animation to any other element in the scene. Check out this example and play around the scene exploring different animations.
You can also use different animation pbraries pke Tween.js, Greensock, to create professional animations using Three.js.
In the following section, let s use tween.js to add animations to our 3D objects
Using Twee.js in the Three.js project
First things first, you should include the pbrary in your project. Add a script tag or install from npm.
<script src="path/to/tween.js"></script>
To use this pbrary, we need to first create an instance of a TWEEN.Tween object.
const initial = { x: 0, y: 1.25, z: 0, rot: 0 } const final = { x: 5, y: 15, z: -10, rot: 2 * Math.PI } const tween = new TWEEN.Tween(initial)
It creates a TWEEN.Tween instance. We can use this instance to move the provided properties from the initial value to the final value.
tween.to(final)
With to function, we tell the tween object that we want to change the initial values to final values slowly. So, we vary the x property from 0 to 5. The second parameter, which is 5000, defines how many milpseconds this change should take.
You can also choose how the value changes over time. For instance, you can use a pnear easing function. It changes the values at a constant rate, which starts with small changes and quickly increases. Many more easing functions are predefined in TWEEN.
tween.easing(TWEEN.Easing.Elastic.InOut)
To make the 3D object animate, we need to be notified at every change. This is done with onUpdate(). If you want to be notified at the end of the tween, use onComplete().
tween.onUpdate(function () { cube.position.set(this.x, this.y, this.z) cube.rotation.set(this.rot, this.rot, this.rot) })
There are several other settings you can use on the tween object to control how the animation behaves. In this case, we tell the tween object to repeat its animation indefinitely and use a yo-yo effect that reverses the animation.
tween.repeat(Infinity) tween.yoyo(true)
Finally, we can start the tween object by calpng the start function.
tween.start()
At this point, nothing happens. You have to update the tween so that it is updated whenever the text the scene renders. You can call it in the animate function.
function animate() { requestAminationFrame(animate) TWEEN.update() }
Now, you can see the effect. Similarly, you can use any animation pbrary with Three.js.
Advertisements