Skip to content

Commit

Permalink
WIP: per model animation clips
Browse files Browse the repository at this point in the history
  • Loading branch information
webprofusion-chrisc committed Nov 12, 2020
1 parent d1bc255 commit b5edc0d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 49 deletions.
2 changes: 1 addition & 1 deletion lib/dojo3d/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 webprofusion.com
Copyright (c) 2020 webprofusion.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
39 changes: 37 additions & 2 deletions lib/dojo3d/src/lib/SceneObject.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { Model } from "./Model";
import { Object3D } from "three";
import { AnimationMixer } from "three";
import { LoadedModel } from "./World";

export class SceneObject {

public update: () => void;

constructor(public model: Model, public obj: Object3D) {
mixer: AnimationMixer;
clips: any;
obj: any;

constructor(public loadedModel: LoadedModel, public definition: Model,) {
this.obj = loadedModel.obj;
this.clips = loadedModel.animationClips;

this.setClips(this.obj, this.clips);
this.playAllClips();
}

setPosition(x: number, y: number, z: number) {
Expand All @@ -22,4 +31,30 @@ export class SceneObject {
this.obj.rotateY(y);
this.obj.rotateZ(z);
}

onUpdate(timeDelta) {
if (this.mixer) {
this.mixer.update(timeDelta);
}
}

setClips(obj, clips) {
if (this.mixer) {
this.mixer.stopAllAction();
this.mixer.uncacheRoot(this.mixer.getRoot());
this.mixer = null;
}

this.clips = clips;
if (!clips.length) return;

this.mixer = new AnimationMixer(obj);
}

playAllClips() {
this.clips.forEach((clip) => {
this.mixer.clipAction(clip).reset().play();
// this.state.actionStates[clip.name] = true;
});
}
}
93 changes: 47 additions & 46 deletions lib/dojo3d/src/lib/world.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AnimationMixer, BoxGeometry, DirectionalLight, Mesh, MeshStandardMaterial, PerspectiveCamera, PlaneBufferGeometry, Scene, sRGBEncoding, Vector3, WebGLRenderer } from 'three';
import { BoxGeometry, DirectionalLight, Mesh, MeshStandardMaterial, PerspectiveCamera, PlaneBufferGeometry, Scene, sRGBEncoding, Vector3, WebGLRenderer } from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import { Model, ModelCatalog } from './Model';
Expand All @@ -10,14 +10,17 @@ export interface Viewpoint {
position: Vector3;
}

export interface LoadedModel {
obj: any;
animationClips: any;
}

class World {

scene: Scene;
camera: PerspectiveCamera;
renderer: WebGLRenderer;
controls: OrbitControls;
mixer: AnimationMixer;
clips: any;

modelCatalog: ModelCatalog;

Expand Down Expand Up @@ -150,24 +153,37 @@ class World {
return this.modelCatalog.models.find(m => m.name == name);
}

public async addSceneObject(model: Model, scale = 1) {
public async addSceneObject(definition: Model, scale = 1) {

let url = model.path;
let url = definition.path;

if (!url.startsWith("http")) {
url = this.assetsBaseUrl + "models/" + model.path;
url = this.assetsBaseUrl + "models/" + definition.path;
}

const obj = await this.loadModel(url, scale);
const loadedModel = await this.loadModel(url, true, scale);

var sceneObject = new SceneObject(loadedModel, definition);

this.sceneObjects.push(sceneObject);

return sceneObject;
}

public async addSceneModelAsObject(m: LoadedModel, scale = 1) {


var sceneObject = new SceneObject(model, obj);
m.obj.scale.set(scale, scale, scale);
this.scene.add(m.obj);

var sceneObject = new SceneObject(m, null);

this.sceneObjects.push(sceneObject);

return sceneObject;
}

public async loadModel(gltf: string, scale = 1): Promise<any> {
public async loadModel(gltf: string, addModelToScene: boolean = true, scale = 1): Promise<LoadedModel> {
var loader = new GLTFLoader();

// var dracoLoader = new DRACOLoader();
Expand All @@ -190,28 +206,29 @@ class World {
//scale new scene
scene.scale.set(scale, scale, scale);

this.setClips(scene, clips);
/*
gltf.animations; // Array<THREE.AnimationClip>
gltf.scene; // THREE.Group
gltf.scenes; // Array<THREE.Group>
gltf.cameras; // Array<THREE.Camera>
gltf.asset; // Object
*/
if (addModelToScene) {

/*
gltf.animations; // Array<THREE.AnimationClip>
gltf.scene; // THREE.Group
gltf.scenes; // Array<THREE.Group>
gltf.cameras; // Array<THREE.Camera>
gltf.asset; // Object
*/

this.playAllClips();

gltf.scene.scale.set(scale, scale, scale);
gltf.scene.scale.set(scale, scale, scale);

this.scene.add(gltf.scene);
this.scene.add(gltf.scene);

gltf.animations; // Array<THREE.AnimationClip>
gltf.scene; // THREE.Group
gltf.scenes; // Array<THREE.Group>
gltf.cameras; // Array<THREE.Camera>
gltf.asset; // Object
gltf.animations; // Array<THREE.AnimationClip>
gltf.scene; // THREE.Group
gltf.scenes; // Array<THREE.Group>
gltf.cameras; // Array<THREE.Camera>
gltf.asset; // Object
}

resolve(gltf.scene);
resolve({ obj: scene, animationClips: clips });
},
// called while loading is progressing
(xhr) => {
Expand All @@ -229,25 +246,7 @@ class World {
});
}

setClips(obj, clips) {
if (this.mixer) {
this.mixer.stopAllAction();
this.mixer.uncacheRoot(this.mixer.getRoot());
this.mixer = null;
}

this.clips = clips;
if (!clips.length) return;

this.mixer = new AnimationMixer(obj);
}

playAllClips() {
this.clips.forEach((clip) => {
this.mixer.clipAction(clip).reset().play();
// this.state.actionStates[clip.name] = true;
});
}

public render(time) {
requestAnimationFrame((t) => {
Expand All @@ -259,9 +258,11 @@ class World {

this.renderer.render(this.scene, this.camera);

const dt = (time - this.prevTime) / 1000;
const deltaTime = (time - this.prevTime) / 1000;

this.mixer && this.mixer.update(dt);
for (let o of this.sceneObjects) {
o.onUpdate(deltaTime);
}

this.renderer.render(this.scene, this.camera);

Expand Down

0 comments on commit b5edc0d

Please sign in to comment.