-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
1546 lines (1358 loc) · 55.5 KB
/
Copy pathmain.js
File metadata and controls
1546 lines (1358 loc) · 55.5 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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as THREE from "three/webgpu";
import CameraControls from "camera-controls";
import { RoundedBoxGeometry } from "three/addons/geometries/RoundedBoxGeometry.js";
import { DRACOLoader } from "three/addons/loaders/DRACOLoader.js";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
import { CSS3DObject, CSS3DRenderer } from "three/addons/renderers/CSS3DRenderer.js";
import { mergeGeometries } from "three/addons/utils/BufferGeometryUtils.js";
import { RectAreaLightTexturesLib } from "three/addons/lights/RectAreaLightTexturesLib.js";
let camera, scene, rendererCSS3D, rendererWebGPU;
let controls;
let iframe;
let clock;
let powerButtonMesh;
let preFullscreenPosition = null;
let preFullscreenTarget = null;
// Animated objects
let curtainLeft, curtainRight;
let curtainLeftOrigPos, curtainRightOrigPos;
let minuteHandGroup, hourHandGroup;
let particleGeometry, particlePositions, particleSeeds;
// Scene constants
const WORLD_SHIFT_X = 2340;
const WORLD_SHIFT_Z = 2750;
const FLOOR_Y = -2050;
const CEILING_Y = 1900;
const ROOM_W = 6000;
const ROOM_D = 6100;
const WALL_LEFT_X = -4000 + WORLD_SHIFT_X; // fixed — next to desk
const WALL_BACK_Z = -3750 + WORLD_SHIFT_Z; // fixed — next to desk
const WALL_RIGHT_X = WALL_LEFT_X + ROOM_W; // +2100
const ROOM_CX = (WALL_LEFT_X + WALL_RIGHT_X) / 2; // -950
const ROOM_CZ = WALL_BACK_Z + ROOM_D / 2; // -700
const ROOM_H = CEILING_Y - FLOOR_Y; // 3950
// Desk sits in the back-left corner
const DESK_CX = -2440 + WORLD_SHIFT_X;
const DESK_CZ = -2900 + WORLD_SHIFT_Z;
const DESK_Y = -550;
const SCREEN_X = DESK_CX + 100;
const SCREEN_Y = 0;
const SCREEN_Z = DESK_CZ + 150;
const MONITOR_X = DESK_CX + 100;
const MONITOR_Y = DESK_Y;
const MONITOR_Z = DESK_CZ - 184;
const SCREEN_SIZE = { width: 852, height: 588 };
const SCREEN_TILT = THREE.MathUtils.degToRad(-10);
const CURTAIN_SEG_COLS = 16;
const CURTAIN_SEG_ROWS = 20;
/**
* Atari ST-like RAL 7038 coloured matt plastic material
*/
const atariMaterial = new THREE.MeshPhysicalMaterial({
color: 0xafb0a8,
roughness: 0.35,
metalness: 0.05,
clearcoat: 0.15,
clearcoatRoughness: 0.5,
vertexColors: false,
});
const gltfLoader = new GLTFLoader();
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath("https://www.gstatic.com/draco/v1/decoders/");
gltfLoader.setDRACOLoader(dracoLoader);
function loadGltf(url) {
return new Promise((resolve, reject) => {
gltfLoader.load(url, (gltf) => resolve(gltf.scene), undefined, reject);
});
}
function loadTexture(url) {
return new Promise((resolve, reject) => {
new THREE.TextureLoader().load(url, resolve, undefined, reject);
});
}
function forEachMesh(parent, callback) {
parent.traverse((child) => {
if (child.isMesh) callback(child);
});
}
function setStyles(element, styles) {
Object.assign(element.style, styles);
}
function setGeometryVertexColor(geometry, color) {
const colors = new Float32Array(geometry.attributes.position.count * 3);
for (let i = 0; i < geometry.attributes.position.count; i++) {
colors[i * 3] = color.r;
colors[i * 3 + 1] = color.g;
colors[i * 3 + 2] = color.b;
}
geometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
}
function resizeHandler() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
rendererWebGPU.setPixelRatio(Math.min(window.devicePixelRatio, 2));
rendererWebGPU.setSize(window.innerWidth, window.innerHeight);
rendererCSS3D.setSize(window.innerWidth, window.innerHeight);
}
function animateCurtainPanel(mesh, origPositions, t, phase) {
const pos = mesh.geometry.attributes.position;
for (let row = 0; row <= CURTAIN_SEG_ROWS; row++) {
const vFrac = row / CURTAIN_SEG_ROWS;
const amp = vFrac * vFrac * 35;
for (let col = 0; col <= CURTAIN_SEG_COLS; col++) {
const idx = row * (CURTAIN_SEG_COLS + 1) + col;
const ox = origPositions[idx * 3];
const oy = origPositions[idx * 3 + 1];
const oz = origPositions[idx * 3 + 2];
pos.setXYZ(
idx,
ox + Math.sin(t * 0.7 + vFrac * 3.0 + phase) * amp,
oy,
oz + Math.cos(t * 0.5 + vFrac * 2.0 + phase) * amp * 0.5,
);
}
}
pos.needsUpdate = true;
mesh.geometry.computeVertexNormals();
}
function animateClock() {
if (!minuteHandGroup || !hourHandGroup) return;
const now = new Date();
const seconds = now.getSeconds() + now.getMilliseconds() / 1000;
const minutes = now.getMinutes() + seconds / 60;
const hours = (now.getHours() % 12) + minutes / 60;
// Clock is on right wall, viewed from -X; positive rotation.x = clockwise
minuteHandGroup.rotation.x = (minutes / 60) * Math.PI * 2;
hourHandGroup.rotation.x = (hours / 12) * Math.PI * 2;
}
function animateDustParticles(t) {
if (!particleGeometry) return;
const pos = particlePositions;
const count = particleSeeds.length;
for (let i = 0; i < count; i++) {
pos[i * 3 + 1] += 0.25;
pos[i * 3] += Math.sin(t * 0.25 + particleSeeds[i]) * 0.18;
pos[i * 3 + 2] += Math.cos(t * 0.2 + particleSeeds[i]) * 0.18;
if (pos[i * 3 + 1] > 1500) {
pos[i * 3 + 1] = FLOOR_Y + 50;
pos[i * 3] = (Math.random() - 0.5) * 5000 + ROOM_CX;
pos[i * 3 + 2] = (Math.random() - 0.5) * 4000 + ROOM_CZ;
}
}
particleGeometry.attributes.position.needsUpdate = true;
}
function animate() {
const delta = clock.getDelta();
const t = clock.getElapsedTime();
controls.update(delta);
if (curtainLeft) animateCurtainPanel(curtainLeft, curtainLeftOrigPos, t, 0);
if (curtainRight) animateCurtainPanel(curtainRight, curtainRightOrigPos, t, Math.PI * 0.6);
animateClock();
animateDustParticles(t);
rendererWebGPU.render(scene, camera);
rendererCSS3D.render(scene, camera);
}
function applyAtariMaterial(parent) {
forEachMesh(parent, (child) => {
child.material = atariMaterial;
});
}
function enableShadows(parent) {
forEachMesh(parent, (child) => {
child.castShadow = true;
child.receiveShadow = true;
});
}
function createRoom() {
const group = new THREE.Group();
const wallMat = new THREE.MeshStandardMaterial({ color: 0xe8ddd0, roughness: 0.88, metalness: 0 });
const ceilMat = new THREE.MeshStandardMaterial({ color: 0xf5f0e8, roughness: 0.92, metalness: 0 });
const skirtMat = new THREE.MeshStandardMaterial({ color: 0xf2ece4, roughness: 0.7, metalness: 0 });
// Back wall
const backWall = new THREE.Mesh(new THREE.PlaneGeometry(ROOM_W, ROOM_H), wallMat);
backWall.position.set(ROOM_CX, FLOOR_Y + ROOM_H / 2, WALL_BACK_Z);
backWall.receiveShadow = true;
group.add(backWall);
// Left wall
const leftWall = new THREE.Mesh(new THREE.PlaneGeometry(ROOM_D, ROOM_H), wallMat);
leftWall.rotation.y = Math.PI / 2;
leftWall.position.set(WALL_LEFT_X, FLOOR_Y + ROOM_H / 2, ROOM_CZ);
leftWall.receiveShadow = true;
group.add(leftWall);
// Right wall
const rightWall = new THREE.Mesh(new THREE.PlaneGeometry(ROOM_D, ROOM_H), wallMat);
rightWall.rotation.y = -Math.PI / 2;
rightWall.position.set(WALL_RIGHT_X, FLOOR_Y + ROOM_H / 2, ROOM_CZ);
rightWall.receiveShadow = true;
group.add(rightWall);
// Ceiling
const ceiling = new THREE.Mesh(new THREE.PlaneGeometry(ROOM_W, ROOM_D), ceilMat);
ceiling.rotation.x = Math.PI / 2;
ceiling.position.set(ROOM_CX, CEILING_Y, ROOM_CZ);
group.add(ceiling);
// Floor planks
const plankW = 375;
const plankCount = ROOM_W / plankW;
const plankGeo = new THREE.BoxGeometry(plankW - 4, 6, ROOM_D);
const plankMats = [
new THREE.MeshStandardMaterial({ color: 0x6b4c35, roughness: 0.82, metalness: 0 }),
new THREE.MeshStandardMaterial({ color: 0x7a5a42, roughness: 0.82, metalness: 0 }),
];
const plankCounts = [Math.ceil(plankCount / 2), Math.floor(plankCount / 2)];
const plankIMs = plankCounts.map((n, ci) => {
const im = new THREE.InstancedMesh(plankGeo, plankMats[ci], n);
im.receiveShadow = true;
return im;
});
const _plankMatrix = new THREE.Matrix4();
const _plankIdx = [0, 0];
for (let i = 0; i < plankCount; i++) {
const ci = i % 2;
_plankMatrix.makeTranslation(WALL_LEFT_X + plankW * i + plankW / 2, FLOOR_Y + 3, ROOM_CZ);
plankIMs[ci].setMatrixAt(_plankIdx[ci]++, _plankMatrix);
}
plankIMs.forEach(im => group.add(im));
// Skirting boards — merged into one draw call
const skirtH = 80;
const skirtD = 18;
const skirtY = FLOOR_Y + skirtH / 2;
const skirtGeos = [
(() => { const g = new THREE.BoxGeometry(ROOM_W, skirtH, skirtD); g.translate(ROOM_CX, skirtY, WALL_BACK_Z + skirtD / 2); return g; })(),
(() => { const g = new THREE.BoxGeometry(skirtD, skirtH, ROOM_D); g.translate(WALL_LEFT_X + skirtD / 2, skirtY, ROOM_CZ); return g; })(),
(() => { const g = new THREE.BoxGeometry(skirtD, skirtH, ROOM_D); g.translate(WALL_RIGHT_X - skirtD / 2, skirtY, ROOM_CZ); return g; })(),
];
const skirtMerged = mergeGeometries(skirtGeos);
skirtGeos.forEach(g => g.dispose());
const skirtMesh = new THREE.Mesh(skirtMerged, skirtMat);
skirtMesh.castShadow = true;
skirtMesh.receiveShadow = true;
group.add(skirtMesh);
scene.add(group);
}
function createWindow() {
const wy = FLOOR_Y + 2100;
const wz = WALL_BACK_Z + ROOM_D * 0.75;
const wW = 1400;
const wH = 1800;
const winGroup = new THREE.Group();
winGroup.position.set(WALL_LEFT_X, 0, wz);
winGroup.rotation.y = Math.PI / 2;
scene.add(winGroup);
const fZ = 1;
// Frame
const frameMat = new THREE.MeshStandardMaterial({ color: 0xf5f0e8, roughness: 0.7, metalness: 0 });
const frameThick = 55;
const frameDepth = 70;
const frameParts = [
[wW + frameThick * 2, frameThick, frameDepth, 0, wy + wH / 2 + frameThick / 2, fZ],
[wW + frameThick * 2, frameThick, frameDepth, 0, wy - wH / 2 - frameThick / 2, fZ],
[frameThick, wH + frameThick * 2, frameDepth, -wW / 2 - frameThick / 2, wy, fZ],
[frameThick, wH + frameThick * 2, frameDepth, wW / 2 + frameThick / 2, wy, fZ],
[30, wH, 40, 0, wy, fZ + 10],
[wW, 30, 40, 0, wy + 100, fZ + 10],
];
const frameGeos = frameParts.map(([fw, fh, fd, fx, fy, fz]) => {
const g = new THREE.BoxGeometry(fw, fh, fd);
g.translate(fx, fy, fz);
return g;
});
const frameMerged = mergeGeometries(frameGeos);
frameGeos.forEach(g => g.dispose());
const frameMesh = new THREE.Mesh(frameMerged, frameMat);
frameMesh.castShadow = true;
winGroup.add(frameMesh);
// Glass panes
const glassMat = new THREE.MeshPhysicalMaterial({
color: 0x08161a,
transmission: 0.95,
roughness: 0.05,
metalness: 0,
ior: 1.5,
thickness: 2,
depthWrite: false,
});
const paneW = (wW - 30) / 2 - 10;
const paneH = wH - 30;
const paneGeos = [-wW / 4 - 5, wW / 4 + 5].map(px => {
const g = new THREE.PlaneGeometry(paneW, paneH);
g.translate(px, wy, fZ + 15);
return g;
});
const paneMerged = mergeGeometries(paneGeos);
paneGeos.forEach(g => g.dispose());
winGroup.add(new THREE.Mesh(paneMerged, glassMat));
// Curtain rod — wide enough to hold curtains on either side, 50% thicker
const curtainW = 560;
const totalCurtainSpan = wW + curtainW * 2 + 200;
const rodMat = new THREE.MeshStandardMaterial({ color: 0x8b7040, roughness: 0.3, metalness: 0.7 });
const rodY = wy + wH / 2 + 120;
const rod = new THREE.Mesh(new THREE.CylinderGeometry(21, 21, totalCurtainSpan, 12), rodMat);
rod.rotation.z = Math.PI / 2;
rod.position.set(0, rodY, 60);
winGroup.add(rod);
const finialIM = new THREE.InstancedMesh(new THREE.SphereGeometry(30, 10, 8), rodMat, 2);
const _finialMatrix = new THREE.Matrix4();
[-1, 1].forEach((side, idx) => {
finialIM.setMatrixAt(idx, _finialMatrix.makeTranslation(side * (totalCurtainSpan / 2 + 10), rodY, 60));
});
winGroup.add(finialIM);
// Curtain panels — top at rod height, with static wave folds
const curtainMat = new THREE.MeshStandardMaterial({
color: 0x1E3A5F,
roughness: 0.85,
metalness: 0,
side: THREE.DoubleSide,
});
const curtainBottom = wy - wH / 2 - frameThick - 80;
const curtainH = rodY - curtainBottom;
const curtainCenterY = curtainBottom + curtainH / 2;
function applyCurtainWave(geo) {
const pos = geo.attributes.position;
for (let i = 0; i < pos.count; i++) {
const x = pos.getX(i);
const uFrac = (x / (curtainW / 2) + 1) / 2;
pos.setZ(i, Math.sin(uFrac * Math.PI * 5) * 25);
}
pos.needsUpdate = true;
geo.computeVertexNormals();
}
const cGeoL = new THREE.PlaneGeometry(curtainW, curtainH, CURTAIN_SEG_COLS, CURTAIN_SEG_ROWS);
applyCurtainWave(cGeoL);
curtainLeft = new THREE.Mesh(cGeoL, curtainMat);
curtainLeft.position.set(-wW / 2 - curtainW / 2 + 40, curtainCenterY, 55);
curtainLeft.castShadow = true;
winGroup.add(curtainLeft);
curtainLeftOrigPos = new Float32Array(curtainLeft.geometry.attributes.position.array);
const cGeoR = new THREE.PlaneGeometry(curtainW, curtainH, CURTAIN_SEG_COLS, CURTAIN_SEG_ROWS);
applyCurtainWave(cGeoR);
curtainRight = new THREE.Mesh(cGeoR, curtainMat);
curtainRight.position.set(wW / 2 + curtainW / 2 - 40, curtainCenterY, 55);
curtainRight.castShadow = true;
winGroup.add(curtainRight);
curtainRightOrigPos = new Float32Array(curtainRight.geometry.attributes.position.array);
}
function createBookshelf() {
const group = new THREE.Group();
const shelfMat = new THREE.MeshStandardMaterial({ color: 0x5c3d20, roughness: 0.7, metalness: 0 });
const shelfX = WALL_RIGHT_X - 1200;
const shelfZ = WALL_BACK_Z + 250;
const shelfW = 1375;
const shelfFullH = 2400;
const shelfDepth = 380;
const shelfBaseY = FLOOR_Y + shelfFullH / 2;
// Panels, back panel, and internal shelves — merged into one draw call
const shelfStructGeos = [];
const panels = [
[shelfW, 22, shelfDepth, shelfX, FLOOR_Y + shelfFullH - 11, shelfZ],
[shelfW, 22, shelfDepth, shelfX, FLOOR_Y + 11, shelfZ],
[22, shelfFullH, shelfDepth, shelfX - shelfW / 2 + 11, shelfBaseY, shelfZ],
[22, shelfFullH, shelfDepth, shelfX + shelfW / 2 - 11, shelfBaseY, shelfZ],
[22, shelfFullH, 22, shelfX - shelfW / 2 - 11, shelfBaseY, shelfZ + shelfDepth / 2 - 11],
];
for (const [w, h, d, x, y, z] of panels) {
const g = new THREE.BoxGeometry(w, h, d);
g.translate(x, y, z);
shelfStructGeos.push(g);
}
// Back panel
const backGeo = new THREE.BoxGeometry(shelfW - 44, shelfFullH, 14);
backGeo.translate(shelfX, shelfBaseY, shelfZ - shelfDepth / 2 + 7);
shelfStructGeos.push(backGeo);
// Internal shelves
const shelfCount = 5;
const shelfSpacing = shelfFullH / (shelfCount + 1);
const shelfYPositions = [];
for (let i = 1; i <= shelfCount; i++) {
const sy = FLOOR_Y + shelfSpacing * i;
shelfYPositions.push(sy);
const g = new THREE.BoxGeometry(shelfW - 44, 22, shelfDepth);
g.translate(shelfX, sy, shelfZ);
shelfStructGeos.push(g);
}
const shelfStructMerged = mergeGeometries(shelfStructGeos);
shelfStructGeos.forEach(g => g.dispose());
const shelfStructMesh = new THREE.Mesh(shelfStructMerged, shelfMat);
shelfStructMesh.castShadow = true;
shelfStructMesh.receiveShadow = true;
group.add(shelfStructMesh);
// Books
const bookColors = [
0x8b0000, 0x2f4f4f, 0xdaa520, 0x4b3832, 0x556b2f, 0x800020, 0xc19a6b, 0x1c3a5e, 0x704214, 0x5b2333, 0x3b5998,
0x8b4513,
];
const rng = (() => {
let s = 42;
return () => {
s = (s * 9301 + 49297) % 233280;
return s / 233280;
};
})();
const bookGeos = [];
const _bookColor = new THREE.Color();
const _bookMatrix = new THREE.Matrix4();
for (const sy of shelfYPositions) {
let curX = shelfX - shelfW / 2 + 30;
const maxX = shelfX + shelfW / 2 - 30;
let layFlat = rng() > 0.6;
while (curX < maxX - 40) {
const bW = 40 + rng() * 45;
const bH = 200 + rng() * 130;
const bD = shelfDepth - 40;
if (layFlat && rng() > 0.5) {
const stackCount = 2 + Math.floor(rng() * 2);
for (let s = 0; s < stackCount; s++) {
const fw = 80 + rng() * 60;
const fh = 20 + rng() * 12;
const fd = bD * (0.7 + rng() * 0.3);
const geo = new THREE.BoxGeometry(fw, fh, fd);
_bookColor.setHex(bookColors[Math.floor(rng() * bookColors.length)]);
setGeometryVertexColor(geo, _bookColor);
_bookMatrix.makeTranslation(curX + fw / 2, sy + 11 + fh * (s + 0.5), shelfZ + (rng() - 0.5) * 20);
geo.applyMatrix4(_bookMatrix);
bookGeos.push(geo);
}
curX += 110 + rng() * 40;
layFlat = false;
} else {
const tilt = (rng() - 0.5) * 0.22;
const geo = new THREE.BoxGeometry(bW, bH, bD);
_bookColor.setHex(bookColors[Math.floor(rng() * bookColors.length)]);
setGeometryVertexColor(geo, _bookColor);
_bookMatrix.makeRotationZ(tilt).setPosition(curX + bW / 2, sy + 11 + (bH / 2) * Math.cos(tilt), shelfZ + (rng() - 0.5) * 20);
geo.applyMatrix4(_bookMatrix);
bookGeos.push(geo);
curX += bW + 4 + rng() * 10;
}
}
}
const bookMerged = mergeGeometries(bookGeos);
bookGeos.forEach(g => g.dispose());
const bookMesh = new THREE.Mesh(bookMerged, new THREE.MeshStandardMaterial({ roughness: 0.85, vertexColors: true }));
bookMesh.receiveShadow = true;
group.add(bookMesh);
scene.add(group);
}
async function createPoster() {
const pW = 1300;
const pH = 1500;
const px = WALL_RIGHT_X - 2;
const py = FLOOR_Y + ROOM_H / 2 + 200;
const pz = -2200 + WORLD_SHIFT_Z;
// Black background — no frame
const matBoard = new THREE.Mesh(
new THREE.PlaneGeometry(pW, pH), // rotated: height along Z, width along Y
new THREE.MeshStandardMaterial({ color: 0x111111 }),
);
matBoard.position.set(px, py, pz);
matBoard.rotation.y = -Math.PI / 2;
scene.add(matBoard);
const map = await loadTexture("textures/atari.png");
const posterMat = new THREE.MeshStandardMaterial({ map, transparent: true });
const poster = new THREE.Mesh(
new THREE.PlaneGeometry(pW * 0.75, (pW * 1.2) * 0.75),
posterMat,
);
poster.position.set(px - 1, py, pz);
poster.rotation.y = -Math.PI / 2;
scene.add(poster);
}
function createRug() {
const group = new THREE.Group();
const rugY = FLOOR_Y + 8;
// Rug in front of the sofa — square, based on longest side (3000)
// Sofa faces -X (rotated -90° on Y, against right wall), so "in front" is toward -X
const sofaCX = WALL_RIGHT_X - 620 * 0.75 - 40;
const rugCX = sofaCX - 2000; // in front of the sofa
const rugCZ = 500 + WORLD_SHIFT_Z; // match sofa Z
// Rug layers + medallions — merged per colour into 2 draw calls
const _rugRot = new THREE.Matrix4().makeRotationX(-Math.PI / 2);
const rugGeosA = []; // 0xa03a22
const rugGeosB = []; // 0xc4793a
for (const { s, y, color } of [
{ s: 3000, y: rugY, color: 0xa03a22 },
{ s: 2800, y: rugY + 2, color: 0xc4793a },
{ s: 2500, y: rugY + 4, color: 0xa03a22 },
{ s: 1800, y: rugY + 6, color: 0xc4793a },
]) {
const g = new THREE.PlaneGeometry(s, s);
g.applyMatrix4(_rugRot);
g.translate(rugCX, y, rugCZ);
(color === 0xa03a22 ? rugGeosA : rugGeosB).push(g);
}
// Medallions share the same colours
const mOuter = new THREE.CircleGeometry(480, 48);
mOuter.applyMatrix4(_rugRot); mOuter.translate(rugCX, rugY + 7, rugCZ);
rugGeosB.push(mOuter);
const mInner = new THREE.CircleGeometry(320, 48);
mInner.applyMatrix4(_rugRot); mInner.translate(rugCX, rugY + 8, rugCZ);
rugGeosA.push(mInner);
const rugMatA = new THREE.MeshStandardMaterial({ color: 0xa03a22, roughness: 0.95, metalness: 0 });
const rugMatB = new THREE.MeshStandardMaterial({ color: 0xc4793a, roughness: 0.95, metalness: 0 });
const rugMeshA = new THREE.Mesh(mergeGeometries(rugGeosA), rugMatA);
rugMeshA.receiveShadow = true;
rugGeosA.forEach(g => g.dispose());
group.add(rugMeshA);
const rugMeshB = new THREE.Mesh(mergeGeometries(rugGeosB), rugMatB);
rugMeshB.receiveShadow = true;
rugGeosB.forEach(g => g.dispose());
group.add(rugMeshB);
scene.add(group);
}
function createPendantLight() {
const group = new THREE.Group();
const lx = ROOM_CX;
const lz = ROOM_CZ;
const bulbY = CEILING_Y - 820;
// Cord
const cord = new THREE.Mesh(
new THREE.CylinderGeometry(5, 5, 600, 8),
new THREE.MeshStandardMaterial({ color: 0x1a1a1a, roughness: 0.8 }),
);
cord.position.set(lx, CEILING_Y - 300, lz);
group.add(cord);
// Shade outer (matte black)
const shadeOutMat = new THREE.MeshStandardMaterial({
color: 0x1a1a1a,
roughness: 0.6,
metalness: 0.1,
side: THREE.FrontSide,
});
const shadeOut = new THREE.Mesh(new THREE.CylinderGeometry(40, 260, 300, 32, 1, true), shadeOutMat);
shadeOut.position.set(lx, CEILING_Y - 750, lz);
shadeOut.castShadow = true;
group.add(shadeOut);
// Shade inner (warm glow)
const shadeInMat = new THREE.MeshStandardMaterial({
color: 0xf0c060,
roughness: 0.9,
side: THREE.BackSide,
emissive: 0xf0c060,
emissiveIntensity: 0.8,
});
const shadeIn = new THREE.Mesh(new THREE.CylinderGeometry(40, 260, 300, 32, 1, true), shadeInMat);
shadeIn.position.set(lx, CEILING_Y - 750, lz);
group.add(shadeIn);
// Shade top cap
const cap = new THREE.Mesh(
new THREE.CylinderGeometry(40, 40, 20, 32),
new THREE.MeshStandardMaterial({ color: 0x1a1a1a, roughness: 0.6, metalness: 0.1 }),
);
cap.position.set(lx, CEILING_Y - 605, lz);
cap.castShadow = true;
group.add(cap);
// Bulb
const bulb = new THREE.Mesh(
new THREE.SphereGeometry(32, 16, 16),
new THREE.MeshStandardMaterial({ color: 0xffffcc, emissive: 0xffcc44, emissiveIntensity: 3.0 }),
);
bulb.position.set(lx, bulbY, lz);
group.add(bulb);
scene.add(group);
// Lights added directly to scene (not in group) so targets work correctly
// SpotLight pointing straight down — positioned just below shade opening
// Intensity scaled for ~3000-unit drop to floor with quadratic decay
const shadeBottomY = CEILING_Y - 750 - 150; // shade centre − half height
const spotLight = new THREE.SpotLight(0xffd580, 8000000, 0, Math.PI / 3, 0.4, 2);
spotLight.position.set(lx, shadeBottomY + 10, lz);
spotLight.target.position.set(lx, FLOOR_Y, lz);
spotLight.castShadow = true;
spotLight.shadow.mapSize.set(2048, 2048);
spotLight.shadow.camera.near = 100;
spotLight.shadow.camera.far = 8000;
scene.add(spotLight);
scene.add(spotLight.target);
// PointLight for a restrained amount of omnidirectional bulb spill
const fillLight = new THREE.PointLight(0xffd580, 450000, 0, 2);
fillLight.position.set(lx, bulbY, lz);
scene.add(fillLight);
}
function createSofa() {
const sofaGroup = new THREE.Group();
const sofaW = 1800;
const sofaD = 620;
const legH = 120;
const baseH = 80;
const baseTop = legH + baseH;
const seatY = baseTop;
const cushMat = new THREE.MeshStandardMaterial({ color: 0x7a6050, roughness: 0.78, metalness: 0 });
const frameMat = new THREE.MeshStandardMaterial({ color: 0x3d2010, roughness: 0.65, metalness: 0 });
const pillowColors = [0xc4a882, 0xa07858, 0xd4b896];
// Legs — InstancedMesh
const legGeo = new THREE.CylinderGeometry(24, 20, legH, 12);
const legIM = new THREE.InstancedMesh(legGeo, frameMat, 4);
legIM.castShadow = true;
sofaGroup.add(legIM);
const _legMatrix = new THREE.Matrix4();
[[-1, -1], [1, -1], [-1, 1], [1, 1]].forEach(([lx, lz], idx) => {
_legMatrix.makeTranslation(lx * (sofaW / 2 - 80), legH / 2, lz * (sofaD / 2 - 60));
legIM.setMatrixAt(idx, _legMatrix);
});
// Base / frame — raised above legs
const base = new THREE.Mesh(new THREE.BoxGeometry(sofaW, baseH, sofaD), frameMat);
base.position.set(0, legH + baseH / 2, 0);
base.castShadow = true;
base.receiveShadow = true;
sofaGroup.add(base);
// Seat cushions
const cushW = (sofaW - 60) / 3;
for (let i = 0; i < 3; i++) {
const cx = -sofaW / 2 + 30 + cushW * i + cushW / 2;
const cush = new THREE.Mesh(new RoundedBoxGeometry(cushW - 14, 100, sofaD - 80, 5, 25), cushMat);
cush.position.set(cx, seatY + 50, 20);
cush.castShadow = true;
cush.receiveShadow = true;
sofaGroup.add(cush);
}
// Back rest
const back = new THREE.Mesh(new RoundedBoxGeometry(sofaW, 480, 130, 5, 25), cushMat);
back.position.set(0, seatY + 300, -sofaD / 2 + 30);
back.rotation.x = THREE.MathUtils.degToRad(-6);
back.castShadow = true;
sofaGroup.add(back);
// Arms
for (const side of [-1, 1]) {
const arm = new THREE.Mesh(new RoundedBoxGeometry(130, 340, sofaD, 4, 18), cushMat);
arm.position.set(side * (sofaW / 2 + 65), seatY + 80, 0);
arm.castShadow = true;
sofaGroup.add(arm);
}
// Throw pillows
const pillowOffsets = [-500, 0, 500];
for (let i = 0; i < 3; i++) {
const pMat = new THREE.MeshStandardMaterial({ color: pillowColors[i], roughness: 0.8 });
const pillow = new THREE.Mesh(new RoundedBoxGeometry(280, 260, 75, 4, 25), pMat);
pillow.position.set(pillowOffsets[i], seatY + 220, -sofaD / 2 + 80);
pillow.rotation.z = (i - 1) * 0.08;
pillow.rotation.x = THREE.MathUtils.degToRad(-15);
pillow.castShadow = true;
sofaGroup.add(pillow);
}
sofaGroup.scale.set(1.5, 1.5, 1.5);
sofaGroup.rotation.y = -Math.PI / 2;
sofaGroup.position.set(WALL_RIGHT_X - (sofaD * 1.5) / 2 - 40, FLOOR_Y, 500 + WORLD_SHIFT_Z);
scene.add(sofaGroup);
}
function createSideTable() {
const group = new THREE.Group();
const tx = WALL_RIGHT_X - 520;
const tz = -1500 + WORLD_SHIFT_Z;
const tableTopY = FLOOR_Y + 560;
const woodMat = new THREE.MeshStandardMaterial({ color: 0x5c3d20, roughness: 0.62, metalness: 0 });
const top = new THREE.Mesh(new THREE.CylinderGeometry(210, 210, 30, 40), woodMat);
top.position.set(tx, tableTopY, tz);
top.castShadow = true;
top.receiveShadow = true;
group.add(top);
const ped = new THREE.Mesh(new THREE.CylinderGeometry(28, 55, 520, 16), woodMat);
ped.position.set(tx, tableTopY - 275, tz);
ped.castShadow = true;
group.add(ped);
const base = new THREE.Mesh(new THREE.CylinderGeometry(160, 160, 22, 32), woodMat);
base.position.set(tx, FLOOR_Y + 11, tz);
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const mugMat = new THREE.MeshStandardMaterial({ color: 0xe8ddd0, roughness: 0.7 });
const mug = new THREE.Mesh(new THREE.CylinderGeometry(38, 32, 88, 20), mugMat);
mug.position.set(tx - 60, tableTopY + 59, tz + 30);
mug.castShadow = true;
group.add(mug);
const handle = new THREE.Mesh(new THREE.TorusGeometry(32, 8, 8, 16, Math.PI), mugMat);
handle.rotation.z = Math.PI / 2;
handle.position.set(tx - 60 + 44, tableTopY + 59, tz + 30);
group.add(handle);
const coffee = new THREE.Mesh(
new THREE.CircleGeometry(28, 20),
new THREE.MeshStandardMaterial({ color: 0x3a1f0a, roughness: 0.3 }),
);
coffee.rotation.x = -Math.PI / 2;
coffee.position.set(tx - 60, tableTopY + 103, tz + 30);
group.add(coffee);
const bookOnTable = new THREE.Mesh(
new THREE.BoxGeometry(180, 22, 230),
new THREE.MeshStandardMaterial({ color: 0x2f4f4f, roughness: 0.85 }),
);
bookOnTable.rotation.y = 0.3;
bookOnTable.position.set(tx + 40, tableTopY + 26, tz - 30);
bookOnTable.castShadow = true;
group.add(bookOnTable);
scene.add(group);
}
function createFilingCabinet() {
const group = new THREE.Group();
const fx = DESK_CX + 1480;
const fz = DESK_CZ - 200;
const cabinetH = 1200;
const cabinetW = 450;
const cabinetD = 560;
const cabinetY = FLOOR_Y + cabinetH / 2;
const bodyMat = new THREE.MeshStandardMaterial({ color: 0x8a9ba8, roughness: 0.55, metalness: 0.35 });
const drawerMat = new THREE.MeshStandardMaterial({ color: 0x7a8b98, roughness: 0.5, metalness: 0.4 });
const chromeMat = new THREE.MeshStandardMaterial({ color: 0xc0c0c0, roughness: 0.1, metalness: 0.9 });
const body = new THREE.Mesh(new THREE.BoxGeometry(cabinetW, cabinetH, cabinetD), bodyMat);
body.position.set(fx, cabinetY, fz);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const drawerCount = 3;
const drawerH = (cabinetH - 60) / drawerCount - 12;
const drawerGeos = [];
const handleGeos = [];
const _handleRot = new THREE.Matrix4().makeRotationZ(Math.PI / 2);
for (let i = 0; i < drawerCount; i++) {
const dy = FLOOR_Y + 30 + drawerH / 2 + i * (drawerH + 12);
const dg = new THREE.BoxGeometry(cabinetW - 10, drawerH, 22);
dg.translate(fx, dy, fz + cabinetD / 2 + 4);
drawerGeos.push(dg);
const hg = new THREE.CylinderGeometry(7, 7, 90, 10);
hg.applyMatrix4(_handleRot);
hg.translate(fx, dy, fz + cabinetD / 2 + 18);
handleGeos.push(hg);
}
const drawerMerged = mergeGeometries(drawerGeos);
drawerGeos.forEach(g => g.dispose());
group.add(new THREE.Mesh(drawerMerged, drawerMat));
const handleMerged = mergeGeometries(handleGeos);
handleGeos.forEach(g => g.dispose());
group.add(new THREE.Mesh(handleMerged, chromeMat));
const topSurf = new THREE.Mesh(new THREE.BoxGeometry(cabinetW, 16, cabinetD), bodyMat);
topSurf.position.set(fx, FLOOR_Y + cabinetH + 8, fz);
topSurf.castShadow = true;
topSurf.receiveShadow = true;
group.add(topSurf);
scene.add(group);
}
function createWallClock() {
const group = new THREE.Group();
const cx = WALL_RIGHT_X - 2;
const cy = FLOOR_Y + 2900;
const cz = 500 + WORLD_SHIFT_Z;
const radius = 400;
const faceMat = new THREE.MeshStandardMaterial({ color: 0xf5f0e8, roughness: 0.7, metalness: 0 });
const rimMat = new THREE.MeshStandardMaterial({ color: 0x2c2c2c, roughness: 0.5, metalness: 0.1 });
const handMat = new THREE.MeshStandardMaterial({ color: 0x1a1a1a, roughness: 0.5 });
const markerMat = new THREE.MeshStandardMaterial({ color: 0x333333, roughness: 0.5 });
// Face
const face = new THREE.Mesh(new THREE.CylinderGeometry(radius, radius, 18, 64), faceMat);
face.rotation.z = Math.PI / 2;
face.position.set(cx, cy, cz);
face.castShadow = true;
group.add(face);
// Rim
const rim = new THREE.Mesh(new THREE.TorusGeometry(radius + 8, 16, 8, 64), rimMat);
rim.rotation.y = Math.PI / 2;
rim.position.set(cx, cy, cz);
group.add(rim);
// Hour markers — 12 o'clock is at top (+Y), going clockwise when viewed from -X
const mr = radius - 28;
const largeMarkerIM = new THREE.InstancedMesh(new THREE.BoxGeometry(10, 45, 8), markerMat, 4);
const smallMarkerIM = new THREE.InstancedMesh(new THREE.BoxGeometry(6, 28, 8), markerMat, 8);
const _markerMatrix = new THREE.Matrix4();
const _markerQ = new THREE.Quaternion();
const _markerPos = new THREE.Vector3();
const _markerScale = new THREE.Vector3(1, 1, 1);
const _xAxis = new THREE.Vector3(1, 0, 0);
let largeIdx = 0, smallIdx = 0;
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
_markerPos.set(cx - 10, cy + Math.cos(angle) * mr, cz - Math.sin(angle) * mr);
_markerQ.setFromAxisAngle(_xAxis, -angle);
_markerMatrix.compose(_markerPos, _markerQ, _markerScale);
if (i % 3 === 0) largeMarkerIM.setMatrixAt(largeIdx++, _markerMatrix);
else smallMarkerIM.setMatrixAt(smallIdx++, _markerMatrix);
}
group.add(largeMarkerIM);
group.add(smallMarkerIM);
// Clock hands — rotate around X axis
// When viewed from -X: +Y is up (12), rotation.x positive goes from +Y toward -Z (clockwise)
hourHandGroup = new THREE.Group();
hourHandGroup.position.set(cx - 5, cy, cz);
const hourHand = new THREE.Mesh(new THREE.BoxGeometry(10, 220, 14), handMat);
hourHand.position.set(0, 110, 0);
hourHandGroup.add(hourHand);
group.add(hourHandGroup);
minuteHandGroup = new THREE.Group();
minuteHandGroup.position.set(cx - 6, cy, cz);
const minuteHand = new THREE.Mesh(new THREE.BoxGeometry(10, 310, 8), handMat);
minuteHand.position.set(0, 155, 0);
minuteHandGroup.add(minuteHand);
group.add(minuteHandGroup);
// Center cap
const centerCap = new THREE.Mesh(new THREE.SphereGeometry(14, 10, 8), rimMat);
centerCap.position.set(cx - 14, cy, cz);
group.add(centerCap);
scene.add(group);
}
function createDustParticles() {
const count = 90;
particlePositions = new Float32Array(count * 3);
particleSeeds = new Float32Array(count);
const rng = Math.random;
for (let i = 0; i < count; i++) {
particlePositions[i * 3] = (rng() - 0.5) * 5500 + ROOM_CX;
particlePositions[i * 3 + 1] = FLOOR_Y + rng() * (CEILING_Y - FLOOR_Y);
particlePositions[i * 3 + 2] = (rng() - 0.5) * 4000 + ROOM_CZ;
particleSeeds[i] = rng() * Math.PI * 2;
}
particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute("position", new THREE.BufferAttribute(particlePositions, 3));
const particleMat = new THREE.PointsMaterial({
size: 9,
color: 0xffeecc,
transparent: true,
opacity: 0.45,
sizeAttenuation: true,
blending: THREE.AdditiveBlending,
depthWrite: false,
});
const particles = new THREE.Points(particleGeometry, particleMat);
scene.add(particles);
}
function createDeskLamp() {
const lampMat = new THREE.MeshStandardMaterial({ color: 0x1a1a1a, roughness: 0.5, metalness: 0.5 });
// All positions in world space — lamp sits left of the computer on the desk
const baseX = DESK_CX - 1200;
const baseY = DESK_Y;
const baseZ = DESK_CZ - 500;
// Where we want the shade to aim: the computer / keyboard area
const targetX = DESK_CX;
const targetY = DESK_Y - 50;
const targetZ = DESK_CZ;
// --- Base ---
const base = new THREE.Mesh(new THREE.CylinderGeometry(110, 120, 30, 24), lampMat);
base.position.set(baseX, baseY, baseZ);
base.castShadow = true;
scene.add(base);
// --- Arm keypoints (world space) ---
// Lower arm: 15° back from vertical; upper arm: 55° from lower arm → -40° from vertical
const arm1Len = 500;
const arm2Len = 562;
const arm1Angle = THREE.MathUtils.degToRad(15); // 15° back from vertical
const arm2Angle = THREE.MathUtils.degToRad(15 - 55); // 55° clockwise from arm1 = -40° from vertical
const j0 = new THREE.Vector3(baseX, baseY + 15, baseZ);
// Elbow: arm1 goes up and slightly back (-Z)
const j1 = new THREE.Vector3(
baseX,
baseY + 15 + Math.cos(arm1Angle) * arm1Len,
baseZ - Math.sin(arm1Angle) * arm1Len,
);
// Head: arm2 swings forward (+Z) and down from elbow
const headPos = new THREE.Vector3(
baseX,
j1.y + Math.cos(arm2Angle) * arm2Len,
j1.z - Math.sin(arm2Angle) * arm2Len,
);
// Helper: cylinder between two points
function armBetween(a, b, radius) {
const dir = new THREE.Vector3().subVectors(b, a);
const len = dir.length();
const mid = new THREE.Vector3().addVectors(a, b).multiplyScalar(0.5);
const arm = new THREE.Mesh(new THREE.CylinderGeometry(radius, radius, len, 10), lampMat);
arm.position.copy(mid);
arm.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), dir.normalize());
arm.castShadow = true;
scene.add(arm);
}
// Joint spheres
const joint1 = new THREE.Mesh(new THREE.SphereGeometry(18, 12, 8), lampMat);
joint1.position.copy(j0);
joint1.castShadow = true;
scene.add(joint1);
const joint2 = new THREE.Mesh(new THREE.SphereGeometry(16, 12, 8), lampMat);
joint2.position.copy(j1);
joint2.castShadow = true;
scene.add(joint2);
// Arms
armBetween(j0, j1, 12);
armBetween(j1, headPos, 10);
// --- Shade — build in a group, then orient toward target ---
const shadeGroup = new THREE.Group();
shadeGroup.position.copy(headPos);
scene.add(shadeGroup);
// Point the shade's -Y axis toward the target:
// Build a rotation that maps (0,-1,0) to the direction from head to target
const aimDir = new THREE.Vector3().subVectors(
new THREE.Vector3(targetX, targetY, targetZ),
headPos,