-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
273 lines (216 loc) · 9 KB
/
index.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
// Declare global variables
let model;
let scene;
let ambientLight;
let renderer;
let cameraDistance;
let controls;
let dark = false;
// Entry point to initialize the application
function init() {
setupScene(); // Setup 3D scene
setupRenderer(); // Setup WebGL renderer
setupLights(); // Setup ambient and directional lights
setupEventListeners(); // Setup event listeners
animate(); // Start animation loop
}
// Function to set up the 3D scene
function setupScene() {
// Create a new scene
scene = new THREE.Scene();
// Set up the camera to keep the model centered
const modelCenter = new THREE.Vector3(0, 0, 0);
cameraDistance = 10; // Setting up constant distance to ensure responsiveness
// Create a perspective camera
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
// Set the camera's position relative to the model's center
camera.position.copy(modelCenter.clone().add(new THREE.Vector3(cameraDistance, cameraDistance / 2, cameraDistance)));
}
// Function to load the 3D model
function loadModel(modelPath) {
const loader = new THREE.GLTFLoader();
loader.load(
modelPath,
function (gltf) {
if (model) {
scene.remove(model); // Remove the previous model from the scene
}
if (modelPath === "lowpoly_backpack/scene.gltf"){
cameraDistance = 1;
} else if (modelPath === "sugarcube_corner/scene.gltf"){
cameraDistance = 5;
} else if (modelPath === "2x2_cube/scene.gltf"){
cameraDistance = 2;
} else if (modelPath === "forest_house/scene.gltf"){
cameraDistance = 15;
}
model = gltf.scene;
scene.add(model);
// Automatically center the model
const bbox = new THREE.Box3().setFromObject(model);
const center = bbox.getCenter(new THREE.Vector3());
model.position.sub(center);
// Apply smooth shading to model materials
model.traverse((child) => {
if (child.isMesh) {
child.castShadow = true; // Enable shadow casting
child.receiveShadow = true; // Enable shadow receiving
// Adjust material properties for better shadow effects
child.material.flatShading = true; // Apply flat shading
child.material.side = THREE.DoubleSide; // Ensure both sides of geometry receive shadows
}
});
// Update camera position
camera.position.copy(center.clone().add(new THREE.Vector3(cameraDistance, cameraDistance / 2, cameraDistance)));
// Setup orbit controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.enableZoom = true;
},
undefined,
//Handling errors in loading the model
function (error) {
console.error('Error loading model:', error);
}
);
}
// Function to set up the WebGL renderer
function setupRenderer() {
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
// Set the pixel ratio to match the device's pixel density for better rendering on high-resolution displays
renderer.setPixelRatio(window.devicePixelRatio);
// Set the background color of the renderer, during night its dark blue and at day it is sky blue
renderer.setClearColor(0x191970);
renderer.shadowMap.enabled = true; // Enable shadow mapping in the renderer
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // Soft shadows for smoother edges
document.body.appendChild(renderer.domElement);
}
// Function to set up ambient and directional lights
function setupLights() {
// Add ambient light
ambientLight = new THREE.AmbientLight(0xffffff, 0.5); // Adjust ambient light intensity
scene.add(ambientLight);
// Add directional light (simulating sunlight)
const sunLight = new THREE.DirectionalLight(0xffe4c4, 1);
sunLight.position.set(40, 50, 40);
// Enable shadow casting from the directional light
sunLight.castShadow = true;
sunLight.shadow.mapSize.width = 2048; // Shadow map resolution
sunLight.shadow.mapSize.height = 2048;
// Set up shadow camera frustum to focus the shadow in a diagonal direction
const shadowCameraSize = 20;
sunLight.shadow.camera.left = -shadowCameraSize;
sunLight.shadow.camera.right = shadowCameraSize;
sunLight.shadow.camera.top = -shadowCameraSize;
sunLight.shadow.camera.bottom = shadowCameraSize;
sunLight.shadow.camera.near = 1;
sunLight.shadow.camera.far = 100;
scene.add(sunLight);
// Configure model to receive and cast shadows
if (model) {
model.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
}
}
// Function to set up event listeners
function setupEventListeners() {
window.addEventListener('resize', onWindowResize);
//Calling function when the mouse/cursor hovers over the model
renderer.domElement.addEventListener('mousemove', handleMouseMove);
//When the mouse/cursor is in the screen, the background music plays
renderer.domElement.addEventListener('mouseenter', playAmbientSound);
//When the mouse/cursor leaves the screen, the background music stops
renderer.domElement.addEventListener('mouseleave', pauseAmbientSound);
//Handles arrow key such that when its arrow up, the camera moves forward
//and when its arrow down, then the camera moves backward
document.addEventListener('keydown', handleKeyDown); // Keyboard input
}
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Rotate the model continuously around the y-axis
if (model) {
model.rotation.y += 0.003;
}
// Update controls if available
if (controls) {
controls.update();
}
// Render the scene
renderer.render(scene, camera);
}
// Function to handle window resizing
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// Function to handle mouse move events
function handleMouseMove(event) {
const mouseX = (event.clientX / window.innerWidth) * 2 - 1;
const mouseY = -(event.clientY / window.innerHeight) * 2 + 1;
// Create a raycaster from the camera position and mouse coordinates
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera({ x: mouseX, y: mouseY }, camera);
// Perform raycasting to detect intersections with scene objects
const intersects = raycaster.intersectObjects(scene.children, true);
// Check if there are any intersections with the model
if (intersects.length > 0) {
// Set custom cursor style when hovering over the model
renderer.domElement.style.cursor = 'url("media/cursor-glow.png"), auto';
} else {
// Reset cursor to default when not hovering over any object
renderer.domElement.style.cursor = 'auto';
}
}
// Function to play ambient sound
function playAmbientSound() {
const ambientSound = document.getElementById('ambientSound');
ambientSound.play();
}
// Function to pause ambient sound
function pauseAmbientSound() {
const ambientSound = document.getElementById('ambientSound');
ambientSound.pause();
}
// Function to handle keyboard input for navigation
function handleKeyDown(event) {
const speed = 0.3;
switch (event.key) {
case 'ArrowUp':
camera.position.z -= speed; // Move camera forward
break;
case 'ArrowDown':
camera.position.z += speed; // Move camera backward
break;
}
}
// Function to toggle ambient light
function toggleLights() {
dark = !dark; // Toggle state when clicked on mode button
// Toggle background color
renderer.setClearColor(dark ? 0x191970 : 0x87CEEB);
const modeButton = document.getElementById('modeButton'); //Refers to mode button
const modeDisplay = document.getElementById('modeDisplay'); //Refers to moon and sun images
if (dark) {
modeButton.style.backgroundColor = 'blue';
modeButton.style.backgroundImage = 'url(/media/night.png)';
modeButton.style.backgroundPosition = 'left';
modeDisplay.src = 'media/moon.png';
ambientLight.intensity = 0.3; // Ambient light off (dark mode)
} else {
modeButton.style.backgroundColor = 'rgb(39, 196, 236)';
modeButton.style.backgroundImage = 'url(/media/day.png)';
modeButton.style.backgroundPosition = 'right';
modeDisplay.src = 'media/sun.png';
ambientLight.intensity = 1; // Ambient light on (day mode)
}
}
// Call init() to start the application
init();