-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
773 lines (665 loc) · 21.2 KB
/
Copy pathindex.html
File metadata and controls
773 lines (665 loc) · 21.2 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
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Quest 2 WebXR Controller Stream</title>
<style>
:root {
--bg: #f4f1ea;
--panel: #fffaf0;
--panel-strong: #f0e6d2;
--line: #d0c2a8;
--text: #2b2418;
--muted: #6f6451;
--ok: #2f7d4a;
--warn: #a55a18;
--bad: #a12626;
}
body {
font-family: "Noto Sans SC", "Segoe UI", sans-serif;
margin: 0;
background: radial-gradient(circle at top, #fff7e8, var(--bg) 50%);
color: var(--text);
}
.app {
max-width: 980px;
margin: 0 auto;
padding: 20px;
}
h1 {
margin: 0 0 6px;
font-size: 28px;
}
.subtitle {
margin: 0 0 18px;
color: var(--muted);
}
.panel {
background: var(--panel);
border: 1px solid var(--line);
border-radius: 16px;
padding: 16px;
box-shadow: 0 10px 28px rgba(67, 49, 13, 0.08);
margin-bottom: 16px;
}
.controls {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 12px;
align-items: end;
}
.field {
display: flex;
flex-direction: column;
gap: 6px;
}
label {
font-size: 13px;
color: var(--muted);
}
input {
font-size: 16px;
padding: 10px 12px;
border-radius: 10px;
border: 1px solid var(--line);
background: white;
color: var(--text);
}
button {
font-size: 16px;
padding: 12px 16px;
border-radius: 999px;
border: 1px solid var(--text);
background: var(--text);
color: #fff7e8;
cursor: pointer;
transition: transform 120ms ease, opacity 120ms ease;
}
button.secondary {
background: transparent;
color: var(--text);
}
button:hover {
transform: translateY(-1px);
}
button:disabled {
opacity: 0.5;
cursor: default;
transform: none;
}
.button-row {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.status-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
gap: 10px;
}
.card {
border: 1px solid var(--line);
border-radius: 12px;
padding: 12px;
background: var(--panel);
}
.card .label {
display: block;
color: var(--muted);
font-size: 12px;
margin-bottom: 6px;
text-transform: uppercase;
letter-spacing: 0.04em;
}
.card .value {
font-size: 18px;
font-weight: 700;
}
.ok { color: var(--ok); }
.warn { color: var(--warn); }
.bad { color: var(--bad); }
.hint {
margin-top: 10px;
color: var(--muted);
font-size: 13px;
}
pre {
white-space: pre-wrap;
word-break: break-all;
font-size: 14px;
margin: 0;
max-height: 40vh;
overflow: auto;
border: 1px solid var(--line);
border-radius: 12px;
padding: 10px;
background: #fffdf7;
}
@media (max-width: 640px) {
.app {
padding: 14px;
}
h1 {
font-size: 24px;
}
}
</style>
</head>
<body>
<div class="app">
<h1>Quest Controller Stream</h1>
<p class="subtitle">进入 XR 后采集左右手控制器位姿和按钮状态,并通过 WebSocket 推送到电脑端。</p>
<section class="panel">
<div class="controls">
<div class="field">
<label for="wsUrlInput">WebSocket 地址</label>
<input id="wsUrlInput" type="text" placeholder="ws://192.168.1.50:8765" />
</div>
<div class="field">
<label for="sendRateInput">发送频率 (Hz)</label>
<input id="sendRateInput" type="number" min="1" max="120" step="1" value="120" />
</div>
<div class="button-row">
<button id="startBtn">Start XR Stream</button>
<button id="endBtn" class="secondary">End XR Session</button>
<button id="saveBtn" class="secondary">Save Settings</button>
</div>
</div>
<div class="hint">
无线使用时,把地址改成电脑局域网 IP。若 Quest 浏览器限制 `immersive-vr`,需要改成 `https` / `wss`。
</div>
</section>
<section class="panel">
<div class="status-grid">
<div class="card">
<span class="label">XR</span>
<div id="xrStatus" class="value warn">Idle</div>
</div>
<div class="card">
<span class="label">WebSocket</span>
<div id="wsStatus" class="value warn">Disconnected</div>
</div>
<div class="card">
<span class="label">Send Rate</span>
<div id="sendRateStatus" class="value">0.0 Hz</div>
</div>
<div class="card">
<span class="label">Packets</span>
<div id="packetStatus" class="value">0</div>
</div>
<div class="card">
<span class="label">Left Pose</span>
<div id="leftPoseStatus" class="value warn">Missing</div>
</div>
<div class="card">
<span class="label">Right Pose</span>
<div id="rightPoseStatus" class="value warn">Missing</div>
</div>
</div>
</section>
<section class="panel">
<pre id="log"></pre>
</section>
</div>
<script>
const logEl = document.getElementById("log");
const startBtn = document.getElementById("startBtn");
const endBtn = document.getElementById("endBtn");
const saveBtn = document.getElementById("saveBtn");
const wsUrlInput = document.getElementById("wsUrlInput");
const sendRateInput = document.getElementById("sendRateInput");
const xrStatusEl = document.getElementById("xrStatus");
const wsStatusEl = document.getElementById("wsStatus");
const sendRateStatusEl = document.getElementById("sendRateStatus");
const packetStatusEl = document.getElementById("packetStatus");
const leftPoseStatusEl = document.getElementById("leftPoseStatus");
const rightPoseStatusEl = document.getElementById("rightPoseStatus");
let session = null;
let refSpace = null;
let gl = null;
let xrCanvas = null;
let ws = null;
let wsReconnectTimer = null;
let seq = 0;
let debugCounter = 0;
let lastFrameTime = 0;
let lastStatsTime = 0;
let sendAccumulatorMs = 0;
let packetsSent = 0;
let packetsSentWindow = 0;
let shouldReconnectWS = false;
const SETTINGS_KEY = "quest-controller-stream-settings";
const DEFAULT_SEND_RATE_HZ = 120;
let triedSetFrameRate = false;
let requestedFrameRate = null;
function getDefaultWSUrl() {
const paramUrl = new URLSearchParams(window.location.search).get("ws");
if (paramUrl) return paramUrl;
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
const host = window.location.hostname || "127.0.0.1";
return `${protocol}//${host}:8765`;
}
function loadSettings() {
try {
const raw = localStorage.getItem(SETTINGS_KEY);
if (!raw) return null;
return JSON.parse(raw);
} catch {
return null;
}
}
function saveSettings() {
const settings = {
wsUrl: wsUrlInput.value.trim(),
sendRateHz: sanitizeSendRate(sendRateInput.value),
};
localStorage.setItem(SETTINGS_KEY, JSON.stringify(settings));
sendRateInput.value = String(settings.sendRateHz);
log("设置已保存");
}
function sanitizeSendRate(value) {
const n = Number(value);
if (!Number.isFinite(n)) return DEFAULT_SEND_RATE_HZ;
return Math.min(120, Math.max(1, Math.round(n)));
}
function getSendIntervalMs() {
return 1000 / sanitizeSendRate(sendRateInput.value);
}
function setStatus(el, text, level) {
el.textContent = text;
el.className = `value ${level || ""}`.trim();
}
function refreshButtonState() {
startBtn.disabled = !!session;
endBtn.disabled = !session;
}
function updatePoseStatus(leftValid, rightValid) {
setStatus(leftPoseStatusEl, leftValid ? "Tracking" : "Missing", leftValid ? "ok" : "warn");
setStatus(rightPoseStatusEl, rightValid ? "Tracking" : "Missing", rightValid ? "ok" : "warn");
}
function resetRuntimeStats() {
sendAccumulatorMs = 0;
lastStatsTime = 0;
packetsSent = 0;
packetsSentWindow = 0;
packetStatusEl.textContent = "0";
sendRateStatusEl.textContent = "0.0 Hz";
updatePoseStatus(false, false);
}
function log(...args) {
const s = args.map(v => {
if (typeof v === "object") {
try {
return JSON.stringify(v);
} catch {
return String(v);
}
}
return String(v);
}).join(" ");
console.log(s);
logEl.textContent = s + "\n" + logEl.textContent.slice(0, 12000);
}
function clearWSReconnect() {
if (wsReconnectTimer) {
clearTimeout(wsReconnectTimer);
wsReconnectTimer = null;
}
}
function scheduleWSReconnect() {
if (!shouldReconnectWS || wsReconnectTimer) return;
clearWSReconnect();
wsReconnectTimer = setTimeout(() => {
wsReconnectTimer = null;
if (shouldReconnectWS && session) {
connectWSNonBlocking();
}
}, 1500);
}
function connectWSNonBlocking() {
const url = wsUrlInput.value.trim();
if (!url) {
setStatus(wsStatusEl, "Missing URL", "bad");
log("WebSocket 地址为空");
return;
}
if (ws && (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING)) {
return;
}
try {
setStatus(wsStatusEl, "Connecting", "warn");
ws = new WebSocket(url);
ws.binaryType = "arraybuffer";
ws.addEventListener("open", () => {
clearWSReconnect();
setStatus(wsStatusEl, "Connected", "ok");
log("WS 已连接", url);
});
ws.addEventListener("close", () => {
setStatus(wsStatusEl, shouldReconnectWS ? "Reconnecting" : "Disconnected", shouldReconnectWS ? "warn" : "bad");
log("WS 已断开");
ws = null;
scheduleWSReconnect();
});
ws.addEventListener("error", () => {
setStatus(wsStatusEl, "Error", "bad");
log("WS 错误(不会阻塞 XR 启动)");
});
} catch (err) {
setStatus(wsStatusEl, "Create Failed", "bad");
log("WS 创建失败:", err.message);
scheduleWSReconnect();
}
}
function buttonValue(gamepad, idx) {
if (!gamepad || !gamepad.buttons || idx >= gamepad.buttons.length) return 0;
const b = gamepad.buttons[idx];
return typeof b.value === "number" ? b.value : 0;
}
function buttonPressed(gamepad, idx) {
if (!gamepad || !gamepad.buttons || idx >= gamepad.buttons.length) return 0;
return gamepad.buttons[idx].pressed ? 1 : 0;
}
function getBestPose(xrFrame, src, refSpace) {
const gripPose = src.gripSpace ? xrFrame.getPose(src.gripSpace, refSpace) : null;
if (gripPose) return gripPose;
const rayPose = src.targetRaySpace ? xrFrame.getPose(src.targetRaySpace, refSpace) : null;
if (rayPose) return rayPose;
return null;
}
function encodeHand(seqNum, handId, pose, gamepad) {
const arr = new Float32Array(20);
arr[0] = seqNum;
arr[1] = handId;
arr[2] = pose ? 1 : 0;
if (pose) {
const p = pose.transform.position;
const q = pose.transform.orientation;
arr[3] = p.x;
arr[4] = p.y;
arr[5] = p.z;
arr[6] = q.x;
arr[7] = q.y;
arr[8] = q.z;
arr[9] = q.w;
}
const axes = gamepad?.axes ?? [];
const mapping = gamepad?.mapping ?? "";
let thumbX = 0;
let thumbY = 0;
if (mapping === "xr-standard") {
thumbX = axes[2] ?? axes[0] ?? 0;
thumbY = axes[3] ?? axes[1] ?? 0;
} else {
thumbX = axes[0] ?? 0;
thumbY = axes[1] ?? 0;
}
arr[10] = thumbX;
arr[11] = thumbY;
arr[12] = buttonValue(gamepad, 0); // trigger
arr[13] = buttonValue(gamepad, 1); // squeeze
arr[14] = buttonPressed(gamepad, 3); // thumbstick click
arr[15] = buttonPressed(gamepad, 4); // primary A/X
arr[16] = buttonPressed(gamepad, 5); // secondary B/Y
arr[17] = buttonPressed(gamepad, 0); // trigger pressed
arr[18] = buttonPressed(gamepad, 1); // squeeze pressed
arr[19] = 0;
return arr;
}
async function trySetFrameRateOnce() {
if (!session || triedSetFrameRate) return;
triedSetFrameRate = true;
if (
!("supportedFrameRates" in session) ||
typeof session.updateTargetFrameRate !== "function"
) {
log("当前浏览器不支持 supportedFrameRates / updateTargetFrameRate");
return;
}
try {
const rates = Array.from(session.supportedFrameRates || []);
log("supportedFrameRates =", JSON.stringify(rates));
if (!rates.length) {
log("supportedFrameRates 为空,保持默认刷新率");
return;
}
requestedFrameRate = rates.includes(120) ? 120 : Math.max(...rates);
log("准备请求刷新率 =", requestedFrameRate);
// 注意:这里只发请求,不阻塞整个启动链路
session.updateTargetFrameRate(requestedFrameRate)
.then(() => {
log("刷新率请求成功 =", requestedFrameRate);
if ("frameRate" in session) {
log("当前 session.frameRate =", session.frameRate);
}
})
.catch(err => {
log("刷新率请求失败:", err.message);
});
} catch (err) {
log("trySetFrameRateOnce 出错:", err.message);
}
}
function onXRFrame(time, xrFrame) {
if (!session) return;
session.requestAnimationFrame(onXRFrame);
// 首帧里再请求刷新率,更稳
if (!triedSetFrameRate) {
trySetFrameRateOnce();
}
if (lastFrameTime !== 0 && debugCounter % 120 === 0) {
const dt = time - lastFrameTime;
const fps = dt > 0 ? (1000 / dt) : 0;
const frameRateText = ("frameRate" in session) ? session.frameRate : "n/a";
log(
"frame dt =", dt.toFixed(2) + "ms",
"approx fps =", fps.toFixed(1),
"session.frameRate =", frameRateText
);
}
const frameDelta = lastFrameTime === 0 ? 0 : Math.max(0, time - lastFrameTime);
lastFrameTime = time;
const left = new Float32Array(20);
const right = new Float32Array(20);
let leftPoseValid = false;
let rightPoseValid = false;
left[0] = seq;
left[1] = 0;
right[0] = seq;
right[1] = 1;
for (const src of session.inputSources) {
if (!src || (src.handedness !== "left" && src.handedness !== "right")) {
continue;
}
const pose = getBestPose(xrFrame, src, refSpace);
const encoded = encodeHand(
seq,
src.handedness === "left" ? 0 : 1,
pose,
src.gamepad || null
);
if (src.handedness === "left") {
left.set(encoded);
leftPoseValid = !!encoded[2];
} else if (src.handedness === "right") {
right.set(encoded);
rightPoseValid = !!encoded[2];
}
if (debugCounter % 180 === 0 && src.gamepad) {
log(
src.handedness,
"mapping=", src.gamepad.mapping,
"axes=", JSON.stringify(Array.from(src.gamepad.axes)),
"buttons.length=", src.gamepad.buttons.length
);
}
}
const packet = new Float32Array(40);
packet.set(left, 0);
packet.set(right, 20);
updatePoseStatus(leftPoseValid, rightPoseValid);
sendAccumulatorMs += frameDelta;
const sendIntervalMs = getSendIntervalMs();
if (ws && ws.readyState === WebSocket.OPEN && sendIntervalMs > 0) {
let sendCount = 0;
while (sendAccumulatorMs >= sendIntervalMs && sendCount < 4) {
ws.send(packet.buffer);
sendAccumulatorMs -= sendIntervalMs;
packetsSent++;
packetsSentWindow++;
sendCount++;
}
if (sendCount > 0) {
packetStatusEl.textContent = String(packetsSent);
}
} else if (sendAccumulatorMs > sendIntervalMs) {
// 没连上时只保留一个周期以内的累计量,避免恢复连接后瞬间补发过多旧包
sendAccumulatorMs = sendIntervalMs;
}
if (lastStatsTime === 0) {
lastStatsTime = time;
} else if (time - lastStatsTime >= 1000) {
const hz = packetsSentWindow * 1000 / (time - lastStatsTime);
sendRateStatusEl.textContent = `${hz.toFixed(1)} Hz`;
packetsSentWindow = 0;
lastStatsTime = time;
}
debugCounter++;
seq++;
const baseLayer = session.renderState.baseLayer;
if (baseLayer) {
gl.bindFramebuffer(gl.FRAMEBUFFER, baseLayer.framebuffer);
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
gl.clearColor(0.08, 0.08, 0.08, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
}
}
async function startXR() {
if (session) {
log("XR 已经在运行");
return;
}
saveSettings();
resetRuntimeStats();
shouldReconnectWS = true;
refreshButtonState();
setStatus(xrStatusEl, "Starting", "warn");
log("开始启动");
if (!navigator.xr) {
setStatus(xrStatusEl, "Unavailable", "bad");
log("navigator.xr 不存在");
return;
}
const supported = await navigator.xr.isSessionSupported("immersive-vr");
log("immersive-vr supported =", supported);
if (!supported) {
setStatus(xrStatusEl, "Unsupported", "bad");
return;
}
xrCanvas = document.createElement("canvas");
xrCanvas.width = 16;
xrCanvas.height = 16;
xrCanvas.style.display = "none";
document.body.appendChild(xrCanvas);
gl = xrCanvas.getContext("webgl2", {
xrCompatible: true,
alpha: false,
antialias: true,
depth: true,
stencil: false
});
if (!gl) {
setStatus(xrStatusEl, "WebGL Failed", "bad");
log("获取 webgl2 失败");
return;
}
await gl.makeXRCompatible();
// 先拿 XR session,别被 WS 阻塞
session = await navigator.xr.requestSession("immersive-vr", {
requiredFeatures: ["local-floor"],
});
setStatus(xrStatusEl, "Running", "ok");
refreshButtonState();
session.addEventListener("inputsourceschange", (ev) => {
log(
"inputsourceschange",
"added=", ev.added.length,
"removed=", ev.removed.length,
"current=", session.inputSources.length
);
});
session.addEventListener("end", () => {
log("XR session ended");
setStatus(xrStatusEl, "Ended", "warn");
session = null;
refSpace = null;
gl = null;
if (xrCanvas) {
xrCanvas.remove();
xrCanvas = null;
}
lastFrameTime = 0;
triedSetFrameRate = false;
requestedFrameRate = null;
shouldReconnectWS = false;
clearWSReconnect();
refreshButtonState();
if (ws) {
try { ws.close(); } catch {}
ws = null;
}
setStatus(wsStatusEl, "Disconnected", "bad");
});
// 不需要做 onframeratechange 检测,直接监听即可
session.addEventListener("frameratechange", () => {
log("frameratechange:", ("frameRate" in session) ? session.frameRate : "unknown");
});
session.updateRenderState({
baseLayer: new XRWebGLLayer(session, gl),
});
refSpace = await session.requestReferenceSpace("local-floor");
log("XR ready");
// XR 已经准备好后,再去连 WS;连不上也不影响 XR 进入
connectWSNonBlocking();
session.requestAnimationFrame(onXRFrame);
log("XR frame loop started");
}
async function endXR() {
if (!session) {
log("当前没有 XR session");
return;
}
try {
await session.end();
} catch (err) {
log("结束 session 失败:", err.message);
}
}
startBtn.addEventListener("click", () => {
startXR().catch(err => {
console.error(err);
setStatus(xrStatusEl, "Start Failed", "bad");
refreshButtonState();
log("启动失败:", err.message);
});
});
endBtn.addEventListener("click", () => {
endXR().catch(err => {
console.error(err);
log("结束失败:", err.message);
});
});
saveBtn.addEventListener("click", saveSettings);
{
const settings = loadSettings();
wsUrlInput.value = settings?.wsUrl || getDefaultWSUrl();
sendRateInput.value = String(settings?.sendRateHz || DEFAULT_SEND_RATE_HZ);
refreshButtonState();
setStatus(xrStatusEl, "Idle", "warn");
setStatus(wsStatusEl, "Disconnected", "bad");
updatePoseStatus(false, false);
}
</script>
</body>
</html>