-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
3540 lines (2970 loc) · 131 KB
/
index.html
File metadata and controls
3540 lines (2970 loc) · 131 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover" />
<title>Quest OBJ Viewer version 53</title>
<style>
:root {
--bg: #0b0d12;
--panel: rgba(18, 22, 32, 0.88);
--panel2: rgba(255,255,255,0.08);
--text: #f4f7fb;
--muted: #b7c0d1;
--line: rgba(255,255,255,0.14);
--accent: #7cc7ff;
--bad: #ff8b8b;
--good: #96f0b1;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
touch-action: none;
}
canvas {
display: block;
width: 100%;
height: 100%;
}
#ui {
position: fixed;
top: max(12px, env(safe-area-inset-top));
left: max(12px, env(safe-area-inset-left));
width: min(460px, calc(100vw - 24px));
max-height: calc(100vh - 24px);
overflow: auto;
padding: 14px;
background: var(--panel);
border: 1px solid var(--line);
border-radius: 18px;
box-shadow: 0 18px 60px rgba(0,0,0,0.45);
backdrop-filter: blur(14px);
z-index: 10;
}
#ui.collapsed {
width: auto;
max-width: calc(100vw - 24px);
}
#ui.collapsed .body {
display: none;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 10px;
}
h1 {
margin: 0;
font-size: 18px;
line-height: 1.1;
letter-spacing: -0.02em;
}
.sub {
color: var(--muted);
font-size: 13px;
margin-top: 4px;
}
button, label.filebutton {
appearance: none;
border: 1px solid var(--line);
color: var(--text);
background: var(--panel2);
border-radius: 14px;
padding: 10px 12px;
font-weight: 650;
font-size: 14px;
cursor: pointer;
user-select: none;
min-height: 42px;
}
button:hover, label.filebutton:hover {
background: rgba(255,255,255,0.13);
}
button.primary, label.primary {
background: rgba(124,199,255,0.18);
border-color: rgba(124,199,255,0.45);
}
button.small {
padding: 8px 10px;
min-height: 34px;
border-radius: 12px;
font-size: 13px;
}
button.axis {
min-width: 68px;
padding: 8px 10px;
min-height: 38px;
border-radius: 12px;
}
input[type="file"] {
display: none;
}
.row {
display: flex;
gap: 8px;
flex-wrap: wrap;
margin: 10px 0;
}
.axisPanel {
margin: 10px 0;
padding: 12px;
border-top: 1px solid var(--line);
border-bottom: 1px solid var(--line);
}
.axisTitle {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-bottom: 10px;
}
.axisTitle b {
font-size: 14px;
}
.axisGrid {
display: grid;
grid-template-columns: 34px 1fr 1fr;
gap: 8px;
align-items: center;
}
.axisGrid .label {
color: var(--muted);
font-weight: 800;
text-align: center;
}
.rotationReadout {
margin-top: 10px;
color: var(--muted);
font-size: 12px;
line-height: 1.35;
}
.control {
display: grid;
grid-template-columns: 1fr auto;
gap: 10px;
align-items: center;
padding: 10px 0;
border-top: 1px solid var(--line);
}
.control:first-of-type {
border-top: 0;
}
.control span {
color: var(--muted);
font-size: 13px;
line-height: 1.3;
}
input[type="number"], select {
width: 132px;
border: 1px solid var(--line);
background: rgba(0,0,0,0.22);
color: var(--text);
border-radius: 12px;
padding: 9px 10px;
font-size: 14px;
}
select {
width: 172px;
}
input[type="checkbox"] {
width: 22px;
height: 22px;
accent-color: #7cc7ff;
}
#status {
margin-top: 10px;
padding: 10px;
min-height: 42px;
color: var(--muted);
background: rgba(0,0,0,0.22);
border: 1px solid var(--line);
border-radius: 14px;
font-size: 13px;
line-height: 1.45;
white-space: pre-wrap;
}
#arConfirm {
position: fixed;
left: 50%;
top: max(18px, env(safe-area-inset-top));
transform: translateX(-50%);
z-index: 30;
display: none;
padding: 10px 14px;
border-radius: 999px;
border: 1px solid rgba(150,240,177,0.7);
background: rgba(18, 60, 34, 0.82);
color: #d9ffe4;
font-weight: 800;
font-size: 14px;
box-shadow: 0 12px 44px rgba(0,0,0,0.45);
backdrop-filter: blur(12px);
pointer-events: none;
}
#arConfirm.show {
display: block;
}
.hint {
color: var(--muted);
font-size: 12px;
line-height: 1.45;
margin-top: 10px;
}
.ok { color: var(--good); }
.err { color: var(--bad); }
#VRButton {
position: fixed !important;
left: auto !important;
right: max(12px, env(safe-area-inset-right)) !important;
bottom: max(12px, env(safe-area-inset-bottom)) !important;
z-index: 20 !important;
border-radius: 999px !important;
font: 700 14px system-ui, sans-serif !important;
padding: 12px 16px !important;
min-width: 112px !important;
}
#ARButton {
position: fixed !important;
left: auto !important;
right: max(12px, env(safe-area-inset-right)) !important;
bottom: calc(max(12px, env(safe-area-inset-bottom)) + 58px) !important;
z-index: 20 !important;
border-radius: 999px !important;
font: 700 14px system-ui, sans-serif !important;
padding: 12px 16px !important;
min-width: 150px !important;
background: rgba(124,199,255,0.22) !important;
border: 1px solid rgba(124,199,255,0.55) !important;
}
@media (max-width: 520px) {
#ui { font-size: 14px; }
h1 { font-size: 16px; }
button, label.filebutton { width: 100%; }
.row button, .row label.filebutton { flex: 1 1 140px; }
.axisGrid { grid-template-columns: 28px 1fr 1fr; }
button.axis { min-width: 0; }
}
</style>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.164.1/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.164.1/examples/jsm/"
}
}
</script>
</head>
<body>
<div id="arConfirm">AR FLOOR SET</div>
<div id="ui">
<div class="topbar">
<div>
<h1>Quest OBJ Viewer version 53</h1>
<div class="sub">Designed for <b>Zenith 1.obj</b> exported in <b>mm</b></div>
</div>
<button id="collapseBtn" class="small" title="Hide/show controls">Hide</button>
</div>
<div class="body">
<div class="row">
<label class="filebutton primary" for="objInput">Choose OBJ</label>
<input id="objInput" type="file" accept=".obj" />
<label class="filebutton" for="materialInput">Add MTL / Textures</label>
<input id="materialInput" type="file" multiple accept=".mtl,.png,.jpg,.jpeg,.webp,.bmp,.gif,.tga,.exr" />
<button id="resetBtn">Reset view</button>
</div>
<div class="axisPanel">
<div class="axisTitle">
<b>Rotate model in 90° steps</b>
<button id="resetRotationBtn" class="small">Reset rotation</button>
</div>
<div class="axisGrid">
<div class="label">X</div>
<button class="axis" data-axis="x" data-dir="-1">X −90°</button>
<button class="axis" data-axis="x" data-dir="1">X +90°</button>
<div class="label">Y</div>
<button class="axis" data-axis="y" data-dir="-1">Y −90°</button>
<button class="axis" data-axis="y" data-dir="1">Y +90°</button>
<div class="label">Z</div>
<button class="axis" data-axis="z" data-dir="-1">Z −90°</button>
<button class="axis" data-axis="z" data-dir="1">Z +90°</button>
</div>
<div id="rotationReadout" class="rotationReadout">
Extra rotation: X 0°, Y 0°, Z 0°
</div>
</div>
<div class="control">
<div>
<b>Scale locked</b><br />
<span>Scale is fixed at 0.001, so Fusion millimetres always convert to metres for true 1:1 VR/AR. It cannot be changed by the UI.</span>
</div>
<input id="scaleInput" type="number" step="0.001" min="0.001" max="0.001" value="0.001" disabled />
</div>
<div class="control">
<div>
<b>Fusion / CAD Z-up fix</b><br />
<span>Leave this on if the model appears lying down without it.</span>
</div>
<input id="zUpInput" type="checkbox" checked />
</div>
<div class="control">
<div>
<b>Longest axis vertical</b><br />
<span>Normally on. Automatically rotates the model so its longest dimension becomes the vertical Z/up-down axis.</span>
</div>
<input id="longestAxisVerticalInput" type="checkbox" checked />
</div>
<div class="control">
<div>
<b>Centre model + floor align</b><br />
<span>Places the model in the middle of the grid with its lowest point on the floor.</span>
</div>
<input id="floorInput" type="checkbox" checked />
</div>
<div class="control">
<div>
<b>Small model viewing height</b><br />
<span>For a 30 cm model, use floor mode for true-size placement, or raise it for close inspection outside XR.</span>
</div>
<select id="heightModeInput">
<option value="floor" selected>On floor / original</option>
<option value="topHead">Top at head height</option>
<option value="centreHead">Centre at head height</option>
<option value="table">Table height</option>
</select>
</div>
<div class="control">
<div>
<b>Show metre grid</b><br />
<span>Each large grid square is 1 metre.</span>
</div>
<input id="gridInput" type="checkbox" checked />
</div>
<div class="control">
<div>
<b>Vertical height ruler</b><br />
<span>Normally off. Shows a true-size ruler to the right of the OBJ as you look at it. Choose 1 m, 2 m, current floor-to-ceiling height, or the loaded model’s full length. Its zero end stays on the floor.</span>
</div>
<select id="heightRulerInput">
<option value="off" selected>Off</option>
<option value="1">1 m ruler</option>
<option value="2">2 m ruler</option>
<option value="ceiling">Floor to ceiling</option>
<option value="model">Loaded model full length</option>
</select>
</div>
<div class="control">
<div>
<b>XR controller movement</b><br />
<span>Left stick moves only the OBJ horizontally in VR and AR, so the floor/grid stay fixed. Right stick rolls or raises the OBJ.</span>
</div>
<input id="moveInput" type="checkbox" checked />
</div>
<div class="control">
<div>
<b>Show controllers in XR</b><br />
<span>Shows controller models and rays attached to the same XR rig as the headset, so they stay aligned with real tracking.</span>
</div>
<input id="showControllersInput" type="checkbox" checked />
</div>
<div class="control">
<div>
<b>Show tracked look-a-like hands in XR</b><br />
<span>When Quest hand tracking is active, shows the realistic look-a-like hand models. Each hand disappears automatically when you pick up that side's controller. Apart from the optional left/right pinch interactions below, hands cannot affect the OBJ or controls.</span>
</div>
<input id="showHandsInput" type="checkbox" checked />
</div>
<div class="control">
<div>
<b>Left-hand pinch centre placement</b><br />
<span>Pinch the left thumb and index fingertip together to place the centre of the OBJ at your pinch point. Keep pinching to carry its centre with your hand, then open the pinch to leave it there. The floor, markers and clipping heights remain fixed.</span>
</div>
<input id="leftPinchCentreInput" type="checkbox" checked />
</div>
<div class="control">
<div>
<b>Right-hand pinch move</b><br />
<span>Pinch the right thumb and index fingertip together to pick up and move the OBJ in any direction. Open the pinch to let go. When left-hand centre placement is active, it takes priority. The floor, markers and clipping heights remain fixed.</span>
</div>
<input id="pinchMoveInput" type="checkbox" checked />
</div>
<div class="control">
<div>
<b>Measure between index fingertips</b><br />
<span>When both hands are pointing with only the index finger extended and the thumb, middle, ring and little fingers folded, displays the live fingertip-to-fingertip distance in millimetres inside VR/AR.</span>
</div>
<input id="measureFingertipsInput" type="checkbox" checked />
</div>
<div class="control">
<div>
<b>Measure between controllers</b><br />
<span>In VR/AR, squeeze and hold both controller grip buttons to display the live distance between two amber measuring tips positioned 10 cm in front of the controllers. Place the tips on object edges or surfaces; release either grip to hide the measurement.</span>
</div>
<input id="measureControllersInput" type="checkbox" checked />
</div>
<div class="control">
<div>
<b>Right-stick spin axis</b><br />
<span>Use Auto for rockets. It picks the model’s longest local axis. If it spins wrong, try X, Y, or Z.</span>
</div>
<select id="spinAxisInput">
<option value="auto" selected>Auto longest axis</option>
<option value="x">Model X axis</option>
<option value="y">Model Y axis</option>
<option value="z">Model Z axis</option>
</select>
</div>
<div class="control">
<div>
<b>Passthrough / AR mode</b><br />
<span>Use the <b>Enter Passthrough</b> button. The opaque floor and grid are hidden automatically so your real room shows behind the model.</span>
</div>
<input id="arHideFloorInput" type="checkbox" checked />
</div>
<div class="control">
<div>
<b>AR floor placement</b><br />
<span>In passthrough, keep the bottom of the model on the calibrated floor. To put the OBJ at a real floor position, place the <b>left controller</b> there, then hold <b>A</b> and squeeze the <b>right grip</b>. The model base and horizontal centre are placed at that point.</span>
</div>
<input id="arFloorInput" type="checkbox" checked />
</div>
<div class="control">
<div>
<b>XR floor calibration</b><br />
<span>If the floor is wrong in VR/AR, put the <b>left controller</b> on the real floor, then press/hold <b>A</b> on the right controller and press the <b>right trigger</b>. In <b>AR only</b>, hold <b>A</b> and squeeze the <b>right grip</b> instead to set the floor and place the OBJ at the left controller position. You can also nudge the floor up/down.</span>
</div>
<div class="row" style="justify-content:flex-end;margin:0;">
<button id="floorUpBtn" class="small" type="button">Floor +10 cm</button>
<button id="floorDownBtn" class="small" type="button">Floor −10 cm</button>
</div>
</div>
<div class="control">
<div>
<b>AR clipping cuts</b><br />
<span>AR/passthrough only. Off until an AR ceiling cut is set. After setting the ceiling in passthrough, press left controller <b>X</b> to toggle clipping on/off.</span>
</div>
<input id="clippingEnabledInput" type="checkbox" />
</div>
<div class="control">
<div>
<b>A + right trigger mode</b><br />
<span>Use Auto normally. Put the left controller at the floor or ceiling/cut height, then hold A and press the right trigger. For a lower AR ceiling/cut height, choose Set AR ceiling cut first.</span>
</div>
<select id="clipSetModeInput">
<option value="auto" selected>Auto: VR floor / AR floor-ceiling</option>
<option value="floor">Set floor cut</option>
<option value="ceiling">Set AR ceiling cut</option>
</select>
</div>
<div class="control">
<div>
<b>AR ceiling cut adjustment</b><br />
<span>Fine-tune the passthrough/AR ceiling clipping plane if the controller point is slightly off. This is disabled in VR.</span>
</div>
<div class="row" style="justify-content:flex-end;margin:0;">
<button id="ceilingUpBtn" class="small" type="button">Ceiling +10 cm</button>
<button id="ceilingDownBtn" class="small" type="button">Ceiling −10 cm</button>
</div>
</div>
<div id="status">Select <b>Zenith 1.obj</b> first, then use <b>Add MTL / Textures</b> to apply the material file separately.</div>
<div class="hint">
Touch/drag to rotate outside VR. Scroll to zoom.
Use Enter VR for a virtual room, or Enter Passthrough for Quest AR/passthrough. In AR, put the left controller on the real floor where the OBJ should stand, then hold A and squeeze the right controller grip to place the model base and horizontal centre at that floor point. In VR and AR/passthrough, pinch the left thumb and index fingertip together to put the centre of the OBJ exactly at that pinch point and carry it while held, or pinch with the right hand to move the OBJ freely; open the pinch to release it. The floor and XR tracking space stay fixed. The left stick moves only the OBJ horizontally. Right stick rolls the rocket around its engine-to-nose-cone axis, right stick up/down adjusts model height, and right-stick click resets only the OBJ height back to the calibrated floor. Put the left controller at the calibration height, hold A on the right controller, and press the right trigger to set the floor or AR ceiling cut. Controller visuals are attached to the headset XR rig, not the OBJ. With Quest hand tracking, the realistic look-a-like hands follow your real hands and disappear automatically as soon as the matching controller becomes active. Apart from the enabled left/right pinch placement actions, they cannot affect the OBJ or controls. Point with both hands: keep each index finger extended and fold the thumb, middle, ring and little fingers to display the live fingertip-to-fingertip measurement in millimetres. With Touch controllers, squeeze and hold both grip buttons to draw a live measurement between amber probe tips positioned 10 cm in front of each controller; place those tips on object edges or surfaces, then read the distance in millimetres. Left controller X toggles AR cut-off clipping on/off after an AR ceiling is set. No controller-mounted UI panel is included.
</div>
</div>
</div>
<script type="module">
import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import { OBJLoader } from "three/addons/loaders/OBJLoader.js";
import { MTLLoader } from "three/addons/loaders/MTLLoader.js";
import { VRButton } from "three/addons/webxr/VRButton.js";
import { ARButton } from "three/addons/webxr/ARButton.js";
import { XRControllerModelFactory } from "three/addons/webxr/XRControllerModelFactory.js";
import { XRHandModelFactory } from "three/addons/webxr/XRHandModelFactory.js";
import { RoomEnvironment } from "three/addons/environments/RoomEnvironment.js";
const objInput = document.getElementById("objInput");
const materialInput = document.getElementById("materialInput");
const statusEl = document.getElementById("status");
const scaleInput = document.getElementById("scaleInput");
const LOCKED_MODEL_SCALE = 0.001;
function forceLockedScale() {
// Keep the model permanently true-size: Fusion mm → metres.
// This protects against UI edits, browser autofill, and gesture code.
scaleInput.value = LOCKED_MODEL_SCALE.toFixed(3);
scaleInput.min = LOCKED_MODEL_SCALE.toFixed(3);
scaleInput.max = LOCKED_MODEL_SCALE.toFixed(3);
scaleInput.step = LOCKED_MODEL_SCALE.toFixed(3);
scaleInput.disabled = true;
scaleInput.readOnly = true;
}
forceLockedScale();
const zUpInput = document.getElementById("zUpInput");
const longestAxisVerticalInput = document.getElementById("longestAxisVerticalInput");
const floorInput = document.getElementById("floorInput");
const heightModeInput = document.getElementById("heightModeInput");
const gridInput = document.getElementById("gridInput");
const heightRulerInput = document.getElementById("heightRulerInput");
const moveInput = document.getElementById("moveInput");
const showControllersInput = document.getElementById("showControllersInput");
const showHandsInput = document.getElementById("showHandsInput");
const leftPinchCentreInput = document.getElementById("leftPinchCentreInput");
const pinchMoveInput = document.getElementById("pinchMoveInput");
const measureFingertipsInput = document.getElementById("measureFingertipsInput");
const measureControllersInput = document.getElementById("measureControllersInput");
const spinAxisInput = document.getElementById("spinAxisInput");
const arHideFloorInput = document.getElementById("arHideFloorInput");
const arFloorInput = document.getElementById("arFloorInput");
const floorUpBtn = document.getElementById("floorUpBtn");
const floorDownBtn = document.getElementById("floorDownBtn");
const clippingEnabledInput = document.getElementById("clippingEnabledInput");
const clipSetModeInput = document.getElementById("clipSetModeInput");
const ceilingUpBtn = document.getElementById("ceilingUpBtn");
const ceilingDownBtn = document.getElementById("ceilingDownBtn");
const resetBtn = document.getElementById("resetBtn");
const collapseBtn = document.getElementById("collapseBtn");
const resetRotationBtn = document.getElementById("resetRotationBtn");
const rotationReadout = document.getElementById("rotationReadout");
const ui = document.getElementById("ui");
const arConfirm = document.getElementById("arConfirm");
const scene = new THREE.Scene();
const normalBackground = new THREE.Color(0x0b0d12);
scene.background = normalBackground;
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, powerPreference: "high-performance" });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.xr.enabled = true;
renderer.xr.setReferenceSpaceType("local-floor");
renderer.localClippingEnabled = true;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.0;
document.body.appendChild(renderer.domElement);
const xrOptionalFeatures = ["local-floor", "bounded-floor", "hand-tracking"];
const vrButton = VRButton.createButton(renderer, {
optionalFeatures: xrOptionalFeatures
});
document.body.appendChild(vrButton);
const arButton = ARButton.createButton(renderer, {
optionalFeatures: [...xrOptionalFeatures, "dom-overlay"],
domOverlay: { root: document.body }
});
// Three.js ARButton labels are usually strings such as "START AR" / "STOP AR".
// Do not use a plain .replace("AR", ...), because it also replaces the "AR"
// inside "START" and can create broken labels like "STPASSTHROUGHT AR".
let relabellingARButton = false;
function relabelARButtonForPassthrough() {
if (relabellingARButton) return;
const currentText = String(arButton.textContent || "").trim();
const upperText = currentText.toUpperCase();
let nextText = currentText;
if (!currentText || upperText.includes("START") || upperText.includes("ENTER")) {
nextText = "ENTER PASSTHROUGH";
} else if (upperText.includes("STOP") || upperText.includes("EXIT")) {
nextText = "EXIT PASSTHROUGH";
} else if (upperText.includes("AR NOT SUPPORTED")) {
nextText = "PASSTHROUGH NOT SUPPORTED";
} else if (upperText.includes("WEBXR NOT AVAILABLE")) {
nextText = "WEBXR NOT AVAILABLE";
}
if (nextText !== currentText) {
relabellingARButton = true;
arButton.textContent = nextText;
relabellingARButton = false;
}
}
relabelARButtonForPassthrough();
new MutationObserver(relabelARButtonForPassthrough).observe(arButton, {
childList: true,
characterData: true,
subtree: true
});
document.body.appendChild(arButton);
const pmremGenerator = new THREE.PMREMGenerator(renderer);
scene.environment = pmremGenerator.fromScene(new RoomEnvironment(), 0.04).texture;
const cameraRig = new THREE.Group();
scene.add(cameraRig);
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 2000);
camera.position.set(2.8, 1.7, 3.2);
cameraRig.add(camera);
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 0.8, 0);
controls.enableDamping = true;
controls.dampingFactor = 0.06;
controls.screenSpacePanning = true;
const controller1 = renderer.xr.getController(0);
const controller2 = renderer.xr.getController(1);
const controllerGrip1 = renderer.xr.getControllerGrip(0);
const controllerGrip2 = renderer.xr.getControllerGrip(1);
const hand1 = renderer.xr.getHand(0);
const hand2 = renderer.xr.getHand(1);
// Controllers are children of the same rig as the XR camera, keeping their
// visible positions aligned with real tracking if cameraRig is moved.
cameraRig.add(controller1, controller2, controllerGrip1, controllerGrip2, hand1, hand2);
// Hands normally act only as tracked visuals. Separate left/right thumb/index
// pinch interactions below may reposition only the OBJ when their toggles are enabled.
// They never change clipping, calibration, marker or XR-rig transforms.
// Floor/ceiling marker anchors live in XR tracking space, not in the OBJ/model space.
// They are parented to cameraRig on purpose: the camera and controllers are also
// children of this rig, so marker local positions remain locked to the same real-world
// XR reference-space position even when virtual locomotion moves the rig.
const markerAnchorRoot = new THREE.Group();
markerAnchorRoot.name = "xr_marker_anchor_root";
cameraRig.add(markerAnchorRoot);
const controllerModelFactory = new XRControllerModelFactory();
const controllerModel1 = controllerModelFactory.createControllerModel(controllerGrip1);
const controllerModel2 = controllerModelFactory.createControllerModel(controllerGrip2);
controllerGrip1.add(controllerModel1);
controllerGrip2.add(controllerModel2);
// Use only Three.js' realistic mesh-style tracked hand model. Hand joints are
// used only for the optional right-hand pinch OBJ translation and index measurement;
// never for clipping, calibration or UI clicks.
const handModelFactory = new XRHandModelFactory();
const handRoots = [hand1, hand2];
const handModels = handRoots.map((handRoot) => {
const model = handModelFactory.createHandModel(handRoot, "mesh");
model.visible = false;
handRoot.add(model);
return model;
});
// Live index-fingertip measurement overlay. This reads tracked hand joints only;
// it never moves, grabs or otherwise interacts with the OBJ.
const fingertipMeasurement = new THREE.Group();
fingertipMeasurement.name = "index_fingertip_measurement";
fingertipMeasurement.visible = false;
scene.add(fingertipMeasurement);
const fingertipLinePositions = new Float32Array(6);
const fingertipLineGeometry = new THREE.BufferGeometry();
fingertipLineGeometry.setAttribute("position", new THREE.BufferAttribute(fingertipLinePositions, 3));
const fingertipLine = new THREE.Line(
fingertipLineGeometry,
new THREE.LineBasicMaterial({
color: 0x7cc7ff,
transparent: true,
opacity: 0.96,
depthTest: false,
depthWrite: false
})
);
fingertipLine.frustumCulled = false;
fingertipLine.renderOrder = 1000;
fingertipMeasurement.add(fingertipLine);
const fingertipDotMaterial = new THREE.MeshBasicMaterial({
color: 0x96f0b1,
transparent: true,
opacity: 0.98,
depthTest: false,
depthWrite: false
});
const leftFingertipDot = new THREE.Mesh(new THREE.SphereGeometry(0.0045, 16, 12), fingertipDotMaterial);
const rightFingertipDot = new THREE.Mesh(new THREE.SphereGeometry(0.0045, 16, 12), fingertipDotMaterial.clone());
leftFingertipDot.renderOrder = 1001;
rightFingertipDot.renderOrder = 1001;
fingertipMeasurement.add(leftFingertipDot, rightFingertipDot);
const fingertipLabelCanvas = document.createElement("canvas");
fingertipLabelCanvas.width = 512;
fingertipLabelCanvas.height = 128;
const fingertipLabelContext = fingertipLabelCanvas.getContext("2d");
const fingertipLabelTexture = new THREE.CanvasTexture(fingertipLabelCanvas);
fingertipLabelTexture.colorSpace = THREE.SRGBColorSpace;
const fingertipLabel = new THREE.Sprite(
new THREE.SpriteMaterial({
map: fingertipLabelTexture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
fingertipLabel.scale.set(0.18, 0.045, 1);
fingertipLabel.renderOrder = 1002;
fingertipMeasurement.add(fingertipLabel);
const INDEX_JOINT_NAMES = [
"index-finger-metacarpal",
"index-finger-phalanx-proximal",
"index-finger-phalanx-intermediate",
"index-finger-phalanx-distal",
"index-finger-tip"
];
const FOLDED_FINGER_JOINT_NAMES = {
thumb: [
"thumb-metacarpal",
"thumb-phalanx-proximal",
"thumb-phalanx-distal",
"thumb-tip"
],
middle: [
"middle-finger-metacarpal",
"middle-finger-phalanx-proximal",
"middle-finger-phalanx-intermediate",
"middle-finger-phalanx-distal",
"middle-finger-tip"
],
ring: [
"ring-finger-metacarpal",
"ring-finger-phalanx-proximal",
"ring-finger-phalanx-intermediate",
"ring-finger-phalanx-distal",
"ring-finger-tip"
],
little: [
"pinky-finger-metacarpal",
"pinky-finger-phalanx-proximal",
"pinky-finger-phalanx-intermediate",
"pinky-finger-phalanx-distal",
"pinky-finger-tip"
]
};
const leftIndexJointPositions = INDEX_JOINT_NAMES.map(() => new THREE.Vector3());
const rightIndexJointPositions = INDEX_JOINT_NAMES.map(() => new THREE.Vector3());
const leftFoldedJointPositions = Object.fromEntries(
Object.entries(FOLDED_FINGER_JOINT_NAMES).map(([finger, joints]) => [finger, joints.map(() => new THREE.Vector3())])
);
const rightFoldedJointPositions = Object.fromEntries(
Object.entries(FOLDED_FINGER_JOINT_NAMES).map(([finger, joints]) => [finger, joints.map(() => new THREE.Vector3())])
);
const fingerDirectionA = new THREE.Vector3();
const fingerDirectionB = new THREE.Vector3();
const fingertipLabelPosition = new THREE.Vector3();
let displayedFingertipMillimetres = null;
// Live controller measurement overlay. This is separate from hand-tracking
// measurement: squeeze and hold both controller grips to turn it on.
// Endpoints sit a short distance ahead of each controller along its WebXR
// target-ray direction, so the amber dots can be placed on object surfaces.
const controllerMeasurement = new THREE.Group();
controllerMeasurement.name = "controller_grip_measurement";
controllerMeasurement.visible = false;
scene.add(controllerMeasurement);
const controllerMeasureLinePositions = new Float32Array(6);
const controllerMeasureLineGeometry = new THREE.BufferGeometry();
controllerMeasureLineGeometry.setAttribute("position", new THREE.BufferAttribute(controllerMeasureLinePositions, 3));
const controllerMeasureLine = new THREE.Line(
controllerMeasureLineGeometry,
new THREE.LineBasicMaterial({
color: 0xffd27c,
transparent: true,
opacity: 0.98,
depthTest: false,
depthWrite: false
})
);
controllerMeasureLine.frustumCulled = false;
controllerMeasureLine.renderOrder = 1003;
controllerMeasurement.add(controllerMeasureLine);
const controllerMeasureDotMaterial = new THREE.MeshBasicMaterial({
color: 0xffd27c,
transparent: true,
opacity: 0.99,
depthTest: false,
depthWrite: false
});
const leftControllerMeasureDot = new THREE.Mesh(new THREE.SphereGeometry(0.006, 16, 12), controllerMeasureDotMaterial);
const rightControllerMeasureDot = new THREE.Mesh(new THREE.SphereGeometry(0.006, 16, 12), controllerMeasureDotMaterial.clone());
leftControllerMeasureDot.renderOrder = 1004;
rightControllerMeasureDot.renderOrder = 1004;
controllerMeasurement.add(leftControllerMeasureDot, rightControllerMeasureDot);
const controllerMeasureLabelCanvas = document.createElement("canvas");
controllerMeasureLabelCanvas.width = 640;
controllerMeasureLabelCanvas.height = 128;
const controllerMeasureLabelContext = controllerMeasureLabelCanvas.getContext("2d");
const controllerMeasureLabelTexture = new THREE.CanvasTexture(controllerMeasureLabelCanvas);
controllerMeasureLabelTexture.colorSpace = THREE.SRGBColorSpace;
const controllerMeasureLabel = new THREE.Sprite(
new THREE.SpriteMaterial({
map: controllerMeasureLabelTexture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
controllerMeasureLabel.scale.set(0.26, 0.052, 1);
controllerMeasureLabel.renderOrder = 1005;
controllerMeasurement.add(controllerMeasureLabel);
const CONTROLLER_MEASURE_PROBE_OFFSET = 0.10; // 10 cm in front of each controller's pointing origin.
const leftControllerMeasurePosition = new THREE.Vector3();
const rightControllerMeasurePosition = new THREE.Vector3();
const controllerMeasureLabelPosition = new THREE.Vector3();
let displayedControllerMillimetres = null;
// Left-hand pinch-to-centre state. While held, the model centre follows the
// midpoint between the tracked left index fingertip and thumb tip.
const LEFT_PINCH_START_DISTANCE = 0.024;
const LEFT_PINCH_RELEASE_DISTANCE = 0.040;
const leftPinchIndexPosition = new THREE.Vector3();
const leftPinchThumbPosition = new THREE.Vector3();
const leftPinchMidpoint = new THREE.Vector3();
const leftPinchCentreDelta = new THREE.Vector3();
let isLeftPinchCentringModel = false;
// Right-hand pinch-to-move state. The two thresholds provide hysteresis:
// a firm pinch starts movement, while a wider opening is needed to release.
const RIGHT_PINCH_START_DISTANCE = 0.024;
const RIGHT_PINCH_RELEASE_DISTANCE = 0.040;
const RIGHT_PINCH_MAX_FRAME_DELTA = 0.16;
const rightPinchIndexPosition = new THREE.Vector3();
const rightPinchThumbPosition = new THREE.Vector3();
const rightPinchMidpoint = new THREE.Vector3();
const previousRightPinchMidpoint = new THREE.Vector3();
const rightPinchDelta = new THREE.Vector3();
let isRightPinchMovingModel = false;
function hideFingertipMeasurement() {
fingertipMeasurement.visible = false;
displayedFingertipMillimetres = null;
}
function hideControllerMeasurement() {
controllerMeasurement.visible = false;
displayedControllerMillimetres = null;
}
function getTrackedHandRoot(handedness) {
for (const handRoot of handRoots) {
const source = handRoot.userData.inputSource || null;
if (
isTrackedHandSource(source) &&
source.handedness === handedness &&
!hasActiveControllerForHandedness(handedness)
) {
return handRoot;
}
}
return null;
}
function getJointWorldPosition(handRoot, jointName, target) {
const joint = handRoot?.joints?.[jointName];
if (!joint || joint.visible === false) return false;
joint.getWorldPosition(target);
return Number.isFinite(target.x) && Number.isFinite(target.y) && Number.isFinite(target.z);
}
function readJointPositions(handRoot, jointNames, jointPositions) {
for (let i = 0; i < jointNames.length; i += 1) {
if (!getJointWorldPosition(handRoot, jointNames[i], jointPositions[i])) {
return false;
}
}
return true;
}
function getTrackedPathLength(jointPositions) {
let pathLength = 0;
for (let i = 1; i < jointPositions.length; i += 1) {
pathLength += jointPositions[i - 1].distanceTo(jointPositions[i]);
}
return pathLength;
}
function isIndexFingerExtended(handRoot, jointPositions) {
if (!readJointPositions(handRoot, INDEX_JOINT_NAMES, jointPositions)) return false;
const pathLength = getTrackedPathLength(jointPositions);
if (pathLength < 0.025) return false;
const straightReach = jointPositions[0].distanceTo(jointPositions[jointPositions.length - 1]);
const straightness = straightReach / pathLength;
fingerDirectionA.subVectors(jointPositions[2], jointPositions[0]).normalize();
fingerDirectionB.subVectors(jointPositions[4], jointPositions[3]).normalize();
// The index finger must remain long and straight instead of hooked or curled.
return straightness > 0.88 && fingerDirectionA.dot(fingerDirectionB) > 0.72;
}
function isFingerFolded(handRoot, fingerName, jointPositions) {
const jointNames = FOLDED_FINGER_JOINT_NAMES[fingerName];
if (!readJointPositions(handRoot, jointNames, jointPositions)) return false;
const pathLength = getTrackedPathLength(jointPositions);
if (pathLength < 0.018) return false;
const straightReach = jointPositions[0].distanceTo(jointPositions[jointPositions.length - 1]);
const straightness = straightReach / pathLength;
fingerDirectionA.subVectors(jointPositions[2], jointPositions[0]).normalize();
fingerDirectionB.subVectors(
jointPositions[jointPositions.length - 1],
jointPositions[jointPositions.length - 2]
).normalize();