The model viewer camera now resets when loading a model with "Open file...".

This commit is contained in:
Daan Vanden Bosch 2020-01-05 19:23:21 +01:00
parent 3836612972
commit 3983ce2613
3 changed files with 22 additions and 13 deletions

View File

@ -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);

View File

@ -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<CharacterClassModel | undefined>,
): 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);

View File

@ -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<