BufferGeometry
Object3D
Raycaster
Camera
CubeCamera
PerspectiveCamera
OrthographicCamera
StereoCamera
Clock
Curve
CurvePath
Path
Shape
ShapePath
ArrowHelper
AxesHelper
BoxHelper
Box3Helper
CameraHelper
DirectionalLightHelper
GridHelper
PolarGridHelper
HemisphereLightHelper
PlaneHelper
PointLightHelper
SkeletonHelper
SpotLightHelper
Light
PointLight
RectAreaLight
SpotLight
DirectionalLight
HemisphereLight
LightShadow
PointLightShadow
AnimationLoader
AudioLoader
BufferGeometryLoader
CompressedTextureLoader
CubeTextureLoader
DataTextureLoader
FileLoader
ImageBitmapLoader
ImageLoader
Loader
LoaderUtils
MaterialLoader
ObjectLoader
TextureLoader
LoadingManager
Material
Box2
Box3
Color
Cylindrical
Euler
Frustum
Interpolant
Line3
MathUtils
Matrix3
Matrix4
Plane
Quaternion
AnimationAction
AnimationClip
AnimationMixer
AnimationObjectGroup
AnimationUtils
keyframeTrack
PropertyBinding
PropertyMixer
BooleanKeyframeTrack
QuaternionKeyframeTrack
StringKeyframeTrack
Audio
AudioAnalyser
AudioContext
AudioListener
PositionalAudio

AnimationClip.optimize()

The AnimationClip.optimize() method is a function in the Three.js library that simplifies an animation clip by removing redundant keyframes. This results in smaller memory usage and faster animation playback.

Syntax

animationClip.optimize();

Parameters

animationClip (required) - An instance of the AnimationClip class that represents the animation clip to be optimized.

Return Value

The AnimationClip.optimize() method does not return anything. It modifies the animationClip object in place.

Description

When creating complex animations, it is common to create keyframes for each frame of the animation. However, many of these keyframes may have the same values, resulting in redundant data. The AnimationClip.optimize() method identifies these redundant keyframes and removes them, reducing the size of the animation clip.

The optimization is done in three steps:

  1. Reduction of values: Adjacent keyframes with the same values are merged together, resulting in fewer keyframes.

  2. Threshold reduction: Keyframes that are within a specified threshold distance of each other are merged together. This reduces the number of keyframes without significantly affecting the animation quality.

  3. Delta-encoded compression: Keyframes are further compressed by storing only the difference between consecutive keyframes. This reduces the size of the animation data even further.

Example

import * as THREE from 'three';

const animationClip = new THREE.AnimationClip(...);

// Optimize the animation clip
animationClip.optimize();

Notes

  • The AnimationClip.optimize() method is applied to the animation clip after it is created.
  • This method is recommended for complex animations with many keyframes, but may not be necessary for simple animations with few keyframes.
  • It is important to note that optimizing an animation clip may alter the animation, especially if the threshold distance is set too high. Therefore, it is recommended to test the optimized animation before using it in production.