@uxfront/scene 0.1.0 → 0.2.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,76 @@
1
+ import { smoothstep } from "../../gl/math.js";
2
+ import { defineFormation, mergeViews } from "../../gl/formation.js";
3
+ import { CARDS, CARD_NAME, cardPoint, cardProgress, cardReach } from "./geometry.js";
4
+ //#region src/formations/stack/index.ts
5
+ /**
6
+ * Each card's label is its framework's name, in the card's header: the
7
+ * `UxAnchor` tag starts a little to the right of the anchor, so the anchor sits
8
+ * just before the spot the artwork leaves for it.
9
+ */
10
+ const LABEL = [CARD_NAME[0] - .07, CARD_NAME[1]];
11
+ /**
12
+ * A stack of seven glass cards, one per rail of the prism, each holding the
13
+ * same component, drawn in light and tinted in its rail's colour. They're
14
+ * dealt one after another as the chapter is read: the first lands in front,
15
+ * and each next one fans out from behind the one before it. The component
16
+ * plays on the same beat on all of them: the default sign-up form types its
17
+ * email, switches "remember me" on and presses submit. Follows the prism:
18
+ * rail `i` flows into card `i`.
19
+ *
20
+ * Anchors `stack:0` to `stack:6` sit in the cards' headers, front to back,
21
+ * where the name goes, and show once each card is in place.
22
+ */
23
+ function stack(options) {
24
+ const anchors = Array.from({ length: CARDS }, (_, index) => ({
25
+ id: String(index),
26
+ place: ({ approach, local, time }, out) => {
27
+ cardPoint(index, LABEL, time, out);
28
+ return smoothstep(.85, 1, cardReach(index, cardProgress(approach, local)));
29
+ }
30
+ }));
31
+ return defineFormation({
32
+ key: options.key ?? "stack",
33
+ label: options.label,
34
+ views: mergeViews({
35
+ desktop: {
36
+ eye: [
37
+ 0,
38
+ 2.7,
39
+ 9.8
40
+ ],
41
+ target: [
42
+ .99,
43
+ .44,
44
+ -1.5
45
+ ],
46
+ fov: 30,
47
+ span: [1.8, 1.55],
48
+ shift: [.34, 0]
49
+ },
50
+ mobile: {
51
+ eye: [
52
+ 0,
53
+ 2.5,
54
+ 9.6
55
+ ],
56
+ target: [
57
+ .84,
58
+ .44,
59
+ -1.5
60
+ ],
61
+ fov: 34,
62
+ span: [2.05, 2.1],
63
+ shift: [0, .2]
64
+ }
65
+ }, options.views),
66
+ look: {
67
+ bloom: 1,
68
+ reflect: .5,
69
+ ...options.look
70
+ },
71
+ anchors,
72
+ shader: () => import("./shader.js").then((m) => m.createShader(options.art))
73
+ });
74
+ }
75
+ //#endregion
76
+ export { stack };
@@ -0,0 +1,193 @@
1
+ import { BASE_PARTICLES } from "../../gl/constants.js";
2
+ import { packShapes } from "../../gl/shapes.js";
3
+ import { glslArray, glslFloat, glslVec } from "../../gl/glsl.js";
4
+ import { SPECTRUM } from "../prism/spectrum.js";
5
+ import { BEHIND, CARD, CARDS, CARD_APPROACH, CARD_CORNER, CARD_SCALE, FRONT } from "./geometry.js";
6
+ import { CARD_ROLE, KNOB_TRAVEL, SIGN_UP_FORM_ART } from "./art.js";
7
+ //#region src/formations/stack/shader.ts
8
+ const ART_SHARE = .57;
9
+ const LAST = CARDS - 1;
10
+ /** The card's own border, drawn before its artwork. */
11
+ const BORDER = {
12
+ kind: "rrect",
13
+ c: [0, 0],
14
+ h: CARD,
15
+ r: CARD_CORNER,
16
+ bright: .75
17
+ };
18
+ function createShader(art = SIGN_UP_FORM_ART) {
19
+ const table = packShapes([BORDER, ...art], (shape) => shape.role ?? CARD_ROLE.still);
20
+ const typing = art.find((shape) => shape.role === CARD_ROLE.typing);
21
+ const typedLength = typing?.kind === "seg" ? typing.b[0] - typing.a[0] : 0;
22
+ return { glsl: `
23
+ const vec2 $CARD = ${glslVec(CARD)};
24
+ const float $CORNER = ${glslFloat(CARD_CORNER)};
25
+ const float $SCALE = ${glslFloat(CARD_SCALE)};
26
+ const vec3 $FRONT = ${glslVec(FRONT)};
27
+ const vec3 $BEHIND = ${glslVec(BEHIND)};
28
+ const float $KNOB_TRAVEL = ${glslFloat(KNOB_TRAVEL)};
29
+ const float $TYPED_LENGTH = ${glslFloat(typedLength)};
30
+
31
+ // The prism's spectrum, so each rail keeps its colour as it becomes a card.
32
+ const vec3 $SPECTRUM[${CARDS}] = ${glslArray("vec3", SPECTRUM.map(glslVec))};
33
+
34
+ const int $SHAPES = ${table.cdf.length};
35
+ const vec4 $SHAPE_A[$SHAPES] = ${glslArray("vec4", table.a.map(glslVec))};
36
+ const vec4 $SHAPE_B[$SHAPES] = ${glslArray("vec4", table.b.map(glslVec))};
37
+ const float $SHAPE_CDF[$SHAPES] = ${glslArray("float", table.cdf.map(glslFloat))};
38
+ // Line brightness independent of how much artwork a card carries.
39
+ const float $GAIN = ${glslFloat(table.totalWeight / (BASE_PARTICLES * ART_SHARE / CARDS) * 120)};
40
+
41
+ // Cards are dealt as the chapter scrolls in and is read (see cardProgress).
42
+ float $progress(Frame f) {
43
+ if (f.approach < 1.0) return f.approach * ${glslFloat(CARD_APPROACH)};
44
+ return mix(${glslFloat(CARD_APPROACH)}, 1.0, f.local);
45
+ }
46
+ float $reach(int i, float progress) {
47
+ float o = float(i) * 0.08;
48
+ return smoothstep(o, o + 0.45, progress);
49
+ }
50
+
51
+ // The same beat on every card, every four seconds: the email is typed, one
52
+ // character at a time, "remember me" switches on, submit is pressed, and the
53
+ // form clears. x: toggle, y: press, z: typed share of the email, w: caret.
54
+ vec4 $beat(float time) {
55
+ float ph = fract(time * 0.25);
56
+ float clear = 1.0 - smoothstep(0.9, 0.95, ph);
57
+ float typed = floor(clamp((ph - 0.04) / 0.3, 0.0, 1.0) * 18.0) / 18.0 * clear;
58
+ float on = smoothstep(0.42, 0.5, ph) * clear;
59
+ float press = exp(-pow((ph - 0.64) * 16.0, 2.0));
60
+ float caret = step(0.5, fract(time * 1.8)) * clear;
61
+ return vec4(on, press, typed, caret);
62
+ }
63
+
64
+ float $ease(float reach) {
65
+ return 1.0 - pow(1.0 - reach, 3.0);
66
+ }
67
+
68
+ // How far back card i sits, in cards. Card 0 is the front card; each next card
69
+ // fans out from behind the one before it as it's dealt.
70
+ float $depth(int i, float progress) {
71
+ if (i == 0) return 0.0;
72
+ return float(i) - 1.0 + $ease($reach(i, progress));
73
+ }
74
+
75
+ vec3 $slot(float depth, float time) {
76
+ return $FRONT + $BEHIND * depth + vec3(0.0, sin(time * 0.7) * 0.03, 0.0);
77
+ }
78
+
79
+ // Card-local point q (art units) of card i in the world. The front card flies
80
+ // in from below and in front, tipped towards the viewer.
81
+ vec3 $place(int i, vec2 q, float progress, float time) {
82
+ vec3 w = vec3(q * $SCALE, 0.0);
83
+ if (i > 0) return $slot($depth(i, progress), time) + w;
84
+ float e = $ease($reach(0, progress));
85
+ float tip = (1.0 - e) * 0.9;
86
+ vec3 tipped = vec3(w.x, w.y * cos(tip), w.y * sin(tip));
87
+ return $slot(0.0, time) + tipped + vec3(0.3, -1.4, 2.2) * (1.0 - e);
88
+ }
89
+
90
+ float $sdCard(vec2 p) {
91
+ vec2 d = abs(p) - $CARD + $CORNER;
92
+ return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0) - $CORNER;
93
+ }
94
+
95
+ // How much of card i, at q, the cards in front of it hide. The renderer has no
96
+ // depth test, so what's behind is dimmed instead, as through frosted glass. A
97
+ // card's place hides what's behind it from the start of its deal, so the next
98
+ // card only shows once it fans out past it.
99
+ float $covered(int i, vec2 q, float progress) {
100
+ float depth = $depth(i, progress);
101
+ float cover = 0.0;
102
+ for (int j = 0; j < i; j++) {
103
+ vec2 rel = q + (depth - $depth(j, progress)) * $BEHIND.xy / $SCALE;
104
+ float inside = 1.0 - smoothstep(-0.03, 0.02, $sdCard(rel));
105
+ cover = max(cover, inside * smoothstep(0.0, 0.15, $reach(j, progress)));
106
+ }
107
+ return cover;
108
+ }
109
+
110
+ int $findShape(float x) {
111
+ for (int k = 0; k < $SHAPES - 1; k++) {
112
+ if (x < $SHAPE_CDF[k]) return k;
113
+ }
114
+ return $SHAPES - 1;
115
+ }
116
+
117
+ vec2 $shapePoint(int k, float b, float c, vec4 beat, out float light) {
118
+ vec4 A = $SHAPE_A[k];
119
+ vec4 B = $SHAPE_B[k];
120
+ int role = int(B.z + 0.5);
121
+ light = B.w;
122
+ // The line being typed grows with the email.
123
+ vec2 q = shapePoint(A, B, role == ${CARD_ROLE.typing} ? b * beat.z : b, c);
124
+ if (role == ${CARD_ROLE.knob}) {
125
+ q.x += beat.x * $KNOB_TRAVEL;
126
+ light *= mix(0.8, 1.3, beat.x);
127
+ } else if (role == ${CARD_ROLE.track}) {
128
+ light *= mix(0.25, 1.6, beat.x);
129
+ } else if (role == ${CARD_ROLE.button}) {
130
+ // Pressed in towards its centre.
131
+ vec2 centre = int(B.x + 0.5) == 0 ? (A.xy + A.zw) * 0.5 : A.xy;
132
+ q = centre + (q - centre) * (1.0 - beat.y * 0.06);
133
+ light *= 1.0 + beat.y * 1.8;
134
+ } else if (role == ${CARD_ROLE.typing}) {
135
+ // The same particles over a shorter line: keep its brightness even.
136
+ light *= max(beat.z, 0.001);
137
+ } else if (role == ${CARD_ROLE.caret}) {
138
+ q.x += beat.z * $TYPED_LENGTH;
139
+ light *= beat.w;
140
+ }
141
+ return q;
142
+ }
143
+
144
+ Particle $main(uint id, float sel, Frame f) {
145
+ float progress = $progress(f);
146
+ float time = f.time;
147
+ uint s = seedOf(id, 0x5AC4ED07u);
148
+ float a = rnd(s);
149
+ float b = rnd(s);
150
+ float c = rnd(s);
151
+ float d = rnd(s);
152
+ Particle P = blank();
153
+ float sweep = mod(time * 0.5, 7.0) - 3.0;
154
+
155
+ if (sel < ${glslFloat(.3500000000000001)}) {
156
+ // Frosted glass, with a glint sweeping across the stack.
157
+ int i = min(int(a * ${glslFloat(CARDS)}), ${LAST});
158
+ float e = $reach(i, progress);
159
+ vec2 q = (vec2(b, c) * 2.0 - 1.0) * $CARD;
160
+ P.pos = $place(i, q, progress, time);
161
+ float glint = exp(-pow((P.pos.x - sweep) * 2.2, 2.0));
162
+ P.col = mix($SPECTRUM[i], vec3(1.0), 0.4) * (0.016 + glint * 0.035) * smoothstep(0.0, 0.3, e);
163
+ P.col *= 1.0 - 0.9 * $covered(i, q, progress);
164
+ P.size = 0.75;
165
+ P.refl = 1.0;
166
+ } else if (sel < 0.92) {
167
+ // Seven cards, one per rail, each holding the same component.
168
+ int i = min(int((sel - ${glslFloat(.3500000000000001)}) / ${glslFloat(ART_SHARE)} * ${glslFloat(CARDS)}), ${LAST});
169
+ float e = $reach(i, progress);
170
+ float light;
171
+ vec2 q = $shapePoint($findShape(a), b, c, $beat(time), light);
172
+ P.pos = $place(i, q, progress, time);
173
+ float glint = exp(-pow((P.pos.x - sweep) * 2.2, 2.0));
174
+ float landed = exp(-pow((e - 0.97) * 10.0, 2.0));
175
+ vec3 tint = mix($SPECTRUM[i], vec3(1.0), 0.18);
176
+ P.col = tint * light * $GAIN * (1.0 + glint * 0.8 + landed) * smoothstep(0.0, 0.3, e);
177
+ P.col *= 1.0 - 0.97 * $covered(i, q, progress);
178
+ P.size = 0.75;
179
+ P.refl = 1.0;
180
+ } else {
181
+ // Dust caught in the light.
182
+ vec3 p = vec3(mix(-5.0, 5.0, a), mix(-1.35, 2.6, b), mix(-4.0, 2.5, c));
183
+ p.x = mod(p.x + time * 0.04 + 5.0, 10.0) - 5.0;
184
+ P.pos = p;
185
+ P.col = vec3(0.7, 0.75, 0.9) * 0.05 * step(0.4, d);
186
+ P.size = 0.9;
187
+ }
188
+ return P;
189
+ }
190
+ ` };
191
+ }
192
+ //#endregion
193
+ export { createShader };
@@ -0,0 +1,98 @@
1
+ import { lerp, smoothstep } from "../../gl/math.js";
2
+ import "../prism/geometry.js";
3
+ //#region src/formations/threshold/geometry.ts
4
+ /** One beam per rail of the prism, top to bottom in its order. */
5
+ const BEAMS = 7;
6
+ /** The film: a disc of light standing in the dark, its centre and radius. */
7
+ const CENTER = [
8
+ 0,
9
+ .15,
10
+ 0
11
+ ];
12
+ const RADIUS = 1.35;
13
+ const deg = (d) => d * Math.PI / 180;
14
+ const unit = (v) => {
15
+ const l = Math.hypot(...v);
16
+ return [
17
+ v[0] / l,
18
+ v[1] / l,
19
+ v[2] / l
20
+ ];
21
+ };
22
+ /**
23
+ * The film stands turned about the vertical: its normal points right and away,
24
+ * so its right-hand side comes towards the viewer.
25
+ */
26
+ const TURN = deg(40);
27
+ const NORMAL = [
28
+ Math.cos(TURN),
29
+ 0,
30
+ -Math.sin(TURN)
31
+ ];
32
+ /** In the film's plane: across (horizontal, towards the viewer) and up. */
33
+ const ACROSS = [
34
+ Math.sin(TURN),
35
+ 0,
36
+ Math.cos(TURN)
37
+ ];
38
+ const UP = [
39
+ 0,
40
+ 1,
41
+ 0
42
+ ];
43
+ /**
44
+ * The beams come in from the left, parallel, and go through the film. Beyond
45
+ * it they're `FASTER` times faster: they turn the corner and converge, over
46
+ * `CONVERGE` world units, into one white stream that races off into the
47
+ * distance, the prism in reverse.
48
+ */
49
+ const IN_DIR = [
50
+ 1,
51
+ 0,
52
+ 0
53
+ ];
54
+ const OUT_DIR = unit([
55
+ .28,
56
+ 0,
57
+ -1
58
+ ]);
59
+ const CONVERGE = 2.6;
60
+ const FASTER = 2.2;
61
+ /**
62
+ * Where the beams cross the film: down its vertical diameter, like the prism's
63
+ * rails. The height of the first above the centre, and the step to the next.
64
+ */
65
+ const CROSSING = [.84, .28];
66
+ /**
67
+ * Beams break through one after another as the chapter is read: beam `i` from
68
+ * `from + i * step`, over `span` of the progress.
69
+ */
70
+ const BREAK = {
71
+ from: .22,
72
+ step: .05,
73
+ span: .25
74
+ };
75
+ /** Share of the progress that happens while the chapter scrolls in. */
76
+ const BEAM_APPROACH = .3;
77
+ /** Progress: starts with the morph into the formation, not once it's held. */
78
+ const beamProgress = (approach, local) => approach < 1 ? approach * BEAM_APPROACH : lerp(BEAM_APPROACH, 1, local);
79
+ /** How far beam `index` has broken through the film, 0 → 1 (see `BREAK`). */
80
+ const beamReach = (index, progress) => {
81
+ const from = BREAK.from + index * BREAK.step;
82
+ return smoothstep(from, from + BREAK.span, progress);
83
+ };
84
+ /** Where the seven meet, level with the middle beam: past the film, round the corner. */
85
+ const MERGE = [
86
+ CENTER[0] + IN_DIR[0] * .9 + OUT_DIR[0] * 1.5,
87
+ CENTER[1] + CROSSING[0] - 3 * CROSSING[1],
88
+ CENTER[2] + IN_DIR[2] * .9 + OUT_DIR[2] * 1.5
89
+ ];
90
+ /** A point `t` world units down the white stream, past where the seven meet. */
91
+ function streamPoint(t, out) {
92
+ out[0] = MERGE[0] + OUT_DIR[0] * t;
93
+ out[1] = MERGE[1] + OUT_DIR[1] * t;
94
+ out[2] = MERGE[2] + OUT_DIR[2] * t;
95
+ return out;
96
+ }
97
+ //#endregion
98
+ export { ACROSS, BEAMS, BEAM_APPROACH, BREAK, CENTER, CONVERGE, CROSSING, FASTER, IN_DIR, MERGE, NORMAL, OUT_DIR, RADIUS, UP, beamProgress, beamReach, streamPoint };
@@ -0,0 +1,16 @@
1
+ import { Formation, FormationOptions } from "../../gl/types.js";
2
+ //#region src/formations/threshold/index.d.ts
3
+ /**
4
+ * The seven beams of light, one per rail of the prism, cross a film of light
5
+ * into whatever comes next. The film is a soap film on a ring: its colours
6
+ * swirl as it drains, and ripples spread from where each beam goes through.
7
+ * Beyond it the light is faster: the beams turn the corner and converge into
8
+ * one white stream, the prism in reverse, which races off into the distance,
9
+ * its pulses stretched into streaks. They break through one after another as
10
+ * the chapter is read, and the stream brightens as each one joins it.
11
+ *
12
+ * Anchor `threshold:stream` sits just above the white stream, for its label,
13
+ * and shows once every beam has joined it.
14
+ */
15
+ export declare function threshold(options: FormationOptions): Formation;
16
+ //#endregion
@@ -0,0 +1,75 @@
1
+ import { smoothstep } from "../../gl/math.js";
2
+ import { defineFormation, mergeViews } from "../../gl/formation.js";
3
+ import { BEAMS, CONVERGE, beamProgress, beamReach, streamPoint } from "./geometry.js";
4
+ //#region src/formations/threshold/index.ts
5
+ /** The white stream's label: how far down the stream, and how far above it. */
6
+ const LABEL_AT = .3;
7
+ const LABEL_ABOVE = .32;
8
+ /**
9
+ * The seven beams of light, one per rail of the prism, cross a film of light
10
+ * into whatever comes next. The film is a soap film on a ring: its colours
11
+ * swirl as it drains, and ripples spread from where each beam goes through.
12
+ * Beyond it the light is faster: the beams turn the corner and converge into
13
+ * one white stream, the prism in reverse, which races off into the distance,
14
+ * its pulses stretched into streaks. They break through one after another as
15
+ * the chapter is read, and the stream brightens as each one joins it.
16
+ *
17
+ * Anchor `threshold:stream` sits just above the white stream, for its label,
18
+ * and shows once every beam has joined it.
19
+ */
20
+ function threshold(options) {
21
+ return defineFormation({
22
+ key: options.key ?? "threshold",
23
+ label: options.label,
24
+ views: mergeViews({
25
+ desktop: {
26
+ eye: [
27
+ 1.5,
28
+ .6,
29
+ 10.5
30
+ ],
31
+ target: [
32
+ .9,
33
+ .1,
34
+ -.6
35
+ ],
36
+ fov: 30,
37
+ span: [2.9, 1.8],
38
+ shift: [-.3, 0]
39
+ },
40
+ mobile: {
41
+ eye: [
42
+ 1.5,
43
+ .6,
44
+ 10.5
45
+ ],
46
+ target: [
47
+ .75,
48
+ .1,
49
+ -.6
50
+ ],
51
+ fov: 34,
52
+ span: [1.8, 1.5],
53
+ shift: [0, .3]
54
+ }
55
+ }, options.views),
56
+ look: {
57
+ bloom: 1.15,
58
+ reflect: .6,
59
+ ...options.look
60
+ },
61
+ anchors: [{
62
+ id: "stream",
63
+ place: ({ approach, local }, out) => {
64
+ streamPoint(LABEL_AT, out);
65
+ out[1] += LABEL_ABOVE;
66
+ const joined = (CONVERGE + LABEL_AT) / 11;
67
+ const reach = beamReach(BEAMS - 1, beamProgress(approach, local));
68
+ return smoothstep(joined, Math.min(1, joined + .25), reach);
69
+ }
70
+ }],
71
+ shader: () => import("./shader.js").then((m) => m.shader)
72
+ });
73
+ }
74
+ //#endregion
75
+ export { threshold };
@@ -0,0 +1,186 @@
1
+ import { glslArray, glslFloat, glslVec } from "../../gl/glsl.js";
2
+ import { SPECTRUM } from "../prism/spectrum.js";
3
+ import { ACROSS, BEAMS, BEAM_APPROACH, BREAK, CENTER, CONVERGE, CROSSING, FASTER, IN_DIR, MERGE, NORMAL, OUT_DIR, RADIUS, UP } from "./geometry.js";
4
+ //#region src/formations/threshold/shader.ts
5
+ const SPLIT = FASTER * 5 / (11 + FASTER * 5);
6
+ const RATE = .04;
7
+ const SPEED = 5 * RATE / SPLIT;
8
+ const shader = { glsl: `
9
+ const vec3 $C = ${glslVec(CENTER)};
10
+ const float $R = ${glslFloat(RADIUS)};
11
+ const vec3 $N = ${glslVec(NORMAL)};
12
+ const vec3 $U = ${glslVec(ACROSS)};
13
+ const vec3 $V = ${glslVec(UP)};
14
+ const vec3 $IN = ${glslVec(IN_DIR)};
15
+ const vec3 $OUT = ${glslVec(OUT_DIR)};
16
+ const float $IN_LEN = ${glslFloat(5)};
17
+ const float $OUT_LEN = ${glslFloat(11)};
18
+ const float $FASTER = ${glslFloat(FASTER)};
19
+ const float $CONVERGE = ${glslFloat(CONVERGE)};
20
+ const vec3 $MERGE = ${glslVec(MERGE)};
21
+ // The next framework's light: white, with a faint sheen of every colour.
22
+ const vec3 $WHITE = vec3(1.0, 0.97, 0.94);
23
+ const float $SPLIT = ${glslFloat(SPLIT)};
24
+ const float $RATE = ${glslFloat(RATE)};
25
+ const float $SPEED = ${glslFloat(SPEED)};
26
+
27
+ // The prism's spectrum, one colour per beam.
28
+ const vec3 $SPECTRUM[${BEAMS}] = ${glslArray("vec3", SPECTRUM.map(glslVec))};
29
+
30
+ float $progress(Frame f) {
31
+ if (f.approach < 1.0) return f.approach * ${glslFloat(BEAM_APPROACH)};
32
+ return mix(${glslFloat(BEAM_APPROACH)}, 1.0, f.local);
33
+ }
34
+ float $reach(int i, float progress) {
35
+ float o = ${glslFloat(BREAK.from)} + float(i) * ${glslFloat(BREAK.step)};
36
+ return smoothstep(o, o + ${glslFloat(BREAK.span)}, progress);
37
+ }
38
+
39
+ // Where beam i crosses the film, in the film's plane (across, up).
40
+ vec2 $crossing(int i) { return vec2(0.0, ${glslFloat(CROSSING[0])} - float(i) * ${glslFloat(CROSSING[1])}); }
41
+ vec3 $onFilm(vec2 q) { return $C + $U * q.x + $V * q.y; }
42
+
43
+ // How far along its beam a particle is, from its phase.
44
+ float $along(float phase) {
45
+ if (phase < $SPLIT) return phase / $SPLIT * $IN_LEN;
46
+ return $IN_LEN + (phase - $SPLIT) / (1.0 - $SPLIT) * $OUT_LEN;
47
+ }
48
+ // Beyond the film, the beam turns the corner and converges with the others on
49
+ // $MERGE, then runs on as one white stream. merged: 0 at the film, 1 from $MERGE.
50
+ vec3 $beamPoint(vec3 through, float dist, out float merged) {
51
+ merged = 0.0;
52
+ if (dist < $IN_LEN) return through - $IN * ($IN_LEN - dist);
53
+ float t = dist - $IN_LEN;
54
+ merged = 1.0;
55
+ if (t > $CONVERGE) return $MERGE + $OUT * (t - $CONVERGE);
56
+ float u = t / $CONVERGE;
57
+ merged = u;
58
+ vec3 p1 = through + $IN * $CONVERGE * 0.4;
59
+ vec3 p2 = $MERGE - $OUT * $CONVERGE * 0.35;
60
+ float v = 1.0 - u;
61
+ return v * v * v * through + 3.0 * v * v * u * p1 + 3.0 * v * u * u * p2 + u * u * u * $MERGE;
62
+ }
63
+ // When the light at dist set off: pulses travel with it, faster beyond the film.
64
+ float $arrival(float dist) {
65
+ if (dist < $IN_LEN) return dist / $SPEED;
66
+ return $IN_LEN / $SPEED + (dist - $IN_LEN) / ($SPEED * $FASTER);
67
+ }
68
+
69
+ // A soap film drains: thin at the top, thicker below, in swirling bands.
70
+ float $thickness(vec2 q, float t) {
71
+ vec2 w = q + 0.3 * vec2(sin(q.y * 2.3 + t * 0.4), sin(q.x * 2.9 - t * 0.35));
72
+ w += 0.12 * vec2(sin(w.y * 4.1 - t * 0.6), sin(w.x * 3.7 + t * 0.5));
73
+ return 0.35 - w.y * 0.4 + 0.08 * sin(length(w) * 3.0 - t * 0.3);
74
+ }
75
+ // Thin-film interference: the colour of a film of thickness h.
76
+ vec3 $iridescence(float h) {
77
+ return 0.55 + 0.45 * cos(TAU * (h * 1.7 + vec3(0.0, 0.33, 0.67)));
78
+ }
79
+
80
+ // Rings spreading over the film from where each beam goes through, three at a
81
+ // time, and one wide ring as it breaks through.
82
+ float $ripples(vec2 q, float progress, float time) {
83
+ float w = 0.0;
84
+ for (int i = 0; i < ${BEAMS}; i++) {
85
+ float reach = $reach(i, progress);
86
+ if (reach <= 0.0) continue;
87
+ float d = length(q - $crossing(i));
88
+ for (int k = 0; k < 3; k++) {
89
+ float r = fract(time * 0.22 + float(k) / 3.0 + float(i) * 0.137);
90
+ float ring = exp(-pow((d - r * $R * 1.3) * 24.0, 2.0));
91
+ w += ring * pow(1.0 - r, 2.0) * smoothstep(0.0, 0.1, r);
92
+ }
93
+ float burst = smoothstep(0.0, 0.8, reach);
94
+ w += exp(-pow((d - burst * $R * 2.2) * 14.0, 2.0)) * (1.0 - burst) * 3.0;
95
+ }
96
+ return w;
97
+ }
98
+
99
+ Particle $main(uint id, float sel, Frame f) {
100
+ float progress = $progress(f);
101
+ float time = f.time;
102
+ uint s = seedOf(id, 0x7E5A01D3u);
103
+ float a = rnd(s);
104
+ float b = rnd(s);
105
+ float c = rnd(s);
106
+ float d = rnd(s);
107
+ Particle P = blank();
108
+ vec2 g = gauss(b, c);
109
+ float halo = step(0.82, d);
110
+
111
+ if (sel < 0.46) {
112
+ // Seven beams: slow on this side of the film, faster beyond it.
113
+ int i = min(int(sel / 0.46 * ${glslFloat(BEAMS)}), ${BEAMS - 1});
114
+ float reach = $reach(i, progress);
115
+ float dist = $along(fract(a + time * $RATE));
116
+ float beyond = step($IN_LEN, dist);
117
+ float grown = $IN_LEN + reach * $OUT_LEN;
118
+ float merged;
119
+ vec3 p = $beamPoint($onFilm($crossing(i)), dist, merged);
120
+ float joined = smoothstep(0.55, 1.0, merged);
121
+ vec3 side = normalize(cross(mix($IN, $OUT, merged), $V));
122
+ vec2 off = g * mix(0.014, 0.05, halo) * (1.0 + 0.4 * joined);
123
+ P.pos = p + $V * off.x + side * off.y;
124
+ // Each beam keeps its own beat until it joins the stream, then they pulse as one.
125
+ float beat = float(i) * 1.3 * (1.0 - joined);
126
+ float pulse = pow(0.5 + 0.5 * sin(($arrival(dist) - time) * 4.0 + beat), 10.0);
127
+ float front = beyond * smoothstep(grown - 0.5, grown, dist) * (1.0 - smoothstep(0.97, 1.0, reach));
128
+ float shown = 1.0 - step(grown, dist);
129
+ float fade = smoothstep(0.0, 2.5, dist) * (1.0 - smoothstep($IN_LEN + $OUT_LEN * 0.3, $IN_LEN + $OUT_LEN * 0.8, dist));
130
+ // Sparser beyond the film, where the light moves faster: brighter to match.
131
+ // Where the seven overlap, each gives less, or the stream blows out.
132
+ float gain = mix(1.0, $FASTER * 1.2, beyond) * mix(1.0, 0.4, joined);
133
+ // The colours mix into white as the beams meet, and the stream shimmers.
134
+ vec3 white = mix($WHITE, $iridescence((dist - $IN_LEN) * 0.12 - time * 0.08), 0.12);
135
+ vec3 col = mix($SPECTRUM[i], white, smoothstep(0.25, 1.0, merged) * beyond);
136
+ P.col = col * mix(0.8, 0.1, halo) * (1.0 + pulse * 1.6 + front * 3.0) * gain * fade * shown;
137
+ P.size = mix(0.8, 1.1, halo);
138
+ P.refl = 1.0;
139
+ } else if (sel < 0.55) {
140
+ // Ripple rings, crisp over the film's glow.
141
+ int i = min(int(a * ${glslFloat(BEAMS)}), ${BEAMS - 1});
142
+ float reach = $reach(i, progress);
143
+ float k = floor(b * 3.0);
144
+ float r = fract(time * 0.22 + k / 3.0 + float(i) * 0.137);
145
+ vec2 q = $crossing(i) + r * $R * 1.3 * vec2(cos(c * TAU), sin(c * TAU));
146
+ float inside = 1.0 - smoothstep($R * 0.97, $R, length(q));
147
+ P.pos = $onFilm(q);
148
+ float life = pow(1.0 - r, 2.0) * smoothstep(0.0, 0.1, r);
149
+ P.col = mix($SPECTRUM[i], vec3(1.0), 0.55) * 0.3 * life * inside * smoothstep(0.0, 0.3, reach);
150
+ P.size = 0.75;
151
+ P.refl = 1.0;
152
+ } else if (sel < 0.85) {
153
+ // The film, iridescent, lit up by the ripples.
154
+ float rr = $R * sqrt(b);
155
+ vec2 q = rr * vec2(cos(c * TAU), sin(c * TAU));
156
+ float w = $ripples(q, progress, time);
157
+ float h = $thickness(q, time);
158
+ float edge = smoothstep($R * 0.7, $R, rr);
159
+ P.pos = $onFilm(q) + $N * w * 0.02;
160
+ P.col = $iridescence(h) * (0.028 + 0.04 * edge + 0.14 * w);
161
+ P.size = 0.9;
162
+ P.refl = 1.0;
163
+ } else if (sel < 0.9) {
164
+ // The ring that holds the film.
165
+ float ang = a * TAU;
166
+ vec2 q = ($R + g.x * 0.006) * vec2(cos(ang), sin(ang));
167
+ P.pos = $onFilm(q) + $N * g.y * 0.008;
168
+ float glint = pow(0.5 + 0.5 * cos(ang - time * 0.4), 10.0);
169
+ P.col = mix(vec3(0.82, 0.87, 1.0), $iridescence(ang / TAU + time * 0.03), 0.4) * (0.5 + glint);
170
+ P.size = 0.8;
171
+ P.refl = 1.0;
172
+ } else {
173
+ // Specks carried by the same current: slow on this side, fast beyond.
174
+ vec2 q = vec2(mix(-4.0, 4.0, a), mix(-1.2, 2.6, b));
175
+ float dist = $along(fract(c + time * $RATE * 0.5));
176
+ float beyond = step($IN_LEN, dist);
177
+ vec3 through = $onFilm(q);
178
+ P.pos = dist < $IN_LEN ? through - $IN * ($IN_LEN - dist) : through + $OUT * (dist - $IN_LEN);
179
+ P.col = vec3(0.7, 0.75, 0.95) * 0.06 * step(0.35, d) * mix(1.0, $FASTER, beyond);
180
+ P.size = 0.9;
181
+ }
182
+ return P;
183
+ }
184
+ ` };
185
+ //#endregion
186
+ export { shader };
package/dist/gl/glsl.d.ts CHANGED
@@ -6,4 +6,8 @@
6
6
  */
7
7
  /** Formats a number as a GLSL float literal, for constants interpolated into shaders. */
8
8
  export declare const glslFloat: (n: number) => string;
9
+ /** Formats two to four numbers as a GLSL vector literal, like `vec3(1.0, 0.5, 0.0)`. */
10
+ export declare const glslVec: (values: readonly number[]) => string;
11
+ /** Formats GLSL literals of one type as an array constructor, like `float[2](0.5, 1.0)`. */
12
+ export declare const glslArray: (type: string, items: readonly string[]) => string;
9
13
  //#endregion
package/dist/gl/glsl.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { SHAPE_POINT } from "./shapes.js";
1
2
  //#region src/gl/glsl.ts
2
3
  /**
3
4
  * The particle pipeline shared by every formation, and the post-processing
@@ -6,6 +7,13 @@
6
7
  */
7
8
  /** Formats a number as a GLSL float literal, for constants interpolated into shaders. */
8
9
  const glslFloat = (n) => Number.isInteger(n) ? `${n}.0` : `${n}`;
10
+ /** Formats two to four numbers as a GLSL vector literal, like `vec3(1.0, 0.5, 0.0)`. */
11
+ const glslVec = (values) => {
12
+ if (values.length < 2 || values.length > 4) throw new Error(`A GLSL vector holds 2 to 4 numbers, not ${values.length}.`);
13
+ return `vec${values.length}(${values.map(glslFloat).join(", ")})`;
14
+ };
15
+ /** Formats GLSL literals of one type as an array constructor, like `float[2](0.5, 1.0)`. */
16
+ const glslArray = (type, items) => `${type}[${items.length}](\n ${items.join(",\n ")}\n)`;
9
17
  /** Helpers every formation can use: hashing, noise, curl flow and path sampling. */
10
18
  const COMMON = `
11
19
  #define PI 3.141592653589793
@@ -127,7 +135,7 @@ vec2 rrectPoint(vec4 g, float r, float t) {
127
135
  float a = d / rr;
128
136
  return c + vec2(-inner.x, inner.y) + r * vec2(-cos(a), sin(a));
129
137
  }
130
- `;
138
+ ${SHAPE_POINT}`;
131
139
  /** Declarations every formation's `$main` works with. */
132
140
  const PARTICLE_TYPES = `
133
141
  struct Particle {
@@ -416,4 +424,4 @@ void main() {
416
424
  }
417
425
  `;
418
426
  //#endregion
419
- export { BLOOM_DOWN_FRAG, BLOOM_PREFILTER_FRAG, BLOOM_UP_FRAG, COMMON, COMPOSITE_FRAG, PARTICLE_FRAG, PARTICLE_MAIN, PARTICLE_TYPES, PARTICLE_UNIFORMS, QUAD_VERT, TRAIL_FRAG, glslFloat };
427
+ export { BLOOM_DOWN_FRAG, BLOOM_PREFILTER_FRAG, BLOOM_UP_FRAG, COMMON, COMPOSITE_FRAG, PARTICLE_FRAG, PARTICLE_MAIN, PARTICLE_TYPES, PARTICLE_UNIFORMS, QUAD_VERT, TRAIL_FRAG, glslArray, glslFloat, glslVec };