Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Generate Cameras For M2 Model #101

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ export * from './db/DbManager.js';
export * from './light/SceneLight.js';
export * from './map/MapManager.js';
export * from './model/ModelManager.js';
export * from './model/Model.js';
export * from './sound/SoundManager.js';
export * from './texture/TextureManager.js';
export * from './util.js';
4 changes: 2 additions & 2 deletions src/lib/map/DoodadManager.ts
Original file line number Diff line number Diff line change
@@ -153,9 +153,9 @@ class DoodadManager {

const areaBounds = new THREE.Sphere();

const doodadModels = await Promise.all(
const doodadModels = (await Promise.all(
doodadDefs.map((doodadDef) => this.#modelManager.get(doodadDef.name)),
);
)).map(({model}) => model);

for (let i = 0; i < doodadDefs.length; i++) {
const model = doodadModels[i];
34 changes: 32 additions & 2 deletions src/lib/model/ModelManager.ts
Original file line number Diff line number Diff line change
@@ -10,13 +10,15 @@ import ModelLoader from './loader/ModelLoader.js';
import { MaterialSpec, ModelSpec, TextureSpec } from './loader/types.js';
import SceneLight from '../light/SceneLight.js';
import ModelAnimator from './ModelAnimator.js';
import { DEFAULT_UP } from './util.js';

type ModelResources = {
name: string;
geometry: THREE.BufferGeometry;
materials: MaterialSpec[];
animator: ModelAnimator;
skinned: boolean;
cameras: THREE.PerspectiveCamera[];
};

type ModelManagerOptions = {
@@ -45,7 +47,13 @@ class ModelManager {

async get(path: string) {
const resources = await this.#getResources(path);
return this.#createModel(resources);
const [model] = await Promise.all([
this.#createModel(resources),
]);
return {
model,
cameras: resources.cameras,
}
}

update(deltaTime: number, camera: THREE.Camera) {
@@ -77,16 +85,17 @@ class ModelManager {

async #loadResources(refId: string, path: string) {
const spec = await this.#loader.loadSpec(path);

const animator = this.#createAnimator(spec);
const geometry = this.#createGeometry(spec);
const cameras = this.#createCameras(spec);

const resources: ModelResources = {
name: spec.name,
geometry,
materials: spec.materials,
animator,
skinned: spec.skinned,
cameras: cameras,
};

this.#loaded.set(refId, resources);
@@ -297,6 +306,27 @@ class ModelManager {

return animator;
}

#createCameras(spec: ModelSpec): THREE.PerspectiveCamera[] {
const ASPECT_RATIO = 1;
return spec.cameras.map((cameraSpec) => {
const camera = new THREE.PerspectiveCamera(
(cameraSpec.fieldOfView / Math.sqrt(1.0 + Math.pow(ASPECT_RATIO, 2.0)) * 57.2958),
ASPECT_RATIO,
cameraSpec.nearClip,
cameraSpec.farClip,
);
camera.userData.fov = cameraSpec.fieldOfView;
camera.up = DEFAULT_UP;

const [x, y, z] = cameraSpec.positionBase;
camera.position.set(x, y, z);

const [targetX, targetY, targetZ] = cameraSpec.targetBase;
camera.lookAt(targetX, targetY, targetZ);
return camera;
});
}
}

export default ModelManager;
2 changes: 2 additions & 0 deletions src/lib/model/loader/ModelLoaderWorker.ts
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ class ModelLoaderWorker extends SceneWorker {
const textureWeights = model.textureWeights;
const textureTransforms = model.textureTransforms;
const materialColors = model.colors;
const cameras = model.cameras.map((c) => c.toObject());

// Expand geometry bounds by sequence bounds to produce model bounds
const extent = geometry.bounds.extent.slice(0);
@@ -57,6 +58,7 @@ class ModelLoaderWorker extends SceneWorker {
textureWeights,
textureTransforms,
materialColors,
cameras,
};

const transfer = [spec.geometry.vertexBuffer, spec.geometry.indexBuffer, ...boneBuffers];
2 changes: 2 additions & 0 deletions src/lib/model/loader/types.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import {
M2TextureTransform,
M2TextureWeight,
M2Track,
M2Camera,
} from '@wowserhq/format';

type ModelBounds = {
@@ -79,6 +80,7 @@ type ModelSpec = {
textureWeights: M2TextureWeight[];
textureTransforms: M2TextureTransform[];
materialColors: M2Color[];
cameras: M2Camera[];
};

export { ModelBounds, ModelSpec, BoneSpec, MaterialSpec, TextureSpec, SequenceSpec };
4 changes: 3 additions & 1 deletion src/lib/model/util.ts
Original file line number Diff line number Diff line change
@@ -15,4 +15,6 @@ const getTrackInterpolation = (trackType: number): THREE.InterpolationModes => {
}
};

export { getTrackInterpolation };
const DEFAULT_UP = new THREE.Vector3(0, 0, 1);

export { getTrackInterpolation, DEFAULT_UP };