entangled-worlds-orbiters-shared 4.2.0 → 5.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/README.md +3 -2
- package/dist/shortcuts/catalogue.d.ts +72 -0
- package/dist/shortcuts/catalogue.js +118 -0
- package/dist/shortcuts/chord.d.ts +24 -2
- package/dist/shortcuts/chord.js +86 -8
- package/dist/shortcuts/format.d.ts +7 -4
- package/dist/shortcuts/format.js +40 -14
- package/dist/shortcuts/index.d.ts +4 -8
- package/dist/shortcuts/index.js +8 -11
- package/dist/shortcuts/match.d.ts +14 -6
- package/dist/shortcuts/match.js +14 -8
- package/dist/shortcuts/source.d.ts +62 -0
- package/dist/shortcuts/source.js +139 -0
- package/dist/sky/geometry.d.ts +31 -0
- package/dist/sky/geometry.js +84 -0
- package/dist/sky/index.d.ts +6 -0
- package/dist/sky/index.js +6 -0
- package/dist/sky/params.d.ts +44 -0
- package/dist/sky/params.js +52 -0
- package/dist/sky/shaders.d.ts +14 -0
- package/dist/sky/shaders.js +149 -0
- package/dist/sky/sky.d.ts +24 -0
- package/dist/sky/sky.js +268 -0
- package/dist/sky/worldSkyFile.d.ts +16 -0
- package/dist/sky/worldSkyFile.js +51 -0
- package/package.json +8 -2
- package/src/shortcuts/catalogue.ts +172 -0
- package/src/shortcuts/chord.ts +96 -6
- package/src/shortcuts/format.ts +44 -17
- package/src/shortcuts/index.ts +31 -28
- package/src/shortcuts/match.ts +25 -13
- package/src/shortcuts/source.ts +204 -0
- package/src/sky/geometry.ts +99 -0
- package/src/sky/index.ts +16 -0
- package/src/sky/params.ts +89 -0
- package/src/sky/shaders.ts +159 -0
- package/src/sky/sky.ts +330 -0
- package/src/sky/worldSkyFile.ts +60 -0
- package/dist/shortcuts/entangledWorlds.d.ts +0 -21
- package/dist/shortcuts/entangledWorlds.js +0 -40
- package/dist/shortcuts/labels.d.ts +0 -10
- package/dist/shortcuts/labels.js +0 -130
- package/dist/shortcuts/orbiters.d.ts +0 -55
- package/dist/shortcuts/orbiters.js +0 -109
- package/dist/shortcuts/overrides.d.ts +0 -30
- package/dist/shortcuts/overrides.js +0 -81
- package/dist/shortcuts/sections.d.ts +0 -23
- package/dist/shortcuts/sections.js +0 -19
- package/dist/shortcuts/site.d.ts +0 -9
- package/dist/shortcuts/site.js +0 -25
- package/src/shortcuts/entangledWorlds.ts +0 -51
- package/src/shortcuts/labels.ts +0 -139
- package/src/shortcuts/orbiters.ts +0 -132
- package/src/shortcuts/overrides.ts +0 -110
- package/src/shortcuts/sections.ts +0 -36
- package/src/shortcuts/site.ts +0 -33
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// What a world sets on its sky. Plain data, so it persists as-is; `normalizeSkyParams` is the one
|
|
2
|
+
// boundary that turns whatever was stored (or nothing) into a complete, in-range set.
|
|
3
|
+
|
|
4
|
+
/** Where the stars come from: a seed, or the world's own sky (the backend's file for its host star). */
|
|
5
|
+
export type SkySource = 'seeded' | 'world';
|
|
6
|
+
|
|
7
|
+
export interface SkyParams {
|
|
8
|
+
source: SkySource;
|
|
9
|
+
/** Same seed → same sky. Also seeds the stand-in stars and the nebula under a world sky. */
|
|
10
|
+
seed: number;
|
|
11
|
+
/** Stars drawn: the device's star budget. A world sky spends it on its catalogue stars first
|
|
12
|
+
* (brightest first), then its stand-ins, then faint fill. */
|
|
13
|
+
count: number;
|
|
14
|
+
/** Star brightness multiplier. */
|
|
15
|
+
brightness: number;
|
|
16
|
+
/** Star size in pixels at a 1080-pixel-tall frame, for the brightest ordinary star. */
|
|
17
|
+
size: number;
|
|
18
|
+
/** 0 = every star white, 1 = full blackbody colour. */
|
|
19
|
+
colour: number;
|
|
20
|
+
/** 0 = steady, 1 = full twinkle. */
|
|
21
|
+
twinkle: number;
|
|
22
|
+
/** Nebula amount, 0 = no nebula layer at all (the cheapest sky). */
|
|
23
|
+
nebula: number;
|
|
24
|
+
/** Nebula hue, degrees. */
|
|
25
|
+
hue: number;
|
|
26
|
+
/** How far the nebula's hue wanders around `hue`, degrees. */
|
|
27
|
+
hueSpread: number;
|
|
28
|
+
/** How much stars and clouds gather into a Milky Way band, 0..1. */
|
|
29
|
+
band: number;
|
|
30
|
+
/** Band tilt, degrees. Seeded skies only: a world sky's band is the galactic plane. */
|
|
31
|
+
bandTilt: number;
|
|
32
|
+
/** Constellation figures' strength, 0 = hidden. World skies only. */
|
|
33
|
+
constellations: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const DEFAULT_SKY_PARAMS: Readonly<SkyParams> = Object.freeze({
|
|
37
|
+
source: 'seeded',
|
|
38
|
+
seed: 1,
|
|
39
|
+
count: 40000,
|
|
40
|
+
brightness: 1,
|
|
41
|
+
size: 3,
|
|
42
|
+
colour: 0.7,
|
|
43
|
+
twinkle: 0.4,
|
|
44
|
+
nebula: 0.6,
|
|
45
|
+
hue: 255,
|
|
46
|
+
hueSpread: 50,
|
|
47
|
+
band: 0.6,
|
|
48
|
+
bandTilt: 60,
|
|
49
|
+
constellations: 0,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
type NumericKey = Exclude<keyof SkyParams, 'source'>;
|
|
53
|
+
|
|
54
|
+
/** Valid range of every numeric param, for UIs and for `normalizeSkyParams`. */
|
|
55
|
+
export const SKY_PARAM_RANGES: Readonly<Record<NumericKey, { min: number; max: number }>> = Object.freeze({
|
|
56
|
+
seed: { min: 0, max: 2 ** 31 - 1 },
|
|
57
|
+
count: { min: 0, max: 100000 },
|
|
58
|
+
brightness: { min: 0, max: 3 },
|
|
59
|
+
size: { min: 0.5, max: 5 },
|
|
60
|
+
colour: { min: 0, max: 1 },
|
|
61
|
+
twinkle: { min: 0, max: 1 },
|
|
62
|
+
nebula: { min: 0, max: 1 },
|
|
63
|
+
hue: { min: 0, max: 360 },
|
|
64
|
+
hueSpread: { min: 0, max: 180 },
|
|
65
|
+
band: { min: 0, max: 1 },
|
|
66
|
+
bandTilt: { min: 0, max: 180 },
|
|
67
|
+
constellations: { min: 0, max: 1 },
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
/** Params that only change uniforms: no rebuild, no re-bake. */
|
|
71
|
+
export const SKY_LOOK_KEYS = ['brightness', 'size', 'colour', 'twinkle', 'constellations', 'nebula', 'hue', 'hueSpread'] as const;
|
|
72
|
+
/** Params that rebuild the star buffer (at most once a frame); all but `count` also re-bake the glow's shape. */
|
|
73
|
+
export const SKY_SHAPE_KEYS = ['source', 'seed', 'count', 'band', 'bandTilt'] as const;
|
|
74
|
+
|
|
75
|
+
/** A complete, in-range set from anything stored: missing or invalid fields take the default. */
|
|
76
|
+
export function normalizeSkyParams(input: unknown): SkyParams {
|
|
77
|
+
const raw = (input && typeof input === 'object' ? input : {}) as Record<string, unknown>;
|
|
78
|
+
const out: SkyParams = { ...DEFAULT_SKY_PARAMS };
|
|
79
|
+
out.source = raw.source === 'world' ? 'world' : 'seeded';
|
|
80
|
+
for (const key of Object.keys(SKY_PARAM_RANGES) as NumericKey[]) {
|
|
81
|
+
const v = raw[key];
|
|
82
|
+
if (typeof v !== 'number' || !Number.isFinite(v)) continue;
|
|
83
|
+
const { min, max } = SKY_PARAM_RANGES[key];
|
|
84
|
+
out[key] = Math.min(max, Math.max(min, v));
|
|
85
|
+
}
|
|
86
|
+
out.seed = Math.round(out.seed);
|
|
87
|
+
out.count = Math.round(out.count);
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// The sky's shaders. Only the nebula bake runs noise, and only once per change; every per-frame
|
|
2
|
+
// shader is a handful of instructions.
|
|
3
|
+
|
|
4
|
+
/** Ordinary stars are never drawn bigger than this (pixels at a 1080-pixel-tall frame); glow stars 3.2×. */
|
|
5
|
+
export const MAX_STAR_SIZE = 5;
|
|
6
|
+
/** Brightness (0..1) above which a star joins the glow tier: ~1% of a seeded sky; on a world sky,
|
|
7
|
+
* every catalogue star brighter than apparent magnitude ~1. */
|
|
8
|
+
export const GLOW_SEEDED = 0.965;
|
|
9
|
+
export const GLOW_WORLD = 0.73;
|
|
10
|
+
|
|
11
|
+
export const STAR_VERTEX = /* glsl */ `
|
|
12
|
+
attribute vec4 aColourBrightness;
|
|
13
|
+
uniform float uTime;
|
|
14
|
+
uniform float uBrightness;
|
|
15
|
+
uniform float uSize;
|
|
16
|
+
uniform float uColour;
|
|
17
|
+
uniform float uTwinkle;
|
|
18
|
+
uniform float uPixelScale;
|
|
19
|
+
uniform float uGlowAt;
|
|
20
|
+
varying vec3 vColour;
|
|
21
|
+
varying float vAlpha;
|
|
22
|
+
varying float vGlow;
|
|
23
|
+
float hash(float n) { return fract(sin(n) * 43758.5453); }
|
|
24
|
+
void main() {
|
|
25
|
+
// Rotation only: the sky is at infinity. xyww pins depth to the far plane, so bodies in front
|
|
26
|
+
// hide the stars behind them before their fragments run. (Points cannot streak at w = 0.)
|
|
27
|
+
vec4 p = projectionMatrix * vec4(mat3(modelViewMatrix) * normalize(position), 1.0);
|
|
28
|
+
gl_Position = p.xyww;
|
|
29
|
+
float mag = aColourBrightness.a;
|
|
30
|
+
float h = hash(float(gl_VertexID) * 1.618);
|
|
31
|
+
// Twinkle reads the playback clock, so a recording is the same every time.
|
|
32
|
+
float tw = 1.0 + uTwinkle * 0.45 * sin(uTime * (1.3 + 3.0 * h) + h * 6.2831) * (0.4 + 0.6 * (1.0 - mag));
|
|
33
|
+
vGlow = step(uGlowAt, mag);
|
|
34
|
+
float px = (0.8 + (uSize - 0.8) * pow(mag, 0.6)) * uPixelScale;
|
|
35
|
+
px = min(px, ${MAX_STAR_SIZE.toFixed(1)} * uPixelScale);
|
|
36
|
+
// Glow stars: a bigger sprite whose rim is the halo; the core stays star-sized.
|
|
37
|
+
px *= 1.0 + 2.2 * vGlow;
|
|
38
|
+
// Sub-pixel stars: draw one pixel and fade, so energy stays and nothing shimmers.
|
|
39
|
+
float fade = clamp(px, 0.0, 1.0);
|
|
40
|
+
gl_PointSize = max(px, 1.0);
|
|
41
|
+
float lum = dot(aColourBrightness.rgb, vec3(0.2126, 0.7152, 0.0722));
|
|
42
|
+
vColour = mix(vec3(lum), aColourBrightness.rgb, uColour) / max(lum, 0.2);
|
|
43
|
+
vAlpha = uBrightness * fade * tw * (0.55 + 0.45 * mag);
|
|
44
|
+
}
|
|
45
|
+
`;
|
|
46
|
+
|
|
47
|
+
export const STAR_FRAGMENT = /* glsl */ `
|
|
48
|
+
varying vec3 vColour;
|
|
49
|
+
varying float vAlpha;
|
|
50
|
+
varying float vGlow;
|
|
51
|
+
void main() {
|
|
52
|
+
vec2 d = gl_PointCoord - 0.5;
|
|
53
|
+
float r2 = dot(d, d) * 4.0;
|
|
54
|
+
// Ordinary star: a soft core. Glow star: a tighter core plus a wide halo and a thin cross, so a
|
|
55
|
+
// handful of stars read as bright without a bloom pass.
|
|
56
|
+
float core = exp(-r2 * mix(3.5, 11.0, vGlow));
|
|
57
|
+
float halo = vGlow * (0.4 * exp(-r2 * 3.0) + 0.3 * exp(-abs(d.x * d.y) * 700.0) * (1.0 - r2));
|
|
58
|
+
// No discard (it defeats early depth on tile GPUs); additive black adds nothing.
|
|
59
|
+
gl_FragColor = vec4(vColour * (core + max(halo, 0.0)) * vAlpha, 1.0);
|
|
60
|
+
}
|
|
61
|
+
`;
|
|
62
|
+
|
|
63
|
+
// Lines and the nebula box sit at a real radius just inside the far plane, not at xyww: pinning z to
|
|
64
|
+
// w breaks clipping at w = 0, and an edge that crosses behind the camera then streaks across the
|
|
65
|
+
// frame. The box's corners are √3 out, hence 0.5 of the far plane.
|
|
66
|
+
|
|
67
|
+
export const NEBULA_VERTEX = /* glsl */ `
|
|
68
|
+
uniform float uFar;
|
|
69
|
+
varying vec3 vDir;
|
|
70
|
+
void main() {
|
|
71
|
+
vDir = position;
|
|
72
|
+
gl_Position = projectionMatrix * vec4(mat3(modelViewMatrix) * position * uFar * 0.5, 1.0);
|
|
73
|
+
}
|
|
74
|
+
`;
|
|
75
|
+
|
|
76
|
+
// Colours the baked glow field: R its strength, G where its hue wanders, B how much it is the
|
|
77
|
+
// band. Glow, hue and hue spread are uniforms here, so dragging them never re-bakes.
|
|
78
|
+
export const NEBULA_FRAGMENT = /* glsl */ `
|
|
79
|
+
varying vec3 vDir;
|
|
80
|
+
uniform samplerCube uNebula;
|
|
81
|
+
uniform float uAmount;
|
|
82
|
+
uniform float uHue;
|
|
83
|
+
uniform float uSpread;
|
|
84
|
+
vec3 hsv(float h, float s, float v) {
|
|
85
|
+
vec3 k = clamp(abs(mod(h / 60.0 + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0);
|
|
86
|
+
return v * mix(vec3(1.0), k, s);
|
|
87
|
+
}
|
|
88
|
+
void main() {
|
|
89
|
+
vec3 f = textureCube(uNebula, vDir).rgb;
|
|
90
|
+
vec3 c = hsv(uHue + uSpread * (f.g - 0.5) * 3.0, 0.7 - 0.3 * f.b, 1.0) * f.r * uAmount;
|
|
91
|
+
// Under a pixel of dither (interleaved gradient noise): the 8-bit screen would otherwise band
|
|
92
|
+
// the glow's last faint steps into black.
|
|
93
|
+
float dither = fract(52.9829189 * fract(dot(gl_FragCoord.xy, vec2(0.06711056, 0.00583715)))) - 0.5;
|
|
94
|
+
gl_FragColor = vec4(max(c + dither / 255.0, 0.0), 1.0);
|
|
95
|
+
}
|
|
96
|
+
`;
|
|
97
|
+
|
|
98
|
+
export const LINE_VERTEX = /* glsl */ `
|
|
99
|
+
uniform float uFar;
|
|
100
|
+
void main() {
|
|
101
|
+
gl_Position = projectionMatrix * vec4(mat3(modelViewMatrix) * normalize(position) * uFar * 0.9, 1.0);
|
|
102
|
+
}
|
|
103
|
+
`;
|
|
104
|
+
|
|
105
|
+
export const LINE_FRAGMENT = /* glsl */ `
|
|
106
|
+
uniform float uLines;
|
|
107
|
+
void main() { gl_FragColor = vec4(vec3(0.45, 0.6, 0.9) * uLines * 0.35, 1.0); }
|
|
108
|
+
`;
|
|
109
|
+
|
|
110
|
+
export const BAKE_VERTEX = /* glsl */ `
|
|
111
|
+
varying vec3 vDir;
|
|
112
|
+
void main() {
|
|
113
|
+
vDir = position;
|
|
114
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
|
115
|
+
}
|
|
116
|
+
`;
|
|
117
|
+
|
|
118
|
+
// Runs once per bake over the cube's texels: domain-warped fbm clouds, gathered into the band,
|
|
119
|
+
// brighter toward its core, cut by dust lanes. It bakes the glow's shape only (strength, hue
|
|
120
|
+
// driver, band share); NEBULA_FRAGMENT colours it.
|
|
121
|
+
export const BAKE_FRAGMENT = /* glsl */ `
|
|
122
|
+
varying vec3 vDir;
|
|
123
|
+
uniform float uSeed;
|
|
124
|
+
uniform float uBand;
|
|
125
|
+
uniform mat3 uToBand;
|
|
126
|
+
uniform vec3 uCoreDir;
|
|
127
|
+
float hash(vec3 p) {
|
|
128
|
+
p = fract(p * 0.3183099 + vec3(0.71, 0.113, 0.419) * (1.0 + uSeed * 0.137));
|
|
129
|
+
p *= 17.0;
|
|
130
|
+
return fract(p.x * p.y * p.z * (p.x + p.y + p.z));
|
|
131
|
+
}
|
|
132
|
+
float noise(vec3 x) {
|
|
133
|
+
vec3 i = floor(x);
|
|
134
|
+
vec3 f = fract(x);
|
|
135
|
+
f = f * f * (3.0 - 2.0 * f);
|
|
136
|
+
return mix(mix(mix(hash(i), hash(i + vec3(1, 0, 0)), f.x), mix(hash(i + vec3(0, 1, 0)), hash(i + vec3(1, 1, 0)), f.x), f.y),
|
|
137
|
+
mix(mix(hash(i + vec3(0, 0, 1)), hash(i + vec3(1, 0, 1)), f.x), mix(hash(i + vec3(0, 1, 1)), hash(i + vec3(1, 1, 1)), f.x), f.y), f.z);
|
|
138
|
+
}
|
|
139
|
+
float fbm(vec3 p) {
|
|
140
|
+
float a = 0.5, s = 0.0;
|
|
141
|
+
for (int i = 0; i < 7; i++) { s += a * noise(p); p = p * 2.03 + vec3(1.7, 9.2, 4.1); a *= 0.5; }
|
|
142
|
+
return s;
|
|
143
|
+
}
|
|
144
|
+
void main() {
|
|
145
|
+
vec3 d = normalize(vDir);
|
|
146
|
+
vec3 q = d * 2.2 + vec3(uSeed * 3.17, uSeed * 1.31, uSeed * 2.07);
|
|
147
|
+
vec3 w = vec3(fbm(q), fbm(q + vec3(5.2, 1.3, 2.8)), fbm(q + vec3(9.1, 4.7, 7.3)));
|
|
148
|
+
float n = fbm(q + 1.9 * w);
|
|
149
|
+
float lat = (uToBand * d).y;
|
|
150
|
+
float bandMask = exp(-lat * lat / 0.06);
|
|
151
|
+
float density = smoothstep(0.42, 0.9, n) * mix(1.0, 0.25 + 1.6 * bandMask, uBand);
|
|
152
|
+
float core = exp(-(1.0 - dot(d, uCoreDir)) * 5.0) * bandMask * uBand;
|
|
153
|
+
density *= 1.0 + 1.8 * core;
|
|
154
|
+
float dust = smoothstep(0.5, 0.75, fbm(q * 2.7 + w * 1.3 + 3.0));
|
|
155
|
+
density *= 1.0 - 0.75 * dust * bandMask * uBand;
|
|
156
|
+
density += 0.12 * uBand * bandMask * (0.6 + 0.4 * n);
|
|
157
|
+
gl_FragColor = vec4(density * 0.42, w.x, bandMask * uBand, 1.0);
|
|
158
|
+
}
|
|
159
|
+
`;
|
package/src/sky/sky.ts
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
// The sky behind a world: stars (one Points draw), an optional nebula + Milky Way whose shape is
|
|
2
|
+
// baked into a small cube target shared by every sky on the renderer with the same shape (one
|
|
3
|
+
// textured box per frame, coloured live), and, under a world sky, the constellation figures (one
|
|
4
|
+
// LineSegments). All three draw after the opaque bodies with depth test
|
|
5
|
+
// on, so sky behind a planet costs nothing.
|
|
6
|
+
//
|
|
7
|
+
// Cost, measured against the six-face painted skycube it replaces (bench/sky): stars alone about
|
|
8
|
+
// 40% of its frame time; with the nebula about the same frame time, at 1/5–1/15 of its GPU memory
|
|
9
|
+
// and with nothing to download.
|
|
10
|
+
|
|
11
|
+
import * as THREE from 'three';
|
|
12
|
+
import { DEFAULT_SKY_PARAMS, SKY_LOOK_KEYS, normalizeSkyParams, type SkyParams } from './params.js';
|
|
13
|
+
import { buildLineGeometry, buildStarGeometry, galacticBandFrame, seededBandFrame, type BandFrame } from './geometry.js';
|
|
14
|
+
import { createLcg } from '../star/layout.js';
|
|
15
|
+
import type { WorldSky } from './worldSkyFile.js';
|
|
16
|
+
import {
|
|
17
|
+
BAKE_FRAGMENT,
|
|
18
|
+
BAKE_VERTEX,
|
|
19
|
+
GLOW_SEEDED,
|
|
20
|
+
GLOW_WORLD,
|
|
21
|
+
LINE_FRAGMENT,
|
|
22
|
+
LINE_VERTEX,
|
|
23
|
+
NEBULA_FRAGMENT,
|
|
24
|
+
NEBULA_VERTEX,
|
|
25
|
+
STAR_FRAGMENT,
|
|
26
|
+
STAR_VERTEX,
|
|
27
|
+
} from './shaders.js';
|
|
28
|
+
|
|
29
|
+
export interface CreateSkyOptions {
|
|
30
|
+
/** The world's sky (`parseWorldSky`), used when `params.source` is `'world'`. */
|
|
31
|
+
world?: WorldSky | null;
|
|
32
|
+
/** Nebula cube face size: 512 on desktops, 256 on phones (the clouds are soft either way). */
|
|
33
|
+
nebulaSize?: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface Sky {
|
|
37
|
+
/** Add to the scene. Rotate it to turn the sky. */
|
|
38
|
+
readonly object: THREE.Group;
|
|
39
|
+
/** Applies a partial change: uniforms only for look params, a re-bake for nebula params, a rebuild for shape params. */
|
|
40
|
+
setParams(next: Partial<SkyParams>): void;
|
|
41
|
+
/** Swaps the world sky (a new host, or none). Rebuilds when the source is `'world'`. */
|
|
42
|
+
setWorld(world: WorldSky | null): void;
|
|
43
|
+
/** Call once a frame before rendering: clock time in seconds (the playback clock) and the drawing
|
|
44
|
+
* buffer's height in pixels. Runs a pending nebula bake. */
|
|
45
|
+
update(timeSec: number, drawingBufferHeight: number): void;
|
|
46
|
+
/** Bytes the sky holds on the GPU. */
|
|
47
|
+
gpuBytes(): number;
|
|
48
|
+
dispose(): void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const RENDER_ORDER = 1000;
|
|
52
|
+
|
|
53
|
+
// ── The glow's shape, shared ────────────────────────────────────────────────────────────────
|
|
54
|
+
// What the bake writes depends only on the seed, the band and its frame, so every sky on one
|
|
55
|
+
// renderer with the same of those (every world sky with the default look) shares one cube and
|
|
56
|
+
// one bake. Glow, hue and hue spread colour it at draw time.
|
|
57
|
+
|
|
58
|
+
interface GlowField {
|
|
59
|
+
target: THREE.WebGLCubeRenderTarget;
|
|
60
|
+
bytes: number;
|
|
61
|
+
refs: number;
|
|
62
|
+
baked: boolean;
|
|
63
|
+
/** Bake inputs, set when the field is made. */
|
|
64
|
+
seed: number;
|
|
65
|
+
band: number;
|
|
66
|
+
frame: BandFrame;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface BakeKit {
|
|
70
|
+
scene: THREE.Scene;
|
|
71
|
+
geometry: THREE.BoxGeometry;
|
|
72
|
+
material: THREE.ShaderMaterial;
|
|
73
|
+
fields: Map<string, GlowField>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const kits = new WeakMap<THREE.WebGLRenderer, BakeKit>();
|
|
77
|
+
|
|
78
|
+
function kitFor(renderer: THREE.WebGLRenderer): BakeKit {
|
|
79
|
+
let kit = kits.get(renderer);
|
|
80
|
+
if (!kit) {
|
|
81
|
+
const uniforms = {
|
|
82
|
+
uSeed: { value: 0 },
|
|
83
|
+
uBand: { value: 0 },
|
|
84
|
+
uToBand: { value: new THREE.Matrix3() },
|
|
85
|
+
uCoreDir: { value: new THREE.Vector3(1, 0, 0) },
|
|
86
|
+
};
|
|
87
|
+
const geometry = new THREE.BoxGeometry(2, 2, 2);
|
|
88
|
+
const material = new THREE.ShaderMaterial({ uniforms, vertexShader: BAKE_VERTEX, fragmentShader: BAKE_FRAGMENT, side: THREE.BackSide, depthTest: false, depthWrite: false });
|
|
89
|
+
const scene = new THREE.Scene();
|
|
90
|
+
scene.add(new THREE.Mesh(geometry, material));
|
|
91
|
+
kit = { scene, geometry, material, fields: new Map() };
|
|
92
|
+
kits.set(renderer, kit);
|
|
93
|
+
}
|
|
94
|
+
return kit;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function acquireField(renderer: THREE.WebGLRenderer, size: number, seed: number, band: number, frame: BandFrame, frameKey: string): { key: string; field: GlowField } {
|
|
98
|
+
const kit = kitFor(renderer);
|
|
99
|
+
const key = `${size}|${seed}|${band}|${frameKey}`;
|
|
100
|
+
let field = kit.fields.get(key);
|
|
101
|
+
if (!field) {
|
|
102
|
+
// Half-float where the GPU can render to it: the glow fades into the black over a handful of
|
|
103
|
+
// 8-bit levels, which show as hard stair-stepped contours. No depth: the bake tests none.
|
|
104
|
+
const ext = renderer.extensions;
|
|
105
|
+
const halfFloat = Boolean(ext?.has('EXT_color_buffer_half_float') || ext?.has('EXT_color_buffer_float'));
|
|
106
|
+
const target = new THREE.WebGLCubeRenderTarget(size, {
|
|
107
|
+
generateMipmaps: false,
|
|
108
|
+
depthBuffer: false,
|
|
109
|
+
minFilter: THREE.LinearFilter,
|
|
110
|
+
magFilter: THREE.LinearFilter,
|
|
111
|
+
type: halfFloat ? THREE.HalfFloatType : THREE.UnsignedByteType,
|
|
112
|
+
});
|
|
113
|
+
field = { target, bytes: 6 * size * size * (halfFloat ? 8 : 4), refs: 0, baked: false, seed, band, frame };
|
|
114
|
+
kit.fields.set(key, field);
|
|
115
|
+
}
|
|
116
|
+
field.refs++;
|
|
117
|
+
return { key, field };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function releaseField(renderer: THREE.WebGLRenderer, key: string): void {
|
|
121
|
+
const kit = kits.get(renderer);
|
|
122
|
+
const field = kit?.fields.get(key);
|
|
123
|
+
if (!kit || !field || --field.refs > 0) return;
|
|
124
|
+
field.target.dispose();
|
|
125
|
+
kit.fields.delete(key);
|
|
126
|
+
// The last field gone: the bake's own box and shader go too, until a glow is wanted again.
|
|
127
|
+
if (kit.fields.size === 0) {
|
|
128
|
+
kit.geometry.dispose();
|
|
129
|
+
kit.material.dispose();
|
|
130
|
+
kits.delete(renderer);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function bakeField(renderer: THREE.WebGLRenderer, field: GlowField): void {
|
|
135
|
+
const kit = kitFor(renderer);
|
|
136
|
+
const u = kit.material.uniforms;
|
|
137
|
+
// The shader offsets noise by the seed in float32: a 31-bit seed would erase its fraction, so
|
|
138
|
+
// the shader gets a small value derived from it.
|
|
139
|
+
u.uSeed.value = createLcg(field.seed)() * 1000;
|
|
140
|
+
u.uBand.value = field.band;
|
|
141
|
+
(u.uToBand.value as THREE.Matrix3).setFromMatrix4(field.frame.basis).transpose();
|
|
142
|
+
(u.uCoreDir.value as THREE.Vector3).copy(field.frame.core);
|
|
143
|
+
new THREE.CubeCamera(0.1, 10, field.target).update(renderer, kit.scene);
|
|
144
|
+
field.baked = true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function createSky(renderer: THREE.WebGLRenderer, initial: Partial<SkyParams> = {}, options: CreateSkyOptions = {}): Sky {
|
|
148
|
+
let p: SkyParams = normalizeSkyParams({ ...DEFAULT_SKY_PARAMS, ...initial });
|
|
149
|
+
let world: WorldSky | null = options.world ?? null;
|
|
150
|
+
const nebulaSize = options.nebulaSize ?? 512;
|
|
151
|
+
const usesWorld = () => p.source === 'world' && world !== null;
|
|
152
|
+
const frame = (): BandFrame => (usesWorld() ? galacticBandFrame() : seededBandFrame(p.seed, p.bandTilt));
|
|
153
|
+
const frameKey = () => (usesWorld() ? 'galactic' : `seeded:${p.bandTilt}`);
|
|
154
|
+
|
|
155
|
+
const farUniform = { value: 1000 };
|
|
156
|
+
const starUniforms = {
|
|
157
|
+
uTime: { value: 0 },
|
|
158
|
+
uBrightness: { value: p.brightness },
|
|
159
|
+
uSize: { value: p.size },
|
|
160
|
+
uColour: { value: p.colour },
|
|
161
|
+
uTwinkle: { value: p.twinkle },
|
|
162
|
+
uPixelScale: { value: 1 },
|
|
163
|
+
uGlowAt: { value: GLOW_SEEDED },
|
|
164
|
+
};
|
|
165
|
+
const starMat = new THREE.ShaderMaterial({
|
|
166
|
+
uniforms: starUniforms,
|
|
167
|
+
vertexShader: STAR_VERTEX,
|
|
168
|
+
fragmentShader: STAR_FRAGMENT,
|
|
169
|
+
blending: THREE.AdditiveBlending,
|
|
170
|
+
depthWrite: false,
|
|
171
|
+
transparent: true,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// The glow: a box at infinity sampling the shared field, coloured by these uniforms.
|
|
175
|
+
const nebulaUniforms = {
|
|
176
|
+
uNebula: { value: null as THREE.CubeTexture | null },
|
|
177
|
+
uFar: farUniform,
|
|
178
|
+
uAmount: { value: p.nebula },
|
|
179
|
+
uHue: { value: p.hue },
|
|
180
|
+
uSpread: { value: p.hueSpread },
|
|
181
|
+
};
|
|
182
|
+
const nebulaMat = new THREE.ShaderMaterial({
|
|
183
|
+
uniforms: nebulaUniforms,
|
|
184
|
+
vertexShader: NEBULA_VERTEX,
|
|
185
|
+
fragmentShader: NEBULA_FRAGMENT,
|
|
186
|
+
side: THREE.BackSide,
|
|
187
|
+
depthWrite: false,
|
|
188
|
+
});
|
|
189
|
+
const nebula = new THREE.Mesh(new THREE.BoxGeometry(2, 2, 2), nebulaMat);
|
|
190
|
+
nebula.name = 'SkyNebula';
|
|
191
|
+
nebula.frustumCulled = false;
|
|
192
|
+
// Opaque, but drawn after every opaque body: the depth test skips the pixels a planet covers.
|
|
193
|
+
nebula.renderOrder = RENDER_ORDER;
|
|
194
|
+
// Every layer reads the camera's far plane: whichever draws first sets it for the frame.
|
|
195
|
+
const trackFar: THREE.Object3D['onBeforeRender'] = (_r, _s, cam) => {
|
|
196
|
+
farUniform.value = (cam as THREE.PerspectiveCamera).far;
|
|
197
|
+
};
|
|
198
|
+
nebula.onBeforeRender = trackFar;
|
|
199
|
+
|
|
200
|
+
const lineUniforms = { uLines: { value: p.constellations }, uFar: farUniform };
|
|
201
|
+
const lineMat = new THREE.ShaderMaterial({ uniforms: lineUniforms, vertexShader: LINE_VERTEX, fragmentShader: LINE_FRAGMENT, blending: THREE.AdditiveBlending, depthWrite: false, transparent: true });
|
|
202
|
+
|
|
203
|
+
const object = new THREE.Group();
|
|
204
|
+
object.name = 'Sky';
|
|
205
|
+
object.add(nebula);
|
|
206
|
+
let stars: THREE.Points | null = null;
|
|
207
|
+
let lines: THREE.LineSegments | null = null;
|
|
208
|
+
let linesFor: WorldSky | null = null;
|
|
209
|
+
|
|
210
|
+
// The glow field this sky holds (none while the glow is off: its memory goes back).
|
|
211
|
+
let held: { key: string; field: GlowField } | null = null;
|
|
212
|
+
const syncGlow = () => {
|
|
213
|
+
const wanted = p.nebula > 0 ? `${nebulaSize}|${p.seed}|${p.band}|${frameKey()}` : null;
|
|
214
|
+
if (held?.key === wanted) return;
|
|
215
|
+
if (held) releaseField(renderer, held.key);
|
|
216
|
+
held = wanted ? acquireField(renderer, nebulaSize, p.seed, p.band, frame(), frameKey()) : null;
|
|
217
|
+
nebulaUniforms.uNebula.value = held ? held.field.target.texture : null;
|
|
218
|
+
nebula.visible = held !== null;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const buildStars = () => {
|
|
222
|
+
if (stars) {
|
|
223
|
+
object.remove(stars);
|
|
224
|
+
stars.geometry.dispose();
|
|
225
|
+
}
|
|
226
|
+
const w = usesWorld() ? world : null;
|
|
227
|
+
stars = new THREE.Points(buildStarGeometry({ seed: p.seed, count: p.count, band: p.band, frame: frame(), world: w }), starMat);
|
|
228
|
+
stars.name = 'SkyStars';
|
|
229
|
+
stars.frustumCulled = false;
|
|
230
|
+
stars.renderOrder = RENDER_ORDER + 1;
|
|
231
|
+
stars.onBeforeRender = trackFar;
|
|
232
|
+
object.add(stars);
|
|
233
|
+
starUniforms.uGlowAt.value = w ? GLOW_WORLD : GLOW_SEEDED;
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
// The figures depend on the world sky alone: kept across every other change.
|
|
237
|
+
const syncLines = () => {
|
|
238
|
+
const w = usesWorld() ? world : null;
|
|
239
|
+
if (w === linesFor) return;
|
|
240
|
+
linesFor = w;
|
|
241
|
+
if (lines) {
|
|
242
|
+
object.remove(lines);
|
|
243
|
+
lines.geometry.dispose();
|
|
244
|
+
lines = null;
|
|
245
|
+
}
|
|
246
|
+
if (w && w.lines.length > 0) {
|
|
247
|
+
lines = new THREE.LineSegments(buildLineGeometry(w), lineMat);
|
|
248
|
+
lines.name = 'SkyConstellations';
|
|
249
|
+
lines.frustumCulled = false;
|
|
250
|
+
lines.renderOrder = RENDER_ORDER + 2;
|
|
251
|
+
lines.onBeforeRender = trackFar;
|
|
252
|
+
object.add(lines);
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
const applyLook = () => {
|
|
257
|
+
starUniforms.uBrightness.value = p.brightness;
|
|
258
|
+
starUniforms.uSize.value = p.size;
|
|
259
|
+
starUniforms.uColour.value = p.colour;
|
|
260
|
+
starUniforms.uTwinkle.value = p.twinkle;
|
|
261
|
+
lineUniforms.uLines.value = p.constellations;
|
|
262
|
+
if (lines) lines.visible = p.constellations > 0;
|
|
263
|
+
nebulaUniforms.uAmount.value = p.nebula;
|
|
264
|
+
nebulaUniforms.uHue.value = p.hue;
|
|
265
|
+
nebulaUniforms.uSpread.value = p.hueSpread;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// A rebuild runs at most once a frame, from `update`: a density drag that moves many times
|
|
269
|
+
// between frames builds once.
|
|
270
|
+
let rebuildPending = false;
|
|
271
|
+
const rebuild = () => {
|
|
272
|
+
buildStars();
|
|
273
|
+
syncLines();
|
|
274
|
+
syncGlow();
|
|
275
|
+
applyLook();
|
|
276
|
+
};
|
|
277
|
+
rebuild();
|
|
278
|
+
|
|
279
|
+
const bytes = (g: THREE.BufferGeometry) => {
|
|
280
|
+
let n = 0;
|
|
281
|
+
for (const a of Object.values(g.attributes)) n += (a as THREE.BufferAttribute).array.byteLength;
|
|
282
|
+
return n;
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
return {
|
|
286
|
+
object,
|
|
287
|
+
setParams(next) {
|
|
288
|
+
const prev = p;
|
|
289
|
+
p = normalizeSkyParams({ ...p, ...next });
|
|
290
|
+
// A world sky's band is the galactic plane: its tilt changes nothing there.
|
|
291
|
+
const changed = (Object.keys(next) as (keyof SkyParams)[]).filter((k) => prev[k] !== p[k] && !(k === 'bandTilt' && usesWorld()));
|
|
292
|
+
if (changed.length === 0) return;
|
|
293
|
+
applyLook();
|
|
294
|
+
// Glow on or off: hold or let go of its field now (no star work).
|
|
295
|
+
syncGlow();
|
|
296
|
+
if (!changed.every((k) => (SKY_LOOK_KEYS as readonly string[]).includes(k))) rebuildPending = true;
|
|
297
|
+
},
|
|
298
|
+
setWorld(next) {
|
|
299
|
+
if (next === world) return;
|
|
300
|
+
world = next;
|
|
301
|
+
if (p.source === 'world') rebuildPending = true;
|
|
302
|
+
},
|
|
303
|
+
update(timeSec, drawingBufferHeight) {
|
|
304
|
+
if (rebuildPending) {
|
|
305
|
+
rebuildPending = false;
|
|
306
|
+
rebuild();
|
|
307
|
+
}
|
|
308
|
+
if (held && !held.field.baked) bakeField(renderer, held.field);
|
|
309
|
+
starUniforms.uTime.value = timeSec;
|
|
310
|
+
starUniforms.uPixelScale.value = drawingBufferHeight / 1080;
|
|
311
|
+
},
|
|
312
|
+
gpuBytes() {
|
|
313
|
+
let n = stars ? bytes(stars.geometry) : 0;
|
|
314
|
+
if (lines) n += bytes(lines.geometry);
|
|
315
|
+
// The glow field is shared: each sky holding it counts its share.
|
|
316
|
+
if (held) n += held.field.bytes / held.field.refs;
|
|
317
|
+
return n;
|
|
318
|
+
},
|
|
319
|
+
dispose() {
|
|
320
|
+
stars?.geometry.dispose();
|
|
321
|
+
lines?.geometry.dispose();
|
|
322
|
+
starMat.dispose();
|
|
323
|
+
lineMat.dispose();
|
|
324
|
+
nebula.geometry.dispose();
|
|
325
|
+
nebulaMat.dispose();
|
|
326
|
+
if (held) releaseField(renderer, held.key);
|
|
327
|
+
held = null;
|
|
328
|
+
},
|
|
329
|
+
};
|
|
330
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// The world-sky file: a host star's night sky, worked out once by the backend's catalogue sync and
|
|
2
|
+
// read here without a copy or any star maths. Format 1, little-endian (N stars, L line vertices):
|
|
3
|
+
//
|
|
4
|
+
// offset size type field
|
|
5
|
+
// 0 4 ASCII magic "WSKY"
|
|
6
|
+
// 4 2 uint16 format version (1)
|
|
7
|
+
// 6 2 uint16 header size in bytes; stars start here
|
|
8
|
+
// 8 4 uint32 dataset version
|
|
9
|
+
// 12 4 uint32 N, star count
|
|
10
|
+
// 16 4 uint32 L, line-vertex count (even: vertices 2k, 2k+1 are one segment)
|
|
11
|
+
// 20 4 uint32 stand-in count: seeded stars to add for an Earth-like sky
|
|
12
|
+
// 24 8 — reserved
|
|
13
|
+
// H 6·N int16 × 3 star directions (unit vector × 32767), scene frame
|
|
14
|
+
// H + 6N 4·N uint8 × 4 star colour (r g b) + apparent brightness (a: mag −1 → 255, 6.5 → 0)
|
|
15
|
+
// H + 10N 6·L int16 × 3 constellation line vertices (arcs already split)
|
|
16
|
+
//
|
|
17
|
+
// Scene frame: y toward celestial north; x = cos(dec)·cos(ra), z = −cos(dec)·sin(ra).
|
|
18
|
+
|
|
19
|
+
export const WORLD_SKY_MAGIC = 'WSKY';
|
|
20
|
+
export const WORLD_SKY_FORMAT = 1;
|
|
21
|
+
|
|
22
|
+
export interface WorldSky {
|
|
23
|
+
datasetVersion: number;
|
|
24
|
+
starCount: number;
|
|
25
|
+
/** Star directions, `Int16 × 3` per star; a view into the file. */
|
|
26
|
+
directions: Int16Array;
|
|
27
|
+
/** Star colour + brightness, `Uint8 × 4` per star; a view into the file. */
|
|
28
|
+
colourBrightness: Uint8Array;
|
|
29
|
+
/** Constellation line vertices, `Int16 × 3` each, in segment pairs; a view into the file. */
|
|
30
|
+
lines: Int16Array;
|
|
31
|
+
/** Seeded stars to add so a far host's sky still reads Earth-like. */
|
|
32
|
+
standIns: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Wraps a world-sky file. Throws on anything that is not a complete format-1 file. */
|
|
36
|
+
export function parseWorldSky(buffer: ArrayBuffer): WorldSky {
|
|
37
|
+
if (buffer.byteLength < 24) throw new Error('world sky: file too short');
|
|
38
|
+
const view = new DataView(buffer);
|
|
39
|
+
const magic = String.fromCharCode(view.getUint8(0), view.getUint8(1), view.getUint8(2), view.getUint8(3));
|
|
40
|
+
if (magic !== WORLD_SKY_MAGIC) throw new Error('world sky: not a WSKY file');
|
|
41
|
+
const format = view.getUint16(4, true);
|
|
42
|
+
if (format !== WORLD_SKY_FORMAT) throw new Error(`world sky: unsupported format ${format}`);
|
|
43
|
+
const header = view.getUint16(6, true);
|
|
44
|
+
const datasetVersion = view.getUint32(8, true);
|
|
45
|
+
const starCount = view.getUint32(12, true);
|
|
46
|
+
const lineVertices = view.getUint32(16, true);
|
|
47
|
+
const standIns = view.getUint32(20, true);
|
|
48
|
+
if (header < 24 || header % 2 !== 0) throw new Error('world sky: bad header size');
|
|
49
|
+
if (lineVertices % 2 !== 0) throw new Error('world sky: odd line-vertex count');
|
|
50
|
+
const size = header + 10 * starCount + 6 * lineVertices;
|
|
51
|
+
if (buffer.byteLength < size) throw new Error('world sky: file truncated');
|
|
52
|
+
return {
|
|
53
|
+
datasetVersion,
|
|
54
|
+
starCount,
|
|
55
|
+
directions: new Int16Array(buffer, header, 3 * starCount),
|
|
56
|
+
colourBrightness: new Uint8Array(buffer, header + 6 * starCount, 4 * starCount),
|
|
57
|
+
lines: new Int16Array(buffer, header + 10 * starCount, 3 * lineVertices),
|
|
58
|
+
standIns,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { ShortcutAction } from './match.js';
|
|
2
|
-
export declare const EW_STUDIO_SECTION = "ew-studio";
|
|
3
|
-
export declare const EW_PLAYER_SECTION = "ew-player";
|
|
4
|
-
/** The studio's actions all act on what is open in it; the player has no other scope. */
|
|
5
|
-
export type EntangledWorldsShortcut = ShortcutAction<'studio' | 'player'>;
|
|
6
|
-
/** How the studio's command menu groups its actions, in its order. */
|
|
7
|
-
export declare const EW_STUDIO_COMMAND_GROUPS: readonly [{
|
|
8
|
-
readonly id: "edit";
|
|
9
|
-
readonly label: "Edit";
|
|
10
|
-
}, {
|
|
11
|
-
readonly id: "playback";
|
|
12
|
-
readonly label: "Playback";
|
|
13
|
-
}, {
|
|
14
|
-
readonly id: "steps";
|
|
15
|
-
readonly label: "Steps";
|
|
16
|
-
}, {
|
|
17
|
-
readonly id: "help";
|
|
18
|
-
readonly label: "Help";
|
|
19
|
-
}];
|
|
20
|
-
export declare const EW_STUDIO_SHORTCUTS: readonly EntangledWorldsShortcut[];
|
|
21
|
-
export declare const EW_PLAYER_SHORTCUTS: readonly EntangledWorldsShortcut[];
|