-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_animation.js
More file actions
111 lines (95 loc) · 3.03 KB
/
test_animation.js
File metadata and controls
111 lines (95 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
function renderOnce(p) {
const delta = p.clock.getDelta();
p.cameraMixer.update(delta);
p.lookatMixer.update(delta);
// p.controls.update();
p.camera.lookAt(p.model.getObjectByName('CameraLookat').position);
p.renderer.render(p.scene, p.camera);
console.log(p.camera.position, p.model.getObjectByName('CameraLookat').position);
}
function render(p) {
renderOnce(p);
requestAnimationFrame(() => render(p));
}
async function test_animation() {
const canvas = document.querySelector('#canvas');
const renderer = new THREE.WebGLRenderer({ canvas });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(canvas.clientWidth, canvas.clientHeight, false);
renderer.shadowMap.enabled = true;
const aspect = canvas.clientWidth / canvas.clientHeight;
let camera = new THREE.PerspectiveCamera(30, aspect, 0.1, 1000);
camera.position.x = 15;
camera.position.y = 15;
camera.position.z = 15;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xFFFFFF);
const loader = new GLTFLoader();
let cameraMixer;
let lookatMixer;
loader.load('static/models/arcade_room.gltf', (gltf) => {
const model = gltf.scene;
model.position.set(0, 0, 0);
const animations = gltf.animations;
const cameraAnimation = animations[0];
camera.lookAt(model.getObjectByName('CameraLookat').position);
if (cameraAnimation) {
cameraMixer = new THREE.AnimationMixer(camera);
lookatMixer = new THREE.AnimationMixer(model.getObjectByName('CameraLookat'));
cameraAnimation.tracks.forEach((track) => {
if (track.name.includes('GameIsoCam.')) {
track.name = track.name.replace('GameIsoCam', camera.uuid);
}
});
let action = cameraMixer.clipAction(cameraAnimation);
action.play();
action.repetitions = 1;
action.clampWhenFinished = true;
action = lookatMixer.clipAction(animations[1]);
action.play();
action.repetitions = 1;
action.clampWhenFinished = true;
};
scene.add(model);
const rectLight = new THREE.RectAreaLight(0xFEB9FF, 5, 10, 10);
rectLight.position.set(0, 6.76781, 0);
rectLight.lookAt(0, 0, 0);
scene.add(rectLight);
const mat = model.getObjectByName('Screen').material
mat.color.r = 1;
mat.color.g = 0;
mat.color.b = 1;
console.log(mat);
model.traverse((child) => {
if (child.isLight) {
child.intensity = 0;
}
});
let p = {
camera: camera,
canvas: canvas,
scene: scene,
renderer: renderer,
clock: new THREE.Clock(),
// controls: new OrbitControls(camera, canvas),
cameraMixer: cameraMixer,
lookatMixer: lookatMixer,
model: model,
};
p.clock.stop();
renderOnce(p)
function launchAnimation() {
p.clock.start();
requestAnimationFrame(() => render(p));
}
window.launchAnimation = launchAnimation;
}, (xhr) => {
console.log((xhr.loaded / xhr.total * 100) + '% loaded');
}, (error) => {
console.error('An error happened', error);
});
}
test_animation();