From 3983ce26133032316f25b67b95cfb0bc25834f11 Mon Sep 17 00:00:00 2001 From: Daan Vanden Bosch Date: Sun, 5 Jan 2020 19:23:21 +0100 Subject: [PATCH] The model viewer camera now resets when loading a model with "Open file...". --- .../model/ModelToolBarController.ts | 8 ++----- src/viewer/rendering/ModelRenderer.ts | 21 ++++++++++++++++++- src/viewer/stores/ModelStore.ts | 6 ------ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/viewer/controllers/model/ModelToolBarController.ts b/src/viewer/controllers/model/ModelToolBarController.ts index b5f20caf..1b04188e 100644 --- a/src/viewer/controllers/model/ModelToolBarController.ts +++ b/src/viewer/controllers/model/ModelToolBarController.ts @@ -56,13 +56,9 @@ export class ModelToolBarController extends Controller { const cursor = new ArrayBufferCursor(buffer, Endianness.Little); if (file.name.endsWith(".nj")) { - const nj_object = parse_nj(cursor)[0]; - - this.store.set_current_nj_object(nj_object); + this.store.set_current_nj_object(parse_nj(cursor)[0]); } else if (file.name.endsWith(".xj")) { - const nj_object = parse_xj(cursor)[0]; - - this.store.set_current_nj_object(nj_object); + this.store.set_current_nj_object(parse_xj(cursor)[0]); } else if (file.name.endsWith(".njm")) { this.store.set_current_animation(undefined); this.store.set_current_nj_motion(undefined); diff --git a/src/viewer/rendering/ModelRenderer.ts b/src/viewer/rendering/ModelRenderer.ts index cd8ef734..184edd77 100644 --- a/src/viewer/rendering/ModelRenderer.ts +++ b/src/viewer/rendering/ModelRenderer.ts @@ -25,6 +25,7 @@ import { Disposer } from "../../core/observable/Disposer"; import { ChangeEvent } from "../../core/observable/Observable"; import { LogManager } from "../../core/Logger"; import { ModelStore } from "../stores/ModelStore"; +import { CharacterClassModel } from "../model/CharacterClassModel"; const logger = LogManager.get("viewer/rendering/ModelRenderer"); @@ -37,10 +38,13 @@ const DEFAULT_SKINNED_MATERIAL = new MeshLambertMaterial({ color: 0xffffff, side: DoubleSide, }); +const CAMERA_POSITION = Object.freeze(new Vector3(0, 10, 20)); +const CAMERA_LOOKAT = Object.freeze(new Vector3(0, 0, 0)); export class ModelRenderer extends Renderer implements Disposable { private readonly disposer = new Disposer(); private readonly clock = new Clock(); + private character_class_active: boolean; private mesh?: Object3D; private skeleton_helper?: SkeletonHelper; private animation?: { @@ -54,7 +58,10 @@ export class ModelRenderer extends Renderer implements Disposable { constructor(private readonly store: ModelStore, three_renderer: DisposableThreeRenderer) { super(three_renderer); + this.character_class_active = store.current_character_class.val != undefined; + this.disposer.add_all( + store.current_character_class.observe(this.current_character_class_changed), store.current_nj_object.observe(this.nj_object_or_xvm_changed), store.current_textures.observe(this.nj_object_or_xvm_changed), store.current_nj_motion.observe(this.nj_motion_changed), @@ -65,7 +72,7 @@ export class ModelRenderer extends Renderer implements Disposable { ); this.init_camera_controls(); - this.reset_camera(new Vector3(0, 10, 20), new Vector3(0, 0, 0)); + this.reset_camera(CAMERA_POSITION, CAMERA_LOOKAT); } set_size(width: number, height: number): void { @@ -93,6 +100,18 @@ export class ModelRenderer extends Renderer implements Disposable { } } + private current_character_class_changed = ( + change: ChangeEvent, + ): void => { + const character_class_active = change.value != undefined; + + if (this.character_class_active !== character_class_active) { + this.reset_camera(CAMERA_POSITION, CAMERA_LOOKAT); + } + + this.character_class_active = character_class_active; + }; + private nj_object_or_xvm_changed = (): void => { if (this.mesh) { this.scene.remove(this.mesh); diff --git a/src/viewer/stores/ModelStore.ts b/src/viewer/stores/ModelStore.ts index 167134ed..a6599904 100644 --- a/src/viewer/stores/ModelStore.ts +++ b/src/viewer/stores/ModelStore.ts @@ -30,12 +30,6 @@ import { NjObject } from "../../core/data_formats/parsing/ninja"; const logger = LogManager.get("viewer/stores/ModelStore"); -export type NjData = { - readonly nj_object: NjObject; - readonly bone_count: number; - readonly has_skeleton: boolean; -}; - export class ModelStore extends Store { // Character classes and their animations. private readonly _current_character_class: WritableProperty<