spi3der 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/APP.md +40 -0
- package/Dockerfile +4 -0
- package/MCP.md +149 -0
- package/PLAYER.md +136 -0
- package/README.md +35 -2
- package/api/mcp.mjs +31 -0
- package/api/store.mjs +158 -0
- package/bin/spi3der.mjs +20 -0
- package/emulator.html +17 -0
- package/engine/8fa668821759/bevel.js +265 -0
- package/engine/8fa668821759/camera.js +76 -0
- package/engine/8fa668821759/character.js +1019 -0
- package/engine/8fa668821759/check.js +83 -0
- package/engine/8fa668821759/csg.js +342 -0
- package/engine/8fa668821759/edit.js +48 -0
- package/engine/8fa668821759/galaxy.js +265 -0
- package/engine/8fa668821759/gl.js +798 -0
- package/engine/8fa668821759/gpu.js +1356 -0
- package/engine/8fa668821759/index.js +48 -0
- package/engine/8fa668821759/instances.js +41 -0
- package/engine/8fa668821759/joints.js +355 -0
- package/engine/8fa668821759/kernel-bytes.js +2 -0
- package/engine/8fa668821759/kernel.js +82 -0
- package/engine/8fa668821759/lathe.js +87 -0
- package/engine/8fa668821759/material.js +19 -0
- package/engine/8fa668821759/mesh.js +131 -0
- package/engine/8fa668821759/order.js +125 -0
- package/engine/8fa668821759/pick.js +85 -0
- package/engine/8fa668821759/place.js +17 -0
- package/engine/8fa668821759/planet.js +376 -0
- package/engine/8fa668821759/rig.js +417 -0
- package/engine/8fa668821759/saw.js +121 -0
- package/engine/8fa668821759/sky.js +428 -0
- package/engine/8fa668821759/svg.js +239 -0
- package/engine/8fa668821759/text.js +231 -0
- package/engine/8fa668821759/triangulate.js +72 -0
- package/engine/8fa668821759/types.js +2 -0
- package/engine/8fa668821759/vec.js +22 -0
- package/engine/8fa668821759/version.js +7 -0
- package/engine/8fa668821759/vrtsh.js +354 -0
- package/engine/8fa668821759/wire.js +346 -0
- package/favicon.svg +15 -0
- package/help.md +289 -0
- package/icons/apple-touch-icon.png +0 -0
- package/icons/favicon-32.png +0 -0
- package/icons/icon-192.png +0 -0
- package/icons/icon-512.png +0 -0
- package/icons/icon-maskable-512.png +0 -0
- package/icons/icon-maskable.svg +13 -0
- package/icons/icon.svg +15 -0
- package/index.html +13730 -0
- package/manifest.webmanifest +37 -0
- package/mcp/engine/bevel.js +265 -0
- package/mcp/engine/camera.js +76 -0
- package/mcp/engine/character.js +1019 -0
- package/mcp/engine/check.js +83 -0
- package/mcp/engine/csg.js +342 -0
- package/mcp/engine/edit.js +48 -0
- package/mcp/engine/galaxy.js +265 -0
- package/mcp/engine/gl.js +798 -0
- package/mcp/engine/gpu.js +1356 -0
- package/mcp/engine/index.js +48 -0
- package/mcp/engine/instances.js +41 -0
- package/mcp/engine/joints.js +355 -0
- package/mcp/engine/kernel-bytes.js +2 -0
- package/mcp/engine/kernel.js +82 -0
- package/mcp/engine/lathe.js +87 -0
- package/mcp/engine/material.js +19 -0
- package/mcp/engine/mesh.js +131 -0
- package/mcp/engine/order.js +125 -0
- package/mcp/engine/pick.js +85 -0
- package/mcp/engine/place.js +17 -0
- package/mcp/engine/planet.js +376 -0
- package/mcp/engine/rig.js +417 -0
- package/mcp/engine/saw.js +121 -0
- package/mcp/engine/sky.js +428 -0
- package/mcp/engine/svg.js +239 -0
- package/mcp/engine/text.js +231 -0
- package/mcp/engine/triangulate.js +72 -0
- package/mcp/engine/types.js +2 -0
- package/mcp/engine/vec.js +22 -0
- package/mcp/engine/version.js +7 -0
- package/mcp/engine/vrtsh.js +354 -0
- package/mcp/engine/wire.js +346 -0
- package/mcp/http.js +292 -0
- package/mcp/live.js +95 -0
- package/mcp/package.json +10 -0
- package/mcp/scene.js +1027 -0
- package/mcp/serve.js +87 -0
- package/mcp/server.js +97 -0
- package/media/face-of-spider.png +0 -0
- package/media/spider-paint-2015.png +0 -0
- package/media/spider-web.png +0 -0
- package/models/deepsky-lark.scene.json +1 -0
- package/models/deepsky.scene.json +1 -0
- package/models/globe.scene.json +1 -0
- package/models/lark-4.scene.json +1 -0
- package/models/lark.game.json +14 -0
- package/models/spider-build-v2.md +215 -0
- package/models/spider-build-v2.mjs +355 -0
- package/models/spider-build-v2.schema.json +1001 -0
- package/nginx.conf +12 -0
- package/package.json +45 -4
- package/player.html +274 -0
- package/sky.html +133 -0
- package/sky.md +285 -0
- package/src/demo.js +92 -0
- package/src/emulator/devices.js +22 -0
- package/src/emulator/xr.js +362 -0
- package/src/math.js +77 -0
- package/sw.js +37 -0
- package/xr-compare.html +332 -0
- package/xr-test.html +248 -0
package/xr-compare.html
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>XR compare: three.js vs 3Draft</title>
|
|
7
|
+
<style>
|
|
8
|
+
:root { color-scheme: dark; }
|
|
9
|
+
body { margin: 0; background: #111; color: #e8e4d8; font: 16px/1.45 ui-monospace, Menlo, monospace; }
|
|
10
|
+
header { display: flex; gap: 12px; align-items: center; padding: 14px 16px; flex-wrap: wrap; }
|
|
11
|
+
h1 { font-size: 17px; margin: 0 10px 0 0; }
|
|
12
|
+
button { font: 700 18px/1 ui-monospace, monospace; padding: 16px 22px; border-radius: 12px; border: 0; cursor: pointer; }
|
|
13
|
+
#three { background: #2aa7b5; color: #071214; }
|
|
14
|
+
#draft { background: #d9a441; color: #1a1205; }
|
|
15
|
+
button:disabled { opacity: 0.35; }
|
|
16
|
+
p { margin: 0 16px 10px; color: #a9a59a; max-width: 820px; }
|
|
17
|
+
pre { margin: 10px 16px 24px; padding: 14px; background: #000; border-radius: 10px; white-space: pre-wrap; word-break: break-word; font-size: 15px; }
|
|
18
|
+
.err { color: #ff6b4a; }
|
|
19
|
+
.ok { color: #7be08a; }
|
|
20
|
+
</style>
|
|
21
|
+
</head>
|
|
22
|
+
<body>
|
|
23
|
+
<header>
|
|
24
|
+
<h1>XR compare</h1>
|
|
25
|
+
<button id="three" disabled>three.js VR</button>
|
|
26
|
+
<button id="draft" disabled>3Draft VR</button>
|
|
27
|
+
<button id="draft2" disabled style="background:#c9772b;color:#1a1205">3Draft VR · layer like three.js</button>
|
|
28
|
+
<span id="relay" style="font-size:18px">remote log: <b id="code" style="font-size:26px;color:#7be08a">····</b></span>
|
|
29
|
+
</header>
|
|
30
|
+
<canvas id="flat" width="320" height="320" style="display:block;margin:0 16px 10px;width:240px;height:240px;border-radius:10px;background:#000" title="3Draft's WebGL2 renderer outside VR"></canvas>
|
|
31
|
+
<p>The same scene twice: a turning teal cube 1.5 m ahead at eye height, a lying golden egg to the right, a floor grid and
|
|
32
|
+
stars round you. <b>three.js</b> draws it with its own renderer and WebXRManager; <b>3Draft</b> with the 3Draft engine's WebGL2
|
|
33
|
+
renderer (the viewer's), in the plainest WebXR loop. If three.js shows it and 3Draft does not, the fault is in the 3Draft
|
|
34
|
+
renderer; if both show it, it is in the viewer around it. Leave VR with the <b>left X</b> (or the Meta button); the log below
|
|
35
|
+
(the newest line last) tells what happened. 3Draft's background is dark blue, three.js's near black. The small picture is
|
|
36
|
+
3Draft's WebGL2 renderer outside VR. <b>Remote log:</b> every line also goes to ntfy.sh under the code above (tell the code to
|
|
37
|
+
the assistant: it reads the log live from there).</p>
|
|
38
|
+
<pre id="log"></pre>
|
|
39
|
+
<script type="module">
|
|
40
|
+
import { createGl, box, lathe, merge, orbit } from './engine/8fa668821759/index.js';
|
|
41
|
+
|
|
42
|
+
const logEl = document.getElementById('log');
|
|
43
|
+
// the remote log: lines are sent in batches to a public ntfy.sh topic named by a random code (nothing private goes there:
|
|
44
|
+
// the browser, WebGL facts, errors), so the log can be read on another device while the headset is on
|
|
45
|
+
const code = Array.from(crypto.getRandomValues(new Uint8Array(4)), (b) => 'abcdefghjkmnpqrstuvwxyz23456789'[b % 31]).join('');
|
|
46
|
+
document.getElementById('code').textContent = code;
|
|
47
|
+
const topic = `spider-xr-${code}`;
|
|
48
|
+
let outbox = [], sending = false;
|
|
49
|
+
const flush = async () => {
|
|
50
|
+
if (sending || !outbox.length) return;
|
|
51
|
+
sending = true;
|
|
52
|
+
const body = outbox.join('\n');
|
|
53
|
+
outbox = [];
|
|
54
|
+
try {
|
|
55
|
+
await fetch(`https://ntfy.sh/${topic}`, { method: 'POST', body });
|
|
56
|
+
} catch (e) {
|
|
57
|
+
console.warn('[xr-compare] remote log failed', e);
|
|
58
|
+
}
|
|
59
|
+
sending = false;
|
|
60
|
+
};
|
|
61
|
+
setInterval(flush, 1000);
|
|
62
|
+
const log = (msg, cls = '') => {
|
|
63
|
+
const line = `${new Date().toISOString().slice(11, 19)} ${msg}`;
|
|
64
|
+
const div = document.createElement('div');
|
|
65
|
+
div.textContent = line;
|
|
66
|
+
if (cls) div.className = cls;
|
|
67
|
+
logEl.append(div);
|
|
68
|
+
outbox.push(`${cls === 'err' ? '!! ' : ''}${line}`);
|
|
69
|
+
(cls === 'err' ? console.error : console.log)('[xr-compare]', msg);
|
|
70
|
+
};
|
|
71
|
+
// every shader that does not compile and every program that does not link, in any WebGL2 context of the page
|
|
72
|
+
{
|
|
73
|
+
const P = WebGL2RenderingContext.prototype, compile = P.compileShader, link = P.linkProgram;
|
|
74
|
+
P.compileShader = function (sh) {
|
|
75
|
+
compile.call(this, sh);
|
|
76
|
+
if (!this.getShaderParameter(sh, this.COMPILE_STATUS)) log(`shader compile failed: ${this.getShaderInfoLog(sh)}`, 'err');
|
|
77
|
+
};
|
|
78
|
+
P.linkProgram = function (pr) {
|
|
79
|
+
link.call(this, pr);
|
|
80
|
+
if (!this.getProgramParameter(pr, this.LINK_STATUS)) log(`program link failed: ${this.getProgramInfoLog(pr)}`, 'err');
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
window.addEventListener('error', (e) => log(`page error: ${e.message}`, 'err'));
|
|
84
|
+
window.addEventListener('unhandledrejection', (e) => log(`unhandled: ${e.reason?.message ?? e.reason}`, 'err'));
|
|
85
|
+
log(navigator.userAgent);
|
|
86
|
+
|
|
87
|
+
// ---- the scene, in metres; the floor is y = 0 (local-floor), the eye ~1.6 m up
|
|
88
|
+
const sceneMesh = (() => {
|
|
89
|
+
const parts = [box({ size: [0.4, 0.4, 0.4], at: [0, 0, 0], color: '#2aa7b5' })];
|
|
90
|
+
// (the cube turns: it is rebuilt every frame from this one, see cubeAt)
|
|
91
|
+
return parts[0];
|
|
92
|
+
})();
|
|
93
|
+
const egg = (() => {
|
|
94
|
+
const pts = [];
|
|
95
|
+
for (let i = 0; i <= 16; i++) {
|
|
96
|
+
const u = -0.45 + (0.9 * i) / 16;
|
|
97
|
+
pts.push([u, 0.15 * Math.sqrt(Math.max(0, 1 - (u / 0.45) ** 2))]);
|
|
98
|
+
}
|
|
99
|
+
return lathe(pts, { origin: [1, 1.2, -2], right: [1, 0, 0], up: [0, 1, 0], normal: [0, 0, 1] }, 24, { color: '#d9a441' });
|
|
100
|
+
})();
|
|
101
|
+
const grid = (() => {
|
|
102
|
+
const lines = [];
|
|
103
|
+
for (let i = -10; i <= 10; i++) {
|
|
104
|
+
lines.push(box({ size: [10, 0.01, 0.015], at: [0, 0, i * 0.5], color: '#4fb3bf' }));
|
|
105
|
+
lines.push(box({ size: [0.015, 0.01, 10], at: [i * 0.5, 0, 0], color: '#4fb3bf' }));
|
|
106
|
+
}
|
|
107
|
+
return merge(...lines);
|
|
108
|
+
})();
|
|
109
|
+
const stars = (() => {
|
|
110
|
+
const out = [];
|
|
111
|
+
let seed = 7;
|
|
112
|
+
const rnd = () => ((seed = (seed * 1664525 + 1013904223) >>> 0) / 4294967296);
|
|
113
|
+
for (let i = 0; i < 500; i++) {
|
|
114
|
+
const u = rnd() * 2 - 1, a = rnd() * Math.PI * 2, r = Math.sqrt(1 - u * u);
|
|
115
|
+
out.push(box({ size: [0.25, 0.25, 0.25], at: [50 * r * Math.cos(a), 50 * u, 50 * r * Math.sin(a)], color: '#ffffff' }));
|
|
116
|
+
}
|
|
117
|
+
return merge(...out);
|
|
118
|
+
})();
|
|
119
|
+
const cubeAt = (t) => {
|
|
120
|
+
const c1 = Math.cos(t), s1 = Math.sin(t), c2 = Math.cos(t * 0.77), s2 = Math.sin(t * 0.77);
|
|
121
|
+
return { ...sceneMesh, positions: sceneMesh.positions.map(([x, y, z]) => {
|
|
122
|
+
[y, z] = [y * c2 - z * s2, y * s2 + z * c2];
|
|
123
|
+
[x, z] = [x * c1 + z * s1, -x * s1 + z * c1];
|
|
124
|
+
return [x, y + 1.4, z - 1.5];
|
|
125
|
+
}) };
|
|
126
|
+
};
|
|
127
|
+
const still = merge(egg, grid, stars);
|
|
128
|
+
log(`scene: cube, egg, grid, ${500} stars (${still.faces.length + sceneMesh.faces.length} faces)`);
|
|
129
|
+
// 3Draft's WebGL2 renderer outside VR (and the GPU's limits): does the renderer itself work on this device?
|
|
130
|
+
try {
|
|
131
|
+
const flat = await createGl(document.getElementById('flat'), { onError: (m) => log(`3Draft flat: ${m}`, 'err') });
|
|
132
|
+
const g = flat.context;
|
|
133
|
+
const dbg = g.getExtension('WEBGL_debug_renderer_info');
|
|
134
|
+
log(`GPU: ${dbg ? g.getParameter(dbg.UNMASKED_RENDERER_WEBGL) : g.getParameter(g.RENDERER)} · max vertex uniform vectors ${g.getParameter(g.MAX_VERTEX_UNIFORM_VECTORS)} · fragment ${g.getParameter(g.MAX_FRAGMENT_UNIFORM_VECTORS)} · textures ${g.getParameter(g.MAX_TEXTURE_SIZE)} · vertex attribs ${g.getParameter(g.MAX_VERTEX_ATTRIBS)}`);
|
|
135
|
+
flat.render(merge(still, cubeAt(0.6)), orbit({ yaw: 0.3, pitch: 0.25 }), { size: 240, background: [0.03, 0.05, 0.14], smooth: 40, studio: true, fit: { center: [0.4, 1.2, -1.6], radius: 1.6 } });
|
|
136
|
+
for (let e = g.getError(), n = 0; e !== g.NO_ERROR && n < 4; e = g.getError(), n++) log(`3Draft flat: GL error 0x${e.toString(16)}`, 'err');
|
|
137
|
+
log('3Draft flat (outside VR): drawn — the small picture should show the cube and the egg', 'ok');
|
|
138
|
+
} catch (e) {
|
|
139
|
+
log(`3Draft flat (outside VR) failed: ${e.message}`, 'err');
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// the left controller's X (xr-standard button 4): leave VR
|
|
143
|
+
const leftX = (session) => [...session.inputSources].some((src) => src.handedness === 'left' && Boolean(src.gamepad?.buttons?.[4]?.pressed));
|
|
144
|
+
// ---- WebXR support
|
|
145
|
+
const threeBtn = document.getElementById('three'), draftBtn = document.getElementById('draft'), draft2Btn = document.getElementById('draft2');
|
|
146
|
+
let running = null;
|
|
147
|
+
(async () => {
|
|
148
|
+
if (!navigator.xr) return log('navigator.xr is missing: no WebXR here', 'err');
|
|
149
|
+
const ok = await navigator.xr.isSessionSupported('immersive-vr').catch((e) => (log(`isSessionSupported: ${e.message}`, 'err'), false));
|
|
150
|
+
log(`immersive-vr supported: ${ok}`, ok ? 'ok' : 'err');
|
|
151
|
+
threeBtn.disabled = draftBtn.disabled = draft2Btn.disabled = !ok;
|
|
152
|
+
})();
|
|
153
|
+
|
|
154
|
+
// ---- three.js (r170), fetched now so the click stays the user's gesture
|
|
155
|
+
const THREE_URL = 'https://cdn.jsdelivr.net/npm/three@0.170.0/build/three.module.js';
|
|
156
|
+
const threeReady = import(THREE_URL).then((m) => (log(`three.js r${m.REVISION} loaded`), m), (e) => (log(`three.js did not load: ${e.message}`, 'err'), null));
|
|
157
|
+
threeBtn.onclick = async () => {
|
|
158
|
+
if (running) return running.end();
|
|
159
|
+
const THREE = await threeReady;
|
|
160
|
+
if (!THREE) return;
|
|
161
|
+
let session;
|
|
162
|
+
try {
|
|
163
|
+
session = await navigator.xr.requestSession('immersive-vr', { optionalFeatures: ['local-floor'] });
|
|
164
|
+
} catch (e) {
|
|
165
|
+
return log(`three.js: requestSession failed: ${e.name}: ${e.message}`, 'err');
|
|
166
|
+
}
|
|
167
|
+
log('three.js: session created', 'ok');
|
|
168
|
+
const canvas = document.createElement('canvas');
|
|
169
|
+
canvas.style.cssText = 'position:fixed;inset:0;width:100%;height:100%;pointer-events:none;z-index:5';
|
|
170
|
+
document.body.append(canvas);
|
|
171
|
+
session.addEventListener('end', () => canvas.remove());
|
|
172
|
+
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
|
|
173
|
+
renderer.xr.enabled = true;
|
|
174
|
+
renderer.xr.setReferenceSpaceType('local-floor');
|
|
175
|
+
const scene = new THREE.Scene();
|
|
176
|
+
scene.background = new THREE.Color(0x05060a);
|
|
177
|
+
scene.add(new THREE.HemisphereLight(0xffffff, 0x334455, 1.2));
|
|
178
|
+
const sun = new THREE.DirectionalLight(0xffffff, 1.5);
|
|
179
|
+
sun.position.set(2, 4, 1);
|
|
180
|
+
scene.add(sun);
|
|
181
|
+
const cube = new THREE.Mesh(new THREE.BoxGeometry(0.4, 0.4, 0.4), new THREE.MeshStandardMaterial({ color: 0x2aa7b5 }));
|
|
182
|
+
cube.position.set(0, 1.4, -1.5);
|
|
183
|
+
scene.add(cube);
|
|
184
|
+
const eggM = new THREE.Mesh(new THREE.SphereGeometry(0.15, 24, 16), new THREE.MeshStandardMaterial({ color: 0xd9a441 }));
|
|
185
|
+
eggM.scale.set(3, 1, 1);
|
|
186
|
+
eggM.position.set(1, 1.2, -2);
|
|
187
|
+
scene.add(eggM);
|
|
188
|
+
scene.add(new THREE.GridHelper(10, 20, 0x4fb3bf, 0x4fb3bf));
|
|
189
|
+
const camera = new THREE.PerspectiveCamera(70, 1, 0.05, 200);
|
|
190
|
+
let frames = 0;
|
|
191
|
+
renderer.setAnimationLoop((t) => {
|
|
192
|
+
if (leftX(session)) return session.end();
|
|
193
|
+
cube.rotation.set(t / 1300, t / 1000, 0);
|
|
194
|
+
renderer.render(scene, camera);
|
|
195
|
+
if (++frames === 1 || frames === 120) log(`three.js: frame ${frames}, presenting ${renderer.xr.isPresenting}`);
|
|
196
|
+
});
|
|
197
|
+
running = session;
|
|
198
|
+
session.addEventListener('end', () => {
|
|
199
|
+
log(`three.js: session ended after ${frames} frames`);
|
|
200
|
+
renderer.setAnimationLoop(null);
|
|
201
|
+
renderer.dispose();
|
|
202
|
+
running = null;
|
|
203
|
+
});
|
|
204
|
+
try {
|
|
205
|
+
await renderer.xr.setSession(session);
|
|
206
|
+
log('three.js: setSession ok');
|
|
207
|
+
} catch (e) {
|
|
208
|
+
log(`three.js: setSession failed: ${e.message}`, 'err');
|
|
209
|
+
session.end();
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// ---- 3Draft: the engine's WebGL2 renderer straight into the XR layer, nothing else
|
|
214
|
+
// 3Draft's GL shaders take a WebGPU-style projection (depth 0..w) and turn it into GL's (-w..w) themselves, so the eye's
|
|
215
|
+
// OpenGL projection is converted first: z' = (z + w) / 2
|
|
216
|
+
const engineProjection = (P) => {
|
|
217
|
+
const m = Float32Array.from(P);
|
|
218
|
+
for (const i of [2, 6, 10, 14]) m[i] = (P[i] + P[i + 1]) * 0.5;
|
|
219
|
+
return m;
|
|
220
|
+
};
|
|
221
|
+
// a GL call tracer: for the first frames, every call on the XR context is followed by getError, and the first failing call
|
|
222
|
+
// (its name and arguments) goes to the log, so a 0x506 says which call the headset refused
|
|
223
|
+
const traceGl = (gl, tag) => {
|
|
224
|
+
const getError = gl.getError.bind(gl);
|
|
225
|
+
const st = { on: false, reported: 0 };
|
|
226
|
+
const proto = WebGL2RenderingContext.prototype;
|
|
227
|
+
for (const name of Object.getOwnPropertyNames(proto)) {
|
|
228
|
+
if (name === 'getError' || name === 'constructor') continue;
|
|
229
|
+
const d = Object.getOwnPropertyDescriptor(proto, name);
|
|
230
|
+
if (typeof d?.value !== 'function') continue;
|
|
231
|
+
const orig = d.value;
|
|
232
|
+
gl[name] = function (...args) {
|
|
233
|
+
const r = orig.apply(this, args);
|
|
234
|
+
if (st.on && st.reported < 6) {
|
|
235
|
+
const e = getError();
|
|
236
|
+
if (e) {
|
|
237
|
+
st.reported++;
|
|
238
|
+
const arg = args.map((a) => (typeof a === 'number' ? (a > 1000 ? `0x${a.toString(16)}` : a) : a === null ? 'null' : a?.constructor?.name ?? typeof a)).join(', ');
|
|
239
|
+
log(`${tag}: GL error 0x${e.toString(16)} from ${name}(${arg}) · framebuffer binding: ${(() => { const fb = proto.getParameter.call(this, this.FRAMEBUFFER_BINDING); return fb ? 'the XR layer' : 'null (the canvas)'; })()}`, 'err');
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return r;
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
gl.getError = getError;
|
|
246
|
+
return st;
|
|
247
|
+
};
|
|
248
|
+
const startDraft = async (layerInit, tag) => {
|
|
249
|
+
if (running) return running.end();
|
|
250
|
+
let session;
|
|
251
|
+
try {
|
|
252
|
+
session = await navigator.xr.requestSession('immersive-vr', { optionalFeatures: ['local-floor'] });
|
|
253
|
+
} catch (e) {
|
|
254
|
+
return log(`${tag}: requestSession failed: ${e.name}: ${e.message}`, 'err');
|
|
255
|
+
}
|
|
256
|
+
log(`${tag}: session created`, 'ok');
|
|
257
|
+
running = session;
|
|
258
|
+
let frames = 0, errors = 0;
|
|
259
|
+
session.addEventListener('end', () => {
|
|
260
|
+
log(`${tag}: session ended after ${frames} frames, ${errors} errors`);
|
|
261
|
+
running = null;
|
|
262
|
+
});
|
|
263
|
+
try {
|
|
264
|
+
const canvas = document.createElement('canvas');
|
|
265
|
+
canvas.width = canvas.height = 64;
|
|
266
|
+
// (in the page, so an emulator that draws into the canvas shows it; a headset draws into the layer)
|
|
267
|
+
canvas.style.cssText = 'position:fixed;inset:0;width:100%;height:100%;pointer-events:none;z-index:5';
|
|
268
|
+
document.body.append(canvas);
|
|
269
|
+
session.addEventListener('end', () => canvas.remove());
|
|
270
|
+
const gpu = await createGl(canvas);
|
|
271
|
+
const gl = gpu.context;
|
|
272
|
+
log(`${tag}: engine renderer ready · ${gl.getParameter(gl.VERSION)}`);
|
|
273
|
+
canvas.addEventListener('webglcontextlost', () => log(`${tag}: WebGL context LOST`, 'err'));
|
|
274
|
+
await gl.makeXRCompatible();
|
|
275
|
+
log(`${tag}: makeXRCompatible ok`);
|
|
276
|
+
const trace = traceGl(gl, tag);
|
|
277
|
+
const layer = new XRWebGLLayer(session, gl, layerInit);
|
|
278
|
+
log(`${tag}: layer options ${JSON.stringify(layerInit)} · framebuffer ${layer.framebuffer ? 'opaque' : 'null'} · status ${(() => { gl.bindFramebuffer(gl.FRAMEBUFFER, layer.framebuffer); const st = gl.checkFramebufferStatus(gl.FRAMEBUFFER); gl.bindFramebuffer(gl.FRAMEBUFFER, null); return st === gl.FRAMEBUFFER_COMPLETE ? 'complete (outside a frame)' : `0x${st.toString(16)} (outside a frame)`; })()}`);
|
|
279
|
+
session.updateRenderState({ baseLayer: layer, depthNear: 0.05, depthFar: 200 });
|
|
280
|
+
log(`${tag}: layer ${layer.framebufferWidth}×${layer.framebufferHeight}`);
|
|
281
|
+
let space;
|
|
282
|
+
try {
|
|
283
|
+
space = await session.requestReferenceSpace('local-floor');
|
|
284
|
+
} catch {
|
|
285
|
+
space = await session.requestReferenceSpace('local');
|
|
286
|
+
log(`${tag}: local-floor refused, local used`);
|
|
287
|
+
}
|
|
288
|
+
const onFrame = (t, frame) => {
|
|
289
|
+
if (leftX(session)) return session.end();
|
|
290
|
+
session.requestAnimationFrame(onFrame);
|
|
291
|
+
frames++;
|
|
292
|
+
try {
|
|
293
|
+
const pose = frame.getViewerPose(space);
|
|
294
|
+
if (!pose) return frames < 5 && log(`${tag}: frame ${frames}: no pose`);
|
|
295
|
+
const L = session.renderState.baseLayer;
|
|
296
|
+
trace.on = frames <= 3;
|
|
297
|
+
if (frames <= 2) {
|
|
298
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, L.framebuffer);
|
|
299
|
+
const st = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
|
|
300
|
+
log(`${tag}: frame ${frames}: framebuffer status ${st === gl.FRAMEBUFFER_COMPLETE ? 'complete' : `0x${st.toString(16)}`} · the same layer ${L === layer}`);
|
|
301
|
+
}
|
|
302
|
+
const mesh = merge(still, cubeAt(t / 1000));
|
|
303
|
+
for (const v of pose.views) {
|
|
304
|
+
const vp = L.getViewport(v);
|
|
305
|
+
if (!vp) continue;
|
|
306
|
+
const p = v.transform.position;
|
|
307
|
+
gpu.render(mesh, 'persp', {
|
|
308
|
+
size: 64,
|
|
309
|
+
background: [0.03, 0.05, 0.14],
|
|
310
|
+
smooth: 40,
|
|
311
|
+
studio: true,
|
|
312
|
+
xr: { framebuffer: L.framebuffer, viewport: [vp.x, vp.y, vp.width, vp.height], projection: engineProjection(v.projectionMatrix), view: v.transform.inverse.matrix, eye: [p.x, p.y, p.z] },
|
|
313
|
+
});
|
|
314
|
+
if (frames <= 2) log(`${tag}: frame ${frames}, ${v.eye} eye ${vp.x},${vp.y} ${vp.width}×${vp.height}`);
|
|
315
|
+
}
|
|
316
|
+
for (let e = gl.getError(), n = 0; e !== gl.NO_ERROR && n < 4; e = gl.getError(), n++) if (errors++ < 10) log(`${tag}: GL error 0x${e.toString(16)} (frame ${frames})`, 'err');
|
|
317
|
+
if (frames === 120) log(`${tag}: 120 frames drawn`, 'ok');
|
|
318
|
+
} catch (e) {
|
|
319
|
+
if (errors++ < 5) log(`${tag}: frame ${frames}: ${e.name}: ${e.message}`, 'err');
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
session.requestAnimationFrame(onFrame);
|
|
323
|
+
} catch (e) {
|
|
324
|
+
log(`${tag}: set-up failed: ${e.name}: ${e.message}`, 'err');
|
|
325
|
+
session.end();
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
draftBtn.onclick = () => startDraft({ antialias: true, depth: true, stencil: false, alpha: true }, '3Draft');
|
|
329
|
+
draft2Btn.onclick = () => startDraft({ antialias: true, depth: true, stencil: false, alpha: true, framebufferScaleFactor: 1 }, '3Draft·three-layer');
|
|
330
|
+
</script>
|
|
331
|
+
</body>
|
|
332
|
+
</html>
|
package/xr-test.html
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>WebXR test</title>
|
|
7
|
+
<style>
|
|
8
|
+
:root { color-scheme: dark; }
|
|
9
|
+
body { margin: 0; background: #111; color: #e8e4d8; font: 15px/1.45 ui-monospace, Menlo, monospace; }
|
|
10
|
+
header { display: flex; gap: 10px; align-items: center; padding: 12px 16px; flex-wrap: wrap; }
|
|
11
|
+
h1 { font-size: 16px; margin: 0 12px 0 0; }
|
|
12
|
+
button { font: 600 15px/1 ui-monospace, monospace; padding: 12px 18px; border-radius: 10px; border: 0; background: #2aa7b5; color: #071214; cursor: pointer; }
|
|
13
|
+
button.red { background: #e2452a; color: #fff; }
|
|
14
|
+
button:disabled { opacity: 0.4; }
|
|
15
|
+
canvas { display: block; width: min(480px, calc(100vw - 32px)); aspect-ratio: 1; margin: 0 16px; border-radius: 10px; background: #000; }
|
|
16
|
+
pre { margin: 12px 16px 24px; padding: 12px; background: #000; border-radius: 10px; white-space: pre-wrap; word-break: break-word; font-size: 14px; max-height: 60vh; overflow: auto; }
|
|
17
|
+
.err { color: #ff6b4a; }
|
|
18
|
+
p { margin: 0 16px; color: #a9a59a; max-width: 760px; }
|
|
19
|
+
</style>
|
|
20
|
+
</head>
|
|
21
|
+
<body>
|
|
22
|
+
<header>
|
|
23
|
+
<h1>WebXR test</h1>
|
|
24
|
+
<button id="enter" disabled>Enter VR</button>
|
|
25
|
+
<button id="copy" class="red" hidden>copy log</button>
|
|
26
|
+
</header>
|
|
27
|
+
<p>The smallest WebXR loop, apart from the 3Draft renderer: a WebGL2 context, an XRWebGLLayer and the session's own
|
|
28
|
+
requestAnimationFrame. In the headset you should see a turning cube 1.5 m in front of you over a grid floor, the left eye
|
|
29
|
+
on a dark red, the right eye on a dark blue background. The log below records the session, the framebuffer, both eyes'
|
|
30
|
+
viewports and every WebGL error; it stays here after you leave VR (and in this browser's storage).</p>
|
|
31
|
+
<canvas id="c"></canvas>
|
|
32
|
+
<pre id="log"></pre>
|
|
33
|
+
<script>
|
|
34
|
+
const logEl = document.getElementById('log');
|
|
35
|
+
const lines = [];
|
|
36
|
+
const t0 = performance.now();
|
|
37
|
+
const log = (msg, err = false) => {
|
|
38
|
+
const line = `${((performance.now() - t0) / 1000).toFixed(2)}s ${msg}`;
|
|
39
|
+
lines.push(line);
|
|
40
|
+
(err ? console.error : console.log)('[xr-test]', msg);
|
|
41
|
+
const div = document.createElement('div');
|
|
42
|
+
div.textContent = line;
|
|
43
|
+
if (err) div.className = 'err';
|
|
44
|
+
logEl.append(div);
|
|
45
|
+
try { localStorage.setItem('xr-test-log', lines.slice(-400).join('\n')); } catch {}
|
|
46
|
+
};
|
|
47
|
+
try {
|
|
48
|
+
const prev = localStorage.getItem('xr-test-log');
|
|
49
|
+
if (prev) log(`(the previous run's log is in localStorage 'xr-test-log', ${prev.split('\n').length} lines)`);
|
|
50
|
+
} catch {}
|
|
51
|
+
log(`user agent: ${navigator.userAgent}`);
|
|
52
|
+
|
|
53
|
+
// ---- WebGL2
|
|
54
|
+
const canvas = document.getElementById('c');
|
|
55
|
+
canvas.width = canvas.height = 480;
|
|
56
|
+
const gl = canvas.getContext('webgl2', { xrCompatible: true, antialias: false, alpha: false });
|
|
57
|
+
if (!gl) log('no WebGL2 context', true);
|
|
58
|
+
else log(`WebGL2: ${gl.getParameter(gl.VERSION)} · ${gl.getParameter(gl.SHADING_LANGUAGE_VERSION)} · renderer ${gl.getParameter(gl.RENDERER)}`);
|
|
59
|
+
canvas.addEventListener('webglcontextlost', (e) => (e.preventDefault(), log('WebGL context LOST', true)));
|
|
60
|
+
canvas.addEventListener('webglcontextrestored', () => log('WebGL context restored (resources must be made again)', true));
|
|
61
|
+
let errCount = 0;
|
|
62
|
+
const glErr = (where) => {
|
|
63
|
+
for (let e = gl.getError(), n = 0; e !== gl.NO_ERROR && n < 8; e = gl.getError(), n++) {
|
|
64
|
+
if (errCount++ < 40) log(`GL error 0x${e.toString(16)} after ${where}`, true);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const vs = `#version 300 es
|
|
69
|
+
in vec3 aPos; in vec3 aCol; uniform mat4 uP, uV, uM; out vec3 vCol;
|
|
70
|
+
void main() { vCol = aCol; gl_Position = uP * uV * uM * vec4(aPos, 1.0); }`;
|
|
71
|
+
const fs = `#version 300 es
|
|
72
|
+
precision highp float; in vec3 vCol; out vec4 o; void main() { o = vec4(vCol, 1.0); }`;
|
|
73
|
+
const shader = (type, src) => {
|
|
74
|
+
const s = gl.createShader(type);
|
|
75
|
+
gl.shaderSource(s, src);
|
|
76
|
+
gl.compileShader(s);
|
|
77
|
+
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) log(`shader: ${gl.getShaderInfoLog(s)}`, true);
|
|
78
|
+
return s;
|
|
79
|
+
};
|
|
80
|
+
const prog = gl.createProgram();
|
|
81
|
+
gl.attachShader(prog, shader(gl.VERTEX_SHADER, vs));
|
|
82
|
+
gl.attachShader(prog, shader(gl.FRAGMENT_SHADER, fs));
|
|
83
|
+
gl.linkProgram(prog);
|
|
84
|
+
if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) log(`link: ${gl.getProgramInfoLog(prog)}`, true);
|
|
85
|
+
const U = { P: gl.getUniformLocation(prog, 'uP'), V: gl.getUniformLocation(prog, 'uV'), M: gl.getUniformLocation(prog, 'uM') };
|
|
86
|
+
// a cube (each face its own colour) and a grid floor, as triangles / lines
|
|
87
|
+
const tris = [], lines3 = [];
|
|
88
|
+
const faces = [[[1, 0, 0], [0.9, 0.3, 0.2]], [[-1, 0, 0], [0.2, 0.8, 0.4]], [[0, 1, 0], [0.95, 0.85, 0.3]], [[0, -1, 0], [0.4, 0.4, 0.45]], [[0, 0, 1], [0.25, 0.55, 0.95]], [[0, 0, -1], [0.85, 0.4, 0.85]]];
|
|
89
|
+
for (const [n, c] of faces) {
|
|
90
|
+
const a = n.findIndex((v) => v !== 0), b = (a + 1) % 3, d = (a + 2) % 3;
|
|
91
|
+
const corner = (s, t) => { const p = [0, 0, 0]; p[a] = n[a] * 0.2; p[b] = s * 0.2; p[d] = t * 0.2; return p; };
|
|
92
|
+
for (const [s, t] of [[-1, -1], [1, -1], [1, 1], [-1, -1], [1, 1], [-1, 1]]) tris.push(...corner(s, t), ...c);
|
|
93
|
+
}
|
|
94
|
+
for (let i = -5; i <= 5; i++) {
|
|
95
|
+
lines3.push(i * 0.5, 0, -2.5, 0.3, 0.6, 0.6, i * 0.5, 0, 2.5, 0.3, 0.6, 0.6);
|
|
96
|
+
lines3.push(-2.5, 0, i * 0.5, 0.3, 0.6, 0.6, 2.5, 0, i * 0.5, 0.3, 0.6, 0.6);
|
|
97
|
+
}
|
|
98
|
+
const vao = (data) => {
|
|
99
|
+
const v = gl.createVertexArray();
|
|
100
|
+
gl.bindVertexArray(v);
|
|
101
|
+
const b = gl.createBuffer();
|
|
102
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, b);
|
|
103
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);
|
|
104
|
+
const aPos = gl.getAttribLocation(prog, 'aPos'), aCol = gl.getAttribLocation(prog, 'aCol');
|
|
105
|
+
gl.enableVertexAttribArray(aPos);
|
|
106
|
+
gl.vertexAttribPointer(aPos, 3, gl.FLOAT, false, 24, 0);
|
|
107
|
+
gl.enableVertexAttribArray(aCol);
|
|
108
|
+
gl.vertexAttribPointer(aCol, 3, gl.FLOAT, false, 24, 12);
|
|
109
|
+
gl.bindVertexArray(null);
|
|
110
|
+
return { v, n: data.length / 6 };
|
|
111
|
+
};
|
|
112
|
+
const cube = vao(tris), grid = vao(lines3);
|
|
113
|
+
glErr('setup');
|
|
114
|
+
|
|
115
|
+
// matrices (column-major)
|
|
116
|
+
const perspective = (fovy, aspect, near, far) => {
|
|
117
|
+
const f = 1 / Math.tan(fovy / 2), nf = 1 / (near - far);
|
|
118
|
+
return [f / aspect, 0, 0, 0, 0, f, 0, 0, 0, 0, (far + near) * nf, -1, 0, 0, 2 * far * near * nf, 0];
|
|
119
|
+
};
|
|
120
|
+
const model = (t, y) => {
|
|
121
|
+
const c = Math.cos(t), s = Math.sin(t);
|
|
122
|
+
return [c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, y, -1.5, 1];
|
|
123
|
+
};
|
|
124
|
+
const translate = (x, y, z) => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1];
|
|
125
|
+
const draw = (P, V, t, floorY) => {
|
|
126
|
+
gl.useProgram(prog);
|
|
127
|
+
gl.uniformMatrix4fv(U.P, false, P);
|
|
128
|
+
gl.uniformMatrix4fv(U.V, false, V);
|
|
129
|
+
gl.uniformMatrix4fv(U.M, false, model(t, floorY + 1.4));
|
|
130
|
+
gl.bindVertexArray(cube.v);
|
|
131
|
+
gl.drawArrays(gl.TRIANGLES, 0, cube.n);
|
|
132
|
+
gl.uniformMatrix4fv(U.M, false, translate(0, floorY, 0));
|
|
133
|
+
gl.bindVertexArray(grid.v);
|
|
134
|
+
gl.drawArrays(gl.LINES, 0, grid.n);
|
|
135
|
+
gl.bindVertexArray(null);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// the page preview (before VR): the same scene from 2 m behind
|
|
139
|
+
let xrSession = null;
|
|
140
|
+
const preview = (now) => {
|
|
141
|
+
if (xrSession) return;
|
|
142
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
143
|
+
gl.viewport(0, 0, 480, 480);
|
|
144
|
+
gl.clearColor(0.08, 0.08, 0.1, 1);
|
|
145
|
+
gl.enable(gl.DEPTH_TEST);
|
|
146
|
+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
|
147
|
+
draw(perspective(1, 1, 0.05, 100), translate(0, -1.6, -1), now / 1000, 0);
|
|
148
|
+
requestAnimationFrame(preview);
|
|
149
|
+
};
|
|
150
|
+
requestAnimationFrame(preview);
|
|
151
|
+
|
|
152
|
+
// ---- WebXR
|
|
153
|
+
const enter = document.getElementById('enter');
|
|
154
|
+
const copyBtn = document.getElementById('copy');
|
|
155
|
+
copyBtn.hidden = false;
|
|
156
|
+
copyBtn.onclick = () => navigator.clipboard?.writeText(lines.join('\n')).then(() => log('log copied'), (e) => log(`copy failed: ${e.message}`, true));
|
|
157
|
+
(async () => {
|
|
158
|
+
if (!navigator.xr) return log('navigator.xr is missing: no WebXR in this browser', true);
|
|
159
|
+
const vr = await navigator.xr.isSessionSupported('immersive-vr').catch((e) => (log(`isSessionSupported failed: ${e.message}`, true), false));
|
|
160
|
+
log(`immersive-vr supported: ${vr}`);
|
|
161
|
+
enter.disabled = !vr;
|
|
162
|
+
})();
|
|
163
|
+
let refSpace = null, layer = null, frames = 0, floor = 0;
|
|
164
|
+
const onFrame = (t, frame) => {
|
|
165
|
+
const session = frame.session;
|
|
166
|
+
session.requestAnimationFrame(onFrame);
|
|
167
|
+
frames++;
|
|
168
|
+
const pose = frame.getViewerPose(refSpace);
|
|
169
|
+
if (!pose) {
|
|
170
|
+
if (frames < 5 || frames % 300 === 0) log(`frame ${frames}: no viewer pose (tracking lost?)`);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const L = session.renderState.baseLayer;
|
|
174
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, L.framebuffer);
|
|
175
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
176
|
+
gl.clearColor(0.05, 0.05, 0.07, 1);
|
|
177
|
+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
|
178
|
+
gl.enable(gl.DEPTH_TEST);
|
|
179
|
+
if (frames <= 3) log(`frame ${frames}: ${pose.views.length} views · framebuffer ${L.framebufferWidth}×${L.framebufferHeight} · emulatedPosition ${pose.emulatedPosition}`);
|
|
180
|
+
pose.views.forEach((view, i) => {
|
|
181
|
+
const vp = L.getViewport(view);
|
|
182
|
+
if (!vp) return frames <= 3 && log(`frame ${frames}: no viewport for view ${i} (${view.eye})`, true);
|
|
183
|
+
gl.viewport(vp.x, vp.y, vp.width, vp.height);
|
|
184
|
+
gl.enable(gl.SCISSOR_TEST);
|
|
185
|
+
gl.scissor(vp.x, vp.y, vp.width, vp.height);
|
|
186
|
+
const left = view.eye !== 'right';
|
|
187
|
+
gl.clearColor(left ? 0.25 : 0.05, 0.05, left ? 0.05 : 0.25, 1); // left eye red, right eye blue
|
|
188
|
+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
|
189
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
190
|
+
draw(view.projectionMatrix, view.transform.inverse.matrix, t / 1000, floor);
|
|
191
|
+
if (frames <= 3) log(` ${view.eye} eye: viewport ${vp.x},${vp.y} ${vp.width}×${vp.height}`);
|
|
192
|
+
});
|
|
193
|
+
glErr(`frame ${frames}`);
|
|
194
|
+
if (frames === 90) log('90 frames drawn (about a second and a half): the loop runs');
|
|
195
|
+
};
|
|
196
|
+
const start = async () => {
|
|
197
|
+
if (xrSession) return xrSession.end();
|
|
198
|
+
let session;
|
|
199
|
+
try {
|
|
200
|
+
session = await navigator.xr.requestSession('immersive-vr', { optionalFeatures: ['local-floor'] });
|
|
201
|
+
} catch (e) {
|
|
202
|
+
return log(`requestSession failed: ${e.name}: ${e.message}`, true);
|
|
203
|
+
}
|
|
204
|
+
xrSession = session;
|
|
205
|
+
enter.textContent = 'Exit VR';
|
|
206
|
+
enter.classList.add('red');
|
|
207
|
+
log(`session created · blend ${session.environmentBlendMode} · visibility ${session.visibilityState} · frame rate ${session.frameRate ?? '?'}`);
|
|
208
|
+
session.addEventListener('visibilitychange', () => log(`visibility: ${session.visibilityState}`));
|
|
209
|
+
session.addEventListener('end', () => {
|
|
210
|
+
log(`session ended after ${frames} frames`);
|
|
211
|
+
xrSession = null;
|
|
212
|
+
frames = 0;
|
|
213
|
+
enter.textContent = 'Enter VR';
|
|
214
|
+
enter.classList.remove('red');
|
|
215
|
+
requestAnimationFrame(preview);
|
|
216
|
+
});
|
|
217
|
+
session.addEventListener('selectstart', (e) => log(`select (${e.inputSource.handedness})`));
|
|
218
|
+
try {
|
|
219
|
+
await gl.makeXRCompatible();
|
|
220
|
+
log('makeXRCompatible: ok');
|
|
221
|
+
} catch (e) {
|
|
222
|
+
log(`makeXRCompatible failed: ${e.message}`, true);
|
|
223
|
+
}
|
|
224
|
+
try {
|
|
225
|
+
layer = new XRWebGLLayer(session, gl, { antialias: true, depth: true });
|
|
226
|
+
session.updateRenderState({ baseLayer: layer, depthNear: 0.05, depthFar: 1000 });
|
|
227
|
+
log(`XRWebGLLayer: framebuffer ${layer.framebufferWidth}×${layer.framebufferHeight} · antialias ${layer.antialias} · ignoreDepthValues ${layer.ignoreDepthValues} · fixedFoveation ${layer.fixedFoveation ?? '-'}`);
|
|
228
|
+
} catch (e) {
|
|
229
|
+
log(`XRWebGLLayer failed: ${e.message}`, true);
|
|
230
|
+
return session.end();
|
|
231
|
+
}
|
|
232
|
+
try {
|
|
233
|
+
refSpace = await session.requestReferenceSpace('local-floor');
|
|
234
|
+
floor = 0;
|
|
235
|
+
log('reference space: local-floor');
|
|
236
|
+
} catch {
|
|
237
|
+
refSpace = await session.requestReferenceSpace('local');
|
|
238
|
+
floor = -1.6;
|
|
239
|
+
log('reference space: local (local-floor refused); the floor is put 1.6 m below the head');
|
|
240
|
+
}
|
|
241
|
+
glErr('session setup');
|
|
242
|
+
session.requestAnimationFrame(onFrame);
|
|
243
|
+
log('session.requestAnimationFrame: asked for the first frame');
|
|
244
|
+
};
|
|
245
|
+
enter.onclick = start;
|
|
246
|
+
</script>
|
|
247
|
+
</body>
|
|
248
|
+
</html>
|