Working With 3D Object Animations
Last updated
Last updated
Unlike in the , in the one, you can work with animations in a more extensive way. You can either run several animations in a row, or just make animations at regular time intervals.
To work with animations, the first step is to add a 3D object to the scene and check it for the presence of animations.
An additional block will appear on the right next to objects with animation.
Here in the dropdown list you can see how many tracks the model has. Also, by clicking the PLAY button, you can play the selected animation.
Therefore, if all the necessary animations are functioning properly, you can start working with them.
Next, inside the code editor of the created script, you need to write a few strings of code.
The first step is to create a new variable that will contain the animation clip that has to be started (string 1).
Let`s take a look at this code: this is a call to this 3D model (if the script is linked to a 3D object), animations is an array of all animation clips of the object, [0] is the sequence number of the clip (the counting always starts from 0, since this is an array, and the last clip will have the number n-1).
Since in this case the model has two clips, you need to create variables for both of them at once (strings 1-2).
Now AnimationMixer needs to be created. AnimationMixer is a player for animating an object on the scene (string 3).
As a parameter of the method, you need to transfer a 3D object that you want to animate.
Next, you need to make AnimationMixer update every frame. If it is not done, the animation simply will not run or it will not work properly. To do this, you need to create a new variable in which the THREE.Clock
object will be placed. It is needed to track time (string 1).
After that, you need to create the update() function. This is the function that calls every frame (strings 8-10).
Inside this function, you need to create a conditional if statement, which, checking for the presence of the mixer, will update the animation every frame. It will look like this (strings 9-11).
That’s it, now everything is ready to start the animation. With AnimationAction, you can start any clip, for example, the first one (string 7).
Now, after we have learned how to start one animation, we can move on to starting several animations in a row. To do this, we will use the THREE.Cloc
k integrated into THREE.js and update()
function.
The code for starting the first animation, created above:
Next, you need to create another conditional if statement inside the update()
function, which will check whether a time has passed since the start of the scene, equal to the duration of the first animation track (clipOne
). Inside the operator, the number of seconds (clipOne.duration
) of the first clip will be compared with the current time (clock.elapsedTime()
).
This is necessary in order to stop the animation of the first clip and immediately play the animation of the second clip (strings 13-15).
Now you need to add the code that will be executed inside the condition. This piece of code should stop playing the first clip and start the second one (string 14).
Thus, after the end of the playing of the first animation of the model, the second one will start immediately.
In order for the 3D object to play animation when the scene starts, a needs to be created.
For instructions on how to create scripts, read.
The next step is to create a variable into which the mixer method (AnimationMixer) will be written. This method returns AnimationAction.
Since this model has two animation clips, we need to create two variables that will return (strings 4-5).