Skip to content

Commit

Permalink
chore(model): avoid unnecessary update matrix world calls
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Jan 31, 2024
1 parent 7f9e0e6 commit 46ba589
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/lib/map/MapManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ class MapManager extends EventTarget {
}

update(deltaTime: number, camera: THREE.Camera) {
this.#doodadManager.update(deltaTime);

this.#mapLight.update(camera);

// If fog end is closer than the configured view distance, use the fog end plus extension to
Expand All @@ -197,6 +195,7 @@ class MapManager extends EventTarget {
this.#cullGroups();

this.#doodadManager.cull(this.#cullingFrustum);
this.#doodadManager.update(deltaTime);
}

dispose() {
Expand Down
12 changes: 7 additions & 5 deletions src/lib/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Model extends THREE.Object3D {
alpha: 1.0;

#mesh: THREE.Mesh;
#skinned: boolean;

constructor(
geometry: THREE.BufferGeometry,
Expand All @@ -20,6 +21,8 @@ class Model extends THREE.Object3D {
) {
super();

this.#skinned = skinned;

// Avoid skinning overhead when model does not make use of bone animations
if (skinned) {
this.#mesh = new THREE.SkinnedMesh(geometry, materials);
Expand Down Expand Up @@ -53,6 +56,10 @@ class Model extends THREE.Object3D {
return this.#mesh.geometry.boundingSphere;
}

get skinned() {
return this.#skinned;
}

hide() {
if (!this.visible) {
return;
Expand Down Expand Up @@ -83,11 +90,6 @@ class Model extends THREE.Object3D {
material: ModelMaterial,
group: THREE.Group,
) {
// Ensure bone matrices are updated (matrix world auto-updates are disabled)
if ((this.#mesh as THREE.SkinnedMesh).isSkinnedMesh) {
this.#mesh.updateMatrixWorld();
}

// Update material uniforms to match animation states
material.prepareMaterial(this);
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/model/ModelAnimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class ModelAnimation extends THREE.Object3D {
this.#animator.clearAction(action);
}

this.#animator.clearAnimation(this);

this.#actions.clear();
this.#playingActions.clear();
this.#suspendedActions.clear();
Expand Down
22 changes: 21 additions & 1 deletion src/lib/model/ModelAnimator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class ModelAnimator {

#stateCounts: Record<string, number> = {};

#modelsByAnimation: Map<ModelAnimation, Model> = new Map();

constructor(loops: Uint32Array, sequences: SequenceSpec[], bones: BoneSpec[]) {
this.#mixer = new THREE.AnimationMixer(new THREE.Object3D());
this.#mixer.timeScale = 1000;
Expand All @@ -44,7 +46,10 @@ class ModelAnimator {
}

createAnimation(model: Model) {
return new ModelAnimation(model, this, this.#bones, this.#stateCounts);
const animation = new ModelAnimation(model, this, this.#bones, this.#stateCounts);
this.#modelsByAnimation.set(animation, model);

return animation;
}

get loops() {
Expand All @@ -60,8 +65,23 @@ class ModelAnimator {
this.#mixer.uncacheAction(action.getClip());
}

clearAnimation(animation: ModelAnimation) {
this.#modelsByAnimation.delete(animation);
}

update(deltaTime: number) {
this.#mixer.update(deltaTime);

for (const model of this.#modelsByAnimation.values()) {
if (!model.visible) {
continue;
}

// Ensure bone matrices are updated (matrix world auto-updates are disabled)
if (model.skinned) {
model.updateMatrixWorld();
}
}
}

getLoop(root: THREE.Object3D, index: number) {
Expand Down

0 comments on commit 46ba589

Please sign in to comment.