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
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
// A sky catalogue (format '3draft-sky'): the easy way to make a space scene. A list of stars, planets, moons, black holes and
|
|
2
|
+
// galaxies, in the units astronomers use (RA / Dec / distance, parallax, AU, solar and Earth radii, spectral classes, K), that
|
|
3
|
+
// `skyScene` turns into a viewer scene ('3draft-scene' v2): the same prototypes, materials and conventions as the deep sky
|
|
4
|
+
// scene, so everything (the planet shader, the galaxy recipes, the black hole tracer, the glows, the find box's categories)
|
|
5
|
+
// works on it. The MCP tool view_sky sends one to the open viewer; the viewer also opens such a file (f↑). Docs: app/sky.md.
|
|
6
|
+
//
|
|
7
|
+
// The scene's frame: 1 unit = 1 light year, the Sun at the origin, equatorial coordinates with z up turned to the viewer's
|
|
8
|
+
// y-up frame as [x, z, -y] (like every star scene).
|
|
9
|
+
export const LY_PER_PC = 3.26156;
|
|
10
|
+
export const LY_PER_AU = 1.58125074e-5;
|
|
11
|
+
export const LY_PER_KM = 1 / 9.4607304725808e12;
|
|
12
|
+
export const SUN_RADIUS_LY = 696340 * LY_PER_KM;
|
|
13
|
+
export const EARTH_RADIUS_LY = 6371 * LY_PER_KM;
|
|
14
|
+
const UNITS = { ly: 1, pc: LY_PER_PC, kpc: LY_PER_PC * 1e3, mpc: LY_PER_PC * 1e6, au: LY_PER_AU, km: LY_PER_KM, kly: 1e3, mly: 1e6, gly: 1e9 };
|
|
15
|
+
const r4 = (v) => Math.round(v * 1e4) / 1e4;
|
|
16
|
+
const sig = (v, d = 6) => Number(v.toPrecision(d));
|
|
17
|
+
const fine = (v) => Number(v.toPrecision(14));
|
|
18
|
+
const hash = (s) => [...s].reduce((h, ch) => (Math.imul(h, 31) + ch.charCodeAt(0)) >>> 0, 2166136261);
|
|
19
|
+
// ---- the prototypes (the same meshes as examples/stars.js: PLAIN.star and GALAXY_PROTOS; a test keeps them equal)
|
|
20
|
+
const sphere = (r, seg, rings, mat) => {
|
|
21
|
+
const positions = [[0, r, 0]];
|
|
22
|
+
for (let i = 1; i < rings; i++)
|
|
23
|
+
for (let j = 0; j < seg; j++) {
|
|
24
|
+
const th = (Math.PI * i) / rings, ph = (2 * Math.PI * j) / seg;
|
|
25
|
+
positions.push([r * Math.sin(th) * Math.sin(ph), r * Math.cos(th), r * Math.sin(th) * Math.cos(ph)]);
|
|
26
|
+
}
|
|
27
|
+
positions.push([0, -r, 0]);
|
|
28
|
+
const faces = [];
|
|
29
|
+
const f = (a, b, c) => faces.push({ v: [a, b, c], mat });
|
|
30
|
+
const R = (i, j) => 1 + (i - 1) * seg + (j % seg);
|
|
31
|
+
for (let j = 0; j < seg; j++)
|
|
32
|
+
f(0, R(1, j), R(1, j + 1));
|
|
33
|
+
for (let i = 1; i < rings - 1; i++)
|
|
34
|
+
for (let j = 0; j < seg; j++)
|
|
35
|
+
(f(R(i, j), R(i + 1, j + 1), R(i, j + 1)), f(R(i, j), R(i + 1, j), R(i + 1, j + 1)));
|
|
36
|
+
for (let j = 0; j < seg; j++)
|
|
37
|
+
f(R(rings - 1, j), positions.length - 1, R(rings - 1, j + 1));
|
|
38
|
+
return { positions, faces };
|
|
39
|
+
};
|
|
40
|
+
const round4 = (m, k = 1) => ({ ...m, positions: m.positions.map((p) => p.map((v) => Math.round(v * k * 1e4) / 1e4)) });
|
|
41
|
+
const disc = (inner, n, mat, t = 0.03) => {
|
|
42
|
+
const positions = [], faces = [];
|
|
43
|
+
const ang = (j) => (2 * Math.PI * j) / n;
|
|
44
|
+
const mid = (1 + inner) / 2;
|
|
45
|
+
for (let j = 0; j < n; j++) {
|
|
46
|
+
const a = ang(j), s = Math.sin(a), c = Math.cos(a);
|
|
47
|
+
positions.push([s, 0, c], [mid * s, t, mid * c], [inner * s, 0, inner * c], [mid * s, -t, mid * c]);
|
|
48
|
+
}
|
|
49
|
+
const V = (j, w) => 4 * (j % n) + w;
|
|
50
|
+
for (let j = 0; j < n; j++)
|
|
51
|
+
for (let w = 0; w < 4; w++) {
|
|
52
|
+
const w2 = (w + 1) % 4, a = V(j, w), b = V(j + 1, w), c = V(j + 1, w2), d = V(j, w2);
|
|
53
|
+
faces.push({ v: [a, b, c], mat }, { v: [a, c, d], mat });
|
|
54
|
+
}
|
|
55
|
+
return { positions, faces };
|
|
56
|
+
};
|
|
57
|
+
const join = (...ms) => { const positions = [], faces = []; for (const m of ms) {
|
|
58
|
+
const o = positions.length;
|
|
59
|
+
positions.push(...m.positions);
|
|
60
|
+
faces.push(...m.faces.map((f) => ({ ...f, v: f.v.map((i) => i + o) })));
|
|
61
|
+
} return round4({ positions, faces }); };
|
|
62
|
+
let protos = null;
|
|
63
|
+
/** The sky prototypes: star (a plain sphere, the glow is a sprite), planet (3 % smaller: the planet shader's sphere covers it), ghost (a galaxy's stand-in), hole (a black hole's ball and disc). */
|
|
64
|
+
export const skyPrototypes = () => {
|
|
65
|
+
if (protos)
|
|
66
|
+
return protos;
|
|
67
|
+
const star = { halo: false, lods: [[64, 32], [32, 16], [16, 8], [8, 4], [4, 2]].map(([s, r]) => round4(sphere(1, s, r, 'core'))) };
|
|
68
|
+
const planet = { ...star, lods: star.lods.map((m) => ({ ...m, positions: m.positions.map((q) => q.map((v) => Math.round(v * 0.97 * 1e5) / 1e5)) })) };
|
|
69
|
+
const ghost = { halo: false, lods: [round4({ ...sphere(1, 8, 4, 'core'), positions: sphere(1, 8, 4, 'core').positions.map(([a, b, c]) => [a, b * 0.75, c]) })] };
|
|
70
|
+
const hole = { halo: false, lods: [[32, 16, 48], [16, 8, 24], [8, 4, 16], [6, 3, 8]].map(([sg, rg, n]) => join(sphere(1, sg, rg, 'core'), (() => { const d = disc(0.35, n, 'disc'); return { ...d, positions: d.positions.map((p) => p.map((v) => v * 4.5)) }; })())) };
|
|
71
|
+
return (protos = { star, planet, ghost, hole });
|
|
72
|
+
};
|
|
73
|
+
// ---- stars: the spectral class decides the material (star-O … star-L, star-D white dwarf, star-X unknown), the temperature the tint
|
|
74
|
+
export const STAR_CLASSES = {
|
|
75
|
+
O: ['#9bb0ff', 1.0], B: ['#aabfff', 1.0], A: ['#cad7ff', 0.95], F: ['#f8f7ff', 0.9], G: ['#fff4ea', 0.85],
|
|
76
|
+
K: ['#ffd2a1', 0.8], M: ['#ffb067', 0.75], D: ['#e8eeff', 0.9], L: ['#c46a3a', 0.4], X: ['#ffcf9a', 0.6],
|
|
77
|
+
};
|
|
78
|
+
const TYPICAL_T = { O: 4e4, B: 2e4, A: 9e3, F: 6.8e3, G: 5.7e3, K: 4.5e3, M: 3.3e3, D: 1.2e4, L: 2e3, X: 5e3 };
|
|
79
|
+
const TYPICAL_L = { O: 1e5, B: 800, A: 20, F: 3, G: 1, K: 0.3, M: 0.02, D: 0.003, L: 1e-4, X: 1 };
|
|
80
|
+
export const starClass = (spect, temp) => {
|
|
81
|
+
if (spect) {
|
|
82
|
+
if (/^\s*(D|WD)/i.test(spect))
|
|
83
|
+
return 'D';
|
|
84
|
+
const m = /[OBAFGKMLTY]/i.exec(spect.replace(/^\s*(sd|d)/i, ''));
|
|
85
|
+
if (m)
|
|
86
|
+
return /[TY]/i.test(m[0]) ? 'L' : m[0].toUpperCase();
|
|
87
|
+
}
|
|
88
|
+
if (temp)
|
|
89
|
+
return temp > 30000 ? 'O' : temp > 10000 ? 'B' : temp > 7500 ? 'A' : temp > 6000 ? 'F' : temp > 5200 ? 'G' : temp > 3700 ? 'K' : temp > 2400 ? 'M' : 'L';
|
|
90
|
+
return 'X';
|
|
91
|
+
};
|
|
92
|
+
/** a blackbody-ish tint for a temperature (K) */
|
|
93
|
+
export const starTint = (t) => {
|
|
94
|
+
const u = (Math.log(Math.min(40000, Math.max(2500, t))) - Math.log(2500)) / (Math.log(40000) - Math.log(2500));
|
|
95
|
+
const stops = [[0, [255, 150, 70]], [0.3, [255, 205, 150]], [0.5, [255, 244, 234]], [0.7, [220, 228, 255]], [1, [155, 176, 255]]];
|
|
96
|
+
let a = stops[0], b = stops[stops.length - 1];
|
|
97
|
+
for (let i = 0; i < stops.length - 1; i++)
|
|
98
|
+
if (u >= stops[i][0] && u <= stops[i + 1][0])
|
|
99
|
+
(a = stops[i], b = stops[i + 1]);
|
|
100
|
+
const w = (u - a[0]) / (b[0] - a[0] || 1);
|
|
101
|
+
return '#' + a[1].map((x, i) => Math.round(x + (b[1][i] - x) * w).toString(16).padStart(2, '0')).join('');
|
|
102
|
+
};
|
|
103
|
+
// ---- planets: the 30 surface recipes of the exoplanets, by class (size and warmth); examples/exoplanets.js uses them too
|
|
104
|
+
const ring = { inner: 1.3, outer: 2.3, color: '#cdbb98' };
|
|
105
|
+
export const EXO_RECIPES = [
|
|
106
|
+
['rock-lava', { kind: 'lava', colors: ['#2a201c', '#4a3a30', '#ff6a1a'] }],
|
|
107
|
+
['rock-lava', { kind: 'lava', colors: ['#3a2a22', '#5c4637', '#ffd25a'] }],
|
|
108
|
+
['rock-hot', { kind: 'rock', colors: ['#6e6258', '#9a8a7a', '#3e3530'] }],
|
|
109
|
+
['rock-hot', { kind: 'cloud', colors: ['#e3c67c', '#f3e2b0', '#b8913c'], atmosphere: { color: '#f2d08a', size: 0.03 } }],
|
|
110
|
+
['rock-hot', { kind: 'cloud', colors: ['#d8925a', '#f0c08a', '#9c5a30'], atmosphere: { color: '#f0a86a', size: 0.03 } }],
|
|
111
|
+
['rock-warm', { kind: 'rock', colors: ['#b08a5c', '#d6b88a', '#7a5a3a'], atmosphere: { color: '#e8c89a', size: 0.012 } }],
|
|
112
|
+
['rock-warm', { kind: 'cloud', colors: ['#e9dcc0', '#f7f0de', '#c8b48c'], atmosphere: { color: '#f5e8c8', size: 0.025 } }],
|
|
113
|
+
['rock-temp', { kind: 'earth', colors: ['#1f528f', '#4f7f3a', '#b79a66'], detail: 0.53, atmosphere: { color: '#7fb2ff', size: 0.02 } }],
|
|
114
|
+
['rock-temp', { kind: 'earth', colors: ['#174a86', '#3e7a46', '#9a8a5a'], detail: 0.7, atmosphere: { color: '#8fc0ff', size: 0.022 } }],
|
|
115
|
+
['rock-temp', { kind: 'earth', colors: ['#2d5a7a', '#8a7a4a', '#c8a066'], detail: 0.38, atmosphere: { color: '#c0c8ff', size: 0.018 } }],
|
|
116
|
+
['rock-cold', { kind: 'rock', colors: ['#a5502e', '#c88060', '#6e3420'], detail: 0.1, atmosphere: { color: '#e8a07a', size: 0.01 } }],
|
|
117
|
+
['rock-cold', { kind: 'rock', colors: ['#8a8c90', '#b8bcc2', '#5c5e62'], detail: 0.18 }],
|
|
118
|
+
['rock-froz', { kind: 'rock', colors: ['#c8d4e0', '#eef4fa', '#8a9cb0'], detail: 0.3 }],
|
|
119
|
+
['sub-hot', { kind: 'cloud', colors: ['#a8683a', '#d29a62', '#6a3a20'], atmosphere: { color: '#e09a60', size: 0.04 } }],
|
|
120
|
+
['sub-hot', { kind: 'cloud', colors: ['#b8b4ac', '#e2ded6', '#8a8680'], atmosphere: { color: '#d8d4cc', size: 0.04 } }],
|
|
121
|
+
['sub-warm', { kind: 'cloud', colors: ['#cdb894', '#ece0c4', '#9a8664'], atmosphere: { color: '#e8dcc0', size: 0.035 } }],
|
|
122
|
+
['sub-warm', { kind: 'earth', colors: ['#2e6a9a', '#7a9aa8', '#a8b8c0'], detail: 0.88, atmosphere: { color: '#a8d0ff', size: 0.03 } }],
|
|
123
|
+
['sub-temp', { kind: 'earth', colors: ['#1e7a8a', '#3a8a7a', '#6aa89a'], detail: 0.94, atmosphere: { color: '#9ae8ff', size: 0.035 } }],
|
|
124
|
+
['sub-cold', { kind: 'ice', colors: ['#b8dce8', '#d8eef4', '#90bccc'], detail: 4, atmosphere: { color: '#d0f0ff', size: 0.03 } }],
|
|
125
|
+
['nep-hot', { kind: 'gas', colors: ['#3e5a5a', '#6a8a7a', '#2a3a3a'], detail: 7, atmosphere: { color: '#8ab8a8', size: 0.025 } }],
|
|
126
|
+
['nep-warm', { kind: 'ice', colors: ['#62b8c8', '#90d8e2', '#3a8a9a'], detail: 5, atmosphere: { color: '#a8eef8', size: 0.025 } }],
|
|
127
|
+
['nep-cold', { kind: 'ice', colors: ['#3f63d0', '#6f92ee', '#22409c'], detail: 5, atmosphere: { color: '#9fb8ff', size: 0.02 } }],
|
|
128
|
+
['jup-lava', { kind: 'gas', colors: ['#2a1a16', '#5a2a1e', '#ff5a20'], detail: 9, spot: true, atmosphere: { color: '#ff7a3a', size: 0.02 } }],
|
|
129
|
+
['jup-lava', { kind: 'gas', colors: ['#3a1e14', '#7a3a22', '#d0402a'], detail: 12, atmosphere: { color: '#e06a3a', size: 0.02 } }],
|
|
130
|
+
['jup-hot', { kind: 'gas', colors: ['#5a3e2a', '#8a6a4a', '#3a261a'], detail: 11, atmosphere: { color: '#b88a5a', size: 0.015 } }],
|
|
131
|
+
['jup-warm', { kind: 'gas', colors: ['#b98d62', '#efe2c8', '#c0603a'], detail: 14, spot: true }],
|
|
132
|
+
['jup-temp', { kind: 'gas', colors: ['#e2dccc', '#f8f4ea', '#b8ae98'], detail: 12, atmosphere: { color: '#f0f0ff', size: 0.012 } }],
|
|
133
|
+
['jup-temp', { kind: 'gas', colors: ['#bccad8', '#e6eef6', '#8a9cb0'], detail: 10 }],
|
|
134
|
+
['jup-cold', { kind: 'gas', colors: ['#cfb27a', '#efe0b5', '#b8975c'], detail: 10, ring }],
|
|
135
|
+
['jup-cold', { kind: 'gas', colors: ['#a8b8cc', '#d8e2ee', '#7a8ca8'], detail: 8, atmosphere: { color: '#c8d8ff', size: 0.012 } }],
|
|
136
|
+
];
|
|
137
|
+
const FALLBACK = { 'sub-lava': 'sub-hot', 'sub-froz': 'sub-cold', 'nep-lava': 'nep-hot', 'nep-temp': 'nep-warm', 'nep-froz': 'nep-cold', 'jup-froz': 'jup-cold' };
|
|
138
|
+
/** a planet's class: size (rock < 1.6 Earth radii, sub < 4, nep < 8, jup) and warmth (lava > 1200 K, hot > 700, warm > 350, temp > 200, cold > 100, froz) */
|
|
139
|
+
export const exoClass = (rE, teq) => {
|
|
140
|
+
const size = rE < 1.6 ? 'rock' : rE < 4 ? 'sub' : rE < 8 ? 'nep' : 'jup';
|
|
141
|
+
const heat = teq > 1200 ? 'lava' : teq > 700 ? 'hot' : teq > 350 ? 'warm' : teq > 200 ? 'temp' : teq > 100 ? 'cold' : 'froz';
|
|
142
|
+
return `${size}-${heat}`;
|
|
143
|
+
};
|
|
144
|
+
/** the recipe of a planet of that size and warmth (a variant of its class by the name) */
|
|
145
|
+
export const exoRecipe = (name, rE, teq) => {
|
|
146
|
+
const c = exoClass(rE, teq);
|
|
147
|
+
let pool = EXO_RECIPES.filter(([k]) => k === c);
|
|
148
|
+
if (!pool.length)
|
|
149
|
+
pool = EXO_RECIPES.filter(([k]) => k === FALLBACK[c]);
|
|
150
|
+
if (!pool.length)
|
|
151
|
+
pool = EXO_RECIPES.filter(([k]) => k.startsWith(c.split('-')[0]));
|
|
152
|
+
return pool[hash(name) % pool.length][1];
|
|
153
|
+
};
|
|
154
|
+
// ---- galaxies
|
|
155
|
+
const GAL_KIND = { s: 'S', spiral: 'S', sb: 'S', barred: 'S', e: 'E', elliptical: 'E', s0: 'E', lenticular: 'E', i: 'I', irregular: 'I', d: 'D', dwarf: 'D', p: 'P', interacting: 'P', merger: 'P', h: 'H', early: 'H' };
|
|
156
|
+
const GAL_GLOW = { S: '#bcd0ff', E: '#ffd9a0', I: '#a9c4ff', D: '#ffe2b8', P: '#c9d6ff', H: '#b9c8ff' };
|
|
157
|
+
const GAL_N = { S: 1, E: 1, I: 0.6, D: 0.4, P: 0.1, H: 0.05 };
|
|
158
|
+
export const SKY_PALETTE = () => [
|
|
159
|
+
...Object.entries(STAR_CLASSES).map(([k, [color, emit]]) => ({ name: `star-${k}`, color, metal: 0, rough: 0.5, alpha: 1, emit, soft: 0 })),
|
|
160
|
+
{ name: 'gal-ghost', color: '#9db4ff', metal: 0, rough: 1, alpha: 0, emit: 0, soft: 0 },
|
|
161
|
+
{ name: 'bh-core', color: '#000000', metal: 0, rough: 1, alpha: 1, emit: 0, soft: 0 },
|
|
162
|
+
{ name: 'bh-disc', color: '#ff9a40', metal: 0, rough: 1, alpha: 1, emit: 1, soft: 0 },
|
|
163
|
+
{ name: 'sky-planet', color: '#8a8a8a', metal: 0, rough: 0.9, alpha: 1, emit: 0 },
|
|
164
|
+
];
|
|
165
|
+
/** what the catalogue can hold, for the docs and the MCP tool's schema */
|
|
166
|
+
export const SKY_TYPES = ['star', 'planet', 'moon', 'dwarf planet', 'exoplanet', 'black hole', 'galaxy'];
|
|
167
|
+
// ---- the conversion
|
|
168
|
+
const num = (v) => (typeof v === 'number' && Number.isFinite(v) ? v : undefined);
|
|
169
|
+
const raDeg = (o) => {
|
|
170
|
+
if (num(o.ra) !== undefined)
|
|
171
|
+
return o.ra;
|
|
172
|
+
if (num(o.ra_h) !== undefined)
|
|
173
|
+
return o.ra_h * 15;
|
|
174
|
+
if (typeof o.ra === 'string') {
|
|
175
|
+
const p = o.ra.trim().split(/[\s:hms]+/).filter(Boolean).map(Number);
|
|
176
|
+
if (p.every(Number.isFinite))
|
|
177
|
+
return ((p[0] ?? 0) + (p[1] ?? 0) / 60 + (p[2] ?? 0) / 3600) * 15;
|
|
178
|
+
}
|
|
179
|
+
return undefined;
|
|
180
|
+
};
|
|
181
|
+
const decDeg = (o) => {
|
|
182
|
+
if (num(o.dec) !== undefined)
|
|
183
|
+
return o.dec;
|
|
184
|
+
if (typeof o.dec === 'string') {
|
|
185
|
+
const s = o.dec.trim();
|
|
186
|
+
const neg = s.startsWith('-');
|
|
187
|
+
const p = s.replace(/^[+-]/, '').split(/[\s:°'"dms]+/).filter(Boolean).map(Number);
|
|
188
|
+
if (p.every(Number.isFinite))
|
|
189
|
+
return (neg ? -1 : 1) * ((p[0] ?? 0) + (p[1] ?? 0) / 60 + (p[2] ?? 0) / 3600);
|
|
190
|
+
}
|
|
191
|
+
return undefined;
|
|
192
|
+
};
|
|
193
|
+
const unitOf = (u, what, warn) => {
|
|
194
|
+
if (u === undefined)
|
|
195
|
+
return 1;
|
|
196
|
+
const k = UNITS[String(u).toLowerCase()];
|
|
197
|
+
if (k === undefined) {
|
|
198
|
+
warn(`${what}: unknown unit "${u}" (ly, pc, kpc, Mpc, AU, km, kly, Mly, Gly); light years used`);
|
|
199
|
+
return 1;
|
|
200
|
+
}
|
|
201
|
+
return k;
|
|
202
|
+
};
|
|
203
|
+
/** equatorial (RA, Dec in degrees, distance in ly) -> the scene's [x, z, -y] */
|
|
204
|
+
export const skyAt = (raD, decD, dist) => {
|
|
205
|
+
const a = (raD * Math.PI) / 180, d = (decD * Math.PI) / 180;
|
|
206
|
+
const x = dist * Math.cos(d) * Math.cos(a), y = dist * Math.cos(d) * Math.sin(a), z = dist * Math.sin(d);
|
|
207
|
+
return [x, z, -y];
|
|
208
|
+
};
|
|
209
|
+
// a place: at [x, y, z] in scene ly; xyz equatorial (with unit); ra / dec with dist (unit) or parallax (mas)
|
|
210
|
+
const placeOf = (o, warn) => {
|
|
211
|
+
if (Array.isArray(o.at) && o.at.length === 3 && o.at.every((v) => num(v) !== undefined))
|
|
212
|
+
return o.at.map((v) => v * unitOf(o.unit, o.name, warn));
|
|
213
|
+
if (Array.isArray(o.xyz) && o.xyz.length === 3) {
|
|
214
|
+
const k = unitOf(o.unit, o.name, warn);
|
|
215
|
+
return [o.xyz[0] * k, o.xyz[2] * k, -o.xyz[1] * k];
|
|
216
|
+
}
|
|
217
|
+
const ra = raDeg(o), dec = decDeg(o);
|
|
218
|
+
if (ra === undefined || dec === undefined)
|
|
219
|
+
return null;
|
|
220
|
+
let dist = num(o.dist);
|
|
221
|
+
if (dist !== undefined)
|
|
222
|
+
dist *= unitOf(o.unit, o.name, warn);
|
|
223
|
+
else if (num(o.parallax) !== undefined) {
|
|
224
|
+
if (o.parallax <= 0) {
|
|
225
|
+
warn(`${o.name}: parallax ${o.parallax} mas is not a distance (left out)`);
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
dist = (1000 / o.parallax) * LY_PER_PC;
|
|
229
|
+
}
|
|
230
|
+
if (dist === undefined)
|
|
231
|
+
return null;
|
|
232
|
+
return skyAt(ra, dec, dist);
|
|
233
|
+
};
|
|
234
|
+
const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
235
|
+
const len = (a) => Math.hypot(...a);
|
|
236
|
+
const unit = (a) => { const l = len(a) || 1; return [a[0] / l, a[1] / l, a[2] / l]; };
|
|
237
|
+
const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
|
238
|
+
// an orbit's frame: its normal from inclination / node (degrees, to the scene's xz plane) or from the name
|
|
239
|
+
const orbitFrame = (o, h) => {
|
|
240
|
+
let n;
|
|
241
|
+
if (num(o.incl) !== undefined) {
|
|
242
|
+
const i = (o.incl * Math.PI) / 180, w = ((o.node ?? 0) * Math.PI) / 180;
|
|
243
|
+
n = [Math.sin(i) * Math.sin(w), Math.cos(i), -Math.sin(i) * Math.cos(w)];
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
const u1 = ((h % 1000) / 1000) * 2 - 1, u2 = (((h >>> 10) % 1000) / 1000) * 2 * Math.PI;
|
|
247
|
+
n = [Math.sqrt(1 - u1 * u1) * Math.cos(u2), u1, Math.sqrt(1 - u1 * u1) * Math.sin(u2)];
|
|
248
|
+
}
|
|
249
|
+
const t1 = unit(cross(n, Math.abs(n[1]) > 0.9 ? [1, 0, 0] : [0, 1, 0]));
|
|
250
|
+
return [n, t1, cross(n, t1)];
|
|
251
|
+
};
|
|
252
|
+
// a galaxy's tilt: [tilt about x, turn about y] (radians) from incl / pa (degrees) or the name
|
|
253
|
+
const galRot = (o, h) => {
|
|
254
|
+
if (Array.isArray(o.rot))
|
|
255
|
+
return o.rot;
|
|
256
|
+
if (num(o.incl) !== undefined)
|
|
257
|
+
return [r4((o.incl * Math.PI) / 180), r4(((o.pa ?? 0) * Math.PI) / 180)];
|
|
258
|
+
return [r4(((h % 1000) / 1000) * 3), r4((((h >>> 10) % 1000) / 1000) * 6.283)];
|
|
259
|
+
};
|
|
260
|
+
/** A sky catalogue ('3draft-sky') -> a viewer scene. Errors that make an object unusable become warnings (the object is left out). */
|
|
261
|
+
export const skyScene = (sky) => {
|
|
262
|
+
if (!sky || typeof sky !== 'object' || !Array.isArray(sky.objects))
|
|
263
|
+
throw new Error('a sky catalogue needs objects: [ … ]');
|
|
264
|
+
const warnings = [];
|
|
265
|
+
const warn = (m) => { if (warnings.length < 200)
|
|
266
|
+
warnings.push(m); };
|
|
267
|
+
const objects = [];
|
|
268
|
+
const paths = [];
|
|
269
|
+
const counts = {};
|
|
270
|
+
const byName = new Map(); // name -> the made object (for `around`)
|
|
271
|
+
const lumOf = new Map();
|
|
272
|
+
const types = (o) => String(o.type ?? 'star').toLowerCase().replace(/[_-]/g, ' ');
|
|
273
|
+
const put = (src, made, type) => {
|
|
274
|
+
if (src.info)
|
|
275
|
+
made.info = String(src.info);
|
|
276
|
+
if (src.category)
|
|
277
|
+
made.category = String(src.category);
|
|
278
|
+
objects.push(made);
|
|
279
|
+
byName.set(made.name.toLowerCase(), made);
|
|
280
|
+
counts[type] = (counts[type] ?? 0) + 1;
|
|
281
|
+
};
|
|
282
|
+
const named = (o, i) => (typeof o.name === 'string' && o.name.trim() ? o.name.trim() : `${types(o)} ${i + 1}`);
|
|
283
|
+
// stars, black holes and galaxies first (they have places of their own), then what goes round them
|
|
284
|
+
const list = sky.objects.map((o, i) => ({ ...o, name: named(o, i) }));
|
|
285
|
+
const free = list.filter((o) => !o.around);
|
|
286
|
+
const bound = list.filter((o) => o.around);
|
|
287
|
+
for (const o of free) {
|
|
288
|
+
const type = types(o);
|
|
289
|
+
const at = placeOf(o, warn);
|
|
290
|
+
if (!at) {
|
|
291
|
+
warn(`${o.name}: no place (give at [x, y, z] ly, or ra / dec with dist or parallax)`);
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
const h = hash(o.name);
|
|
295
|
+
if (type === 'star') {
|
|
296
|
+
const k = starClass(o.spect, num(o.temp));
|
|
297
|
+
const T = num(o.temp) ?? TYPICAL_T[k];
|
|
298
|
+
const distLy = len(at);
|
|
299
|
+
let L = num(o.lum);
|
|
300
|
+
if (L === undefined && num(o.absmag) !== undefined)
|
|
301
|
+
L = 10 ** ((4.83 - o.absmag) / 2.5);
|
|
302
|
+
if (L === undefined && num(o.vmag) !== undefined && distLy > 0)
|
|
303
|
+
L = 10 ** ((4.83 - (o.vmag - 5 * Math.log10(distLy / LY_PER_PC / 10))) / 2.5);
|
|
304
|
+
const rSun = num(o.radius) ?? (L !== undefined ? (k === 'D' ? 0.012 : Math.sqrt(L) / (Math.max(T, 1500) / 5772) ** 2) : k === 'D' ? 0.012 : undefined);
|
|
305
|
+
if (L === undefined)
|
|
306
|
+
L = rSun !== undefined ? rSun ** 2 * (T / 5772) ** 4 : TYPICAL_L[k];
|
|
307
|
+
const r = Math.max(SUN_RADIUS_LY * 0.008, SUN_RADIUS_LY * (rSun ?? Math.sqrt(L) / (Math.max(T, 1500) / 5772) ** 2));
|
|
308
|
+
const glow = Math.max(r + 0.04 + (0.16 * Math.log10(1 + L * 100)) / 2, 0.08 * L ** 0.4);
|
|
309
|
+
const made = { name: o.name, proto: 'star', at: at.map(r4), glow: r4(glow), scale: sig(r, 5), mats: { core: `star-${k}` }, color: o.color ?? starTint(num(o.temp) ?? T) };
|
|
310
|
+
lumOf.set(made, L);
|
|
311
|
+
put(o, made, 'star');
|
|
312
|
+
}
|
|
313
|
+
else if (type === 'black hole') {
|
|
314
|
+
const mass = num(o.mass) ?? 10;
|
|
315
|
+
const scale = num(o.radius_ly) ?? (mass > 1e5 ? 40 * Math.sqrt(mass / 4.3e6) : 0.3); // (drawn big: a real horizon is a few km)
|
|
316
|
+
put(o, { name: o.name, proto: 'hole', at: at.map(r4), scale: sig(scale, 5), mats: { core: 'bh-core', disc: 'bh-disc' }, glow: sig(scale * 17, 5), glowColor: o.color ?? '#cfd8ff', far: true, rot: galRot(o, h), category: 'black hole' }, 'black hole');
|
|
317
|
+
}
|
|
318
|
+
else if (type === 'galaxy') {
|
|
319
|
+
const kind = GAL_KIND[String(o.kind ?? 'spiral').toLowerCase()] ?? (warn(`${o.name}: unknown galaxy kind "${o.kind}" (spiral, elliptical, irregular, dwarf, interacting, early); spiral used`), 'S');
|
|
320
|
+
const radius = num(o.radius_ly) ?? (num(o.diameter_kly) !== undefined ? o.diameter_kly * 500 : kind === 'D' ? 2000 : 40000);
|
|
321
|
+
const g = { kind, seed: h % 2147483646 + 1, n: Math.round(Math.min(60000, Math.max(300, ((30000 * radius) / 45000) * GAL_N[kind]))) };
|
|
322
|
+
for (const f of ['arms', 'pitch', 'bar', 'armShare', 'b', 'c', 'clumps', 'ratio', 'peri', 'incl1', 'incl2', 'time', 'merge', 'agn'])
|
|
323
|
+
if (o[f] !== undefined)
|
|
324
|
+
g[f] = o[f];
|
|
325
|
+
put(o, { name: o.name, proto: 'ghost', at: at.map(r4), scale: r4(radius), mats: { core: 'gal-ghost' }, glow: r4(radius * (kind === 'P' ? 2 : 4)), glowColor: o.color ?? GAL_GLOW[kind], far: true, galaxy: g, rot: galRot(o, h) }, 'galaxy');
|
|
326
|
+
}
|
|
327
|
+
else if (['planet', 'moon', 'dwarf planet', 'exoplanet'].includes(type)) {
|
|
328
|
+
bound.push(o); // a planet with a place of its own: drawn there, lit by the nearest star
|
|
329
|
+
}
|
|
330
|
+
else
|
|
331
|
+
warn(`${o.name}: unknown type "${o.type}" (${SKY_TYPES.join(', ')})`);
|
|
332
|
+
}
|
|
333
|
+
// planets and moons: round the object they name (`around`), a planets' place first so moons can go round them
|
|
334
|
+
const pending = [...bound];
|
|
335
|
+
for (let pass = 0; pass < 3 && pending.length; pass++)
|
|
336
|
+
for (let i = 0; i < pending.length; i++) {
|
|
337
|
+
const o = pending[i];
|
|
338
|
+
const type = types(o);
|
|
339
|
+
const host = o.around ? byName.get(String(o.around).toLowerCase()) : null;
|
|
340
|
+
if (o.around && !host)
|
|
341
|
+
continue; // (its host may come in a later pass)
|
|
342
|
+
pending.splice(i--, 1);
|
|
343
|
+
const h = hash(o.name);
|
|
344
|
+
let at, a = 0;
|
|
345
|
+
if (host) {
|
|
346
|
+
a = num(o.a) !== undefined ? o.a * LY_PER_AU : num(o.a_km) !== undefined ? o.a_km * LY_PER_KM : NaN;
|
|
347
|
+
if (!Number.isFinite(a)) {
|
|
348
|
+
warn(`${o.name}: give its orbit's size, a (AU) or a_km`);
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
const [, t1, t2] = orbitFrame(o, h);
|
|
352
|
+
const ph = num(o.angle) !== undefined ? (o.angle * Math.PI) / 180 : (((h >>> 20) % 1000) / 1000) * 2 * Math.PI;
|
|
353
|
+
at = host.at.map((c, k) => fine(c + a * (Math.cos(ph) * t1[k] + Math.sin(ph) * t2[k])));
|
|
354
|
+
if (o.orbit)
|
|
355
|
+
paths.push({ name: `${o.name} orbit`, kind: 'orbit', color: o.orbitColor ?? '#8fa0b8', pts: Array.from({ length: 129 }, (_, s) => { const u = (s / 128) * 2 * Math.PI; return host.at.map((c, k) => fine(c + a * (Math.cos(u) * t1[k] + Math.sin(u) * t2[k]))); }) });
|
|
356
|
+
}
|
|
357
|
+
else
|
|
358
|
+
at = placeOf(o, warn);
|
|
359
|
+
if (!at) {
|
|
360
|
+
warn(`${o.name}: no place (around a star with a, or a place of its own)`);
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
// the light: the star it (or its planet) goes round, else the nearest star
|
|
364
|
+
let star = host;
|
|
365
|
+
while (star && star.proto !== 'star')
|
|
366
|
+
star = star.host;
|
|
367
|
+
if (!star) {
|
|
368
|
+
let best = Infinity;
|
|
369
|
+
for (const s of objects)
|
|
370
|
+
if (s.proto === 'star') {
|
|
371
|
+
const d = len(sub(s.at, at));
|
|
372
|
+
if (d < best)
|
|
373
|
+
(best = d, star = s);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
const rE = num(o.radius_earth) ?? (num(o.radius_km) !== undefined ? o.radius_km / 6371 : num(o.radius_jup) !== undefined ? o.radius_jup * 11.21 : type === 'moon' ? 0.27 : 1);
|
|
377
|
+
// the warmth: given, else from the star's luminosity and the distance (equilibrium, albedo 0.3)
|
|
378
|
+
const dStar = star ? len(sub(star.at, at)) / LY_PER_AU : 1;
|
|
379
|
+
const teq = num(o.teq) ?? (star ? 278.6 * 0.915 * (lumOf.get(star) ?? 1) ** 0.25 / Math.sqrt(Math.max(dStar, 1e-4)) : 150);
|
|
380
|
+
const recipe = o.surface && typeof o.surface === 'object' ? { kind: 'rock', colors: ['#8a8a8a', '#b8b8b8', '#5a5a5a'], ...o.surface } : exoRecipe(o.name, rE, teq);
|
|
381
|
+
if (o.ring && typeof o.ring === 'object')
|
|
382
|
+
recipe.ring = { inner: 1.3, outer: 2.3, color: '#cdbb98', ...o.ring };
|
|
383
|
+
const radius = rE * EARTH_RADIUS_LY;
|
|
384
|
+
const pole = num(o.incl) !== undefined || host ? orbitFrame(o, h)[0] : unit([Math.sin(h), 1, Math.cos(h)]);
|
|
385
|
+
const cls = exoClass(rE, teq).split('-');
|
|
386
|
+
const sunlit = !star || /^(sun|sol)$/i.test(star.name);
|
|
387
|
+
const category = o.category ?? ((type === 'planet' && !sunlit) || type === 'exoplanet'
|
|
388
|
+
? `${{ lava: 'lava', hot: 'hot', warm: 'warm', temp: 'temperate', cold: 'cold', froz: 'frozen' }[cls[1]]} ${{ rock: 'rocky', sub: 'sub-Neptune', nep: 'Neptune-like', jup: 'gas giant' }[cls[0]]} exoplanet`
|
|
389
|
+
: type);
|
|
390
|
+
const made = {
|
|
391
|
+
name: o.name, proto: 'planet', at, scale: sig(radius), mats: { core: 'sky-planet' }, color: recipe.colors[1], category,
|
|
392
|
+
glow: sig(Math.max(radius * 600, a * 0.004 || radius * 600)), glowColor: recipe.colors[1],
|
|
393
|
+
planet: { ...recipe, seed: h % 997, pole: pole.map((v) => sig(v)), light: star ? [...star.at] : [0, 0, 0] },
|
|
394
|
+
};
|
|
395
|
+
Object.defineProperty(made, 'host', { value: host, enumerable: false });
|
|
396
|
+
put(o, made, type);
|
|
397
|
+
}
|
|
398
|
+
for (const o of pending)
|
|
399
|
+
warn(`${o.name}: around "${o.around}", which is not in the catalogue (left out)`);
|
|
400
|
+
// the start: near the named object (or the first star, or the first object), far enough to see it and what goes round it
|
|
401
|
+
let start = sky.start && Array.isArray(sky.start.eye) ? { eye: sky.start.eye, yaw: sky.start.yaw ?? 0, pitch: sky.start.pitch ?? 0 } : null;
|
|
402
|
+
if (!start && objects.length) {
|
|
403
|
+
const want = sky.start?.near ? byName.get(String(sky.start.near).toLowerCase()) : null;
|
|
404
|
+
if (sky.start?.near && !want)
|
|
405
|
+
warn(`start: no object "${sky.start.near}"`);
|
|
406
|
+
const c = want ?? objects.find((x) => x.proto === 'star') ?? objects[0];
|
|
407
|
+
const kids = objects.filter((x) => x.host === c || x.host?.host === c);
|
|
408
|
+
const reach = Math.max(...kids.map((x) => len(sub(x.at, c.at))), 0);
|
|
409
|
+
const d = num(sky.start?.distance) ?? (reach ? reach * 1.8 : c.proto === 'star' ? 60 * LY_PER_AU : c.proto === 'planet' ? c.scale * 9 : c.scale * 3);
|
|
410
|
+
// a planet is seen from its day side, a little off the line to its star (so it is lit, in phase); the rest from above
|
|
411
|
+
let dir = unit([0.35, 0.45, 1]);
|
|
412
|
+
if (c.proto === 'planet' && !reach) {
|
|
413
|
+
const toStar = unit(sub(c.planet.light, c.at));
|
|
414
|
+
if (len(toStar) > 0.5) {
|
|
415
|
+
const side = unit(cross(toStar, [0, 1, 0]));
|
|
416
|
+
dir = unit([0, 1, 2].map((k) => toStar[k] + 0.7 * side[k] + (k === 1 ? 0.35 : 0)));
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
const eye = c.at.map((v, k) => fine(v + dir[k] * d));
|
|
420
|
+
const look = sub(c.at, eye);
|
|
421
|
+
start = { eye, yaw: r4((Math.atan2(look[0], -look[2]) * 180) / Math.PI), pitch: r4((-Math.asin(look[1] / len(look)) * 180) / Math.PI) };
|
|
422
|
+
}
|
|
423
|
+
const scene = {
|
|
424
|
+
format: '3draft-scene', version: 2, name: sky.name ?? 'sky', palette: SKY_PALETTE(), prototypes: skyPrototypes(),
|
|
425
|
+
...(start ? { start } : {}), vehicle: 'starship', objects, ...(paths.length ? { paths } : {}),
|
|
426
|
+
};
|
|
427
|
+
return { scene, warnings, counts };
|
|
428
|
+
};
|