-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
189 lines (152 loc) · 5.75 KB
/
Copy pathscript.js
File metadata and controls
189 lines (152 loc) · 5.75 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
let scene, camera, renderer, model;
const canvas = document.getElementById('robot-canvas');
let camPos = { x: 0, y: 0, z: 5 };
let lookAt = { x: 0, y: 0, z: 0 };
let modelScale = 0.4;
let autoRotate = false; // Default to OFF
let shadowPlane; // Store globally for theme switching
function setAutoRotate(on) {
autoRotate = on;
const toggle = document.getElementById('rotate-toggle-btn');
toggle.checked = on;
}
function setThemeMode(isNight) {
const body = document.body;
const label = document.getElementById('theme-toggle-label');
if (isNight) {
body.classList.add('night-mode');
label.textContent = 'Night Mode';
if (shadowPlane) shadowPlane.material.color.set(0x222222); // dark for night
} else {
body.classList.remove('night-mode');
label.textContent = 'Day Mode';
if (shadowPlane) shadowPlane.material.color.set(0xffffff); // white for day
}
}
function init() {
scene = new THREE.Scene();
scene.background = null;
const width = window.innerWidth;
const height = window.innerHeight;
camera = new THREE.PerspectiveCamera(30, width / height, 0.1, 100);
camera.position.set(camPos.x, camPos.y, camPos.z);
camera.lookAt(lookAt.x, lookAt.y, lookAt.z);
renderer = new THREE.WebGLRenderer({ canvas, alpha: true, antialias: true });
renderer.setSize(width, height, false);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.outputEncoding = THREE.sRGBEncoding;
// Improved lighting for more vibrant colors
const hemiLight = new THREE.HemisphereLight(0xffffff, 0x888888, 1.3);
hemiLight.position.set(0, 10, 0);
scene.add(hemiLight);
const dirLight1 = new THREE.DirectionalLight(0xffffff, 1.5);
dirLight1.position.set(5, 10, 7.5);
dirLight1.castShadow = true;
dirLight1.shadow.mapSize.width = 1024;
dirLight1.shadow.mapSize.height = 1024;
dirLight1.shadow.camera.near = 1;
dirLight1.shadow.camera.far = 20;
scene.add(dirLight1);
// Add a second directional light from the opposite side
const dirLight2 = new THREE.DirectionalLight(0xffffff, 0.8);
dirLight2.position.set(-7, 6, -5);
scene.add(dirLight2);
// Add a subtle ambient light
const ambientLight = new THREE.AmbientLight(0xffffff, 0.25);
scene.add(ambientLight);
// White shadow plane under model (make it very large to cover the whole screen)
const shadowPlaneGeometry = new THREE.PlaneGeometry(40, 40); // Large enough to cover viewport
const shadowPlaneMaterial = new THREE.MeshStandardMaterial({ color: 0xffffff });
shadowPlane = new THREE.Mesh(shadowPlaneGeometry, shadowPlaneMaterial);
shadowPlane.rotation.x = -Math.PI / 2;
shadowPlane.position.y = -1.1;
shadowPlane.receiveShadow = true;
scene.add(shadowPlane);
const gltfLoader = new GLTFLoader();
renderer.outputColorSpace = THREE.SRGBColorSpace;
// Progress bar elements
const progressBar = document.getElementById('progress-bar');
const progressBarInner = document.getElementById('progress-bar-inner');
let progressBarLabel = document.getElementById('progress-bar-label');
function updateProgressBar(percent) {
progressBarInner.style.width = percent + '%';
progressBarLabel.textContent = 'LOADING... ' + Math.floor(percent) + '%';
if (percent >= 100) {
progressBar.style.display = 'none';
document.getElementById('explore-btn').style.display = 'block';
}
}
// Load GLB model
gltfLoader.load('model/boston.glb', (gltf) => {
model = gltf.scene;
model.position.set(0, -1, 0);
model.scale.set(modelScale, modelScale, modelScale);
model.rotation.set(0, 0, 0);
scene.add(model);
animate();
}, // Progress callback
(progress) => {
var percent = progress.loaded / progress.total * 100;
updateProgressBar(percent);
},
// Error callback
(error) => {
console.error('Error loading GLB model:', error);
});
// Add toggle button logic
const toggle = document.getElementById('rotate-toggle-btn');
toggle.addEventListener('change', (e) => {
console.log('Toggle button changed:', e.target.checked);
setAutoRotate(e.target.checked);
});
setAutoRotate(false);
const themeToggle = document.getElementById('theme-toggle-btn');
themeToggle.addEventListener('change', (e) => {
setThemeMode(themeToggle.checked);
});
setThemeMode(false);
// Add click event to explore button to hide overlay
var exploreBtn = document.getElementById('explore-btn');
if (exploreBtn) {
exploreBtn.addEventListener('click', function() {
var loaderOverlay = document.getElementById('loader-overlay');
if (loaderOverlay) loaderOverlay.style.display = 'none';
});
}
}
function animate() {
requestAnimationFrame(animate);
if (model) {
// Keep model scale consistent
model.scale.set(modelScale, modelScale, modelScale);
// Always keep model upright
model.rotation.x = 0;
// Handle auto-rotation
if (autoRotate) {
model.rotation.y += 0.008;
// Optional: Add some debugging to see if rotation is happening
if (Math.floor(model.rotation.y * 100) % 100 === 0) {
console.log('Model rotating, Y rotation:', model.rotation.y);
}
}
} else {
// Debug: Check if model is not loaded
console.log('Model not loaded yet, autoRotate:', autoRotate);
}
renderer.render(scene, camera);
}
function onWindowResize() {
const width = canvas.clientWidth;
const height = canvas.clientHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
window.addEventListener('resize', onWindowResize);
init();
// To set your own cinematic camera angle:
// camera.position.set(x, y, z);
// camera.lookAt(targetX, targetY, targetZ);