-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
328 lines (295 loc) · 7.79 KB
/
main.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import * as THREE from "three";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import { TWEEN } from "https://unpkg.com/[email protected]/examples/jsm/libs/tween.module.min.js";
let renderer;
let camera;
let controls;
let scene;
const createRenderer = () => {
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
const sceneElementWrapper = document.getElementById("scene");
sceneElementWrapper.appendChild(renderer.domElement);
};
const createCamera = () => {
camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.01,
2000
);
camera.position.set(0, 12, 0);
};
const createScene = () => {
scene = new THREE.Scene();
};
const createControls = () => {
controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.enablePan = true;
controls.minDistance = 0.1;
controls.maxDistance = 20;
controls.minPolarAngle = 0.5;
controls.maxPolarAngle = 1.5;
controls.autoRotate = false;
controls.target = new THREE.Vector3(0, 2, -0.5);
controls.update();
};
const createGround = () => {
const groundGeometry = new THREE.PlaneGeometry(100, 100, 32, 32);
groundGeometry.rotateX(-Math.PI / 2);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x555555,
side: THREE.DoubleSide,
});
const groundMesh = new THREE.Mesh(groundGeometry, groundMaterial);
groundMesh.castShadow = false;
groundMesh.receiveShadow = true;
scene.add(groundMesh);
};
const createLights = () => {
const spotLight1 = new THREE.SpotLight(0xffffff, 3, 50, 0.22, 1);
spotLight1.position.set(-10, 25, -10);
spotLight1.castShadow = true;
spotLight1.shadow.bias = -0.001;
const spotLight2 = new THREE.SpotLight(0xffffff, 3, 200, 0.22, 1);
spotLight2.position.set(10, 25, 25);
spotLight2.castShadow = true;
spotLight2.shadow.bias = -0.001;
scene.add(spotLight1);
scene.add(spotLight2);
};
const loadObject = (objectName) => {
const loadingElement = document.getElementById("loading-element");
const loader = new GLTFLoader().setPath(`public/${objectName}/`);
loader.load(
"scene.gltf",
(gltf) => {
loadingElement.style.display = "none";
createGround();
const mesh = gltf.scene;
mesh.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
mesh.position.set(0, 2, -0.5);
scene.add(mesh);
},
(xhr) => {
var percentComplete = (xhr.loaded / xhr.total) * 100;
loadingElement.innerHTML = "Loading: " + percentComplete.toFixed(2) + "%";
}
);
};
const bindListeners = () => {
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
const menuItemsHTMLCollection = document.querySelectorAll("#menu li");
const menuItems = Array.from(menuItemsHTMLCollection);
menuItems.forEach((menuItem) => {
menuItem.addEventListener("click", (event) => {
const objectId = event.target.getAttribute("id");
const targetPoint = cameraTargetsAndPositions[objectId];
var positionTween = new TWEEN.Tween(camera.position)
.to(
{
x: targetPoint.position.x,
y: targetPoint.position.y,
z: targetPoint.position.z,
},
2000
) // 2000 ms = 2 seconds
.easing(TWEEN.Easing.Quadratic.Out) // Use any easing function that suits the movement
.onUpdate(() => {});
var targetTween = new TWEEN.Tween(controls.target)
.to(
{
x: targetPoint.lookAt.x,
y: targetPoint.lookAt.y,
z: targetPoint.lookAt.z,
},
2000
)
.easing(TWEEN.Easing.Quadratic.Out)
.onUpdate(() => {
controls.update();
});
positionTween.start();
targetTween.start();
});
});
};
const animate = () => {
requestAnimationFrame(animate);
TWEEN.update();
renderer.render(scene, camera);
};
const init = (objectName) => {
createRenderer();
createCamera();
createScene();
createControls();
createLights();
bindListeners();
loadObject(objectName);
animate();
};
init("millennium_falcon");
const satellitePointData = {
position: {
x: 2.2363356030075563,
y: 2.820678683267509,
z: 2.684753953671324,
},
lookAt: {
x: 0,
y: 2,
z: -0.5,
},
};
const machineGunPointData = {
position: {
x: 0.5760624910715781,
y: 3.051817229890369,
z: 0.4757104852229018,
},
lookAt: {
x: -0.3332266219157822,
y: 2.2358192826928676,
z: -0.5257846919885497,
},
};
const frontalPointData = {
position: {
x: -1.6956829343157755,
y: 2.911476432638847,
z: 6.006285240809736,
},
lookAt: {
x: -0.12111220935307855,
y: 1.2546289604273042,
z: -0.3443322137823006,
},
};
const sidePodPointData = {
position: {
x: -3.842264605996623,
y: 2.531246896437086,
z: 3.2344043181914017,
},
lookAt: {
x: -1.2075112121972915,
y: 1.598525061225214,
z: -1.0790626359441582,
},
};
const airConditioningPointData = {
position: {
x: -0.5680513933598731,
y: 3.8647939449647435,
z: -2.8722469298357245,
},
lookAt: {
x: -0.03259431926139941,
y: 2.3942898229981293,
z: -1.899660839467895,
},
};
const warpDrivePointData = {
position: {
x: -2.2960948510395087,
y: 2.3045136500926064,
z: -3.7251187837076998,
},
lookAt: {
x: -0.22176763221662282,
y: 1.7171330696876503,
z: -1.974151811476235,
},
};
const cameraTargetsAndPositions = {
satellite: {
position: {
x: satellitePointData.position.x,
y: satellitePointData.position.y,
z: satellitePointData.position.z,
},
lookAt: {
x: satellitePointData.lookAt.x,
y: satellitePointData.lookAt.y,
z: satellitePointData.lookAt.z,
},
},
machineGun: {
position: {
x: machineGunPointData.position.x,
y: machineGunPointData.position.y,
z: machineGunPointData.position.z,
},
lookAt: {
x: machineGunPointData.lookAt.x,
y: machineGunPointData.lookAt.y,
z: machineGunPointData.lookAt.z,
},
},
frontal: {
position: {
x: frontalPointData.position.x,
y: frontalPointData.position.y,
z: frontalPointData.position.z,
},
lookAt: {
x: frontalPointData.lookAt.x,
y: frontalPointData.lookAt.y,
z: frontalPointData.lookAt.z,
},
},
sidePod: {
position: {
x: sidePodPointData.position.x,
y: sidePodPointData.position.y,
z: sidePodPointData.position.z,
},
lookAt: {
x: sidePodPointData.lookAt.x,
y: sidePodPointData.lookAt.y,
z: sidePodPointData.lookAt.z,
},
},
airConditioning: {
position: {
x: airConditioningPointData.position.x,
y: airConditioningPointData.position.y,
z: airConditioningPointData.position.z,
},
lookAt: {
x: airConditioningPointData.lookAt.x,
y: airConditioningPointData.lookAt.y,
z: airConditioningPointData.lookAt.z,
},
},
warpDrive: {
position: {
x: warpDrivePointData.position.x,
y: warpDrivePointData.position.y,
z: warpDrivePointData.position.z,
},
lookAt: {
x: warpDrivePointData.lookAt.x,
y: warpDrivePointData.lookAt.y,
z: warpDrivePointData.lookAt.z,
},
},
};