-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyApp.js
253 lines (220 loc) · 6.85 KB
/
MyApp.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import { MyContents } from "./MyContents.js";
import { MyGuiInterface } from "./MyGuiInterface.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.lastCameraName = null;
this.cameras = [];
this.frustumSize = 20;
// other attributes
this.renderer = null;
this.controls = null;
this.gui = null;
this.axis = null;
this.contents == null;
}
/**
* 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();
this.setActiveCamera("Custom Perspective");
// Create a renderer with Antialiasing
this.renderer = new THREE.WebGLRenderer({ antialias: true });
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setClearColor("#000000");
// Configure renderer size
this.renderer.setSize(window.innerWidth, window.innerHeight);
// Set shadow map to true
this.renderer.shadowMap.enabled = true;
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
// Append Renderer to DOM
document.getElementById("canvas").appendChild(this.renderer.domElement);
// manage window resizes
window.addEventListener("resize", this.onResize.bind(this), false);
}
/**
* initializes all the cameras
*/
initCameras() {
const aspect = window.innerWidth / window.innerHeight;
// Create a basic perspective camera
const perspective1 = new THREE.PerspectiveCamera(75, aspect, 0.1, 1000);
perspective1.position.set(10, 10, 3);
this.cameras["Perspective"] = perspective1;
// Custom perspective camera
const perspective2 = new THREE.PerspectiveCamera(45, aspect, 0.1, 1000);
perspective2.position.set(30, 30, 0);
this.cameras["Custom Perspective"] = perspective2;
// defines the frustum size for the orthographic cameras. frustumSize = 20
const left = (-this.frustumSize / 2) * aspect;
const right = (this.frustumSize / 2) * aspect;
const top = this.frustumSize / 2;
const bottom = -this.frustumSize / 2;
const near = -this.frustumSize / 2;
const far = this.frustumSize;
// create a left view orthographic camera
const orthoLeft = new THREE.OrthographicCamera(
left,
right,
top,
bottom,
near,
far
);
orthoLeft.up = new THREE.Vector3(0, 1, 0);
orthoLeft.position.set(-this.frustumSize / 4, 0, 0);
orthoLeft.lookAt(new THREE.Vector3(0, 0, 0));
this.cameras["Left"] = orthoLeft;
// create a right view orthographic camera
const orthoRight = new THREE.OrthographicCamera(
left,
right,
top,
bottom,
near,
far
);
orthoRight.up = new THREE.Vector3(0, 1, 0);
orthoRight.position.set(this.frustumSize / 4, 0, 0);
orthoRight.lookAt(new THREE.Vector3(0, 0, 0));
this.cameras["Right"] = orthoRight;
// create a top view orthographic camera
const orthoTop = new THREE.OrthographicCamera(
left,
right,
top,
bottom,
near,
far
);
orthoTop.up = new THREE.Vector3(0, 0, 1);
orthoTop.position.set(0, this.frustumSize / 4, 0);
orthoTop.lookAt(new THREE.Vector3(0, 0, 0));
this.cameras["Top"] = orthoTop;
// create a front view orthographic camera
const orthoFront = new THREE.OrthographicCamera(
left,
right,
top,
bottom,
near,
far
);
orthoFront.up = new THREE.Vector3(0, 1, 0);
orthoFront.position.set(0, 0, this.frustumSize / 4);
orthoFront.lookAt(new THREE.Vector3(0, 0, 0));
this.cameras["Front"] = orthoFront;
const orthoBack = new THREE.OrthographicCamera(
left,
right,
top,
bottom,
near,
far
);
orthoBack.up = new THREE.Vector3(0, 1, 0);
orthoBack.position.set(0, 0, -this.frustumSize / 4);
orthoBack.lookAt(new THREE.Vector3(0, 0, 0));
this.cameras["Back"] = orthoBack;
}
/**
* sets the active camera by name
* @param {String} cameraName
*/
setActiveCamera(cameraName) {
this.activeCameraName = cameraName;
this.activeCamera = this.cameras[this.activeCameraName];
}
/**
* updates the active camera if required
* this function is called in the render loop
* when the active camera name changes
* it updates the active camera and the controls
*/
updateCameraIfRequired() {
// camera changed?
if (this.lastCameraName !== this.activeCameraName) {
this.lastCameraName = this.activeCameraName;
this.activeCamera = this.cameras[this.activeCameraName];
document.getElementById("camera").innerHTML = this.activeCameraName;
// call on resize to update the camera aspect ratio
// among other things
this.onResize();
// are the controls yet?
if (this.controls === null) {
// Orbit controls allow the camera to orbit around a target.
this.controls = new OrbitControls(
this.activeCamera,
this.renderer.domElement
);
this.controls.enableZoom = true;
this.controls.update();
} else {
this.controls.object = this.activeCamera;
}
}
}
/**
* the window resize handler
*/
onResize() {
if (this.activeCamera !== undefined && this.activeCamera !== null) {
this.activeCamera.aspect = window.innerWidth / window.innerHeight;
this.activeCamera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
}
}
/**
*
* @param {MyContents} contents the contents object
*/
setContents(contents) {
this.contents = contents;
}
/**
* @param {MyGuiInterface} contents the gui interface object
*/
setGui(gui) {
this.gui = gui;
}
/**
* the main render function. Called in a requestAnimationFrame loop
*/
render() {
this.stats.begin();
this.updateCameraIfRequired();
// update the animation if contents were provided
if (this.activeCamera !== undefined && this.activeCamera !== null) {
this.contents.update();
}
// required if controls.enableDamping or controls.autoRotate are set to true
this.controls.update();
// render the scene
this.renderer.render(this.scene, this.activeCamera);
// subsequent async calls to the render loop
requestAnimationFrame(this.render.bind(this));
this.lastCameraName = this.activeCameraName;
this.stats.end();
}
}
export { MyApp };