Skip to content

Commit

Permalink
feat(model): add support for color animations
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Jan 24, 2024
1 parent 8c68e7f commit 3e4e4c1
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 7 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
],
"dependencies": {
"@tweenjs/tween.js": "^23.1.1",
"@wowserhq/format": "^0.20.0"
"@wowserhq/format": "^0.21.0"
},
"peerDependencies": {
"three": "^0.160.0"
Expand Down
16 changes: 16 additions & 0 deletions src/lib/model/ModelManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ModelResources = {
animator: ModelAnimator;
textureWeightCount: number;
textureTransformCount: number;
colorCount: number;
};

type ModelManagerOptions = {
Expand Down Expand Up @@ -90,6 +91,7 @@ class ModelManager {
animator,
textureWeightCount: spec.textureWeights.length,
textureTransformCount: spec.textureTransforms.length,
colorCount: spec.colors.length,
};

this.#loaded.set(refId, resources);
Expand Down Expand Up @@ -167,6 +169,7 @@ class ModelManager {
);
const textureWeightIndex = spec.textureWeightIndex;
const textureTransformIndices = spec.textureTransformIndices;
const colorIndex = spec.colorIndex;
const uniforms = { ...this.#sceneLight.uniforms };

return new ModelMaterial(
Expand All @@ -175,6 +178,7 @@ class ModelManager {
textures,
textureWeightIndex,
textureTransformIndices,
colorIndex,
uniforms,
spec.blend,
spec.flags,
Expand Down Expand Up @@ -203,6 +207,7 @@ class ModelManager {
resources.animator,
resources.textureWeightCount,
resources.textureTransformCount,
resources.colorCount,
);

mesh.name = resources.name;
Expand Down Expand Up @@ -247,6 +252,17 @@ class ModelManager {
);
}

for (const [index, color] of spec.colors.entries()) {
animator.registerTrack(`.colors[${index}].color`, color.colorTrack, THREE.ColorKeyframeTrack);

animator.registerTrack(
`.colors[${index}].alpha`,
color.alphaTrack,
THREE.NumberKeyframeTrack,
(value: number) => value / 0x7fff,
);
}

return animator;
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/lib/model/ModelMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class ModelMaterial extends THREE.RawShaderMaterial {
#textureTransformIndices: number[];
#textureTransforms: THREE.Matrix4[];

#colorIndex: number;

#blend: M2_MATERIAL_BLEND;

#materialParams: THREE.Vector4;
Expand All @@ -28,6 +30,7 @@ class ModelMaterial extends THREE.RawShaderMaterial {
textures: THREE.Texture[],
textureWeightIndex: number,
textureTransformIndices: number[],
colorIndex: number,
uniforms: Record<string, THREE.IUniform> = {},
blend = DEFAULT_BLEND,
flags = DEFAULT_FLAGS,
Expand All @@ -38,6 +41,8 @@ class ModelMaterial extends THREE.RawShaderMaterial {
this.#textureTransformIndices = textureTransformIndices;
this.#textureTransforms = [new THREE.Matrix4(), new THREE.Matrix4()];

this.#colorIndex = colorIndex;

this.#blend = blend;

this.#materialParams = new THREE.Vector4(0.0, 0.0, 0.0, 0.0);
Expand Down Expand Up @@ -107,6 +112,10 @@ class ModelMaterial extends THREE.RawShaderMaterial {
return this.#materialParams.y;
}

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

get fogged() {
return this.#materialParams.w;
}
Expand Down
28 changes: 26 additions & 2 deletions src/lib/model/ModelMesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ type ModelTextureTransform = {
scaling: THREE.Vector3;
};

type ModelColor = {
color: THREE.Color;
alpha: number;
};

const _color = new THREE.Color();

class ModelMesh extends THREE.Mesh {
#animator: ModelAnimator;
#animationActions: Set<THREE.AnimationAction> = new Set();
Expand All @@ -18,13 +25,15 @@ class ModelMesh extends THREE.Mesh {

textureWeights: number[] = [];
textureTransforms: ModelTextureTransform[] = [];
colors: ModelColor[] = [];

constructor(
geometry: THREE.BufferGeometry,
materials: THREE.Material[],
animator: ModelAnimator,
textureWeightCount: number,
textureTransformCount: number,
colorCount: number,
) {
super(geometry, materials);

Expand All @@ -49,6 +58,13 @@ class ModelMesh extends THREE.Mesh {
});
}

for (let i = 0; i < colorCount; i++) {
this.colors.push({
color: new THREE.Color(1.0, 1.0, 1.0),
alpha: 1.0,
});
}

// Animations

// Automatically play all loops
Expand Down Expand Up @@ -76,10 +92,18 @@ class ModelMesh extends THREE.Mesh {
material: ModelMaterial,
group: THREE.Group,
) {
const color = this.colors[material.colorIndex];

const textureWeight = this.textureWeights[material.textureWeightIndex] ?? 1.0;
material.alpha = this.#alpha * textureWeight;
const colorAlpha = color?.alpha ?? 1.0;
material.alpha = this.#alpha * textureWeight * colorAlpha;

material.setDiffuseColor(this.#diffuseColor);
const colorColor = color?.color;
_color.copy(this.#diffuseColor);
if (color) {
_color.multiply(colorColor);
}
material.setDiffuseColor(_color);
material.setEmissiveColor(this.#emissiveColor);

for (let i = 0; i < material.textureTransformIndices.length; i++) {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/model/loader/ModelLoaderWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ModelLoaderWorker extends SceneWorker {
const loops = model.loops;
const textureWeights = model.textureWeights;
const textureTransforms = model.textureTransforms;
const colors = model.colors;

const spec: ModelSpec = {
name: model.name,
Expand All @@ -42,6 +43,7 @@ class ModelLoaderWorker extends SceneWorker {
sequences,
textureWeights,
textureTransforms,
colors,
};

const transfer = [spec.geometry.vertexBuffer, spec.geometry.indexBuffer];
Expand Down Expand Up @@ -106,6 +108,7 @@ class ModelLoaderWorker extends SceneWorker {
textures,
textureWeightIndex: batch.textureWeightIndex,
textureTransformIndices: batch.textureTransformIndices,
colorIndex: batch.colorIndex,
vertexShader: batch.vertexShader,
fragmentShader: batch.fragmentShader,
};
Expand Down
3 changes: 3 additions & 0 deletions src/lib/model/loader/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
M2_MATERIAL_BLEND,
M2_TEXTURE_COMPONENT,
M2_VERTEX_SHADER,
M2Color,
M2TextureTransform,
M2TextureWeight,
} from '@wowserhq/format';
Expand All @@ -18,6 +19,7 @@ type MaterialSpec = {
textures: TextureSpec[];
textureWeightIndex: number;
textureTransformIndices: number[];
colorIndex: number;
vertexShader: M2_VERTEX_SHADER;
fragmentShader: M2_FRAGMENT_SHADER;
blend: M2_MATERIAL_BLEND;
Expand Down Expand Up @@ -56,6 +58,7 @@ type ModelSpec = {
loops: Uint32Array;
textureWeights: M2TextureWeight[];
textureTransforms: M2TextureTransform[];
colors: M2Color[];
};

export { ModelSpec, MaterialSpec, TextureSpec, SequenceSpec };

0 comments on commit 3e4e4c1

Please sign in to comment.