@arraypress/effects-astro 0.14.0 → 0.15.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.
@@ -0,0 +1,52 @@
1
+ type Quiet = { x?: number; y?: number; radius?: number; depth?: number } | null;
2
+
3
+ /** Blinds or a prism's spectrum. */
4
+ export type SunWallMode = 'blinds' | 'prism';
5
+
6
+ export interface SunWallConfig {
7
+ /** Blinds or a prism's spectrum. Default 'blinds'. */
8
+ mode?: SunWallMode;
9
+ /** The wall's paint. Default '#cdb89d'. */
10
+ wall?: string;
11
+ /** The sunlight's colour. Default '#ffe2b0'. */
12
+ sun?: string;
13
+ /** How strong the sun is. Default 1. */
14
+ exposure?: number;
15
+ /** How lit the wall is in shade, 0–1. Default 0.34. */
16
+ ambient?: number;
17
+ /** Which way the light falls across the wall, degrees from upright. Default -24. */
18
+ angle?: number;
19
+ /** How many bars in the patch (blinds). Default 11. */
20
+ slats?: number;
21
+ /** How much of each bar's pitch lets light through, 0–1 (blinds). Default 0.55. */
22
+ open?: number;
23
+ /** How much the bars and the patch soften with distance, 0–1. Default 0.6. */
24
+ soft?: number;
25
+ /** The light patch's centre, 0–1 from the top left. Default [0.5, 0.48]. */
26
+ patch?: [number, number];
27
+ /** The patch's width and height, shares of the card's. Default [0.62, 0.72]. */
28
+ size?: [number, number];
29
+ /** The spectrum's width, a share of the height (prism). Default 0.065. */
30
+ band?: number;
31
+ /** How much the spectrum bows (prism). Default 0.05. */
32
+ bow?: number;
33
+ /** How far the light creeps, a share of the height. Default 0.05. */
34
+ drift?: number;
35
+ /** How fast it creeps, cycles a second. Default 0.06. */
36
+ rate?: number;
37
+ /** Fine grain; 0 for none. Default 0.03. */
38
+ grain?: number;
39
+ /** Where its creeping starts. Default 0. */
40
+ seed?: number;
41
+ /** Plays every clock faster or slower; 0 for one still frame. Default 1. */
42
+ speed?: number;
43
+ /** An area dimmed: centre (0–1), radius (share of the height), depth (0–1). */
44
+ quiet?: Quiet;
45
+ }
46
+
47
+ export declare const SUN_MODES: readonly SunWallMode[];
48
+ export declare const SUN_DEFAULTS: Required<Omit<SunWallConfig, 'speed' | 'quiet'>>;
49
+ export declare function spectrumRgb(x: number): [number, number, number];
50
+ export declare function blindsAt(config: SunWallConfig, x: number, y: number, aspect: number, time?: number): number;
51
+ export declare function sunWallStill(config: SunWallConfig): string;
52
+ export declare function sunWall(el: HTMLElement, config: SunWallConfig): () => void;
@@ -0,0 +1,309 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/sun-wall
3
+ *
4
+ * Low sunlight on a plaster wall, in one of two ways a room makes it:
5
+ *
6
+ * - `blinds`: the window's shape thrown on the wall through venetian blinds,
7
+ * a skewed patch of hard bright bars with dark gaps. The bars sharpen
8
+ * nearest the window and soften with distance (the penumbra grows with the
9
+ * throw, as the sun is not a point), and they breathe slightly as the slats
10
+ * tilt; the whole patch creeps as the sun moves.
11
+ * - `prism`: a patch of sunlight with a sliver of spectrum laid across the
12
+ * wall, as a glass edge or a crystal throws one: red on one side to violet
13
+ * on the other, brightest in the yellow-green, its edges soft with
14
+ * dispersion, slightly bowed, sliding slowly.
15
+ *
16
+ * The light is multiplied into the wall's own colour (paint takes light, it
17
+ * doesn't glow), the wall has a fine plaster grain and a soft fall-off from
18
+ * the light, and a filmic shoulder lets the brightest bars burn without
19
+ * clipping.
20
+ *
21
+ * Every clock is in seconds and nothing is kept between frames; the light is
22
+ * here in plain JavaScript too (`blindsAt`, `spectrumRgb`).
23
+ *
24
+ * Opaque: it paints the wall everywhere. Without WebGL nothing is drawn; with
25
+ * prefers-reduced-motion it is one still frame.
26
+ */
27
+
28
+ import { createStage } from './stage.js';
29
+ import { GLSL_HASH33, GLSL_GRADIENT_NOISE } from './hash-noise.js';
30
+ import { parseColor, toHex, mixOklab } from './color.js';
31
+ import { GLSL_QUIET, quietUniform } from './glsl.js';
32
+
33
+ /**
34
+ * @typedef {'blinds' | 'prism'} SunWallMode
35
+ *
36
+ * @typedef {object} SunWallConfig
37
+ * @property {SunWallMode} [mode='blinds'] Blinds or a prism's spectrum.
38
+ * @property {string} [wall='#cdb89d'] The wall's paint.
39
+ * @property {string} [sun='#ffe2b0'] The sunlight's colour.
40
+ * @property {number} [exposure=1] How strong the sun is.
41
+ * @property {number} [ambient=0.34] How lit the wall is in shade, 0–1.
42
+ * @property {number} [angle=-24] Which way the light falls across the wall, degrees from upright.
43
+ * @property {number} [slats=11] How many bars in the patch (blinds).
44
+ * @property {number} [open=0.55] How much of each bar's pitch lets light through, 0–1 (blinds).
45
+ * @property {number} [soft=0.6] How much the bars soften with distance, 0–1 (blinds).
46
+ * @property {[number, number]} [patch=[0.5, 0.48]] The light patch's centre, 0–1 from the top left.
47
+ * @property {[number, number]} [size=[0.62, 0.72]] The patch's width and height, shares of the card's.
48
+ * @property {number} [band=0.065] The spectrum's width, a share of the height (prism).
49
+ * @property {number} [bow=0.05] How much the spectrum bows (prism).
50
+ * @property {number} [drift=0.05] How far the light creeps, a share of the height.
51
+ * @property {number} [rate=0.06] How fast it creeps, cycles a second.
52
+ * @property {number} [grain=0.03] Fine grain; 0 for none.
53
+ * @property {number} [seed=0] Where its creeping starts.
54
+ * @property {number} [speed=1] Plays every clock faster or slower; 0 for one still frame.
55
+ * @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null] An area dimmed (see `quietUniform`).
56
+ */
57
+
58
+ /** The modes, in the order the shader numbers them. */
59
+ export const SUN_MODES = /** @type {const} */ (['blinds', 'prism']);
60
+
61
+ /** The settings' defaults, shared by the shader and the JS twin. */
62
+ export const SUN_DEFAULTS = {
63
+ mode: /** @type {SunWallMode} */ ('blinds'),
64
+ wall: '#cdb89d',
65
+ sun: '#ffe2b0',
66
+ exposure: 1,
67
+ ambient: 0.34,
68
+ angle: -24,
69
+ slats: 11,
70
+ open: 0.55,
71
+ soft: 0.6,
72
+ patch: /** @type {[number, number]} */ ([0.5, 0.48]),
73
+ size: /** @type {[number, number]} */ ([0.62, 0.72]),
74
+ band: 0.065,
75
+ bow: 0.05,
76
+ drift: 0.05,
77
+ rate: 0.06,
78
+ grain: 0.03,
79
+ seed: 0,
80
+ };
81
+
82
+ const FRAG = `
83
+ precision highp float;
84
+ uniform vec2 uRes;
85
+ uniform float uTime;
86
+ uniform float uMode;
87
+ uniform vec3 uWall;
88
+ uniform vec3 uSun;
89
+ uniform float uExposure;
90
+ uniform float uAmbient;
91
+ uniform float uAngle;
92
+ uniform float uSlats;
93
+ uniform float uOpen;
94
+ uniform float uSoft;
95
+ uniform vec2 uPatch;
96
+ uniform vec2 uSize;
97
+ uniform float uBand;
98
+ uniform float uBow;
99
+ uniform float uDrift;
100
+ uniform float uRate;
101
+ uniform float uGrain;
102
+ uniform float uSeed;
103
+ ${GLSL_QUIET}
104
+ ${GLSL_HASH33}
105
+ ${GLSL_GRADIENT_NOISE}
106
+
107
+ // red (0) to violet (1): the visible spectrum as a prism spreads it, brightest in the yellow-green
108
+ vec3 spectrum(float x) {
109
+ vec3 c = mix(vec3(1.0, 0.08, 0.04), vec3(1.0, 0.55, 0.05), smoothstep(0.0, 0.2, x));
110
+ c = mix(c, vec3(1.0, 0.95, 0.2), smoothstep(0.15, 0.35, x));
111
+ c = mix(c, vec3(0.35, 1.0, 0.25), smoothstep(0.32, 0.5, x));
112
+ c = mix(c, vec3(0.1, 0.8, 1.0), smoothstep(0.48, 0.66, x));
113
+ c = mix(c, vec3(0.2, 0.3, 1.0), smoothstep(0.62, 0.82, x));
114
+ c = mix(c, vec3(0.55, 0.15, 0.95), smoothstep(0.8, 1.0, x));
115
+ float lum = 0.55 + 0.45 * exp(-pow((x - 0.36) / 0.3, 2.0));
116
+ return c * lum;
117
+ }
118
+
119
+ void main() {
120
+ vec2 px = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y);
121
+ float aspect = uRes.x / uRes.y;
122
+ vec2 p = px / uRes.y;
123
+ float ph = 6.2831853 * uTime * uRate;
124
+ vec2 creep = uDrift * vec2(sin(ph + uSeed), 0.6 * cos(ph * 0.8 + uSeed));
125
+ vec2 c = uPatch + creep;
126
+ // into the light's frame: v runs along the fall (downward, tilted by the angle), u across it
127
+ float a = radians(uAngle);
128
+ vec2 d = p - c;
129
+ vec2 uv = vec2(cos(a) * d.x + sin(a) * d.y, -sin(a) * d.x + cos(a) * d.y);
130
+ vec2 hs = 0.5 * uSize * vec2(aspect, 1.0);
131
+ // the patch, skewed along the fall as a window thrown at a low sun
132
+ vec2 s = vec2(uv.x - uv.y * 0.35, uv.y);
133
+ float edgeSoft = 0.012 + uSoft * 0.05 * clamp(0.5 + 0.5 * s.y / max(hs.y, 0.01), 0.0, 1.0);
134
+ float patch = smoothstep(hs.x + edgeSoft, hs.x - edgeSoft, abs(s.x)) * smoothstep(hs.y + edgeSoft, hs.y - edgeSoft, abs(s.y));
135
+
136
+ vec3 light = vec3(0.0);
137
+ if (uMode < 0.5) {
138
+ // the bars: across the fall, their pitch from the slat count; penumbra growing with the thr (downward)
139
+ float pitch = (2.0 * hs.y) / uSlats;
140
+ float tilt = uOpen + 0.06 * sin(ph * 1.3 + uSeed * 2.0);
141
+ float k = fract((s.y + hs.y) / pitch);
142
+ float thr = clamp(0.5 + s.y / max(2.0 * hs.y, 0.01), 0.0, 1.0);
143
+ float pen = 0.015 + uSoft * 0.11 * thr;
144
+ float bar = smoothstep(0.0, pen, k) * smoothstep(tilt, tilt - pen, k);
145
+ light = uSun * bar * patch * 1.9;
146
+ } else {
147
+ // a patch of sun, and beside it in the shade the spectrum a glass edge throws: a short bowed streak, red to violet
148
+ // across its width, its colours blurred into each other, fading at both ends
149
+ light = uSun * patch * 0.9;
150
+ vec2 b = uv - vec2(hs.x * 1.25, 0.0);
151
+ float bow = uBow * (b.y * b.y) / max(hs.y, 0.01);
152
+ float across = (b.x - bow) / uBand + 0.5;
153
+ float ends = smoothstep(hs.y * 0.75, hs.y * 0.1, abs(b.y + hs.y * 0.1));
154
+ float inside = smoothstep(-0.25, 0.12, across) * smoothstep(1.25, 0.88, across);
155
+ vec3 spec = spectrum(clamp(across, 0.0, 1.0));
156
+ spec = mix(vec3(dot(spec, vec3(0.3, 0.55, 0.15))), spec, 1.25);
157
+ light += max(spec, 0.0) * inside * ends * 2.6;
158
+ }
159
+
160
+ // the wall: plaster grain, darker toward the floor and the corners, lit by the light it gets
161
+ float plaster = (gnoise(p * 60.0) - 0.5) * 0.5 + (gnoise(p * 190.0) - 0.5) * 0.35;
162
+ float fall = 1.0 - 0.28 * smoothstep(0.2, 1.1, p.y) - 0.18 * smoothstep(0.35, 0.9, length(vec2(p.x - 0.5 * aspect, p.y - 0.45)));
163
+ vec3 albedo = uWall * (1.0 + plaster * 0.06);
164
+ vec2 qc = vec2(p.x - 0.5 * aspect, 0.5 - p.y);
165
+ float open = mix(1.0 - uQuiet.w, 1.0, quietOpen(qc));
166
+ vec3 col = albedo * (uAmbient * fall + light * uExposure * open);
167
+ col = 1.0 - exp(-col * 1.35);
168
+ col += (hash33(vec3(px, floor(uTime * 24.0))).x - 0.5) * uGrain;
169
+ gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
170
+ }
171
+ `;
172
+
173
+ /**
174
+ * The settings, filled with the defaults, with the mode's number.
175
+ *
176
+ * @param {SunWallConfig} config The settings.
177
+ * @returns {typeof SUN_DEFAULTS & { modeIndex: number, speed: number }} The settings.
178
+ */
179
+ function settings(config) {
180
+ const o = { ...SUN_DEFAULTS, speed: 1, ...config };
181
+ return { ...o, modeIndex: Math.max(0, SUN_MODES.indexOf(o.mode)) };
182
+ }
183
+
184
+ /**
185
+ * The visible spectrum as the shader paints it (red at 0, violet at 1).
186
+ *
187
+ * @param {number} x 0–1 across the band.
188
+ * @returns {[number, number, number]} Linear-ish RGB, about 0–1.15.
189
+ */
190
+ export function spectrumRgb(x) {
191
+ const ss = (a, b, t) => {
192
+ const k = Math.min(1, Math.max(0, (t - a) / (b - a)));
193
+ return k * k * (3 - 2 * k);
194
+ };
195
+ const mix = (u, w, t) => u.map((v, i) => v + (w[i] - v) * t);
196
+ let c = mix([1, 0.08, 0.04], [1, 0.55, 0.05], ss(0, 0.2, x));
197
+ c = mix(c, [1, 0.95, 0.2], ss(0.15, 0.35, x));
198
+ c = mix(c, [0.35, 1, 0.25], ss(0.32, 0.5, x));
199
+ c = mix(c, [0.1, 0.8, 1], ss(0.48, 0.66, x));
200
+ c = mix(c, [0.2, 0.3, 1], ss(0.62, 0.82, x));
201
+ c = mix(c, [0.55, 0.15, 0.95], ss(0.8, 1, x));
202
+ const lum = 0.55 + 0.45 * Math.exp(-(((x - 0.36) / 0.3) ** 2));
203
+ return /** @type {[number, number, number]} */ (c.map((v) => v * lum));
204
+ }
205
+
206
+ /**
207
+ * How lit a point on the wall is through the blinds (the shader's bars × patch), 0–1.
208
+ *
209
+ * @param {SunWallConfig} config The settings.
210
+ * @param {number} x Heights from the left.
211
+ * @param {number} y Heights from the top.
212
+ * @param {number} aspect Width over height.
213
+ * @param {number} [time=0] Seconds (already at its speed).
214
+ * @returns {number} 0 in shade, 1 in a bar's full light.
215
+ */
216
+ export function blindsAt(config, x, y, aspect, time = 0) {
217
+ const o = settings(config);
218
+ const ph = 2 * Math.PI * time * o.rate;
219
+ const cx = o.patch[0] * aspect + o.drift * Math.sin(ph + o.seed);
220
+ const cy = o.patch[1] + o.drift * 0.6 * Math.cos(ph * 0.8 + o.seed);
221
+ const a = (o.angle * Math.PI) / 180;
222
+ const dx = x - cx,
223
+ dy = y - cy;
224
+ const u = Math.cos(a) * dx + Math.sin(a) * dy,
225
+ v = -Math.sin(a) * dx + Math.cos(a) * dy;
226
+ const hx = 0.5 * o.size[0] * aspect,
227
+ hy = 0.5 * o.size[1];
228
+ const sx = u - v * 0.35;
229
+ const ss = (e0, e1, t) => {
230
+ const k = Math.min(1, Math.max(0, (t - e0) / (e1 - e0)));
231
+ return k * k * (3 - 2 * k);
232
+ };
233
+ const es = 0.012 + o.soft * 0.05 * Math.min(1, Math.max(0, 0.5 + (0.5 * v) / Math.max(hy, 0.01)));
234
+ const patch = ss(hx + es, hx - es, Math.abs(sx)) * ss(hy + es, hy - es, Math.abs(v));
235
+ const pitch = (2 * hy) / o.slats;
236
+ const tilt = o.open + 0.06 * Math.sin(ph * 1.3 + o.seed * 2);
237
+ const k = (((v + hy) / pitch) % 1 + 1) % 1;
238
+ const thr = Math.min(1, Math.max(0, 0.5 + v / Math.max(2 * hy, 0.01)));
239
+ const pen = 0.015 + o.soft * 0.11 * thr;
240
+ return ss(0, pen, k) * ss(tilt, tilt - pen, k) * patch;
241
+ }
242
+
243
+ /**
244
+ * A CSS `background` that stands in for the light before the shader starts and
245
+ * without WebGL: the wall, with skewed bars (blinds) or a spectrum band (prism).
246
+ *
247
+ * @param {SunWallConfig} config The settings.
248
+ * @returns {string} The CSS background.
249
+ */
250
+ export function sunWallStill(config) {
251
+ const o = settings(config);
252
+ const shade = toHex(mixOklab(parseColor('#000000'), parseColor(o.wall), 0.55));
253
+ const lit = toHex(mixOklab(parseColor(o.wall), parseColor(o.sun), 0.55));
254
+ const wall = `radial-gradient(ellipse at 50% 45%, ${toHex(mixOklab(parseColor(shade), parseColor(o.wall), 0.55))} 0%, ${shade} 100%)`;
255
+ if (o.modeIndex === 0) {
256
+ const pitch = (o.size[1] * 100) / o.slats;
257
+ const bars = `repeating-linear-gradient(${180 + o.angle}deg, ${lit} 0 ${(pitch * o.open).toFixed(2)}%, transparent ${(pitch * o.open).toFixed(2)}% ${pitch.toFixed(2)}%)`;
258
+ const mask = `radial-gradient(ellipse ${Math.round(o.size[0] * 60)}% ${Math.round(o.size[1] * 60)}% at ${Math.round(o.patch[0] * 100)}% ${Math.round(o.patch[1] * 100)}%, transparent 60%, ${shade} 100%)`;
259
+ return [mask, bars, wall].join(', ');
260
+ }
261
+ const stops = [0, 0.2, 0.4, 0.6, 0.8, 1].map((x) => `${toHex(spectrumRgb(x).map((c) => Math.min(1, c * 0.9)))} ${(46 + x * 8).toFixed(1)}%`).join(', ');
262
+ const band = `linear-gradient(${90 + o.angle}deg, transparent 44%, ${stops}, transparent 56%)`;
263
+ return [band, `radial-gradient(ellipse at ${Math.round(o.patch[0] * 100)}% ${Math.round(o.patch[1] * 100)}%, ${lit} 0%, transparent 55%)`, wall].join(', ');
264
+ }
265
+
266
+ /**
267
+ * Starts the light on an element: a canvas inside it (its first).
268
+ *
269
+ * @param {HTMLElement} el The element.
270
+ * @param {SunWallConfig} config The settings.
271
+ * @returns {() => void} Stops it.
272
+ */
273
+ export function sunWall(el, config) {
274
+ const o = settings(config);
275
+ const start = performance.now();
276
+ const { stop } = createStage(el, {
277
+ fragment: FRAG,
278
+ alpha: false,
279
+ still: o.speed <= 0,
280
+ setup({ gl, uniform }) {
281
+ gl.uniform1f(uniform('uMode'), o.modeIndex);
282
+ gl.uniform3fv(uniform('uWall'), parseColor(o.wall));
283
+ gl.uniform3fv(uniform('uSun'), parseColor(o.sun));
284
+ gl.uniform1f(uniform('uExposure'), o.exposure);
285
+ gl.uniform1f(uniform('uAmbient'), o.ambient);
286
+ gl.uniform1f(uniform('uAngle'), o.angle);
287
+ gl.uniform1f(uniform('uSlats'), Math.max(2, o.slats));
288
+ gl.uniform1f(uniform('uOpen'), o.open);
289
+ gl.uniform1f(uniform('uSoft'), o.soft);
290
+ gl.uniform2f(uniform('uSize'), o.size[0], o.size[1]);
291
+ gl.uniform1f(uniform('uBand'), o.band);
292
+ gl.uniform1f(uniform('uBow'), o.bow);
293
+ gl.uniform1f(uniform('uDrift'), o.drift);
294
+ gl.uniform1f(uniform('uRate'), o.rate);
295
+ gl.uniform1f(uniform('uGrain'), o.grain);
296
+ gl.uniform1f(uniform('uSeed'), o.seed);
297
+ },
298
+ resize({ gl, uniform }) {
299
+ const w = Math.max(1, el.clientWidth);
300
+ const h = Math.max(1, el.clientHeight);
301
+ gl.uniform2f(uniform('uPatch'), o.patch[0] * (w / h), o.patch[1]);
302
+ gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, w / h));
303
+ },
304
+ uniforms({ gl, uniform }, now) {
305
+ gl.uniform1f(uniform('uTime'), ((now - start) / 1000) * Math.max(0, o.speed));
306
+ },
307
+ });
308
+ return stop;
309
+ }