Skip to content

Commit

Permalink
feat(model): load bones and bone animations
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Jan 27, 2024
1 parent 6f77d37 commit 7bbf61c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
20 changes: 18 additions & 2 deletions src/lib/model/M2Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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, M2Color } from './types.js';
import { M2Track, M2TextureTransform, M2TextureWeight, M2Color, M2Bone } from './types.js';
import M2Texture, { M2_TEXTURE_COMBINER, M2_TEXTURE_COORD } from './M2Texture.js';
import M2Material from './M2Material.js';
import { m2typedArray } from './io/common.js';
Expand All @@ -12,10 +12,15 @@ import M2Bounds from './M2Bounds.js';
class M2Model {
#name: string;
#flags: number;

#vertices: ArrayBuffer;

#bounds: M2Bounds;
#collisionBounds: M2Bounds;

#bones: M2Bone[] = [];
#boneIndicesById: Uint16Array;

#textures: M2Texture[] = [];
#textureCombos: number[] = [];
#textureCoordCombos: M2_TEXTURE_COORD[] = [];
Expand All @@ -33,6 +38,14 @@ class M2Model {
#sequences: M2Sequence[] = [];
#loops: Uint32Array;

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

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

get bounds() {
return this.#bounds;
}
Expand Down Expand Up @@ -127,6 +140,9 @@ class M2Model {
this.#bounds = new M2Bounds(data.bounds.extent, data.bounds.radius);
this.#collisionBounds = new M2Bounds(data.collisionBounds.extent, data.collisionBounds.radius);

this.#bones = data.bones;
this.#boneIndicesById = data.boneIndicesById;

this.#textureCombos = data.textureCombos;
this.#textureCoordCombos = data.textureCoordCombos;
this.#textureWeightCombos = data.textureWeightCombos;
Expand Down Expand Up @@ -179,4 +195,4 @@ class M2Model {
}

export default M2Model;
export { M2Model, M2Track, M2Color, M2TextureWeight, M2TextureTransform, M2_MODEL_FLAG };
export { M2Model, M2Track, M2Bone, M2Color, M2TextureWeight, M2TextureTransform, M2_MODEL_FLAG };
6 changes: 3 additions & 3 deletions src/lib/model/io/m2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const m2sequence = io.struct({
});

const m2compBone = io.struct({
keyBoneId: io.int32le,
boneId: io.int32le,
flags: io.uint32le,
parentBone: io.int16le,
submeshId: io.uint16le,
parentIndex: io.int16le,
distToParent: io.uint16le,
boneNameCrc: io.uint32le,
translationTrack: m2track(io.float32le, 3),
rotationTrack: m2track(io.int16le, 4),
Expand Down
14 changes: 13 additions & 1 deletion src/lib/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ type M2Track<T> = {
sequenceKeys: T[];
};

type M2Bone = {
boneId: number;
flags: number;
parentIndex: number;
distToParent: number;
boneNameCrc: number;
pivot: Float32Array;
translationTrack: M2Track<Float32Array>;
rotationTrack: M2Track<Int16Array>;
scaleTrack: M2Track<Float32Array>;
};

type M2Color = {
colorTrack: M2Track<Float32Array>;
alphaTrack: M2Track<Int16Array>;
Expand All @@ -20,4 +32,4 @@ type M2TextureWeight = {
weightTrack: M2Track<Int16Array>;
};

export { M2Track, M2Color, M2TextureTransform, M2TextureWeight };
export { M2Track, M2Bone, M2Color, M2TextureTransform, M2TextureWeight };

0 comments on commit 7bbf61c

Please sign in to comment.