Animation: An important resource type in a 3D scene, used to control the movement of various elements in the scene. For example, to make a character in the scene walk, it is difficult to calculate and set the rotation angle of each joint of the character frame by frame. Therefore, for such complex animation requirements, the creators of 3D scene resources prepare the animation in advance and save the keyframe data of the animation and the interpolator type between keyframes in the model file.
ArkGraphics 3D provides APIs for you to play and control animations to achieve the expected rendering effect in the scene.
Import the required modules.
Import the core types provided by ArkGraphics 3D in the page script to create and control 3D scenes, cameras, and animation resources.
import { Animation, Camera, Scene, SceneResourceFactory } from '@kit.ArkGraphics3D'; Load scene resources.
Call Scene.load() to load the .glb (or .gltf) model from the resources/rawfile/ directory of your application, and obtain a scene object upon completion.
Scene.load($rawfile('gltf/BrainStem/glTF/BrainStem.glb'))
.then(async (result: Scene) => {
this.scene = result;
let rf: SceneResourceFactory = this.scene.getResourceFactory();
// ...
}).catch((err: string) => {
console.error(err);
}); Obtain an animation and register callbacks.
Obtain an animation resource from scene.animations[0], enable the animation, and register the onStarted() and onFinished() callbacks to listen for the animation playback status or trigger logic.
ArkGraphics 3D provides the following animation callback APIs:
this.anim = this.scene.animations[0];
if (this.anim) {
this.anim.enabled = true;
// Register the callback function.
this.anim.onStarted(() => {
// ...
this.animationCallbackInvoked = 'animation on start';
});
this.anim.onFinished(() => {
// ...
this.animationCallbackInvoked = 'animation on finish';
});
// ...
} else {
console.error('No animation found in scene.');
} Create a camera and configure scene rendering parameters.
Call SceneResourceFactory.createCamera() to create a camera and adjust the viewing position. Then, wrap the loaded scene into a SceneOptions object and specify the rendering type as ModelType.SURFACE to render the scene content via Component3D.
// Create a new camera.
this.cam = await rf.createCamera({ name: 'Camera' });
// Set the camera.
this.cam.enabled = true;
this.cam.position.z = 5;
this.sceneOpt = { scene: this.scene, modelType: ModelType.SURFACE } as SceneOptions; Build the UI and control the animation.
Render the 3D scene using Component3D and add buttons to the UI to control the animation playback status.
ArkGraphics 3D provides the following APIs to control the animation status:
Button('start')
// ...
.onClick(async () => {
if (!this.scene || !this.scene.animations[0]) {
return;
}
this.anim = this.scene.animations[0];
this.anim.start();
});
Button('pause')
// ...
.onClick(async () => {
if (!this.scene || !this.scene.animations[0]) {
return;
}
this.anim = this.scene.animations[0];
this.anim.pause();
});
Button('stop')
// ...
.onClick(async () => {
if (!this.scene || !this.scene.animations[0]) {
return;
}
this.anim = this.scene.animations[0];
this.anim.stop();
});
Button('finish')
// ...
.onClick(async () => {
if (!this.scene || !this.scene.animations[0]) {
return;
}
this.anim = this.scene.animations[0];
this.anim.finish();
});
Button('restart')
// ...
.onClick(async () => {
if (!this.scene || !this.scene.animations[0]) {
return;
}
this.anim = this.scene.animations[0];
this.anim.restart();
});
Button('seek to 30% progress')
// ...
.onClick(async () => {
if (!this.scene || !this.scene.animations[0]) {
return;
}
this.anim = this.scene.animations[0];
// Locate to 30%.
this.anim.seek(0.3);
});