Skip to content

Commit

Permalink
chore(model): treat mesh as child of model object
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Jan 25, 2024
1 parent b20b755 commit b758701
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/lib/map/DoodadManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import TextureManager from '../texture/TextureManager.js';
import { AssetHost } from '../asset.js';
import { MapAreaSpec } from './loader/types.js';
import MapLight from './light/MapLight.js';
import ModelMesh from '../model/ModelMesh.js';
import Model from '../model/Model.js';

type DoodadManagerOptions = {
host: AssetHost;
Expand Down Expand Up @@ -53,7 +53,7 @@ class DoodadManager {
}

for (const model of group.children) {
(model as ModelMesh).dispose();
(model as Model).dispose();
}

this.#loadedAreas.delete(areaId);
Expand Down
16 changes: 11 additions & 5 deletions src/lib/model/ModelMesh.ts → src/lib/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import ModelMaterial from './ModelMaterial.js';
import ModelAnimator from './ModelAnimator.js';
import { ModelMaterialColor, ModelTextureTransform } from './types.js';

class ModelMesh extends THREE.Mesh {
class Model extends THREE.Object3D {
#mesh: THREE.Mesh;

#animator: ModelAnimator;
#animationActions: Set<THREE.AnimationAction> = new Set();

Expand All @@ -23,7 +25,11 @@ class ModelMesh extends THREE.Mesh {
textureTransformCount: number,
materialColorCount: number,
) {
super(geometry, materials);
super();

this.#mesh = new THREE.Mesh(geometry, materials);
this.#mesh.onBeforeRender = this.#onBeforeRender.bind(this);
this.add(this.#mesh);

this.#animator = animator;

Expand Down Expand Up @@ -72,7 +78,7 @@ class ModelMesh extends THREE.Mesh {
}
}

onBeforeRender(
#onBeforeRender(
renderer: THREE.WebGLRenderer,
scene: THREE.Scene,
camera: THREE.Camera,
Expand All @@ -92,5 +98,5 @@ class ModelMesh extends THREE.Mesh {
}
}

export default ModelMesh;
export { ModelMesh };
export default Model;
export { Model };
12 changes: 6 additions & 6 deletions src/lib/model/ModelManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as THREE from 'three';
import { M2_TEXTURE_COMPONENT, M2_TEXTURE_FLAG } from '@wowserhq/format';
import TextureManager from '../texture/TextureManager.js';
import { AssetHost, normalizePath } from '../asset.js';
import ModelMesh from './ModelMesh.js';
import Model from './Model.js';
import ModelMaterial from './ModelMaterial.js';
import { getVertexShader } from './shader/vertex.js';
import { getFragmentShader } from './shader/fragment.js';
Expand Down Expand Up @@ -47,7 +47,7 @@ class ModelManager {

async get(path: string) {
const resources = await this.#getResources(path);
return this.#createMesh(resources);
return this.#createModel(resources);
}

update(deltaTime: number) {
Expand Down Expand Up @@ -200,8 +200,8 @@ class ModelManager {
return new THREE.Texture();
}

#createMesh(resources: ModelResources) {
const mesh = new ModelMesh(
#createModel(resources: ModelResources) {
const model = new Model(
resources.geometry,
resources.materials,
resources.animator,
Expand All @@ -210,9 +210,9 @@ class ModelManager {
resources.materialColorCount,
);

mesh.name = resources.name;
model.name = resources.name;

return mesh;
return model;
}

#createAnimator(spec: ModelSpec) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/model/ModelMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
M2_MATERIAL_BLEND_TO_THREE_BLEND_TRANSPARENT,
} from './const.js';
import { THREE_BLEND_STATE } from '../blend.js';
import ModelMesh from './ModelMesh.js';
import Model from './Model.js';

const DEFAULT_BLEND: M2_MATERIAL_BLEND = M2_MATERIAL_BLEND.BLEND_OPAQUE;
const DEFAULT_FLAGS: number = 0x0;
Expand Down Expand Up @@ -128,7 +128,7 @@ class ModelMaterial extends THREE.RawShaderMaterial {
this.#materialParams.setZ(lit);
}

prepareMaterial(model: ModelMesh) {
prepareMaterial(model: Model) {
// Colors and weights

const materialColor = model.materialColors[this.#colorIndex];
Expand Down

0 comments on commit b758701

Please sign in to comment.