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/nginx.conf
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "spi3der",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Browser-native 3D scene building, WebXR and ViRtuSH, based on 3Draft",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"spi3der": "./bin/spi3der.mjs"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"start": "node bin/spi3der.mjs",
|
|
12
|
+
"test": "node tests/math.mjs && node tests/smoke.mjs"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"api/",
|
|
16
|
+
"mcp/",
|
|
17
|
+
"bin/",
|
|
18
|
+
"engine/",
|
|
19
|
+
"icons/",
|
|
20
|
+
"media/",
|
|
21
|
+
"models/",
|
|
22
|
+
"src/",
|
|
23
|
+
"index.html",
|
|
24
|
+
"player.html",
|
|
25
|
+
"emulator.html",
|
|
26
|
+
"sky.html",
|
|
27
|
+
"xr-test.html",
|
|
28
|
+
"xr-compare.html",
|
|
29
|
+
"sky.md",
|
|
30
|
+
"help.md",
|
|
31
|
+
"PLAYER.md",
|
|
32
|
+
"APP.md",
|
|
33
|
+
"MCP.md",
|
|
34
|
+
"favicon.svg",
|
|
35
|
+
"manifest.webmanifest",
|
|
36
|
+
"sw.js",
|
|
37
|
+
"Dockerfile",
|
|
38
|
+
"nginx.conf",
|
|
39
|
+
"README.md"
|
|
40
|
+
],
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@vercel/blob": "^2.3.0"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/player.html
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
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>Spider Player</title>
|
|
7
|
+
<!--
|
|
8
|
+
Spider Player: a scene (a 3Draft .scene.json) turning on a turntable round its middle, or a story (a Spider Build v2
|
|
9
|
+
.build.json) played on its own timeline and camera. No UI at all: it is meant for an <iframe>. Controlled by its URL and
|
|
10
|
+
by postMessage commands; it reports back the same way. PLAYER.md has the whole interface.
|
|
11
|
+
Only the renderer, the scene loading and the story format are loaded (about 50-60 kB gzipped), not the editor.
|
|
12
|
+
-->
|
|
13
|
+
<style>
|
|
14
|
+
html, body { margin: 0; height: 100%; overflow: hidden; background: transparent; }
|
|
15
|
+
canvas { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); display: block; }
|
|
16
|
+
</style>
|
|
17
|
+
</head>
|
|
18
|
+
<body>
|
|
19
|
+
<canvas id="view"></canvas>
|
|
20
|
+
<script type="module">
|
|
21
|
+
import { createGl } from './engine/8fa668821759/gl.js';
|
|
22
|
+
import { orbit } from './engine/8fa668821759/camera.js';
|
|
23
|
+
import { merge, bounds } from './engine/8fa668821759/mesh.js';
|
|
24
|
+
import { instantiate } from './engine/8fa668821759/instances.js';
|
|
25
|
+
import * as storyFormat from './models/spider-build-v2.mjs';
|
|
26
|
+
|
|
27
|
+
const canvas = document.getElementById('view');
|
|
28
|
+
const q = new URLSearchParams(location.search);
|
|
29
|
+
const RAD = Math.PI / 180;
|
|
30
|
+
const num = (v, d) => (v !== null && v !== undefined && v !== '' && Number.isFinite(Number(v)) ? Number(v) : d);
|
|
31
|
+
const flag = (v, d) => (v === null || v === undefined ? d : !/^(0|false|no|off)$/i.test(v));
|
|
32
|
+
|
|
33
|
+
// the materials a face may name without the scene defining them (the viewer's own palette)
|
|
34
|
+
const BASE = {
|
|
35
|
+
plaster: { color: '#e9e4d8', rough: 0.9 }, sand: { color: '#c9b79a', rough: 0.95 }, wood: { color: '#8a5a36', rough: 0.8 },
|
|
36
|
+
'dark-wood': { color: '#5a3d2b', rough: 0.7 }, concrete: { color: '#8c8c88', rough: 1 }, slate: { color: '#4f5358', rough: 0.6 },
|
|
37
|
+
grass: { color: '#7d9b4e', rough: 1 }, brick: { color: '#b5473a', rough: 0.85 }, steel: { color: '#9aa7b4', metal: 1, rough: 0.3 },
|
|
38
|
+
gold: { color: '#d9b04a', metal: 1, rough: 0.25 }, glass: { color: '#a9d3e8', metal: 0.2, rough: 0.05, alpha: 0.3 },
|
|
39
|
+
copper: { color: '#c87533', metal: 1, rough: 0.3 }, 'ruby-glass': { color: '#c0263a', metal: 0.2, rough: 0.05, alpha: 0.45 },
|
|
40
|
+
'lacquer-blue': { color: '#2257b8', metal: 0.15, rough: 0.12 }, lamp: { color: '#ffd36b', rough: 0.5, emit: 1 },
|
|
41
|
+
monitor: { color: '#101418', metal: 0.1, rough: 0.3, emit: 0.3 },
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// ---- state
|
|
45
|
+
const S = {
|
|
46
|
+
url: null, // what is loaded
|
|
47
|
+
kind: null, // 'scene' | 'story'
|
|
48
|
+
mesh: null, parts: null, palette: { ...BASE }, center: [0, 0, 0], radius: 1,
|
|
49
|
+
doc: null, t: 0, playing: flag(q.get('autoplay'), true), rate: num(q.get('speed'), 1), loop: flag(q.get('loop'), true),
|
|
50
|
+
loadedKey: null, skin: null, camera: q.get('camera') === 'orbit' ? 'orbit' : 'auto', // auto: a story's own camera, else the turntable
|
|
51
|
+
spin: num(q.get('spin'), 20), // degrees per second (0: still; negative: the other way)
|
|
52
|
+
yaw: num(q.get('yaw'), 30) * RAD, pitch: num(q.get('pitch'), 20) * RAD, zoom: num(q.get('zoom'), 1),
|
|
53
|
+
bg: null, transparent: false, smooth: flag(q.get('smooth'), true), studio: flag(q.get('studio'), true),
|
|
54
|
+
last: 0, endedSent: false, lastCaption: '', timeSentAt: 0,
|
|
55
|
+
};
|
|
56
|
+
const setBackground = (v) => {
|
|
57
|
+
const s = String(v ?? 'transparent').trim().replace(/^#/, '');
|
|
58
|
+
if (!s || s === 'transparent' || s === 'none') {
|
|
59
|
+
S.transparent = true;
|
|
60
|
+
document.body.style.background = 'transparent';
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const hex = /^[0-9a-f]{3}$/i.test(s) ? s.replace(/./g, '$&$&') : s;
|
|
64
|
+
if (!/^[0-9a-f]{6}$/i.test(hex)) throw new Error(`background: "${v}" is not transparent or a #rrggbb colour`);
|
|
65
|
+
S.transparent = false;
|
|
66
|
+
S.bg = [0, 2, 4].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255);
|
|
67
|
+
document.body.style.background = `#${hex}`;
|
|
68
|
+
};
|
|
69
|
+
setBackground(q.get('bg') ?? 'transparent');
|
|
70
|
+
|
|
71
|
+
// ---- talking to the page round it: events out, commands in
|
|
72
|
+
const origin = q.get('origin') || '*'; // (the embedding page's origin: give it to keep the events to that page only)
|
|
73
|
+
const emit = (type, data = {}) => {
|
|
74
|
+
const msg = { source: 'spider-player', type, ...data };
|
|
75
|
+
if (window.parent !== window) window.parent.postMessage(msg, origin);
|
|
76
|
+
window.dispatchEvent(new CustomEvent('spider-player', { detail: msg }));
|
|
77
|
+
};
|
|
78
|
+
const fail = (err) => (console.warn('Spider Player:', err), emit('error', { message: String(err?.message ?? err) }));
|
|
79
|
+
|
|
80
|
+
// ---- loading
|
|
81
|
+
const sceneObjects = (data) => {
|
|
82
|
+
const protos = data.prototypes ?? {};
|
|
83
|
+
return (data.objects ?? [])
|
|
84
|
+
.filter((o) => !o.hidden)
|
|
85
|
+
.map((o) => {
|
|
86
|
+
if (o.proto && protos[o.proto]) {
|
|
87
|
+
const lods = protos[o.proto].lods ?? [protos[o.proto]];
|
|
88
|
+
return { name: o.name, mesh: instantiate(lods[0], { at: o.at, scale: o.scale, mats: o.mats, color: o.color, rot: o.rot }) };
|
|
89
|
+
}
|
|
90
|
+
return { name: o.name, mesh: o.mesh };
|
|
91
|
+
})
|
|
92
|
+
.filter((o) => o.mesh?.positions?.length && o.mesh.faces?.length);
|
|
93
|
+
};
|
|
94
|
+
const usePalette = (list) => {
|
|
95
|
+
S.palette = { ...BASE };
|
|
96
|
+
for (const m of Array.isArray(list) ? list : []) if (m?.name) {
|
|
97
|
+
const { name, ...rest } = m;
|
|
98
|
+
S.palette[name] = rest;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const show = (objects, frame) => {
|
|
102
|
+
S.parts = objects.map((o) => o.mesh);
|
|
103
|
+
S.mesh = S.parts.length ? merge(...S.parts) : { positions: [], faces: [] };
|
|
104
|
+
if (frame && S.parts.length) {
|
|
105
|
+
const b = bounds(S.mesh);
|
|
106
|
+
S.center = b.center;
|
|
107
|
+
S.radius = b.radius || 1;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const load = async (src) => {
|
|
111
|
+
let data = src;
|
|
112
|
+
let url = null;
|
|
113
|
+
if (typeof src === 'string') {
|
|
114
|
+
url = new URL(src, location.href).href;
|
|
115
|
+
const r = await fetch(url);
|
|
116
|
+
if (!r.ok) throw new Error(`${src}: ${r.status} ${r.statusText}`);
|
|
117
|
+
data = await r.json();
|
|
118
|
+
}
|
|
119
|
+
S.url = url;
|
|
120
|
+
S.loadedKey = null;
|
|
121
|
+
S.skin = null;
|
|
122
|
+
S.endedSent = false;
|
|
123
|
+
S.lastCaption = '';
|
|
124
|
+
if (data?.format === storyFormat.FORMAT) {
|
|
125
|
+
const doc = storyFormat.normalizeBuildDocument(data);
|
|
126
|
+
S.kind = 'story';
|
|
127
|
+
S.doc = doc;
|
|
128
|
+
S.t = 0;
|
|
129
|
+
usePalette(doc.scene.palette);
|
|
130
|
+
// the turntable's middle: the finished model (the story's last moment)
|
|
131
|
+
show(storyFormat.constructionAt(doc, doc.timeline.durationMs), true);
|
|
132
|
+
storyAt(0, true);
|
|
133
|
+
} else if (Array.isArray(data?.objects)) {
|
|
134
|
+
S.kind = 'scene';
|
|
135
|
+
S.doc = null;
|
|
136
|
+
usePalette(data.palette);
|
|
137
|
+
show(sceneObjects(data), true);
|
|
138
|
+
} else throw new Error('not a 3Draft scene (.scene.json) or a Spider Build story (.build.json)');
|
|
139
|
+
emit('loaded', state());
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// a story at a moment: its objects (rebuilt only when a construction step is passed), its animation, its captions
|
|
143
|
+
const storyAt = (t, force = false) => {
|
|
144
|
+
const doc = S.doc;
|
|
145
|
+
S.t = Math.max(0, Math.min(t, doc.timeline.durationMs));
|
|
146
|
+
const index = storyFormat.constructionIndexAtTime(doc, S.t);
|
|
147
|
+
if (force || index !== S.loadedKey) {
|
|
148
|
+
const objects = storyFormat.constructionAt(doc, S.t);
|
|
149
|
+
S.skin = storyFormat.animationSkin(doc, objects);
|
|
150
|
+
show(S.skin ? objects.filter((o) => !S.skin.names.has(o.name)) : objects, false);
|
|
151
|
+
S.loadedKey = index;
|
|
152
|
+
}
|
|
153
|
+
if (S.skin) storyFormat.animationAt(doc, S.t, S.skin.bones, S.skin.emission);
|
|
154
|
+
const cues = storyFormat.narrationAt(doc, Math.min(S.t, Math.max(0, doc.timeline.durationMs - 0.001)));
|
|
155
|
+
const text = cues.map((c) => c.text).filter(Boolean).join('\n');
|
|
156
|
+
if (text !== S.lastCaption) ((S.lastCaption = text), emit('caption', { text, cues: cues.map((c) => ({ channel: c.channel, text: c.text ?? '' })) }));
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// ---- drawing
|
|
160
|
+
let gpu = null;
|
|
161
|
+
const view = () => {
|
|
162
|
+
const w = innerWidth, h = innerHeight;
|
|
163
|
+
const size = Math.max(w, h); // the square canvas covers the frame; the camera backs off to keep the model in the short side
|
|
164
|
+
const cover = size / Math.max(1, Math.min(w, h));
|
|
165
|
+
if (S.kind === 'story' && S.camera === 'auto') {
|
|
166
|
+
const c = storyFormat.cameraAt(S.doc, S.t);
|
|
167
|
+
const v = orbit({ yaw: c.yaw * RAD, pitch: c.pitch * RAD, roll: (c.roll ?? 0) * RAD });
|
|
168
|
+
const d = 1e-3;
|
|
169
|
+
const fov = 2 * Math.atan(Math.tan(((c.fov ?? 35) * RAD) / 2) * cover);
|
|
170
|
+
return { size, v, fit: { center: c.eye.map((e, i) => e - v.dir[i] * d), radius: 1, dist: d, fov } };
|
|
171
|
+
}
|
|
172
|
+
const v = orbit({ yaw: S.yaw, pitch: S.pitch });
|
|
173
|
+
return { size, v, fit: { center: S.center, radius: (S.radius * cover) / Math.max(0.05, S.zoom) } };
|
|
174
|
+
};
|
|
175
|
+
const draw = () => {
|
|
176
|
+
if (!gpu || !S.mesh) return;
|
|
177
|
+
const { size, v, fit } = view();
|
|
178
|
+
canvas.style.width = canvas.style.height = `${size}px`;
|
|
179
|
+
gpu.render(S.mesh, v, {
|
|
180
|
+
size, fit, dpr: Math.min(2, devicePixelRatio || 1), palette: S.palette, parts: S.parts,
|
|
181
|
+
transparent: S.transparent, background: S.bg ?? [0, 0, 0], smooth: S.smooth ? 40 : 0, studio: S.studio,
|
|
182
|
+
...(S.skin ? { skin: { meshes: S.skin.meshes, weights: S.skin.weights, bones: S.skin.bones, emission: S.skin.emission } } : {}),
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
const frame = (now) => {
|
|
186
|
+
const dt = S.last ? Math.min(0.1, (now - S.last) / 1000) : 0;
|
|
187
|
+
S.last = now;
|
|
188
|
+
try {
|
|
189
|
+
if (S.kind === 'story' && S.playing) {
|
|
190
|
+
const end = S.doc.timeline.durationMs;
|
|
191
|
+
let t = S.t + dt * 1000 * S.rate;
|
|
192
|
+
if (t >= end) {
|
|
193
|
+
if (S.loop) ((t = 0), (S.endedSent = false));
|
|
194
|
+
else ((t = end), (S.playing = false));
|
|
195
|
+
if (!S.endedSent) ((S.endedSent = true), emit('ended', { loop: S.loop }));
|
|
196
|
+
}
|
|
197
|
+
storyAt(t);
|
|
198
|
+
if (now - S.timeSentAt > 250) ((S.timeSentAt = now), emit('time', { t: Math.round(S.t), durationMs: end }));
|
|
199
|
+
}
|
|
200
|
+
if (S.kind === 'scene' || S.camera === 'orbit') S.yaw += S.spin * RAD * dt;
|
|
201
|
+
draw();
|
|
202
|
+
} catch (err) {
|
|
203
|
+
fail(err);
|
|
204
|
+
}
|
|
205
|
+
requestAnimationFrame(frame);
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// ---- the commands
|
|
209
|
+
const state = () => ({
|
|
210
|
+
url: S.url, kind: S.kind, playing: S.playing, t: Math.round(S.t), durationMs: S.doc?.timeline.durationMs ?? 0, speed: S.rate, loop: S.loop,
|
|
211
|
+
camera: S.camera, spin: S.spin, yaw: +(S.yaw / RAD).toFixed(2), pitch: +(S.pitch / RAD).toFixed(2), zoom: S.zoom,
|
|
212
|
+
background: S.transparent ? 'transparent' : `#${S.bg.map((c) => Math.round(c * 255).toString(16).padStart(2, '0')).join('')}`,
|
|
213
|
+
objects: S.parts?.length ?? 0, title: S.doc?.metadata?.title ?? null,
|
|
214
|
+
});
|
|
215
|
+
const needStory = (cmd) => {
|
|
216
|
+
if (S.kind !== 'story') throw new Error(`${cmd}: only a story (.build.json) has a timeline`);
|
|
217
|
+
};
|
|
218
|
+
const COMMANDS = {
|
|
219
|
+
load: async (m) => load(m.url ?? m.data),
|
|
220
|
+
play: () => (needStory('play'), S.t >= S.doc.timeline.durationMs && (S.t = 0), (S.playing = true), (S.endedSent = false)),
|
|
221
|
+
pause: () => (needStory('pause'), (S.playing = false)),
|
|
222
|
+
toggle: () => (needStory('toggle'), (S.playing = !S.playing)),
|
|
223
|
+
seek: (m) => (needStory('seek'), storyAt(num(m.t, 0), false), (S.endedSent = false)),
|
|
224
|
+
speed: (m) => (S.rate = Math.max(0, Math.min(16, num(m.value, 1)))),
|
|
225
|
+
loop: (m) => (S.loop = Boolean(m.value)),
|
|
226
|
+
camera: (m) => {
|
|
227
|
+
if (!['auto', 'orbit'].includes(m.value)) throw new Error('camera: "auto" (a story\'s own camera) or "orbit" (the turntable)');
|
|
228
|
+
S.camera = m.value;
|
|
229
|
+
},
|
|
230
|
+
spin: (m) => (S.spin = num(m.value, 20)),
|
|
231
|
+
yaw: (m) => (S.yaw = num(m.value, 0) * RAD),
|
|
232
|
+
pitch: (m) => (S.pitch = Math.max(-89, Math.min(89, num(m.value, 20))) * RAD),
|
|
233
|
+
zoom: (m) => (S.zoom = Math.max(0.05, Math.min(50, num(m.value, 1)))),
|
|
234
|
+
background: (m) => setBackground(m.value),
|
|
235
|
+
smooth: (m) => (S.smooth = Boolean(m.value)),
|
|
236
|
+
studio: (m) => (S.studio = Boolean(m.value)),
|
|
237
|
+
state: () => {},
|
|
238
|
+
};
|
|
239
|
+
const run = async (m) => {
|
|
240
|
+
const cmd = m?.cmd;
|
|
241
|
+
if (!COMMANDS[cmd]) throw new Error(`unknown command "${cmd}" (known: ${Object.keys(COMMANDS).join(', ')})`);
|
|
242
|
+
await COMMANDS[cmd](m);
|
|
243
|
+
return state();
|
|
244
|
+
};
|
|
245
|
+
window.addEventListener('message', async (e) => {
|
|
246
|
+
const m = e.data;
|
|
247
|
+
if (!m || typeof m !== 'object' || m.target !== 'spider-player') return;
|
|
248
|
+
try {
|
|
249
|
+
const st = await run(m);
|
|
250
|
+
emit('state', { ...st, id: m.id ?? null, cmd: m.cmd });
|
|
251
|
+
} catch (err) {
|
|
252
|
+
emit('error', { message: String(err?.message ?? err), id: m.id ?? null, cmd: m.cmd });
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
// the same commands for a script on the same page (or same origin): spiderPlayer.send({ cmd: 'pause' })
|
|
256
|
+
window.spiderPlayer = { send: (m) => run(m), state };
|
|
257
|
+
|
|
258
|
+
// ---- start
|
|
259
|
+
try {
|
|
260
|
+
gpu = await createGl(canvas, { onError: (msg) => fail(msg) });
|
|
261
|
+
} catch (err) {
|
|
262
|
+
fail(`WebGL2 is not available: ${err?.message ?? err}`);
|
|
263
|
+
}
|
|
264
|
+
const first = q.get('scene') ?? q.get('src');
|
|
265
|
+
try {
|
|
266
|
+
if (first) await load(first);
|
|
267
|
+
} catch (err) {
|
|
268
|
+
fail(err);
|
|
269
|
+
}
|
|
270
|
+
emit('ready', state());
|
|
271
|
+
requestAnimationFrame(frame);
|
|
272
|
+
</script>
|
|
273
|
+
</body>
|
|
274
|
+
</html>
|
package/sky.html
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
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>Sky guide</title>
|
|
7
|
+
<meta name="description" content="A universe browser with nothing to install: how the viewer handles stars, planets, black holes and galaxies, and how to make your own space scene (by hand or with an AI).">
|
|
8
|
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
9
|
+
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;600&family=Inter:wght@400;600;700&display=swap" rel="stylesheet">
|
|
10
|
+
<style>
|
|
11
|
+
/* the sky guide: sky.md shown as a page (opens in its own tab from the viewer's help) */
|
|
12
|
+
:root {
|
|
13
|
+
--bg: #0d0e0b; --panel: #15170f; --line: #2a2d22; --text: #d9dbcf; --muted: #8a8d80; --cream: #f2eed8; --teal: #2fb8c0; --code: #1b1e15;
|
|
14
|
+
--font: 'Inter', system-ui, sans-serif; --font-mono: 'JetBrains Mono', ui-monospace, monospace;
|
|
15
|
+
}
|
|
16
|
+
* { box-sizing: border-box; }
|
|
17
|
+
html { scroll-behavior: smooth; }
|
|
18
|
+
body { margin: 0; background: var(--bg); color: var(--text); font: 400 16px/1.6 var(--font); }
|
|
19
|
+
/* a few stars behind the page */
|
|
20
|
+
body::before { content: ''; position: fixed; inset: 0; pointer-events: none; opacity: 0.55; background-image:
|
|
21
|
+
radial-gradient(1px 1px at 12% 18%, #fff 50%, transparent 51%), radial-gradient(1px 1px at 78% 12%, #cfe0ff 50%, transparent 51%),
|
|
22
|
+
radial-gradient(1.5px 1.5px at 44% 64%, #ffe2b8 50%, transparent 51%), radial-gradient(1px 1px at 90% 80%, #fff 50%, transparent 51%),
|
|
23
|
+
radial-gradient(1px 1px at 25% 88%, #cfe0ff 50%, transparent 51%), radial-gradient(1px 1px at 63% 36%, #fff 50%, transparent 51%); }
|
|
24
|
+
.wrap { position: relative; display: grid; grid-template-columns: 240px minmax(0, 1fr); gap: 48px; max-width: 1180px; margin: 0 auto; padding: 40px 24px 96px; }
|
|
25
|
+
nav { position: sticky; top: 24px; align-self: start; max-height: calc(100vh - 48px); overflow: auto; font: 500 13px/1.5 var(--font-mono); }
|
|
26
|
+
nav .brand { color: var(--cream); font: 700 15px var(--font); letter-spacing: 0.02em; margin-bottom: 16px; display: block; text-decoration: none; }
|
|
27
|
+
nav .brand small { color: var(--muted); font: 500 11px var(--font-mono); letter-spacing: 0.12em; margin-left: 6px; }
|
|
28
|
+
nav a { display: block; color: var(--muted); text-decoration: none; padding: 3px 0 3px 10px; border-left: 2px solid transparent; }
|
|
29
|
+
nav a.h3 { padding-left: 22px; font-size: 12px; }
|
|
30
|
+
nav a:hover, nav a.on { color: var(--cream); border-left-color: var(--teal); }
|
|
31
|
+
main { min-width: 0; }
|
|
32
|
+
h1 { color: var(--cream); font: 700 clamp(28px, 4vw, 40px)/1.15 var(--font); margin: 0 0 18px; letter-spacing: -0.01em; }
|
|
33
|
+
h2 { color: var(--cream); font: 700 24px/1.25 var(--font); margin: 56px 0 14px; padding-top: 18px; border-top: 1px solid var(--line); }
|
|
34
|
+
h3 { color: var(--cream); font: 600 18px/1.3 var(--font); margin: 32px 0 10px; }
|
|
35
|
+
p, li { max-width: 72ch; }
|
|
36
|
+
a { color: var(--teal); }
|
|
37
|
+
strong { color: var(--cream); }
|
|
38
|
+
code { font: 400 0.88em var(--font-mono); background: var(--code); border: 1px solid var(--line); border-radius: 4px; padding: 1px 5px; color: var(--cream); }
|
|
39
|
+
pre { background: var(--code); border: 1px solid var(--line); border-radius: 8px; padding: 14px 16px; overflow: auto; font: 400 13px/1.55 var(--font-mono); }
|
|
40
|
+
pre code { background: none; border: 0; padding: 0; color: var(--text); }
|
|
41
|
+
ul, ol { padding-left: 22px; }
|
|
42
|
+
.table { overflow-x: auto; margin: 14px 0; }
|
|
43
|
+
table { border-collapse: collapse; font-size: 14px; min-width: 420px; }
|
|
44
|
+
th, td { text-align: left; vertical-align: top; padding: 7px 12px; border-bottom: 1px solid var(--line); }
|
|
45
|
+
th { color: var(--muted); font: 600 11px var(--font-mono); letter-spacing: 0.08em; text-transform: uppercase; }
|
|
46
|
+
td:first-child { white-space: nowrap; }
|
|
47
|
+
.launch { display: flex; flex-wrap: wrap; gap: 10px; margin: 22px 0 6px; }
|
|
48
|
+
.launch a { display: inline-block; padding: 10px 16px; border-radius: 8px; background: var(--teal); color: #071315; font: 600 14px var(--font-mono); text-decoration: none; }
|
|
49
|
+
.launch a + a { background: var(--panel); color: var(--cream); border: 1px solid var(--line); }
|
|
50
|
+
.err { color: #ff8a6a; }
|
|
51
|
+
@media (max-width: 860px) { .wrap { grid-template-columns: 1fr; gap: 0; padding: 24px 16px 72px; } nav { position: static; max-height: none; margin-bottom: 20px; } nav a.h3 { display: none; } }
|
|
52
|
+
</style>
|
|
53
|
+
</head>
|
|
54
|
+
<body>
|
|
55
|
+
<div class="wrap">
|
|
56
|
+
<nav id="toc"><a class="brand" href="./">Sky guide<small>3DRAFT</small></a></nav>
|
|
57
|
+
<main id="doc"><p>loading the guide…</p></main>
|
|
58
|
+
</div>
|
|
59
|
+
<script type="module">
|
|
60
|
+
// Markdown -> HTML: headings (with anchors), paragraphs, lists, tables, fenced code, inline code, bold, italic, links.
|
|
61
|
+
// The text is escaped first. External links open in a new tab; links back into the viewer stay in this one.
|
|
62
|
+
const esc = (t) => t.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
63
|
+
const slug = (t) => t.toLowerCase().replace(/<[^>]+>/g, '').replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
|
|
64
|
+
const inline = (t) => {
|
|
65
|
+
const codes = [];
|
|
66
|
+
t = esc(t).replace(/`([^`]+)`/g, (_, c) => `\u0000${codes.push(c) - 1}\u0000`);
|
|
67
|
+
t = t.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>').replace(/\*([^*]+)\*/g, '<em>$1</em>')
|
|
68
|
+
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_, l, h) => `<a href="${h}"${/^https?:/.test(h) ? ' target="_blank" rel="noopener"' : ''}>${l}</a>`);
|
|
69
|
+
return t.replace(/\u0000(\d+)\u0000/g, (_, i) => `<code>${codes[i]}</code>`);
|
|
70
|
+
};
|
|
71
|
+
const md = (src) => {
|
|
72
|
+
const out = [], toc = [];
|
|
73
|
+
const lines = src.replace(/\r/g, '').split('\n');
|
|
74
|
+
for (let i = 0; i < lines.length; i++) {
|
|
75
|
+
const l = lines[i];
|
|
76
|
+
let m;
|
|
77
|
+
if (!l.trim()) continue;
|
|
78
|
+
if (l.startsWith('```')) {
|
|
79
|
+
const code = [];
|
|
80
|
+
while (++i < lines.length && !lines[i].startsWith('```')) code.push(lines[i]);
|
|
81
|
+
out.push(`<pre><code>${esc(code.join('\n'))}</code></pre>`);
|
|
82
|
+
} else if ((m = l.match(/^(#{1,3}) (.*)/))) {
|
|
83
|
+
const n = m[1].length, h = inline(m[2]), id = slug(h);
|
|
84
|
+
if (n > 1) toc.push([n, id, h.replace(/<[^>]+>/g, '')]);
|
|
85
|
+
out.push(`<h${n} id="${id}">${h}</h${n}>`);
|
|
86
|
+
} else if (l.startsWith('|')) {
|
|
87
|
+
const rows = [];
|
|
88
|
+
while (i < lines.length && lines[i].startsWith('|')) rows.push(lines[i++]);
|
|
89
|
+
i--;
|
|
90
|
+
const cells = (r) => r.replace(/^\||\|$/g, '').split('|').map((c) => inline(c.trim()));
|
|
91
|
+
const [head, , ...body] = rows;
|
|
92
|
+
out.push(`<div class="table"><table><thead><tr>${cells(head).map((c) => `<th>${c}</th>`).join('')}</tr></thead><tbody>${body.map((r) => `<tr>${cells(r).map((c) => `<td>${c}</td>`).join('')}</tr>`).join('')}</tbody></table></div>`);
|
|
93
|
+
} else if (/^[-*] /.test(l)) {
|
|
94
|
+
const items = [];
|
|
95
|
+
while (i < lines.length && /^[-*] /.test(lines[i])) items.push(lines[i++].slice(2));
|
|
96
|
+
i--;
|
|
97
|
+
// the first list (the scene links under the title) becomes buttons
|
|
98
|
+
if (!out.some((x) => x.startsWith('<ul') || x.startsWith('<div class="launch"')) && items.every((t) => /^\[.*\]\(\.\/\?scene=/.test(t)))
|
|
99
|
+
out.push(`<div class="launch">${items.map((t) => inline(t.match(/^\[[^\]]+\]\([^)]+\)/)[0])).join('')}</div><ul>${items.map((t) => `<li>${inline(t)}</li>`).join('')}</ul>`);
|
|
100
|
+
else out.push(`<ul>${items.map((t) => `<li>${inline(t)}</li>`).join('')}</ul>`);
|
|
101
|
+
} else if (/^\d+\. /.test(l)) {
|
|
102
|
+
const items = [];
|
|
103
|
+
while (i < lines.length && /^\d+\. /.test(lines[i])) items.push(lines[i++].replace(/^\d+\. /, ''));
|
|
104
|
+
i--;
|
|
105
|
+
out.push(`<ol>${items.map((t) => `<li>${inline(t)}</li>`).join('')}</ol>`);
|
|
106
|
+
} else {
|
|
107
|
+
const para = [l];
|
|
108
|
+
while (i + 1 < lines.length && lines[i + 1].trim() && !/^(#|\||[-*] |\d+\. |```)/.test(lines[i + 1])) para.push(lines[++i]);
|
|
109
|
+
out.push(`<p>${inline(para.join(' '))}</p>`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return { html: out.join('\n'), toc };
|
|
113
|
+
};
|
|
114
|
+
const doc = document.getElementById('doc'), nav = document.getElementById('toc');
|
|
115
|
+
try {
|
|
116
|
+
const res = await fetch('sky.md');
|
|
117
|
+
if (!res.ok) throw new Error(`sky.md: ${res.status}`);
|
|
118
|
+
const { html, toc } = md(await res.text());
|
|
119
|
+
doc.innerHTML = html;
|
|
120
|
+
nav.insertAdjacentHTML('beforeend', toc.map(([n, id, t]) => `<a class="h${n}" href="#${id}">${t}</a>`).join(''));
|
|
121
|
+
// the section in view is marked in the contents
|
|
122
|
+
const links = new Map([...nav.querySelectorAll('a[href^="#"]')].map((a) => [a.getAttribute('href').slice(1), a]));
|
|
123
|
+
const seen = new IntersectionObserver((es) => {
|
|
124
|
+
for (const e of es) if (e.isIntersecting) { for (const a of links.values()) a.classList.remove('on'); links.get(e.target.id)?.classList.add('on'); }
|
|
125
|
+
}, { rootMargin: '0px 0px -70% 0px' });
|
|
126
|
+
for (const h of doc.querySelectorAll('h2, h3')) seen.observe(h);
|
|
127
|
+
if (location.hash) document.getElementById(location.hash.slice(1))?.scrollIntoView();
|
|
128
|
+
} catch (e) {
|
|
129
|
+
doc.innerHTML = `<p class="err">Could not load the guide (${esc(String(e.message ?? e))}).</p>`;
|
|
130
|
+
}
|
|
131
|
+
</script>
|
|
132
|
+
</body>
|
|
133
|
+
</html>
|