From 58ea74f960b01f00a41fdcd01f7917d9d19b5c31 Mon Sep 17 00:00:00 2001 From: fallenoak Date: Tue, 23 Jan 2024 19:39:22 -0600 Subject: [PATCH] feat(model): improve texture animation handling --- src/lib/model/M2Batch.ts | 14 ++++++++++++++ src/lib/model/M2Model.ts | 27 ++++----------------------- src/lib/model/M2SkinProfile.ts | 5 +++++ src/lib/model/M2TextureTransform.ts | 25 ------------------------- src/lib/model/M2TextureWeight.ts | 13 ------------- src/lib/model/io/common.ts | 8 ++++---- src/lib/model/io/m2.ts | 2 +- src/lib/model/types.ts | 18 ++++++++++++++++++ 8 files changed, 46 insertions(+), 66 deletions(-) delete mode 100644 src/lib/model/M2TextureTransform.ts delete mode 100644 src/lib/model/M2TextureWeight.ts create mode 100644 src/lib/model/types.ts diff --git a/src/lib/model/M2Batch.ts b/src/lib/model/M2Batch.ts index 6183f79..ef6ba46 100644 --- a/src/lib/model/M2Batch.ts +++ b/src/lib/model/M2Batch.ts @@ -10,6 +10,8 @@ class M2Batch { #material: M2Material; #layer: number; #textures: M2Texture[]; + #textureWeightIndex: number; + #textureTransformIndices: number[]; #vertexShader: M2_VERTEX_SHADER; #fragmentShader: M2_FRAGMENT_SHADER; @@ -20,6 +22,8 @@ class M2Batch { material: M2Material, layer: number, textures: M2Texture[], + textureWeightIndex: number, + textureTransformIndices: number[], vertexShader: M2_VERTEX_SHADER, fragmentShader: M2_FRAGMENT_SHADER, ) { @@ -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; } @@ -61,6 +67,14 @@ class M2Batch { return this.#textures; } + get textureWeightIndex() { + return this.#textureWeightIndex; + } + + get textureTransformIndices() { + return this.#textureTransformIndices; + } + get vertexShader() { return this.#vertexShader; } diff --git a/src/lib/model/M2Model.ts b/src/lib/model/M2Model.ts index 2f3e54b..21de3c1 100644 --- a/src/lib/model/M2Model.ts +++ b/src/lib/model/M2Model.ts @@ -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'; @@ -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); @@ -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 }; diff --git a/src/lib/model/M2SkinProfile.ts b/src/lib/model/M2SkinProfile.ts index 06f7ff4..5ad28d3 100644 --- a/src/lib/model/M2SkinProfile.ts +++ b/src/lib/model/M2SkinProfile.ts @@ -87,6 +87,9 @@ 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, @@ -94,6 +97,8 @@ class M2SkinProfile { material, batchData.materialLayer, textures, + textureWeightIndex, + textureTransformIndices, vertexShader, fragmentShader, ); diff --git a/src/lib/model/M2TextureTransform.ts b/src/lib/model/M2TextureTransform.ts deleted file mode 100644 index ca0f333..0000000 --- a/src/lib/model/M2TextureTransform.ts +++ /dev/null @@ -1,25 +0,0 @@ -class M2TextureTransform { - #translationTrack: any; - #rotationTrack: any; - #scalingTrack: any; - - constructor(translationTrack: any, rotationTrack: any, scalingTrack: any) { - this.#translationTrack = translationTrack; - this.#rotationTrack = rotationTrack; - this.#scalingTrack = scalingTrack; - } - - get translationTrack() { - return this.#translationTrack; - } - - get rotationTrack() { - return this.#rotationTrack; - } - - get scalingTrack() { - return this.#scalingTrack; - } -} - -export default M2TextureTransform; diff --git a/src/lib/model/M2TextureWeight.ts b/src/lib/model/M2TextureWeight.ts deleted file mode 100644 index c1364aa..0000000 --- a/src/lib/model/M2TextureWeight.ts +++ /dev/null @@ -1,13 +0,0 @@ -class M2TextureWeight { - #weightTrack: any; - - constructor(weightTrack: any) { - this.#weightTrack = weightTrack; - } - - get weightTrack() { - return this.#weightTrack; - } -} - -export default M2TextureWeight; diff --git a/src/lib/model/io/common.ts b/src/lib/model/io/common.ts index 1c0324e..6791b9d 100644 --- a/src/lib/model/io/common.ts +++ b/src/lib/model/io/common.ts @@ -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 }; diff --git a/src/lib/model/io/m2.ts b/src/lib/model/io/m2.ts index db9b782..5dfe175 100644 --- a/src/lib/model/io/m2.ts +++ b/src/lib/model/io/m2.ts @@ -49,7 +49,7 @@ const m2texture = io.struct({ }); const m2textureWeight = io.struct({ - weightTrack: m2track(io.uint16le), + weightTrack: m2track(io.int16le), }); const m2textureTransform = io.struct({ diff --git a/src/lib/model/types.ts b/src/lib/model/types.ts new file mode 100644 index 0000000..a2b6652 --- /dev/null +++ b/src/lib/model/types.ts @@ -0,0 +1,18 @@ +type M2Track = { + trackType: number; + loopIndex: number; + sequenceTimes: Uint32Array[]; + sequenceKeys: T[]; +}; + +type M2TextureTransform = { + translationTrack: M2Track; + rotationTrack: M2Track; + scalingTrack: M2Track; +}; + +type M2TextureWeight = { + weightTrack: M2Track; +}; + +export { M2Track, M2TextureTransform, M2TextureWeight };