-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathbakedVertexAnimationManager.ts
More file actions
207 lines (181 loc) · 6.6 KB
/
Copy pathbakedVertexAnimationManager.ts
File metadata and controls
207 lines (181 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { type Nullable } from "../types";
import { type Scene } from "../scene";
import { serialize, expandToProperty, serializeAsTexture } from "../Misc/decorators";
import { type BaseTexture } from "../Materials/Textures/baseTexture";
import { Vector4 } from "../Maths/math.vector.pure";
import { type Effect } from "../Materials/effect";
import { EngineStore } from "../Engines/engineStore";
import { SerializationHelper } from "../Misc/decorators.serialization";
/**
* Interface for baked vertex animation texture, see BakedVertexAnimationManager
* @since 5.0
*/
export interface IBakedVertexAnimationManager {
/**
* The vertex animation texture
*/
texture: Nullable<BaseTexture>;
/**
* Gets or sets a boolean indicating if the edgesRenderer is active
*/
isEnabled: boolean;
/**
* The animation parameters for the mesh. See setAnimationParameters()
*/
animationParameters: Vector4;
/**
* The time counter, to pick the correct animation frame.
*/
time: number;
/**
* Binds to the effect.
* @param effect The effect to bind to.
* @param useInstances True when it's an instance.
*/
bind(effect: Effect, useInstances: boolean): void;
/**
* Sets animation parameters.
* @param startFrame The first frame of the animation.
* @param endFrame The last frame of the animation.
* @param offset The offset when starting the animation.
* @param speedFramesPerSecond The frame rate.
*/
setAnimationParameters(startFrame: number, endFrame: number, offset: number, speedFramesPerSecond: number): void;
/**
* Disposes the resources of the manager.
* @param forceDisposeTextures - Forces the disposal of all textures.
*/
dispose(forceDisposeTextures?: boolean): void;
/**
* Get the current class name useful for serialization or dynamic coding.
* @returns "BakedVertexAnimationManager"
*/
getClassName(): string;
}
/**
* This class is used to animate meshes using a baked vertex animation texture
* @see https://doc.babylonjs.com/features/featuresDeepDive/animation/baked_texture_animations
* @since 5.0
*/
export class BakedVertexAnimationManager implements IBakedVertexAnimationManager {
private _scene: Scene;
private _texture: Nullable<BaseTexture> = null;
/**
* The vertex animation texture
*/
@serializeAsTexture()
@expandToProperty("_markSubMeshesAsAttributesDirty")
public accessor texture: Nullable<BaseTexture>;
private _isEnabled = true;
/**
* Enable or disable the vertex animation manager
*/
@serialize()
@expandToProperty("_markSubMeshesAsAttributesDirty")
public accessor isEnabled = true;
/**
* The animation parameters for the mesh. See setAnimationParameters()
*/
@serialize()
public animationParameters: Vector4;
/**
* The time counter, to pick the correct animation frame.
*/
@serialize()
public time = 0;
/**
* Creates a new BakedVertexAnimationManager
* @param scene defines the current scene
*/
constructor(scene?: Nullable<Scene>) {
scene = scene || EngineStore.LastCreatedScene;
if (!scene) {
return;
}
this._scene = scene;
this.animationParameters = new Vector4(0, 0, 0, 30);
}
/** @internal */
public _markSubMeshesAsAttributesDirty(): void {
for (const mesh of this._scene.meshes) {
if ((<any>mesh).bakedVertexAnimationManager === this) {
mesh._markSubMeshesAsAttributesDirty();
}
}
}
/**
* Binds to the effect.
* @param effect The effect to bind to.
* @param useInstances True when it's an instance.
*/
public bind(effect: Effect, useInstances = false): void {
if (!this._texture || !this._isEnabled) {
return;
}
const size = this._texture.getSize();
effect.setFloat2("bakedVertexAnimationTextureSizeInverted", 1.0 / size.width, 1.0 / size.height);
effect.setFloat("bakedVertexAnimationTime", this.time);
if (!useInstances) {
effect.setVector4("bakedVertexAnimationSettings", this.animationParameters);
}
effect.setTexture("bakedVertexAnimationTexture", this._texture);
}
/**
* Clone the current manager
* @returns a new BakedVertexAnimationManager
*/
public clone(): BakedVertexAnimationManager {
const copy = new BakedVertexAnimationManager(this._scene);
this.copyTo(copy);
return copy;
}
/**
* Sets animation parameters.
* @param startFrame The first frame of the animation.
* @param endFrame The last frame of the animation.
* @param offset The offset when starting the animation.
* @param speedFramesPerSecond The frame rate.
*/
public setAnimationParameters(startFrame: number, endFrame: number, offset: number = 0, speedFramesPerSecond: number = 30): void {
this.animationParameters = new Vector4(startFrame, endFrame, offset, speedFramesPerSecond);
}
/**
* Disposes the resources of the manager.
* @param forceDisposeTextures - Forces the disposal of all textures.
*/
public dispose(forceDisposeTextures?: boolean): void {
if (forceDisposeTextures) {
this._texture?.dispose();
}
}
/**
* Get the current class name useful for serialization or dynamic coding.
* @returns "BakedVertexAnimationManager"
*/
public getClassName(): string {
return "BakedVertexAnimationManager";
}
/**
* Makes a duplicate of the current instance into another one.
* @param vatMap define the instance where to copy the info
*/
public copyTo(vatMap: BakedVertexAnimationManager): void {
SerializationHelper.Clone(() => vatMap, this);
}
/**
* Serializes this vertex animation instance
* @returns - An object with the serialized instance.
*/
public serialize(): any {
return SerializationHelper.Serialize(this);
}
/**
* Parses a vertex animation setting from a serialized object.
* @param source - Serialized object.
* @param scene Defines the scene we are parsing for
* @param rootUrl Defines the rootUrl to load from
*/
public parse(source: any, scene: Scene, rootUrl: string): void {
SerializationHelper.Parse(() => this, source, scene, rootUrl);
}
}