-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
516 lines (455 loc) · 18.8 KB
/
index.html
File metadata and controls
516 lines (455 loc) · 18.8 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Studio Apartment Explorer</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
font-family: Arial, sans-serif;
}
#scene-container {
position: absolute;
width: 100%;
height: 100%;
}
#instructions {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.7);
color: white;
padding: 20px;
border-radius: 5px;
text-align: center;
max-width: 400px;
z-index: 100;
pointer-events: none;
}
#instructions.hidden {
display: none;
}
#crosshair {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 10px;
height: 10px;
border-radius: 50%;
background-color: rgba(255, 255, 255, 0.5);
z-index: 10;
pointer-events: none;
}
#crosshair.hidden {
display: none;
}
#loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.7);
color: white;
padding: 20px;
border-radius: 5px;
text-align: center;
z-index: 200;
}
#loading.hidden {
display: none;
}
#model-input {
position: absolute;
top: 10px;
left: 10px;
z-index: 100;
background-color: rgba(0, 0, 0, 0.7);
color: white;
padding: 10px;
border-radius: 5px;
width: 400px;
}
#model-input input[type="text"] {
width: 300px;
padding: 5px;
margin-right: 5px;
}
#model-input button {
padding: 5px 10px;
cursor: pointer;
}
#model-input.hidden {
display: none;
}
#file-input {
display: none;
}
.or-divider {
display: flex;
align-items: center;
margin: 10px 0;
color: #ccc;
}
.or-divider::before,
.or-divider::after {
content: "";
flex: 1;
border-bottom: 1px solid #ccc;
}
.or-divider::before {
margin-right: 10px;
}
.or-divider::after {
margin-left: 10px;
}
.file-upload-btn {
display: inline-block;
background-color: #4CAF50;
color: white;
padding: 8px 12px;
border-radius: 4px;
cursor: pointer;
margin-top: 5px;
text-align: center;
}
.file-upload-btn:hover {
background-color: #45a049;
}
#debug-info {
position: absolute;
bottom: 10px;
left: 10px;
background-color: rgba(0, 0, 0, 0.5);
color: white;
padding: 5px;
font-family: monospace;
font-size: 12px;
z-index: 100;
}
</style>
</head>
<body>
<div id="scene-container"></div>
<div id="crosshair"></div>
<div id="instructions">
<h1>Studio Apartment Explorer</h1>
<p>Click to start exploring</p>
<p><strong>Controls:</strong></p>
<p>W, A, S, D = Move</p>
<p>Mouse = Look around</p>
<p>ESC = Exit controls</p>
</div>
<div id="loading">
<h2>Loading Model...</h2>
<p>Please wait, this may take a moment for large files.</p>
<div id="progress-text">0%</div>
</div>
<div id="model-input">
<h3>Load 3D Model</h3>
<p>Enter URL to GLB model:</p>
<input type="text" id="model-url" placeholder="https://example.com/model.glb">
<button id="load-model-btn">Load Model</button>
<div class="or-divider">OR</div>
<p>Select file from your computer:</p>
<label for="file-input" class="file-upload-btn">Choose GLB File</label>
<input type="file" id="file-input" accept=".glb,.gltf">
<div id="file-name">No file selected</div>
</div>
<div id="debug-info"></div>
<script type="module">
import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js';
import { GLTFLoader } from 'https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/loaders/GLTFLoader.js';
import { PointerLockControls } from 'https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/controls/PointerLockControls.js';
import { DRACOLoader } from 'https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/loaders/DRACOLoader.js';
// Scene setup
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 1.6, 0); // Set initial camera height to approximate eye level
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true;
document.getElementById('scene-container').appendChild(renderer.domElement);
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 10, 10);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
scene.add(directionalLight);
// Add some point lights to simulate lamps/fixtures in the apartment
const pointLight1 = new THREE.PointLight(0xffcc77, 1, 10);
pointLight1.position.set(2, 2, 2);
pointLight1.castShadow = true;
scene.add(pointLight1);
const pointLight2 = new THREE.PointLight(0xffcc77, 1, 10);
pointLight2.position.set(-2, 2, -2);
pointLight2.castShadow = true;
scene.add(pointLight2);
// Controls setup
const controls = new PointerLockControls(camera, document.body);
const instructions = document.getElementById('instructions');
const crosshair = document.getElementById('crosshair');
const loading = document.getElementById('loading');
const modelInput = document.getElementById('model-input');
const debugInfo = document.getElementById('debug-info');
const fileInput = document.getElementById('file-input');
const fileName = document.getElementById('file-name');
loading.classList.add('hidden');
instructions.addEventListener('click', function() {
controls.lock();
});
controls.addEventListener('lock', function() {
instructions.classList.add('hidden');
crosshair.classList.remove('hidden');
modelInput.classList.add('hidden');
});
controls.addEventListener('unlock', function() {
instructions.classList.remove('hidden');
crosshair.classList.add('hidden');
modelInput.classList.remove('hidden');
});
scene.add(controls.getObject());
// Movement controls
let moveForward = false;
let moveBackward = false;
let moveLeft = false;
let moveRight = false;
const velocity = new THREE.Vector3();
const direction = new THREE.Vector3();
let prevTime = performance.now();
const onKeyDown = function(event) {
switch(event.code) {
case 'KeyW':
moveForward = true;
break;
case 'KeyA':
moveLeft = true;
break;
case 'KeyS':
moveBackward = true;
break;
case 'KeyD':
moveRight = true;
break;
}
};
const onKeyUp = function(event) {
switch(event.code) {
case 'KeyW':
moveForward = false;
break;
case 'KeyA':
moveLeft = false;
break;
case 'KeyS':
moveBackward = false;
break;
case 'KeyD':
moveRight = false;
break;
}
};
document.addEventListener('keydown', onKeyDown);
document.addEventListener('keyup', onKeyUp);
// Collision detection setup
const raycaster = new THREE.Raycaster(new THREE.Vector3(), new THREE.Vector3(0, -1, 0), 0, 10);
const collisionObjects = [];
let apartmentModel = null;
let objectUrl = null;
// Create a placeholder floor to start with
const floorGeometry = new THREE.PlaneGeometry(100, 100);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x999999,
roughness: 0.8,
metalness: 0.2
});
const floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.1;
floor.receiveShadow = true;
scene.add(floor);
// Setup DRACO loader for compressed models
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath('https://www.gstatic.com/draco/versioned/decoders/1.5.6/');
// Setup GLTF loader
const loader = new GLTFLoader();
loader.setDRACOLoader(dracoLoader);
// Function to load the apartment model
function loadApartmentModel(modelUrl) {
// Show loading screen
loading.classList.remove('hidden');
const progressText = document.getElementById('progress-text');
// Clear previous model if it exists
if (apartmentModel) {
scene.remove(apartmentModel);
apartmentModel = null;
}
// Revoke previous object URL if exists
if (objectUrl) {
URL.revokeObjectURL(objectUrl);
objectUrl = null;
}
// Clear collision objects except floor
collisionObjects.length = 0;
collisionObjects.push(floor);
// Determine if it's a direct URL or a Mega link
if (modelUrl.includes('mega.nz')) {
progressText.textContent = "Please download the file from Mega and use the file upload option instead.";
return;
}
loader.load(
modelUrl,
function(gltf) {
apartmentModel = gltf.scene;
scene.add(apartmentModel);
// Add collision objects from the model
apartmentModel.traverse(function(object) {
if (object.isMesh) {
object.castShadow = true;
object.receiveShadow = true;
// Add to collision objects if it's not too small
if (object.geometry.boundingBox) {
const size = new THREE.Vector3();
object.geometry.boundingBox.getSize(size);
if (size.x > 0.5 || size.y > 0.5 || size.z > 0.5) {
collisionObjects.push(object);
}
} else {
object.geometry.computeBoundingBox();
collisionObjects.push(object);
}
}
});
// Get the bounding box of the model to set initial camera position
const boundingBox = new THREE.Box3().setFromObject(apartmentModel);
const center = new THREE.Vector3();
boundingBox.getCenter(center);
// Set initial camera position to the center of the model, slightly above the floor
const size = new THREE.Vector3();
boundingBox.getSize(size);
const maxDim = Math.max(size.x, size.z);
// Position camera at an offset from the center
controls.getObject().position.set(
center.x,
center.y + 1.6, // Eye level
center.z + maxDim * 0.3 // Offset from center
);
// Hide loading screen
loading.classList.add('hidden');
console.log('Model loaded successfully');
},
function(xhr) {
if (xhr.lengthComputable) {
const percent = Math.floor((xhr.loaded / xhr.total) * 100);
progressText.textContent = percent + '% loaded';
console.log(percent + '% loaded');
} else {
progressText.textContent = 'Loading... ' + Math.floor(xhr.loaded / 1024 / 1024) + ' MB';
}
},
function(error) {
console.error('An error happened', error);
progressText.textContent = 'Error loading model: ' + error.message;
loading.classList.add('hidden');
}
);
}
// Handle model loading button
document.getElementById('load-model-btn').addEventListener('click', function() {
const modelUrl = document.getElementById('model-url').value.trim();
if (modelUrl) {
loadApartmentModel(modelUrl);
} else {
// If URL field is empty, trigger file input click
fileInput.click();
}
});
// Handle file input change
fileInput.addEventListener('change', function(event) {
const file = event.target.files[0];
if (file) {
fileName.textContent = file.name;
// Create object URL for the file
if (objectUrl) {
URL.revokeObjectURL(objectUrl);
}
objectUrl = URL.createObjectURL(file);
// Load the model from the object URL
loadApartmentModel(objectUrl);
}
});
// Handle file upload button click
document.querySelector('.file-upload-btn').addEventListener('click', function() {
fileInput.click();
});
// Animation loop
function animate() {
requestAnimationFrame(animate);
if (controls.isLocked === true) {
const time = performance.now();
const delta = (time - prevTime) / 1000;
velocity.x -= velocity.x * 10.0 * delta;
velocity.z -= velocity.z * 10.0 * delta;
direction.z = Number(moveForward) - Number(moveBackward);
direction.x = Number(moveRight) - Number(moveLeft);
direction.normalize();
if (moveForward || moveBackward) velocity.z -= direction.z * 400.0 * delta;
if (moveLeft || moveRight) velocity.x -= direction.x * 400.0 * delta;
// Collision detection
const cameraPosition = controls.getObject().position.clone();
let canMove = true;
// Check for collisions in the direction of movement
if (velocity.length() > 0) {
const moveDirection = new THREE.Vector3(velocity.x, 0, velocity.z).normalize();
// Create raycasters for different heights (feet, body, head)
const rayHeight = [0.1, 0.9, 1.6]; // Different heights to check for collisions
const rayDirections = [
moveDirection.clone(),
new THREE.Vector3(-moveDirection.z, 0, moveDirection.x), // 90 degrees left
new THREE.Vector3(moveDirection.z, 0, -moveDirection.x) // 90 degrees right
];
// Check collisions at different heights and directions
for (let h = 0; h < rayHeight.length; h++) {
for (let d = 0; d < rayDirections.length; d++) {
const rayStart = cameraPosition.clone();
rayStart.y = cameraPosition.y - 1.6 + rayHeight[h]; // Adjust for camera height
raycaster.set(rayStart, rayDirections[d]);
const intersections = raycaster.intersectObjects(collisionObjects);
if (intersections.length > 0 && intersections[0].distance < 0.5) {
canMove = false;
break;
}
}
if (!canMove) break;
}
}
// Update debug info
debugInfo.textContent = `Position: ${controls.getObject().position.x.toFixed(2)}, ${controls.getObject().position.y.toFixed(2)}, ${controls.getObject().position.z.toFixed(2)}`;
if (canMove) {
controls.moveRight(-velocity.x * delta);
controls.moveForward(-velocity.z * delta);
}
prevTime = time;
}
renderer.render(scene, camera);
}
// Handle window resize
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener('resize', onWindowResize);
// Start animation loop
animate();
</script>
</body>
</html>