Skip to content

Commit

Permalink
feat(model): improve texture animation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Jan 24, 2024
1 parent 21af431 commit 58ea74f
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 66 deletions.
14 changes: 14 additions & 0 deletions src/lib/model/M2Batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class M2Batch {
#material: M2Material;
#layer: number;
#textures: M2Texture[];
#textureWeightIndex: number;
#textureTransformIndices: number[];
#vertexShader: M2_VERTEX_SHADER;
#fragmentShader: M2_FRAGMENT_SHADER;

Expand All @@ -20,6 +22,8 @@ class M2Batch {
material: M2Material,
layer: number,
textures: M2Texture[],
textureWeightIndex: number,
textureTransformIndices: number[],
vertexShader: M2_VERTEX_SHADER,
fragmentShader: M2_FRAGMENT_SHADER,
) {
Expand All @@ -29,6 +33,8 @@ class M2Batch {
this.#material = material;
this.#layer = layer;
this.#textures = textures;
this.#textureWeightIndex = textureWeightIndex;
this.#textureTransformIndices = textureTransformIndices;
this.#vertexShader = vertexShader;
this.#fragmentShader = fragmentShader;
}
Expand Down Expand Up @@ -61,6 +67,14 @@ class M2Batch {
return this.#textures;
}

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

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

get vertexShader() {
return this.#vertexShader;
}
Expand Down
27 changes: 4 additions & 23 deletions src/lib/model/M2Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { IoMode, IoSource, openStream } from '@wowserhq/io';
import * as io from '@wowserhq/io';
import * as m2Io from './io/m2.js';
import { M2_MODEL_FLAG } from './const.js';
import { M2Track, M2TextureTransform, M2TextureWeight } from './types.js';
import M2Texture, { M2_TEXTURE_COMBINER, M2_TEXTURE_COORD } from './M2Texture.js';
import M2Material from './M2Material.js';
import M2TextureWeight from './M2TextureWeight.js';
import M2TextureTransform from './M2TextureTransform.js';
import { m2typedArray } from './io/common.js';
import M2Sequence from './M2Sequence.js';
import M2Bounds from './M2Bounds.js';
Expand Down Expand Up @@ -128,8 +127,8 @@ class M2Model {
this.#textureTransformCombos = data.textureTransformCombos;

this.#loadTextures(data);
this.#loadTextureTransforms(data);
this.#loadTextureWeights(data);
this.#textureTransforms = data.textureTransforms;
this.#textureWeights = data.textureWeights;

this.#loadMaterials(data);

Expand Down Expand Up @@ -169,25 +168,7 @@ class M2Model {
);
}
}

#loadTextureTransforms(data: any) {
for (const textureTransformData of data.textureTransforms) {
this.#textureTransforms.push(
new M2TextureTransform(
textureTransformData.translationTrack,
textureTransformData.rotationTrack,
textureTransformData.scalingTrack,
),
);
}
}

#loadTextureWeights(data: any) {
for (const textureWeightData of data.textureWeights) {
this.#textureWeights.push(new M2TextureWeight(textureWeightData.weightTrack));
}
}
}

export default M2Model;
export { M2Model, M2_MODEL_FLAG };
export { M2Model, M2Track, M2TextureWeight, M2TextureTransform, M2_MODEL_FLAG };
5 changes: 5 additions & 0 deletions src/lib/model/M2SkinProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,18 @@ class M2SkinProfile {
textures.push(texture);
}

const textureWeightIndex = this.#model.textureWeightCombos[batchData.textureWeightComboIndex];
const textureTransformIndices = batchData.textureTransformIndices.filter((i) => i !== 0xffff);

const batch = new M2Batch(
batchData.flags,
batchData.priorityPlane,
skinSection,
material,
batchData.materialLayer,
textures,
textureWeightIndex,
textureTransformIndices,
vertexShader,
fragmentShader,
);
Expand Down
25 changes: 0 additions & 25 deletions src/lib/model/M2TextureTransform.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/lib/model/M2TextureWeight.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/lib/model/io/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ const m2bounds: IoType = io.struct({

const m2track = (type: IoType, elements = 1): IoType =>
io.struct({
interpolationType: io.uint16le,
globalSequence: io.uint16le,
timestamps: m2array(m2typedArray(io.uint32le)),
values: m2array(m2typedArray(type, elements)),
trackType: io.uint16le,
loopIndex: io.uint16le,
sequenceTimes: m2array(m2typedArray(io.uint32le)),
sequenceKeys: m2array(m2typedArray(type, elements)),
});

export { m2array, m2typedArray, m2string, m2range, m2bounds, m2track };
2 changes: 1 addition & 1 deletion src/lib/model/io/m2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const m2texture = io.struct({
});

const m2textureWeight = io.struct({
weightTrack: m2track(io.uint16le),
weightTrack: m2track(io.int16le),
});

const m2textureTransform = io.struct({
Expand Down
18 changes: 18 additions & 0 deletions src/lib/model/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type M2Track<T> = {
trackType: number;
loopIndex: number;
sequenceTimes: Uint32Array[];
sequenceKeys: T[];
};

type M2TextureTransform = {
translationTrack: M2Track<Float32Array>;
rotationTrack: M2Track<Float32Array>;
scalingTrack: M2Track<Float32Array>;
};

type M2TextureWeight = {
weightTrack: M2Track<Int16Array>;
};

export { M2Track, M2TextureTransform, M2TextureWeight };

0 comments on commit 58ea74f

Please sign in to comment.