@cesarvenzac/figures 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.
@@ -0,0 +1,875 @@
1
+ import { BODY, CAMERA, MOST } from "./figure-space.js";
2
+ /**
3
+ * The figures, drawn: real 3D shapes (a cube, a sphere, a pyramid, a block, a cloud) found
4
+ * by raymarching their distance fields in one fragment shader, in the original Aura's material, the
5
+ * eyes painted on their surface. No geometry and no textures: every pixel asks the shapes how far
6
+ * it is from them.
7
+ *
8
+ * Aura's material, as its experiment drew it on a sphere: colour as noise warped by noise
9
+ * (MaterialX's Perlin noise, as Three's TSL gave it), flowing, through a cosine palette; a rainbow
10
+ * running round the rim, turned from the direction itself so it has no seam; milky towards the
11
+ * middle like a ball of thick glass lit from inside; a gloss where the light lands; and a halo of
12
+ * the rim's own colour outside, with rings while it listens and a light running round while it
13
+ * thinks. Here the rim follows each shape's surface (how far it faces away), so a cube's front is
14
+ * milky and its edges and faces turned away run with colour.
15
+ *
16
+ * Everything that moves arrives as numbers (`FigureScene`, figure-space.ts): how the
17
+ * body is turned, lifted and squashed, a morph into a blob, a star or a heart, where the eyes are
18
+ * and how their lids sit; and each shape's own parts: the cube's cubelets (turning a layer, as a
19
+ * Rubik's cube does, or flying apart), the block's three dials, the pyramid's slabs and capstone,
20
+ * the sphere's size, ripples and moons, the cloud's puffs and raindrops. In front of it, hearts,
21
+ * stars, z's and sparks; behind and under a cloud, its rainbow and its lightning. One WebGL 2 context for every figure on the
22
+ * page (a browser keeps only a few): each figure is drawn into it and copied onto its own canvas
23
+ * straight away. Where WebGL 2 is missing or lost, `drawFigure` says so and the figure keeps its
24
+ * still drawing.
25
+ */
26
+ /** The largest figure drawn, in device pixels; a bigger one is drawn at this and scaled. */
27
+ export const MOST_PIXELS = 512;
28
+ const VERTEX = `#version 300 es
29
+ void main() {
30
+ vec2 p = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2);
31
+ gl_Position = vec4(p * 2. - 1., 0., 1.);
32
+ }`;
33
+ const f = (n) => n.toFixed(4);
34
+ const FRAGMENT = `#version 300 es
35
+ precision highp float;
36
+ precision highp int;
37
+
38
+ uniform vec2 uSize;
39
+ uniform int uShape;
40
+ uniform int uMorphTo;
41
+ uniform float uMorph;
42
+ uniform float uTime;
43
+ uniform mat3 uBody;
44
+ uniform vec3 uBodyPos;
45
+ uniform vec3 uBodyScale;
46
+ uniform float uSplit;
47
+ uniform float uSpread;
48
+ uniform mat3 uCells[27];
49
+ uniform vec4 uLayers;
50
+ uniform vec4 uPieces;
51
+ uniform float uShear;
52
+ uniform float uCore;
53
+ uniform vec4 uBeads[${MOST.beads}];
54
+ uniform int uBeadCount;
55
+ uniform vec4 uPuffs[${MOST.puffs}];
56
+ uniform vec4 uSurface;
57
+ uniform float uRoll;
58
+ uniform vec4 uBits[${MOST.bits}];
59
+ uniform vec4 uBitKinds;
60
+ uniform vec2 uRainbow;
61
+ uniform vec2 uBolt;
62
+ uniform vec3 uEyeO;
63
+ uniform vec3 uEyeR;
64
+ uniform vec3 uEyeU;
65
+ uniform vec3 uEyeF;
66
+ uniform vec4 uEyeSize;
67
+ uniform vec2 uLook;
68
+ uniform vec3 uLidL;
69
+ uniform vec3 uLidR;
70
+ uniform vec4 uGlyph;
71
+ uniform vec3 uInk;
72
+ uniform vec3 uPalA;
73
+ uniform vec3 uPalB;
74
+ uniform vec3 uPalD;
75
+ uniform float uHue;
76
+ uniform float uFlow;
77
+ uniform float uSwirl;
78
+ uniform float uEnergy;
79
+ uniform float uSaturation;
80
+ uniform float uHalo;
81
+ uniform float uRipple;
82
+ uniform float uRings;
83
+ uniform float uArc;
84
+ uniform float uArcAngle;
85
+ uniform float uFloorY;
86
+ uniform float uBottom;
87
+ uniform float uReach;
88
+ uniform float uMilk;
89
+ out vec4 colour;
90
+
91
+ const float TAU = 6.2831853;
92
+ const float OFFSET = 17.3;
93
+ const float CUBE_B = ${f(BODY.cube.half)};
94
+ const float CUBE_R = ${f(BODY.cube.round)};
95
+ const float SPHERE_R = ${f(BODY.sphere.radius)};
96
+ const float PYR_HALF = ${f(BODY.pyramid.half)};
97
+ const float PYR_H = ${f(BODY.pyramid.height)};
98
+ const float PYR_BASE = ${f(BODY.pyramid.base)};
99
+ const float PYR_R = ${f(BODY.pyramid.round)};
100
+ const float CUT_LOW = ${f(BODY.pyramid.cuts[0])};
101
+ const float CUT_HIGH = ${f(BODY.pyramid.cuts[1])};
102
+ const vec3 BLOCK_B = vec3(${f(BODY.block.half[0])}, ${f(BODY.block.half[1])}, ${f(BODY.block.half[2])});
103
+ const float BLOCK_R = ${f(BODY.block.round)};
104
+ const float DIAL = ${f(BODY.block.dial)};
105
+ const float CLOUD_BASE = ${f(BODY.cloud.base)};
106
+ const float CLOUD_K = ${f(BODY.cloud.blend)};
107
+ // Where a poke lands on the sphere: its face.
108
+ const vec3 POKE = vec3(0., .1191, .9929);
109
+ const float CAM_DIST = ${f(CAMERA.distance)};
110
+ const float CAM_PITCH = ${f(CAMERA.pitch)};
111
+ const float VIEW_HALF = ${f(CAMERA.half)};
112
+
113
+ // ---------------------------------------------------------------------------------------------
114
+ // MaterialX's Perlin noise (Bob Jenkins' hash), as Three's mx_perlin_noise_float, and its fractal.
115
+
116
+ uint rotl32(uint x, int k) { return (x << uint(k)) | (x >> uint(32 - k)); }
117
+ uint bjfinal(uint a, uint b, uint c) {
118
+ c ^= b; c -= rotl32(b, 14);
119
+ a ^= c; a -= rotl32(c, 11);
120
+ b ^= a; b -= rotl32(a, 25);
121
+ c ^= b; c -= rotl32(b, 16);
122
+ a ^= c; a -= rotl32(c, 4);
123
+ b ^= a; b -= rotl32(a, 14);
124
+ c ^= b; c -= rotl32(b, 24);
125
+ return c;
126
+ }
127
+ uint hashInt(int x, int y, int z) {
128
+ uint s = 0xdeadbeefu + (3u << 2u) + 13u;
129
+ return bjfinal(s + uint(x), s + uint(y), s + uint(z));
130
+ }
131
+ float gradient(uint hash, float x, float y, float z) {
132
+ uint h = hash & 15u;
133
+ float u = h < 8u ? x : y;
134
+ float v = h < 4u ? y : ((h == 12u || h == 14u) ? x : z);
135
+ return ((h & 1u) != 0u ? -u : u) + ((h & 2u) != 0u ? -v : v);
136
+ }
137
+ float fade(float t) { return t * t * t * (t * (t * 6. - 15.) + 10.); }
138
+ float perlin(vec3 p) {
139
+ vec3 i = floor(p);
140
+ vec3 q = p - i;
141
+ int X = int(i.x), Y = int(i.y), Z = int(i.z);
142
+ float u = fade(q.x), v = fade(q.y), w = fade(q.z);
143
+ float a = mix(mix(gradient(hashInt(X, Y, Z), q.x, q.y, q.z), gradient(hashInt(X + 1, Y, Z), q.x - 1., q.y, q.z), u),
144
+ mix(gradient(hashInt(X, Y + 1, Z), q.x, q.y - 1., q.z), gradient(hashInt(X + 1, Y + 1, Z), q.x - 1., q.y - 1., q.z), u), v);
145
+ float b = mix(mix(gradient(hashInt(X, Y, Z + 1), q.x, q.y, q.z - 1.), gradient(hashInt(X + 1, Y, Z + 1), q.x - 1., q.y, q.z - 1.), u),
146
+ mix(gradient(hashInt(X, Y + 1, Z + 1), q.x, q.y - 1., q.z - 1.), gradient(hashInt(X + 1, Y + 1, Z + 1), q.x - 1., q.y - 1., q.z - 1.), u), v);
147
+ return .982 * mix(a, b, w);
148
+ }
149
+ // mx_fractal_noise_float(p, 2 octaves, lacunarity 2, diminish 0.5)
150
+ float fbm(vec3 p) { return perlin(p) + .5 * perlin(p * 2.); }
151
+
152
+ // ---------------------------------------------------------------------------------------------
153
+ // Shapes
154
+
155
+ float sdRoundBox(vec3 p, vec3 b, float r) {
156
+ vec3 q = abs(p) - b + r;
157
+ return length(max(q, 0.)) + min(max(q.x, max(q.y, q.z)), 0.) - r;
158
+ }
159
+ // Inigo Quilez's square pyramid: base 1 wide at y 0, apex at h.
160
+ float sdPyramid(vec3 p, float h) {
161
+ float m2 = h * h + .25;
162
+ p.xz = abs(p.xz);
163
+ p.xz = (p.z > p.x) ? p.zx : p.xz;
164
+ p.xz -= .5;
165
+ vec3 q = vec3(p.z, h * p.y - .5 * p.x, h * p.x + .5 * p.y);
166
+ float s = max(-q.x, 0.);
167
+ float t = clamp((q.y - .5 * p.z) / (m2 + .25), 0., 1.);
168
+ float a = m2 * (q.x + s) * (q.x + s) + q.y * q.y;
169
+ float b = m2 * (q.x + .5 * t) * (q.x + .5 * t) + (q.y - m2 * t) * (q.y - m2 * t);
170
+ float d2 = min(q.y, -q.x * m2 - q.y * .5) > 0. ? 0. : min(a, b);
171
+ return sqrt((d2 + q.z * q.z) / m2) * sign(max(q.z, -p.y));
172
+ }
173
+ float sdStar5(vec2 p, float r, float rf) {
174
+ const vec2 k1 = vec2(.809016994375, -.587785252292);
175
+ const vec2 k2 = vec2(-k1.x, k1.y);
176
+ p.x = abs(p.x);
177
+ p -= 2. * max(dot(k1, p), 0.) * k1;
178
+ p -= 2. * max(dot(k2, p), 0.) * k2;
179
+ p.x = abs(p.x);
180
+ p.y -= r;
181
+ vec2 ba = rf * vec2(-k1.y, k1.x) - vec2(0., 1.);
182
+ float h = clamp(dot(p, ba) / dot(ba, ba), 0., r);
183
+ return length(p - ba * h) * sign(p.y * ba.x - p.x * ba.y);
184
+ }
185
+ float dot2(vec2 v) { return dot(v, v); }
186
+ float sdHeart(vec2 p) {
187
+ p.x = abs(p.x);
188
+ if (p.y + p.x > 1.) return sqrt(dot2(p - vec2(.25, .75))) - sqrt(2.) / 4.;
189
+ return sqrt(min(dot2(p - vec2(0., 1.)), dot2(p - .5 * max(p.x + p.y, 0.)))) * sign(p.x - p.y);
190
+ }
191
+ float extrude(vec3 p, float d2, float h) {
192
+ vec2 w = vec2(d2, abs(p.z) - h);
193
+ return min(max(w.x, w.y), 0.) + length(max(w, 0.));
194
+ }
195
+
196
+ vec3 turnAbout(vec3 p, int axis, float t) {
197
+ float c = cos(t), s = sin(t);
198
+ if (axis == 0) return vec3(p.x, c * p.y - s * p.z, s * p.y + c * p.z);
199
+ if (axis == 1) return vec3(c * p.x + s * p.z, p.y, -s * p.x + c * p.z);
200
+ return vec3(c * p.x - s * p.y, s * p.x + c * p.y, p.z);
201
+ }
202
+ float smin(float a, float b, float k) {
203
+ float h = max(k - abs(a - b), 0.) / k;
204
+ return min(a, b) - h * h * k * .25;
205
+ }
206
+ float smax(float a, float b, float k) { return -smin(-a, -b, k); }
207
+
208
+ // Layers turning about one axis (the cube's, the block's dials): which layer a point is in, its
209
+ // edges at ±e, and the point as it was before its layer turned. bound is how far the point is from
210
+ // its layer's faces: a ray steps no further, since the next layer may be turned otherwise.
211
+ float layerOf(float x, float e) { return x < -e ? -1. : (x > e ? 1. : 0.); }
212
+ float layerTurn(float l) { return l < 0. ? uLayers.y : (l > 0. ? uLayers.w : uLayers.z); }
213
+ vec3 unturn(vec3 p, float e, out float bound) {
214
+ bound = 1e9;
215
+ if (uLayers.x < 0.) return p;
216
+ int a = int(uLayers.x + .5);
217
+ float x = p[a];
218
+ float l = layerOf(x, e);
219
+ bound = l == 0. ? e - abs(x) : abs(x) - e;
220
+ return turnAbout(p, a, -layerTurn(l));
221
+ }
222
+ vec3 unturnDir(vec3 p, vec3 v, float e) {
223
+ if (uLayers.x < 0.) return v;
224
+ int a = int(uLayers.x + .5);
225
+ return turnAbout(v, a, -layerTurn(layerOf(p[a], e)));
226
+ }
227
+
228
+ // The cube as 27 cubelets, its layers perhaps turning: the gaps open with uSplit, and the
229
+ // cubelets fly apart with uSpread. Two distances: how far a ray may safely step (x), and how far
230
+ // the nearest cubelet is (y), for its edge.
231
+ float cellSize() { return (2. * CUBE_B / 3.) * (1. + .08 * uSplit + uSpread); }
232
+ vec2 cubelets(vec3 p) {
233
+ float S = cellSize();
234
+ float h = CUBE_B / 3. - .014;
235
+ float bound;
236
+ vec3 q = unturn(p, S * .5, bound);
237
+ vec3 c = clamp(floor(q / S + .5), -1., 1.);
238
+ float d = sdRoundBox(q - c * S, vec3(h), .05);
239
+ // Near a turning layer's faces, step no further than to them; far off, the whole cube's
240
+ // reach (a turning corner pokes out of its box) is a safe step.
241
+ float outer = length(p) - (2.6 * S + .12);
242
+ return vec2(min(d, max(max(bound, .004), outer)), d);
243
+ }
244
+
245
+ // The block as three dials round its length, each turned on its own like a cryptex's.
246
+ vec2 dials(vec3 p) {
247
+ float bound;
248
+ vec3 q = unturn(p, DIAL, bound);
249
+ float l = layerOf(p.x, DIAL);
250
+ float g = .01 + .016 * uSplit;
251
+ float slab = l < 0. ? q.x + DIAL + g : (l > 0. ? DIAL + g - q.x : abs(q.x) - (DIAL - g));
252
+ float d = smax(sdRoundBox(q, BLOCK_B, BLOCK_R), slab, .035);
253
+ float outer = length(p) - 1.25;
254
+ return vec2(min(d, max(max(bound, .004), outer)), d);
255
+ }
256
+
257
+ // The pyramid, perhaps apart: a slab sliding out of its middle, its capstone lifted and spun, and
258
+ // leaning like jelly.
259
+ float pyr(vec3 p) {
260
+ vec3 q = (p - vec3(0., PYR_BASE, 0.)) / (PYR_HALF * 2.);
261
+ return sdPyramid(q, PYR_H / (PYR_HALF * 2.)) * (PYR_HALF * 2.) - PYR_R;
262
+ }
263
+ vec3 leaned(vec3 p) {
264
+ if (abs(uShear) < 1e-5) return p;
265
+ float up = clamp((p.y - PYR_BASE) / PYR_H, 0., 1.3);
266
+ return vec3(p.x - uShear * up * up, p.y, p.z);
267
+ }
268
+ vec3 inMiddle(vec3 p) { return p - vec3(uPieces.x, 0., 0.); }
269
+ vec3 inCap(vec3 p) { return turnAbout(p - vec3(uPieces.y, uPieces.z, 0.), 1, -uPieces.w); }
270
+ vec3 pieceDistances(vec3 p) {
271
+ vec3 m = inMiddle(p), c = inCap(p);
272
+ return vec3(max(pyr(p), p.y - CUT_LOW), max(pyr(m), max(CUT_LOW - m.y, m.y - CUT_HIGH)), max(pyr(c), CUT_HIGH - c.y));
273
+ }
274
+ bool apart() { return dot(abs(uPieces), vec4(1.)) > 1e-5; }
275
+ float pyramid(vec3 p) {
276
+ p = leaned(p);
277
+ if (!apart()) return pyr(p);
278
+ vec3 d = pieceDistances(p);
279
+ return min(d.x, min(d.y, d.z));
280
+ }
281
+
282
+ // The sphere, its size, a poke's dent and the ripple running from it, and wrinkles.
283
+ float sphere(vec3 p) {
284
+ float d = length(p) - SPHERE_R * uCore;
285
+ if (uSurface.x > 1e-4 || uSurface.z > 1e-4) {
286
+ float a = acos(clamp(dot(p, POKE) / max(length(p), 1e-4), -1., 1.));
287
+ d += uSurface.z * exp(-a * a * 7.);
288
+ d += uSurface.x * sin(a * 8. - uSurface.y) * exp(-a * .4) * smoothstep(0., .25, a);
289
+ }
290
+ if (uSurface.w > 1e-4) d += uSurface.w * .04 * sin(p.x * 15.) * sin(p.y * 13. + 1.) * sin(p.z * 14. + 2.);
291
+ return d;
292
+ }
293
+
294
+ // The cloud: its puffs run together, flat underneath.
295
+ float cloud(vec3 p) {
296
+ float d = 1e9;
297
+ for (int i = 0; i < ${MOST.puffs}; i++) {
298
+ vec4 b = uPuffs[i];
299
+ if (b.w > 0.) d = smin(d, length(p - b.xyz) - b.w, CLOUD_K);
300
+ }
301
+ return smax(d, CLOUD_BASE - p.y, .08);
302
+ }
303
+
304
+ // Small balls beside the body: a sphere's moons and droplets, a cloud's raindrops.
305
+ float beads(vec3 p) {
306
+ float d = 1e9;
307
+ for (int i = 0; i < ${MOST.beads}; i++) {
308
+ if (i >= uBeadCount) break;
309
+ vec4 b = uBeads[i];
310
+ if (b.w > 0.) d = min(d, length(p - b.xyz) - b.w);
311
+ }
312
+ return d;
313
+ }
314
+
315
+ vec2 shape(vec3 p) {
316
+ if (uShape == 0) {
317
+ float solid = sdRoundBox(p, vec3(CUBE_B), CUBE_R);
318
+ if (uSplit <= .001) return vec2(solid);
319
+ vec2 split = cubelets(p);
320
+ return vec2(mix(solid, split.x, uSplit), mix(solid, split.y, uSplit));
321
+ }
322
+ if (uShape == 1) return vec2(sphere(p));
323
+ if (uShape == 2) return vec2(pyramid(p));
324
+ if (uShape == 3) {
325
+ float solid = sdRoundBox(p, BLOCK_B, BLOCK_R);
326
+ if (uSplit <= .001) return vec2(solid);
327
+ vec2 parted = dials(p);
328
+ return vec2(mix(solid, parted.x, uSplit), mix(solid, parted.y, uSplit));
329
+ }
330
+ return vec2(cloud(p));
331
+ }
332
+ float morphTarget(vec3 p) {
333
+ if (uMorphTo == 1) {
334
+ float wobble = sin(p.x * 4.1 + uTime * 2.3) * sin(p.y * 3.7 - uTime * 1.9) * sin(p.z * 4.3 + uTime * 1.6);
335
+ return (length(p * vec3(1., 1.06, 1.)) - SPHERE_R * .96 - .07 * wobble) * .8;
336
+ }
337
+ if (uMorphTo == 2) return extrude(p, sdStar5(p.xy - vec2(0., .02), .88, .5) + .09, .14) - .13;
338
+ return extrude(p, sdHeart(p.xy / 1.5 + vec2(0., .52)) * 1.5 + .06, .16) - .13;
339
+ }
340
+ vec2 body(vec3 p) {
341
+ vec2 d = shape(p);
342
+ if (uMorphTo > 0 && uMorph > .001) d = mix(d, vec2(morphTarget(p)), uMorph);
343
+ if (uBeadCount > 0) {
344
+ float b = beads(p);
345
+ d = vec2(smin(d.x, b, .12), smin(d.y, b, .12));
346
+ }
347
+ return d;
348
+ }
349
+
350
+ vec3 toBody(vec3 p) { return transpose(uBody) * ((p - uBodyPos) / uBodyScale); }
351
+ vec2 march(vec3 p) {
352
+ float s = min(uBodyScale.x, min(uBodyScale.y, uBodyScale.z));
353
+ return body(toBody(p)) * s;
354
+ }
355
+ float map(vec3 p) { return march(p).y; }
356
+ vec3 normalAt(vec3 p) {
357
+ const vec2 k = vec2(1., -1.);
358
+ const float e = .0015;
359
+ return normalize(k.xyy * map(p + k.xyy * e) + k.yyx * map(p + k.yyx * e) + k.yxy * map(p + k.yxy * e) + k.xxx * map(p + k.xxx * e));
360
+ }
361
+
362
+ // Where a point of the body was before its parts moved, so its colour and its eyes travel with
363
+ // them: a cubelet scrambled or flown apart, a dial or a layer turned, the capstone lifted and
364
+ // spun, the sphere swollen. Any other point, the point itself.
365
+ bool rigid() { return uMorph <= .5; }
366
+ mat3 cellOf(vec3 p, out vec3 q, out vec3 shift) {
367
+ float S = cellSize();
368
+ float bound;
369
+ q = unturn(p, S * .5, bound);
370
+ vec3 c = clamp(floor(q / S + .5), -1., 1.);
371
+ int i = int(c.x + 1.) + int(c.y + 1.) * 3 + int(c.z + 1.) * 9;
372
+ shift = c * (S - 2. * CUBE_B / 3.);
373
+ return uCells[i];
374
+ }
375
+ // Which of the pyramid's pieces a point is on: 0 the base, 1 the middle slab, 2 the capstone.
376
+ int pieceOf(vec3 p) {
377
+ if (!apart()) return 0;
378
+ vec3 d = pieceDistances(leaned(p));
379
+ return d.z < min(d.x, d.y) ? 2 : (d.y < d.x ? 1 : 0);
380
+ }
381
+ vec3 solvedPoint(vec3 p) {
382
+ if (!rigid()) return p;
383
+ if (uShape == 0) {
384
+ vec3 q, shift;
385
+ mat3 m = cellOf(p, q, shift);
386
+ return transpose(m) * (q - shift);
387
+ }
388
+ if (uShape == 1) return p / max(uCore, .05);
389
+ if (uShape == 2) {
390
+ int piece = pieceOf(p);
391
+ vec3 q = leaned(p);
392
+ return piece == 2 ? inCap(q) : (piece == 1 ? inMiddle(q) : q);
393
+ }
394
+ if (uShape == 3) {
395
+ float bound;
396
+ return unturn(p, DIAL, bound);
397
+ }
398
+ return p;
399
+ }
400
+ vec3 solvedDir(vec3 p, vec3 v) {
401
+ if (!rigid()) return v;
402
+ if (uShape == 0) {
403
+ vec3 q, shift;
404
+ mat3 m = cellOf(p, q, shift);
405
+ return transpose(m) * unturnDir(p, v, cellSize() * .5);
406
+ }
407
+ if (uShape == 2 && pieceOf(p) == 2) return turnAbout(v, 1, -uPieces.w);
408
+ if (uShape == 3) return unturnDir(p, v, DIAL);
409
+ return v;
410
+ }
411
+
412
+ // ---------------------------------------------------------------------------------------------
413
+ // Eyes, painted on: shapes in the face's own plane, square-cornered.
414
+
415
+ float sdBox2(vec2 p, vec2 b) {
416
+ vec2 d = abs(p) - b;
417
+ return length(max(d, 0.)) + min(max(d.x, d.y), 0.);
418
+ }
419
+ float sdStroke(vec2 p, vec2 a, vec2 b, float th) {
420
+ float l = length(b - a);
421
+ vec2 d = (b - a) / l;
422
+ vec2 q = p - (a + b) * .5;
423
+ q = mat2(d.x, -d.y, d.y, d.x) * q;
424
+ q = abs(q) - vec2(l + th, th) * .5;
425
+ return length(max(q, 0.)) + min(max(q.x, q.y), 0.);
426
+ }
427
+ // The upper lid's edge at x: a line from the eye's outer side to its inner one.
428
+ float lidAt(float x, float side, vec3 lid) {
429
+ float w = uEyeSize.x, h = uEyeSize.y;
430
+ float top = h * .5;
431
+ float inner = top - lid.x * h, outer = top - lid.y * h;
432
+ float left = side < 0. ? outer : inner;
433
+ float right = side < 0. ? inner : outer;
434
+ return left + (right - left) * (x / w + .5);
435
+ }
436
+ float glyph(int id, vec2 e, float side, vec3 lid) {
437
+ float w = uEyeSize.x, h = uEyeSize.y;
438
+ if (id == 0) {
439
+ float d = sdBox2(e, vec2(w, h) * .5);
440
+ float top = h * .5;
441
+ float slope = (lidAt(w * .5, side, lid) - lidAt(-w * .5, side, lid)) / w;
442
+ d = max(d, (e.y - lidAt(e.x, side, lid)) / sqrt(1. + slope * slope));
443
+ float ry = h * .62, rx = w * .98;
444
+ float cy = mix(-h * .5 - ry - h * .06, h * .06 - ry, lid.z);
445
+ vec2 k = vec2(e.x / rx, (e.y - cy) / ry);
446
+ return max(d, -(length(k) - 1.) * min(rx, ry));
447
+ }
448
+ if (id == 1) {
449
+ float th = h * .2;
450
+ return min(sdStroke(e, vec2(-w * .58, -h * .14), vec2(0., h * .2), th), sdStroke(e, vec2(0., h * .2), vec2(w * .58, -h * .14), th));
451
+ }
452
+ if (id == 2) return sdBox2(e, vec2(w * .62, h * .085));
453
+ if (id == 3) return sdStar5(e * vec2(1., 1.) - vec2(0., -h * .04), w * 1.02, .46);
454
+ if (id == 4) return sdHeart(e / (w * 1.25) + vec2(0., .55)) * (w * 1.25);
455
+ if (id == 5) {
456
+ float th = h * .18, s = w * .52;
457
+ return min(sdStroke(e, vec2(-s), vec2(s), th), sdStroke(e, vec2(-s, s), vec2(s, -s), th));
458
+ }
459
+ float th = h * .19, sx = -side;
460
+ vec2 a = vec2(-w * .44 * sx, h * .34), b = vec2(w * .4 * sx, 0.), c = vec2(-w * .44 * sx, -h * .34);
461
+ return min(sdStroke(e, a, b, th), sdStroke(e, b, c, th));
462
+ }
463
+ // Both eyes at a point of the face's plane: their distance (x), and the glints' (y).
464
+ vec2 eyes(vec2 xy) {
465
+ float size = uEyeSize.w, gap = uEyeSize.z;
466
+ float open = max(uGlyph.w, .06);
467
+ float rect = (uGlyph.x < .5 ? 1. - uGlyph.z : 0.) + (uGlyph.y < .5 ? uGlyph.z : 0.);
468
+ float best = 1e9, glint = 1e9;
469
+ for (int i = 0; i < 2; i++) {
470
+ float side = i == 0 ? -1. : 1.;
471
+ vec3 lid = i == 0 ? uLidL : uLidR;
472
+ vec2 e = (xy - vec2(side * gap, 0.) - uLook) / size;
473
+ e.y /= open;
474
+ float d = mix(glyph(int(uGlyph.x + .5), e, side, lid), glyph(int(uGlyph.y + .5), e, side, lid), uGlyph.z) * size * min(1., open * 1.6);
475
+ best = min(best, d);
476
+ // The glint: the eye's top left, just under the upper lid wherever it has come down to.
477
+ float w = uEyeSize.x, h = uEyeSize.y;
478
+ float gs = w * .27, gx = -w * .34;
479
+ float top = min(h * .39, min(lidAt(gx, side, lid), lidAt(gx + gs, side, lid)) - h * .07);
480
+ float g = sdBox2(e - vec2(gx + gs * .5, top - gs * .5), vec2(gs * .5)) * size;
481
+ glint = min(glint, rect > .5 ? max(g, d) : 1e9);
482
+ }
483
+ return vec2(best, glint);
484
+ }
485
+
486
+ // ---------------------------------------------------------------------------------------------
487
+ // Bits in front: a z (three strokes), and a spark (four points).
488
+
489
+ float sdZ(vec2 p, float s) {
490
+ float th = s * .36;
491
+ return min(min(sdStroke(p, vec2(-s, s), vec2(s, s), th), sdStroke(p, vec2(s, s), vec2(-s, -s), th)), sdStroke(p, vec2(-s, -s), vec2(s, -s), th));
492
+ }
493
+ float sdSpark(vec2 p, float s) {
494
+ vec2 q = abs(p) / s;
495
+ return (sqrt(q.x) + sqrt(q.y) - 1.) * s * .42;
496
+ }
497
+ float hash1(float n) { return fract(sin(n * 127.1 + 311.7) * 43758.5453); }
498
+
499
+ // ---------------------------------------------------------------------------------------------
500
+ // Aura's material
501
+
502
+ vec3 palette(float t) { return uPalA + uPalB * cos(TAU * (uPalD + t)); }
503
+ // The palette at a direction's angle, turned by offset, with no angle (so no seam):
504
+ // cos(θ + φ) = cos θ cos φ − sin θ sin φ.
505
+ vec3 around(vec2 toward, float offset) {
506
+ vec3 phase = (uPalD + offset) * TAU;
507
+ return uPalA + uPalB * (toward.x * cos(phase) - toward.y * sin(phase));
508
+ }
509
+ vec3 grey(vec3 c) { return mix(vec3(dot(c, vec3(.2126, .7152, .0722))), c, uSaturation); }
510
+ vec3 toSrgb(vec3 c) { return mix(c * 12.92, 1.055 * pow(c, vec3(1. / 2.4)) - .055, step(.0031308, c)); }
511
+
512
+ void main() {
513
+ vec2 ndc = gl_FragCoord.xy / uSize * 2. - 1.;
514
+ vec3 target = vec3(0.);
515
+ vec3 ro = target + vec3(0., sin(CAM_PITCH), cos(CAM_PITCH)) * CAM_DIST;
516
+ vec3 fw = normalize(target - ro);
517
+ vec3 rt = normalize(cross(fw, vec3(0., 1., 0.)));
518
+ vec3 up = cross(rt, fw);
519
+ vec3 rd = normalize(fw + (ndc.x * rt + ndc.y * up) * (VIEW_HALF / CAM_DIST));
520
+ float pixel = (2. * VIEW_HALF / CAM_DIST) / uSize.y;
521
+
522
+ // March, keeping the closest the ray came (relative to a pixel there): a near miss is part of
523
+ // the edge, so the silhouette is smoothed without drawing it twice.
524
+ float t = CAM_DIST - 2.4;
525
+ float tEnd = CAM_DIST + 2.4;
526
+ float closest = 1e9;
527
+ float tClosest = t;
528
+ bool hit = false;
529
+ for (int i = 0; i < 110; i++) {
530
+ vec3 p = ro + rd * t;
531
+ vec2 d = march(p);
532
+ float ratio = d.y / (pixel * t);
533
+ if (ratio < closest) { closest = ratio; tClosest = t; }
534
+ if (ratio < .08) { hit = true; break; }
535
+ t += max(d.x * .82, pixel * t * .25);
536
+ if (t > tEnd) break;
537
+ }
538
+ float cover = hit ? 1. : clamp(1. - closest, 0., 1.);
539
+ vec3 p = ro + rd * (hit ? t : tClosest);
540
+ vec3 n = normalAt(p);
541
+ // In the camera's frame: x right, y up, z toward the viewer, as Aura shaded its sphere.
542
+ vec3 nc = vec3(dot(n, rt), dot(n, up), dot(n, -fw));
543
+ float z = clamp(nc.z, 0., 1.);
544
+ float r = clamp(length(nc.xy), 0., 1.);
545
+ // Where the pixel sits round the figure, in the picture.
546
+ vec2 screen = ndc * VIEW_HALF - vec2(uBodyPos.x, (uBodyPos.y - target.y) * cos(CAM_PITCH));
547
+ vec2 around2 = normalize(screen + vec2(1e-4));
548
+ vec2 toward = normalize(nc.xy + around2 * .35 + vec2(1e-4));
549
+
550
+ // The body's own point (before a scramble), for its colour and its eyes.
551
+ vec3 pb = toBody(p);
552
+ vec3 ps = solvedPoint(pb);
553
+ vec3 nb = transpose(uBody) * n;
554
+ vec3 ns = solvedDir(pb, nb);
555
+ // Its colour may roll round on its own (the eyes stay where they are).
556
+ vec3 pc = abs(uRoll) > 1e-5 ? turnAbout(ps, 0, -uRoll) : ps;
557
+
558
+ // Colour as a field: noise warped by noise, in 3D so it wraps the body, flowing with time.
559
+ float scale = uSwirl * .6 + .75;
560
+ vec3 point = pc * 1.25 * scale + vec3(OFFSET, uFlow * .07 + OFFSET, uFlow * .19);
561
+ float warp = fbm(point);
562
+ float field = fbm(point + vec3(warp * (uSwirl * .9 + .2)));
563
+ vec3 inner = palette(field * (uSwirl * .45 + .55) + uHue);
564
+ float churn = fbm(vec3(toward * 1.6 + OFFSET, uFlow * .23)) * uSwirl * .18;
565
+ vec3 ring = around(toward, uHue + churn);
566
+ vec3 surface = mix(inner, ring, smoothstep(.5, 1., r) * .8);
567
+ float rim = pow(1. - z, 1.4);
568
+ surface = mix(vec3(.975, .97, .99), surface, clamp(rim * .6 + uMilk, 0., 1.));
569
+ surface *= mix(1., .9, smoothstep(.82, 1., r));
570
+ vec3 light = normalize(vec3(-.45, .62, .65));
571
+ float lit = dot(nc, light);
572
+ surface *= mix(.88, 1.03, lit * .5 + .5);
573
+ vec2 glossAt = nc.xy - vec2(-.33, .4);
574
+ surface += exp(-dot(glossAt, glossAt) * 11.) * .26;
575
+ vec2 heading = vec2(cos(uArcAngle * TAU), sin(uArcAngle * TAU));
576
+ float comet = smoothstep(.3, 1., dot(around2, heading)) * uArc;
577
+ surface += mix(ring, vec3(1.), .5) * comet * smoothstep(.6, 1., r) * .5;
578
+ surface = grey(surface) * mix(.92, 1.05, clamp(uEnergy, 0., 1.3));
579
+
580
+ // The eyes, in the face's plane, on the side that faces the way they do.
581
+ vec3 rel = ps - uEyeO;
582
+ vec2 xy = vec2(dot(rel, uEyeR), dot(rel, uEyeU));
583
+ vec2 eye = eyes(xy);
584
+ float aa = max(fwidth(eye.x), 1e-4) * .75;
585
+ float facing = smoothstep(.25, .5, dot(ns, uEyeF)) * step(-.3, dot(rel, uEyeF));
586
+ // Only on the body itself: never a moon or a raindrop passing in front.
587
+ if (uBeadCount > 0) facing *= smoothstep(-.005, .02, beads(pb) - shape(pb).y);
588
+ float ink = (1. - smoothstep(-aa, aa, eye.x)) * facing;
589
+ float shine = (1. - smoothstep(-aa, aa, eye.y)) * facing;
590
+ vec3 inkColour = uInk * mix(.9, 1.15, lit * .5 + .5) + exp(-dot(glossAt, glossAt) * 11.) * .12;
591
+ surface = mix(surface, inkColour, ink);
592
+ surface = mix(surface, vec3(.96), shine * .94);
593
+
594
+ // Outside: the halo, the rim's own rainbow fading with distance, rings travelling out while
595
+ // it listens; and a soft shadow on the ground.
596
+ float beyond = max(closest * pixel * tClosest, 0.) / uReach;
597
+ float reach = exp(-beyond * 2.2 / max(uHalo, .05));
598
+ float rings = (sin(beyond * 20. - uRings) * .5 + .5) * uRipple * reach;
599
+ float glow = reach * .55 * uEnergy + rings * .22 + comet * reach * .35;
600
+ // Gone before the canvas's edge, so no card or column ever cuts it square.
601
+ glow *= 1. - smoothstep(.55, .96, length(ndc));
602
+ vec3 ringOut = around(around2, uHue);
603
+ vec3 luma = vec3(dot(ringOut, vec3(.2126, .7152, .0722)));
604
+ vec3 haloColour = clamp(grey((ringOut - luma) * 1.4 + luma), 0., 1.);
605
+ float haloA = clamp(glow, 0., .9) * (1. - cover);
606
+
607
+ float shadowA = 0.;
608
+ if (rd.y < 0.) {
609
+ float tf = (uFloorY - ro.y) / rd.y;
610
+ vec3 fp = ro + rd * tf;
611
+ vec2 off = (fp.xz - uBodyPos.xz) / vec2(1., .55);
612
+ float lift = max(uBottom - uFloorY, 0.);
613
+ float spread = .62 * (1. + lift * .8);
614
+ shadowA = exp(-dot(off, off) / (spread * spread)) * .2 / (1. + lift * 4.);
615
+ }
616
+ shadowA *= (1. - cover) * (1. - haloA);
617
+
618
+ vec3 c = toSrgb(clamp(surface, 0., 1.));
619
+ vec3 h = toSrgb(haloColour);
620
+ float alpha = cover + haloA + shadowA;
621
+ colour = vec4(c * cover + h * haloA, alpha);
622
+
623
+ vec2 picture = ndc * VIEW_HALF;
624
+ float px = 2. * VIEW_HALF / uSize.y;
625
+ float base = uBottom * cos(CAM_PITCH);
626
+
627
+ // The cloud's rainbow, behind it: an arc over it in its colours, sweeping in from the left.
628
+ if (uRainbow.y > 0.) {
629
+ vec2 v = picture - vec2(uBodyPos.x, base);
630
+ float band = (length(v) - 1.) / .26;
631
+ float along = 1. - atan(max(v.y, 0.), v.x) / 3.14159;
632
+ float shown = smoothstep(0., .06, band) * (1. - smoothstep(.94, 1., band)) * step(0., v.y);
633
+ shown *= 1. - smoothstep(uRainbow.x - .08, uRainbow.x, along);
634
+ shown *= 1. - smoothstep(.86, 1., length(ndc));
635
+ float a = shown * uRainbow.y * .8 * (1. - colour.a);
636
+ colour = vec4(colour.rgb + toSrgb(grey(palette(clamp(band, 0., 1.) * .66))) * a, colour.a + a);
637
+ }
638
+
639
+ // Lightning from under the cloud to the ground: a zigzag, and its glow.
640
+ if (uBolt.x > 0.) {
641
+ vec2 top = vec2(uBodyPos.x + .04, base + .03);
642
+ vec2 bottom = vec2(uBodyPos.x - .03, uFloorY * cos(CAM_PITCH));
643
+ float d = 1e9;
644
+ vec2 a = top;
645
+ for (int i = 1; i <= 4; i++) {
646
+ float f = float(i) / 4.;
647
+ vec2 b = mix(top, bottom, f);
648
+ if (i < 4) b.x += (hash1(uBolt.y * 7.3 + float(i)) - .5) * .2;
649
+ d = min(d, sdStroke(picture, a, b, .055 * (1.2 - f * .5)));
650
+ a = b;
651
+ }
652
+ // The bolt in its deep colour, so it shows on a light page; its glow in its light one.
653
+ float core = (1. - smoothstep(-px, px, d)) * uBolt.x;
654
+ float glowA = exp(-max(d, 0.) * 22.) * .55 * uBolt.x * (1. - core);
655
+ vec3 boltColour = toSrgb(grey(mix(palette(2. / 3.), palette(0.), .25)));
656
+ vec3 glowColour = toSrgb(grey(palette(1. / 3.)));
657
+ colour = vec4(colour.rgb * (1. - glowA) + glowColour * glowA, colour.a * (1. - glowA) + glowA);
658
+ colour = vec4(colour.rgb * (1. - core) + boltColour * core, colour.a * (1. - core) + core);
659
+ }
660
+
661
+ // Bits in front of it all: hearts in its own colour, stars in its deep one, z's in ink, sparks.
662
+ for (int i = 0; i < ${MOST.bits}; i++) {
663
+ float kind = uBitKinds[i];
664
+ vec4 b = uBits[i];
665
+ if (kind < .5 || b.w <= 0.) continue;
666
+ vec2 e = picture - b.xy;
667
+ float d;
668
+ vec3 tint;
669
+ if (kind < 1.5) {
670
+ d = sdHeart(e / b.z + vec2(0., .5)) * b.z;
671
+ tint = mix(palette(0.), palette(1. / 3.), smoothstep(-.5, .9, e.y / b.z) * .45);
672
+ } else if (kind < 2.5) {
673
+ d = sdStar5(e, b.z, .45);
674
+ tint = mix(palette(2. / 3.), palette(0.), .3);
675
+ } else if (kind < 3.5) {
676
+ d = sdZ(e, b.z * .5);
677
+ tint = uInk;
678
+ } else {
679
+ d = sdSpark(e, b.z);
680
+ tint = mix(palette(2. / 3.), palette(0.), .5);
681
+ }
682
+ float a = (1. - smoothstep(-px * .75, px * .75, d)) * b.w;
683
+ vec3 shade = toSrgb(clamp(grey(tint), 0., 1.));
684
+ colour = vec4(colour.rgb * (1. - a) + shade * a, colour.a * (1. - a) + a);
685
+ }
686
+ }`;
687
+ const NAMES = [
688
+ "uSize",
689
+ "uShape",
690
+ "uMorphTo",
691
+ "uMorph",
692
+ "uTime",
693
+ "uBody",
694
+ "uBodyPos",
695
+ "uBodyScale",
696
+ "uSplit",
697
+ "uSpread",
698
+ "uCells",
699
+ "uLayers",
700
+ "uPieces",
701
+ "uShear",
702
+ "uCore",
703
+ "uBeads",
704
+ "uBeadCount",
705
+ "uPuffs",
706
+ "uSurface",
707
+ "uRoll",
708
+ "uBits",
709
+ "uBitKinds",
710
+ "uRainbow",
711
+ "uBolt",
712
+ "uEyeO",
713
+ "uEyeR",
714
+ "uEyeU",
715
+ "uEyeF",
716
+ "uEyeSize",
717
+ "uLook",
718
+ "uLidL",
719
+ "uLidR",
720
+ "uGlyph",
721
+ "uInk",
722
+ "uPalA",
723
+ "uPalB",
724
+ "uPalD",
725
+ "uHue",
726
+ "uFlow",
727
+ "uSwirl",
728
+ "uEnergy",
729
+ "uSaturation",
730
+ "uHalo",
731
+ "uRipple",
732
+ "uRings",
733
+ "uArc",
734
+ "uArcAngle",
735
+ "uFloorY",
736
+ "uBottom",
737
+ "uReach",
738
+ "uMilk",
739
+ ];
740
+ /** Not tried yet (undefined), unavailable (null), or ready. */
741
+ let renderer;
742
+ const contexts = new WeakMap();
743
+ const restored = new Set();
744
+ function compile(gl, type, source) {
745
+ const shader = gl.createShader(type);
746
+ if (!shader)
747
+ return null;
748
+ gl.shaderSource(shader, source);
749
+ gl.compileShader(shader);
750
+ if (gl.getShaderParameter(shader, gl.COMPILE_STATUS))
751
+ return shader;
752
+ console.error(gl.getShaderInfoLog(shader));
753
+ return null;
754
+ }
755
+ function create() {
756
+ if (typeof document === "undefined")
757
+ return null;
758
+ const canvas = document.createElement("canvas");
759
+ canvas.width = canvas.height = MOST_PIXELS;
760
+ const gl = canvas.getContext("webgl2", { alpha: true, premultipliedAlpha: true, antialias: false, depth: false, stencil: false, preserveDrawingBuffer: false });
761
+ if (!gl)
762
+ return null;
763
+ const vertex = compile(gl, gl.VERTEX_SHADER, VERTEX);
764
+ const fragment = compile(gl, gl.FRAGMENT_SHADER, FRAGMENT);
765
+ const program = gl.createProgram();
766
+ if (!vertex || !fragment || !program)
767
+ return null;
768
+ gl.attachShader(program, vertex);
769
+ gl.attachShader(program, fragment);
770
+ gl.linkProgram(program);
771
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS))
772
+ return null;
773
+ gl.useProgram(program);
774
+ const at = Object.fromEntries(NAMES.map((name) => [name, gl.getUniformLocation(program, name)]));
775
+ canvas.addEventListener("webglcontextlost", (event) => {
776
+ event.preventDefault();
777
+ renderer = null;
778
+ });
779
+ canvas.addEventListener("webglcontextrestored", () => {
780
+ renderer = undefined;
781
+ for (const redraw of restored)
782
+ redraw();
783
+ });
784
+ return { gl, at };
785
+ }
786
+ /** Whether figures can be drawn here at all (WebGL 2, and the shader compiled). */
787
+ export function canDraw() {
788
+ if (renderer === undefined)
789
+ renderer = create();
790
+ return Boolean(renderer && !renderer.gl.isContextLost());
791
+ }
792
+ /** Draws a figure's scene onto `target` (its own canvas, in device pixels). False when there's no WebGL 2, or it was lost. */
793
+ export function drawFigure(target, scene) {
794
+ if (renderer === undefined)
795
+ renderer = create();
796
+ const r = renderer;
797
+ if (!r || r.gl.isContextLost())
798
+ return false;
799
+ let context = contexts.get(target);
800
+ if (context === undefined) {
801
+ context = target.getContext("2d");
802
+ contexts.set(target, context);
803
+ }
804
+ if (!context)
805
+ return false;
806
+ const { gl, at } = r;
807
+ const size = Math.min(MOST_PIXELS, target.width);
808
+ const { body, eyes, aura } = scene;
809
+ gl.viewport(0, 0, size, size);
810
+ gl.uniform2f(at.uSize, size, size);
811
+ gl.uniform1i(at.uShape, scene.shape);
812
+ gl.uniform1i(at.uMorphTo, scene.morphTo);
813
+ gl.uniform1f(at.uMorph, scene.morph);
814
+ gl.uniform1f(at.uTime, scene.time);
815
+ gl.uniformMatrix3fv(at.uBody, false, body.rotation);
816
+ gl.uniform3fv(at.uBodyPos, body.position);
817
+ gl.uniform3fv(at.uBodyScale, body.scale);
818
+ gl.uniform1f(at.uSplit, scene.split);
819
+ gl.uniform1f(at.uSpread, scene.spread);
820
+ gl.uniformMatrix3fv(at.uCells, false, scene.cells);
821
+ gl.uniform4fv(at.uLayers, scene.layers);
822
+ gl.uniform4fv(at.uPieces, scene.pieces);
823
+ gl.uniform1f(at.uShear, scene.shear);
824
+ gl.uniform1f(at.uCore, scene.core);
825
+ gl.uniform4fv(at.uBeads, scene.beads);
826
+ gl.uniform1i(at.uBeadCount, scene.beadCount);
827
+ gl.uniform4fv(at.uPuffs, scene.puffs);
828
+ gl.uniform4fv(at.uSurface, scene.surface);
829
+ gl.uniform1f(at.uRoll, scene.roll);
830
+ gl.uniform4fv(at.uBits, scene.bits);
831
+ gl.uniform4fv(at.uBitKinds, scene.bitKinds);
832
+ gl.uniform2fv(at.uRainbow, scene.rainbow);
833
+ gl.uniform2fv(at.uBolt, scene.bolt);
834
+ gl.uniform3fv(at.uEyeO, eyes.origin);
835
+ gl.uniform3fv(at.uEyeR, eyes.right);
836
+ gl.uniform3fv(at.uEyeU, eyes.up);
837
+ gl.uniform3fv(at.uEyeF, eyes.forward);
838
+ gl.uniform4f(at.uEyeSize, eyes.width, eyes.height, eyes.gap, eyes.size);
839
+ gl.uniform2f(at.uLook, eyes.dx, eyes.dy);
840
+ gl.uniform3fv(at.uLidL, eyes.left);
841
+ gl.uniform3fv(at.uLidR, eyes.right3);
842
+ gl.uniform4f(at.uGlyph, eyes.glyph, eyes.glyphTo, eyes.glyphMix, eyes.open);
843
+ gl.uniform3fv(at.uInk, scene.ink);
844
+ gl.uniform3fv(at.uPalA, aura.palette.a);
845
+ gl.uniform3fv(at.uPalB, aura.palette.b);
846
+ gl.uniform3fv(at.uPalD, aura.palette.d);
847
+ gl.uniform1f(at.uHue, aura.hue);
848
+ gl.uniform1f(at.uFlow, aura.flow);
849
+ gl.uniform1f(at.uSwirl, aura.swirl);
850
+ gl.uniform1f(at.uEnergy, aura.energy);
851
+ gl.uniform1f(at.uSaturation, aura.saturation);
852
+ gl.uniform1f(at.uHalo, aura.halo);
853
+ gl.uniform1f(at.uRipple, aura.ripple);
854
+ gl.uniform1f(at.uRings, aura.rings);
855
+ gl.uniform1f(at.uArc, aura.arc);
856
+ gl.uniform1f(at.uArcAngle, aura.arcAngle);
857
+ gl.uniform1f(at.uFloorY, scene.floor);
858
+ gl.uniform1f(at.uBottom, scene.bottom);
859
+ gl.uniform1f(at.uReach, scene.reach);
860
+ gl.uniform1f(at.uMilk, aura.milk);
861
+ gl.clearColor(0, 0, 0, 0);
862
+ gl.clear(gl.COLOR_BUFFER_BIT);
863
+ gl.drawArrays(gl.TRIANGLES, 0, 3);
864
+ // The drawing sits at the bottom left of the shared canvas; copy it before the browser clears it.
865
+ context.clearRect(0, 0, target.width, target.height);
866
+ context.drawImage(gl.canvas, 0, MOST_PIXELS - size, size, size, 0, 0, target.width, target.height);
867
+ return true;
868
+ }
869
+ /** Calls `redraw` when a lost context comes back, so a figure can draw again. */
870
+ export function onRestored(redraw) {
871
+ restored.add(redraw);
872
+ return () => {
873
+ restored.delete(redraw);
874
+ };
875
+ }