-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyApp.js
More file actions
173 lines (130 loc) · 4.96 KB
/
MyApp.js
File metadata and controls
173 lines (130 loc) · 4.96 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import Stats from 'three/addons/libs/stats.module.js';
/**
* This class contains the application object
*/
class MyApp {
/**
* the constructor
*/
constructor() {
this.scene = null;
this.stats = null;
// camera related attributes
this.activeCamera = null;
this.activeCameraName = null;
this.lastActiveCameraName = null;
this.cameras = {};
// other attributes
this.renderer = null;
this.controls = null;
this.gui = null;
this.contents = null;
this.clock = new THREE.Clock();
}
/**
* initializes the application
*/
init() {
// Create an empty scene
this.scene = new THREE.Scene();
this.scene.background = new THREE.Color( 0x101010 );
this.stats = new Stats()
this.stats.showPanel(1) // 0: fps, 1: ms, 2: mb, 3+: custom
document.body.appendChild(this.stats.dom)
this.initCameras();
// Create a renderer with Antialiasing
this.renderer = new THREE.WebGLRenderer({ antialias:true });
this.renderer.setPixelRatio( window.devicePixelRatio );
this.renderer.setClearColor("#000000");
this.renderer.shadowMap.enabled = true;
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
// Configure renderer size
this.renderer.setSize( window.innerWidth, window.innerHeight );
document.getElementById("canvas").appendChild(this.renderer.domElement);
this.controls = new OrbitControls(this.activeCamera, this.renderer.domElement);
this.controls.enableZoom = true;
this.controls.update();
this.setActiveCamera('DefaultCamera');
// manage window resizes
window.addEventListener('resize', this.onResize.bind(this), false );
}
initCameras() {
const aspect = window.innerWidth / window.innerHeight;
const defaultCamera = new THREE.PerspectiveCamera(75, aspect, 0.1, 1000);
defaultCamera.position.set(10, 10, 3);
this.cameras['DefaultCamera'] = defaultCamera;
this.activeCamera = defaultCamera;
this.activeCameraName = 'DefaultCamera';
}
setActiveCamera(cameraName) {
if (this.cameras[cameraName]) {
this.lastActiveCameraName = this.activeCameraName;
this.activeCameraName = cameraName;
this.activeCamera = this.cameras[this.activeCameraName];
} else {
console.error(`Camera "${cameraName}" not found.`);
const cameraNames = Object.keys(this.cameras);
if (cameraNames.length > 0) {
this.activeCameraName = cameraNames[0];
this.activeCamera = this.cameras[this.activeCameraName];
} else {
console.error('No cameras available.');
this.activeCamera = null;
}
}
if (this.activeCamera) {
this.activeCamera.aspect = window.innerWidth / window.innerHeight;
this.activeCamera.updateProjectionMatrix();
}
this.updateControls();
}
updateControls() {
if (this.controls) this.controls.dispose();
if (this.activeCameraName !== "FirstPersonCamera") {
this.controls = new OrbitControls(this.activeCamera, this.renderer.domElement);
this.controls.enableZoom = true;
if (this.activeCameraName.userData && this.activeCameraName.userData.target) {
this.controls.target.copy(this.activeCameraName.userData.target);
}
else this.controls.target.set(0, 0, 0);
this.controls.update();
}
}
onResize() {
if (this.activeCamera && this.renderer) {
if (this.activeCamera.isPerspectiveCamera) {
this.activeCamera.aspect = window.innerWidth / window.innerHeight;
this.activeCamera.updateProjectionMatrix();
}
this.renderer.setSize(window.innerWidth, window.innerHeight);
}
}
restartScene() {
this.scene = new THREE.Scene();
}
setContents(contents) {
this.contents = contents;
}
setGui(gui) {
this.gui = gui;
}
/**
* the main render function. Called in a requestAnimationFrame loop
*/
render() {
this.stats.begin();
const delta = this.clock.getDelta();
if (this.activeScene) {
this.activeScene.update(delta);
this.activeScene.render();
}
if (this.contents) this.contents.update(delta);
if (this.controls instanceof OrbitControls) this.controls.update();
if (this.activeCamera) this.renderer.render(this.scene, this.activeCamera);
this.stats.end();
requestAnimationFrame(this.render.bind(this));
}
}
export { MyApp };