@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.
- package/COMPONENTS.md +101 -0
- package/package.json +1 -1
- package/src/components/LensFlare.astro +38 -0
- package/src/components/PaintDrip.astro +40 -0
- package/src/components/PhotoBokeh.astro +40 -0
- package/src/components/SunWall.astro +37 -0
- package/src/index.d.ts +9 -0
- package/src/index.js +5 -0
- package/src/runtime/glass-drop.d.ts +14 -0
- package/src/runtime/glass-drop.js +116 -6
- package/src/runtime/glass-plates.d.ts +63 -0
- package/src/runtime/glass-plates.js +327 -0
- package/src/runtime/glass-stack.d.ts +10 -0
- package/src/runtime/glass-stack.js +71 -3
- package/src/runtime/lens-flare.d.ts +45 -0
- package/src/runtime/lens-flare.js +283 -0
- package/src/runtime/paint-drip.d.ts +89 -0
- package/src/runtime/paint-drip.js +611 -0
- package/src/runtime/photo-bokeh.d.ts +57 -0
- package/src/runtime/photo-bokeh.js +351 -0
- package/src/runtime/shared-gl.d.ts +25 -0
- package/src/runtime/shared-gl.js +229 -0
- package/src/runtime/stage.js +18 -6
- package/src/runtime/sun-wall.d.ts +52 -0
- package/src/runtime/sun-wall.js +309 -0
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/glass-plates
|
|
3
|
+
*
|
|
4
|
+
* A logo cut from thick sheets of glass, laid one over another: the logo's
|
|
5
|
+
* shape as two to four plates of clear or tinted glass, each a little down
|
|
6
|
+
* and across from the one behind, over a photo. Every plate shows its edge
|
|
7
|
+
* (the green-blue band real glass shows through its thickness, brightest
|
|
8
|
+
* along the top), bends and magnifies the photo through its face, its
|
|
9
|
+
* colour deepening where plates overlap, a hairline lip and a dark band
|
|
10
|
+
* inside its rim, and a soft streak of reflected light across it. The
|
|
11
|
+
* stack casts a shadow on the photo. On first show the plates slide apart
|
|
12
|
+
* from one; after that they drift slowly, so the refraction shifts.
|
|
13
|
+
*
|
|
14
|
+
* Over the shared studio of the glass logos (./glass-optics.js); the logo's
|
|
15
|
+
* field is read from a small distance field, so each plate is one texture
|
|
16
|
+
* read per sample.
|
|
17
|
+
*
|
|
18
|
+
* Opaque: it paints its photo (or its `background`) everywhere. Without
|
|
19
|
+
* WebGL the canvas goes and the element's background shows. With reduced
|
|
20
|
+
* motion it is one still frame, the plates apart.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { createStage } from './stage.js';
|
|
24
|
+
import { drawShape, distanceField, blurField } from './shape.js';
|
|
25
|
+
import { parseColor, mixOklab } from './color.js';
|
|
26
|
+
import { GLSL_STUDIO, lightVector } from './glass-optics.js';
|
|
27
|
+
import { loadImage, imageTexture, coverScale } from './image.js';
|
|
28
|
+
|
|
29
|
+
/** The most plates. */
|
|
30
|
+
const MAX_PLATES = 4;
|
|
31
|
+
|
|
32
|
+
const FRAG = `
|
|
33
|
+
precision highp float;
|
|
34
|
+
uniform vec2 uRes;
|
|
35
|
+
uniform sampler2D uSdf;
|
|
36
|
+
uniform float uSpread;
|
|
37
|
+
uniform vec2 uCentre;
|
|
38
|
+
uniform float uBox;
|
|
39
|
+
uniform float uPlates;
|
|
40
|
+
uniform vec2 uStep;
|
|
41
|
+
uniform vec2 uSide;
|
|
42
|
+
uniform float uBevel;
|
|
43
|
+
uniform vec3 uTint[${MAX_PLATES}];
|
|
44
|
+
uniform vec3 uEdgeTint;
|
|
45
|
+
uniform vec3 uGround;
|
|
46
|
+
uniform vec3 uLight;
|
|
47
|
+
uniform vec2 uLightDir;
|
|
48
|
+
uniform float uDensity;
|
|
49
|
+
uniform float uBend;
|
|
50
|
+
uniform float uDisperse;
|
|
51
|
+
uniform float uStreak;
|
|
52
|
+
uniform float uShadow;
|
|
53
|
+
uniform sampler2D uPhoto;
|
|
54
|
+
uniform float uHasPhoto;
|
|
55
|
+
uniform float uAspect;
|
|
56
|
+
uniform vec2 uCover;
|
|
57
|
+
uniform float uDim;
|
|
58
|
+
uniform float uSoft;
|
|
59
|
+
${GLSL_STUDIO}
|
|
60
|
+
|
|
61
|
+
// The backdrop at p (heights, origin bottom left), at a mip bias: the photo covering the element, or the ground.
|
|
62
|
+
vec3 backdrop(vec2 p, float bias) {
|
|
63
|
+
if (uHasPhoto < 0.5) return uGround;
|
|
64
|
+
vec2 uv = (vec2(p.x / uAspect, 1.0 - p.y) - 0.5) * uCover + 0.5;
|
|
65
|
+
return texture2D(uPhoto, clamp(uv, 0.001, 0.999), bias).rgb * uDim;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The logo's signed distance at q (heights from the box's middle), positive inside.
|
|
69
|
+
float field(vec2 q) {
|
|
70
|
+
vec2 uv = q / uBox + 0.5;
|
|
71
|
+
vec2 c = clamp(uv, 0.0, 1.0);
|
|
72
|
+
float d = (texture2D(uSdf, vec2(c.x, 1.0 - c.y)).r - 0.5) * 2.0 * uSpread * uBox;
|
|
73
|
+
return d - length(uv - c) * uBox;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// A plate's height over its face: 0 at its edge, 1 past the bevel, a quarter round so the lip is steep.
|
|
77
|
+
float height(float g) {
|
|
78
|
+
float s = clamp(g / uBevel, 0.0, 1.0);
|
|
79
|
+
return sqrt(s * (2.0 - s));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
void main() {
|
|
83
|
+
vec2 p = gl_FragCoord.xy / uRes.y;
|
|
84
|
+
float px = 1.0 / uRes.y;
|
|
85
|
+
vec3 col = backdrop(p, uSoft * uHasPhoto);
|
|
86
|
+
float n = uPlates;
|
|
87
|
+
vec3 trans = vec3(1.0);
|
|
88
|
+
|
|
89
|
+
// the stack's shadow on the photo: the front plate's shape, down and across, soft
|
|
90
|
+
vec2 front = uCentre + uStep * (n - 1.0) * 0.5;
|
|
91
|
+
float sh = smoothstep(-uBox * 0.06, uBox * 0.02, field(p - front + uSide * 1.6 + uLightDir * uBox * 0.04));
|
|
92
|
+
col *= 1.0 - 0.45 * uShadow * sh;
|
|
93
|
+
|
|
94
|
+
for (int k = 0; k < ${MAX_PLATES}; k++) {
|
|
95
|
+
if (float(k) >= n) break;
|
|
96
|
+
vec2 o = uCentre + uStep * (float(k) - (n - 1.0) * 0.5);
|
|
97
|
+
vec2 q = p - o;
|
|
98
|
+
float g = field(q);
|
|
99
|
+
float face = clamp(g / (px * 1.2) + 0.5, 0.0, 1.0);
|
|
100
|
+
vec3 tint = uTint[k];
|
|
101
|
+
|
|
102
|
+
// the edge: the plate's thickness, seen down and across from its face, where the face isn't
|
|
103
|
+
float ge = max(field(q + uSide * 0.5), field(q + uSide));
|
|
104
|
+
float edge = clamp(ge / (px * 1.2) + 0.5, 0.0, 1.0) * (1.0 - face);
|
|
105
|
+
if (edge > 0.0) {
|
|
106
|
+
// how far down the edge (0 at the face's lip, 1 at the far side), for its light: brightest along the top
|
|
107
|
+
float along = clamp((field(q + uSide * 0.25) > 0.0 ? 0.25 : 0.75), 0.0, 1.0);
|
|
108
|
+
vec3 e = col * mix(vec3(1.0), uEdgeTint, 0.85) * 0.55 + uEdgeTint * 0.28 + uLight * 0.22 * (1.0 - along);
|
|
109
|
+
col = mix(col, e, edge * 0.96);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (face > 0.0) {
|
|
113
|
+
// the face: the photo bent through it (harder at the bevel, magnified toward its middle), the colours parting
|
|
114
|
+
vec2 e2 = vec2(px * 1.5, 0.0);
|
|
115
|
+
vec2 slope = vec2(height(field(q + e2.xy)) - height(field(q - e2.xy)), height(field(q + e2.yx)) - height(field(q - e2.yx))) / (2.0 * e2.x) * uBevel;
|
|
116
|
+
float h = height(g);
|
|
117
|
+
vec3 nrm = normalize(vec3(-slope * 0.6, 1.0));
|
|
118
|
+
vec2 bend = nrm.xy * uBend * (1.0 + float(k) * 0.25) - q * 0.06 * h;
|
|
119
|
+
vec3 split = vec3(1.0 + uDisperse, 1.0, 1.0 - uDisperse);
|
|
120
|
+
vec3 seen = vec3(backdrop(p - bend * split.x, 0.0).r, backdrop(p - bend, 0.0).g, backdrop(p - bend * split.z, 0.0).b);
|
|
121
|
+
trans *= pow(max(tint, vec3(0.02)), vec3(uDensity));
|
|
122
|
+
// what is already drawn behind (lower plates, their edges) shows through, tinted; the photo, bent, over it
|
|
123
|
+
vec3 body = mix(col * tint, seen * trans * 1.15, 0.6);
|
|
124
|
+
// the room it mirrors: little head on, much at the bevel; a soft streak across the face; the lip and a dark band
|
|
125
|
+
float fres = schlick(nrm.z, 0.04);
|
|
126
|
+
vec3 view = vec3(0.0, 0.0, -1.0);
|
|
127
|
+
body += studio(reflect(view, nrm), uLightDir, uLight) * (0.06 + fres * 1.4);
|
|
128
|
+
vec2 across = vec2(-uLightDir.y, uLightDir.x);
|
|
129
|
+
float s = dot(q, normalize(across + uLightDir * 0.6)) / uBox;
|
|
130
|
+
body += uLight * uStreak * 0.16 * exp(-pow((s + 0.12) / 0.07, 2.0)) * h;
|
|
131
|
+
body *= 1.0 - 0.22 * smoothstep(uBevel * 0.9, uBevel * 0.2, g) * smoothstep(0.0, px * 2.0, g);
|
|
132
|
+
float lip = exp(-pow(g / (px * 1.3), 2.0));
|
|
133
|
+
body += uLight * lip * (0.35 + 0.5 * max(dot(-normalize(slope + 1e-6), uLightDir), 0.0));
|
|
134
|
+
col = mix(col, body, face);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
gl_FragColor = vec4(col, 1.0);
|
|
138
|
+
}
|
|
139
|
+
`;
|
|
140
|
+
|
|
141
|
+
/** The defaults: three plates of clear glass with a green-blue edge, over the ground. */
|
|
142
|
+
const DEFAULTS = Object.freeze({
|
|
143
|
+
colors: ['#E8F6FF', '#CFEAF7', '#B6DCF0'],
|
|
144
|
+
edge: '#5FD6B8',
|
|
145
|
+
background: '#0b0b0b',
|
|
146
|
+
light: '#ffffff',
|
|
147
|
+
plates: 3,
|
|
148
|
+
spread: 0.07,
|
|
149
|
+
thickness: 0.045,
|
|
150
|
+
bevel: 0.035,
|
|
151
|
+
density: 0.6,
|
|
152
|
+
bend: 0.022,
|
|
153
|
+
dispersion: 0.35,
|
|
154
|
+
streak: 1,
|
|
155
|
+
shadow: 0.6,
|
|
156
|
+
dim: 0.75,
|
|
157
|
+
soften: 2,
|
|
158
|
+
lightAngle: 120,
|
|
159
|
+
drift: 1,
|
|
160
|
+
intro: true,
|
|
161
|
+
introDuration: 1.4,
|
|
162
|
+
delay: 0,
|
|
163
|
+
logoScale: 0.7,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
/** The logo's field, in pixels across. */
|
|
167
|
+
const FIELD = 360;
|
|
168
|
+
|
|
169
|
+
/** Hermite ease, 0–1. */
|
|
170
|
+
const ease = (x) => {
|
|
171
|
+
const t = Math.min(1, Math.max(0, x));
|
|
172
|
+
return t * t * (3 - 2 * t);
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The plates' tints: the colours as a gradient from the back plate to the front one, blended in Oklab.
|
|
177
|
+
*
|
|
178
|
+
* @param {string[]} colors One to six colours, back first.
|
|
179
|
+
* @param {number} plates How many plates.
|
|
180
|
+
* @returns {Array<[number, number, number]>} RGB 0–1, a plate each.
|
|
181
|
+
*/
|
|
182
|
+
export function plateTints(colors, plates) {
|
|
183
|
+
const rgb = (colors?.length ? colors : DEFAULTS.colors).map(parseColor);
|
|
184
|
+
return Array.from({ length: plates }, (_, i) => {
|
|
185
|
+
const x = (plates > 1 ? i / (plates - 1) : 0) * (rgb.length - 1);
|
|
186
|
+
const a = Math.min(Math.floor(x), rgb.length - 1);
|
|
187
|
+
const b = Math.min(a + 1, rgb.length - 1);
|
|
188
|
+
return /** @type {[number, number, number]} */ (mixOklab(rgb[a], rgb[b], x - a));
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Where plate `k` of `n` sits, as an offset from the stack's middle in shares of its box: each plate a `spread` down and
|
|
194
|
+
* across from the one behind, the stack centred.
|
|
195
|
+
*
|
|
196
|
+
* @param {number} k The plate, 0 the back one.
|
|
197
|
+
* @param {number} n How many.
|
|
198
|
+
* @param {number} spread The step between plates, a share of the box.
|
|
199
|
+
* @returns {{ x: number, y: number }} The offset, x right and y down.
|
|
200
|
+
*/
|
|
201
|
+
export function plateOffset(k, n, spread) {
|
|
202
|
+
const t = k - (n - 1) / 2;
|
|
203
|
+
return { x: t * spread, y: t * spread };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Starts the glass plates on an element: an opaque canvas inside it, filling it.
|
|
208
|
+
*
|
|
209
|
+
* @param {HTMLElement} el The element (its first canvas is drawn into).
|
|
210
|
+
* @param {import('./glass-plates.js').GlassPlatesConfig} config The settings.
|
|
211
|
+
* @returns {() => void} Stops it.
|
|
212
|
+
*/
|
|
213
|
+
export function glassPlates(el, config) {
|
|
214
|
+
const c = { ...DEFAULTS, ...config };
|
|
215
|
+
const plates = Math.max(2, Math.min(MAX_PLATES, Math.round(c.plates)));
|
|
216
|
+
const tints = plateTints(c.colors, plates);
|
|
217
|
+
const tintArray = new Float32Array(MAX_PLATES * 3);
|
|
218
|
+
tints.forEach((t, i) => tintArray.set(t, i * 3));
|
|
219
|
+
const start = performance.now();
|
|
220
|
+
let generation = 0;
|
|
221
|
+
let box = 0.5;
|
|
222
|
+
let sdf = /** @type {WebGLTexture | null} */ (null);
|
|
223
|
+
let photo = /** @type {WebGLTexture | null} */ (null);
|
|
224
|
+
let photoAspect = 1;
|
|
225
|
+
let boxAspect = 1;
|
|
226
|
+
let alive = true;
|
|
227
|
+
|
|
228
|
+
const { stage, stop } = createStage(el, {
|
|
229
|
+
fragment: FRAG,
|
|
230
|
+
alpha: false,
|
|
231
|
+
setup({ gl, uniform }) {
|
|
232
|
+
sdf = gl.createTexture();
|
|
233
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
234
|
+
gl.bindTexture(gl.TEXTURE_2D, sdf);
|
|
235
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
236
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
237
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
238
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
239
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, 1, 1, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, new Uint8Array([0]));
|
|
240
|
+
gl.uniform1i(uniform('uSdf'), 0);
|
|
241
|
+
gl.uniform1i(uniform('uPhoto'), 2);
|
|
242
|
+
gl.uniform1f(uniform('uHasPhoto'), 0);
|
|
243
|
+
gl.uniform1f(uniform('uPlates'), plates);
|
|
244
|
+
gl.uniform3fv(uniform('uTint[0]'), tintArray);
|
|
245
|
+
gl.uniform3fv(uniform('uEdgeTint'), parseColor(c.edge));
|
|
246
|
+
gl.uniform3fv(uniform('uGround'), parseColor(c.background));
|
|
247
|
+
gl.uniform3fv(uniform('uLight'), parseColor(c.light));
|
|
248
|
+
gl.uniform1f(uniform('uDensity'), c.density);
|
|
249
|
+
gl.uniform1f(uniform('uDisperse'), Math.max(0, Math.min(1, c.dispersion)) * 0.25);
|
|
250
|
+
gl.uniform1f(uniform('uStreak'), c.streak);
|
|
251
|
+
gl.uniform1f(uniform('uShadow'), c.shadow);
|
|
252
|
+
gl.uniform1f(uniform('uDim'), c.dim);
|
|
253
|
+
gl.uniform1f(uniform('uSoft'), Math.max(0, Math.min(5, c.soften)));
|
|
254
|
+
gl.uniform2fv(uniform('uLightDir'), lightVector(c.lightAngle));
|
|
255
|
+
return () => {
|
|
256
|
+
gl.deleteTexture(sdf);
|
|
257
|
+
gl.deleteTexture(photo);
|
|
258
|
+
};
|
|
259
|
+
},
|
|
260
|
+
resize({ gl, uniform, canvas }) {
|
|
261
|
+
const aspect = canvas.width / canvas.height;
|
|
262
|
+
boxAspect = aspect;
|
|
263
|
+
const area = c.area ?? { x: 0, y: 0, width: 1, height: 1 };
|
|
264
|
+
box = Math.min(area.width * aspect, area.height) * c.logoScale;
|
|
265
|
+
const centre = { x: (area.x + area.width / 2) * aspect, y: 1 - (area.y + area.height / 2) };
|
|
266
|
+
gl.uniform2f(uniform('uCentre'), centre.x, centre.y);
|
|
267
|
+
gl.uniform1f(uniform('uBox'), box);
|
|
268
|
+
gl.uniform1f(uniform('uAspect'), aspect);
|
|
269
|
+
gl.uniform2fv(uniform('uCover'), coverScale(photoAspect, aspect));
|
|
270
|
+
gl.uniform1f(uniform('uBevel'), c.bevel * box);
|
|
271
|
+
gl.uniform1f(uniform('uBend'), c.bend);
|
|
272
|
+
// the edge shows down and across from each face, as glass seen from a little above and to the left
|
|
273
|
+
gl.uniform2f(uniform('uSide'), -c.thickness * box * 0.55, c.thickness * box);
|
|
274
|
+
const mine = ++generation;
|
|
275
|
+
drawShape({ text: c.text, font: c.font, src: c.src, padding: 0 }, FIELD, FIELD)
|
|
276
|
+
.then((mask) => {
|
|
277
|
+
if (mine !== generation || !stage) return;
|
|
278
|
+
const spread = Math.round(FIELD / 9);
|
|
279
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
280
|
+
gl.bindTexture(gl.TEXTURE_2D, sdf);
|
|
281
|
+
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
|
|
282
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, FIELD, FIELD, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, blurField(distanceField(mask, spread), FIELD, FIELD, FIELD / 200));
|
|
283
|
+
gl.useProgram(stage.program);
|
|
284
|
+
gl.uniform1f(uniform('uSpread'), spread / FIELD);
|
|
285
|
+
stage.redraw();
|
|
286
|
+
})
|
|
287
|
+
.catch((error) => console.warn('[effects-astro]', error));
|
|
288
|
+
},
|
|
289
|
+
uniforms({ gl, uniform, still }, now) {
|
|
290
|
+
const seconds = (now - start) / 1000;
|
|
291
|
+
const t = seconds - c.delay;
|
|
292
|
+
// the plates slide apart from one, then drift slowly so the refraction shifts
|
|
293
|
+
const apart = still || !c.intro ? 1 : ease(t / c.introDuration);
|
|
294
|
+
const sway = still ? 0 : Math.sin(seconds * 0.31) * 0.35 + Math.sin(seconds * 0.17 + 1.2) * 0.2;
|
|
295
|
+
const k = c.spread * box * apart * (1 + sway * 0.25 * c.drift);
|
|
296
|
+
const turn = still ? 0 : Math.sin(seconds * 0.23) * 0.18 * c.drift;
|
|
297
|
+
// each plate down and across from the one behind (y is up in the shader)
|
|
298
|
+
gl.uniform2f(uniform('uStep'), k * Math.cos(-0.785 + turn), k * Math.sin(-0.785 + turn));
|
|
299
|
+
gl.uniform2fv(uniform('uLightDir'), lightVector(c.lightAngle + (still ? 0 : Math.sin(seconds * 0.2) * 14 * c.drift)));
|
|
300
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
301
|
+
gl.bindTexture(gl.TEXTURE_2D, sdf);
|
|
302
|
+
if (photo) {
|
|
303
|
+
gl.activeTexture(gl.TEXTURE2);
|
|
304
|
+
gl.bindTexture(gl.TEXTURE_2D, photo);
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
if (stage && c.backdrop) {
|
|
309
|
+
loadImage(c.backdrop)
|
|
310
|
+
.then((image) => {
|
|
311
|
+
if (!alive) return;
|
|
312
|
+
const { gl, uniform, program } = stage;
|
|
313
|
+
gl.activeTexture(gl.TEXTURE2);
|
|
314
|
+
photo = imageTexture(gl, image, { mipmap: true });
|
|
315
|
+
photoAspect = image.naturalWidth / image.naturalHeight;
|
|
316
|
+
gl.useProgram(program);
|
|
317
|
+
gl.uniform1f(uniform('uHasPhoto'), 1);
|
|
318
|
+
gl.uniform2fv(uniform('uCover'), coverScale(photoAspect, boxAspect));
|
|
319
|
+
stage.redraw();
|
|
320
|
+
})
|
|
321
|
+
.catch((error) => console.warn('[effects-astro]', error));
|
|
322
|
+
}
|
|
323
|
+
return () => {
|
|
324
|
+
alive = false;
|
|
325
|
+
stop();
|
|
326
|
+
};
|
|
327
|
+
}
|
|
@@ -53,6 +53,16 @@ export interface GlassStackConfig {
|
|
|
53
53
|
delay?: number;
|
|
54
54
|
/** How fast the intro plays, 0.25–3. Default 1. */
|
|
55
55
|
speed?: number;
|
|
56
|
+
/** `plates`: the logo cut from sheets of thick glass laid one over another (see glass-plates); the other settings are then GlassPlatesConfig's. Default the disc stack. */
|
|
57
|
+
form?: 'discs' | 'plates';
|
|
58
|
+
/** A photo behind the stack (same origin), covering the element; each disc then refracts it as a thick lens. */
|
|
59
|
+
backdrop?: string;
|
|
60
|
+
/** How soft the backdrop is round the glass (a mip bias, 0–5); through the glass it stays sharp, as a lens brings it into focus. Default 0. */
|
|
61
|
+
soften?: number;
|
|
62
|
+
/** How bright the backdrop shows, 0–1. Default 0.78. */
|
|
63
|
+
dim?: number;
|
|
64
|
+
/** With a backdrop: how far the colours part where the discs bend it, 0–1. Default 0.35. */
|
|
65
|
+
dispersion?: number;
|
|
56
66
|
}
|
|
57
67
|
|
|
58
68
|
/** The tint of each slice: the colours as a gradient from the top slice to the bottom one, blended in Oklab (RGB 0–1). */
|
|
@@ -16,6 +16,10 @@
|
|
|
16
16
|
* runs read from a small texture (./slab-runs.js), nearer discs drawn over
|
|
17
17
|
* farther ones.
|
|
18
18
|
*
|
|
19
|
+
* Over a `backdrop` photo each disc is a thick lens: the photo shows
|
|
20
|
+
* through it magnified toward its middle and bent hard at its rim, the
|
|
21
|
+
* colours parting there, tinted by the glass, deeper where it is thicker.
|
|
22
|
+
*
|
|
19
23
|
* Opaque: it paints its `background` everywhere. Without WebGL the canvas
|
|
20
24
|
* goes and the element's background shows. With reduced motion it is one
|
|
21
25
|
* still frame, every disc grown.
|
|
@@ -28,6 +32,8 @@ import { canHover } from './device.js';
|
|
|
28
32
|
import { pointerIn } from './pointer.js';
|
|
29
33
|
import { GLSL_STUDIO, lightVector } from './glass-optics.js';
|
|
30
34
|
import { sliceRuns, runsTexture, slabGrowth } from './slab-runs.js';
|
|
35
|
+
import { loadImage, imageTexture, coverScale } from './image.js';
|
|
36
|
+
import { glassPlates } from './glass-plates.js';
|
|
31
37
|
|
|
32
38
|
/**
|
|
33
39
|
* @typedef {import('./glass-stack.js').GlassStackConfig} GlassStackConfig
|
|
@@ -54,10 +60,24 @@ uniform float uDensity;
|
|
|
54
60
|
uniform float uReflect;
|
|
55
61
|
uniform float uOpacity;
|
|
56
62
|
uniform vec2 uLightDir;
|
|
63
|
+
uniform sampler2D uPhoto;
|
|
64
|
+
uniform float uHasPhoto;
|
|
65
|
+
uniform float uAspect;
|
|
66
|
+
uniform vec2 uCover;
|
|
67
|
+
uniform float uDim;
|
|
68
|
+
uniform float uSoft;
|
|
69
|
+
// the mip bias the backdrop is read at: soft round the glass, sharp through it (a lens brings what is behind into focus)
|
|
70
|
+
float gBias = 0.0;
|
|
71
|
+
uniform float uDisperse;
|
|
57
72
|
${GLSL_STUDIO}
|
|
58
73
|
|
|
59
74
|
// The lit card behind the stack, and the ground round it.
|
|
60
75
|
vec3 backdrop(vec2 p) {
|
|
76
|
+
if (uHasPhoto > 0.5) {
|
|
77
|
+
// a photo behind the stack, covering the element, dimmed so the glass reads over it
|
|
78
|
+
vec2 uv = (vec2(p.x / uAspect, 1.0 - p.y) - 0.5) * uCover + 0.5;
|
|
79
|
+
return texture2D(uPhoto, clamp(uv, 0.001, 0.999), gBias).rgb * uDim;
|
|
80
|
+
}
|
|
61
81
|
vec2 hs = vec2(uEdge * 0.5 * uCardSize);
|
|
62
82
|
vec2 q = abs(p - uCentre) - hs + uEdge * 0.06;
|
|
63
83
|
float d = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - uEdge * 0.06;
|
|
@@ -81,7 +101,9 @@ vec2 runAt(float x, float slice) {
|
|
|
81
101
|
void main() {
|
|
82
102
|
vec2 p = gl_FragCoord.xy / uRes.y;
|
|
83
103
|
float px = 1.0 / uRes.y;
|
|
104
|
+
gBias = uSoft * uHasPhoto;
|
|
84
105
|
vec3 col = backdrop(p);
|
|
106
|
+
gBias = 0.0;
|
|
85
107
|
float pitch = uEdge / uSlices;
|
|
86
108
|
float top = uCentre.y + uEdge * 0.5;
|
|
87
109
|
float here = floor((top - p.y) / pitch);
|
|
@@ -112,9 +134,14 @@ void main() {
|
|
|
112
134
|
float r = sqrt(q * q + (dy * dy) / (vr * vr));
|
|
113
135
|
float roll = pow(clamp(r, 0.0, 1.0), 4.0);
|
|
114
136
|
vec3 n = normalize(vec3(vec2(q, dy / vr) * roll * 1.6, 1.0));
|
|
115
|
-
|
|
137
|
+
// over a photo the disc is a thick lens: it magnifies what is behind toward its middle and bends it hard at
|
|
138
|
+
// the rim, the colours parting as far as it bends
|
|
139
|
+
vec2 bend = n.xy * (uIor - 1.0) * pitch * mix(1.4, 3.2, uHasPhoto);
|
|
140
|
+
vec2 lensed = p + (vec2(q * pitch * 2.2, dy) * -0.18) * uHasPhoto;
|
|
116
141
|
float path = 1.0 + 1.1 * roll;
|
|
117
|
-
vec3
|
|
142
|
+
vec3 split = vec3(1.0 + uDisperse, 1.0, 1.0 - uDisperse);
|
|
143
|
+
vec3 behind = vec3(backdrop(lensed - bend * split.x).r, backdrop(lensed - bend).g, backdrop(lensed - bend * split.z).b);
|
|
144
|
+
vec3 through = behind * pow(c, vec3(uDensity * path)) * (1.0 + 0.25 * uHasPhoto);
|
|
118
145
|
float fres = schlick(n.z, 0.04);
|
|
119
146
|
vec3 mirrored = studio(reflect(view, n), uLightDir, uLight);
|
|
120
147
|
vec3 face = through * (1.0 - fres * 0.5) + mirrored * (fres * 1.4 + 0.05) * (0.4 + uReflect);
|
|
@@ -165,6 +192,9 @@ const DEFAULTS = Object.freeze({
|
|
|
165
192
|
introDuration: 1.6,
|
|
166
193
|
delay: 0,
|
|
167
194
|
speed: 1,
|
|
195
|
+
dim: 0.78,
|
|
196
|
+
dispersion: 0.35,
|
|
197
|
+
soften: 0,
|
|
168
198
|
});
|
|
169
199
|
|
|
170
200
|
/** The finest the logo is sliced, in columns. */
|
|
@@ -212,6 +242,8 @@ export function stackLayout(config, aspect) {
|
|
|
212
242
|
* @returns {() => void} Stops it.
|
|
213
243
|
*/
|
|
214
244
|
export function glassStack(el, config) {
|
|
245
|
+
// `form: 'plates'`: the logo cut from sheets of thick glass laid one over another (./glass-plates.js)
|
|
246
|
+
if (config.form === 'plates') return glassPlates(el, config);
|
|
215
247
|
const c = { ...DEFAULTS, ...config };
|
|
216
248
|
const slices = Math.max(4, Math.min(64, Math.round(c.slabs)));
|
|
217
249
|
const colors = c.colors?.length >= 2 ? c.colors : c.colors?.length === 1 ? [c.colors[0], c.colors[0]] : DEFAULTS.colors;
|
|
@@ -225,6 +257,11 @@ export function glassStack(el, config) {
|
|
|
225
257
|
let tintTex = /** @type {WebGLTexture | null} */ (null);
|
|
226
258
|
/** The last growth written, so the tints are uploaded only while the discs grow. */
|
|
227
259
|
let grown = -1;
|
|
260
|
+
/** The backdrop photo's texture, its aspect, and the element's, once known. */
|
|
261
|
+
let photo = /** @type {WebGLTexture | null} */ (null);
|
|
262
|
+
let photoAspect = 1;
|
|
263
|
+
let boxAspect = 1;
|
|
264
|
+
let alive = true;
|
|
228
265
|
|
|
229
266
|
/**
|
|
230
267
|
* A nearest-filtered, edge-clamped texture on a unit.
|
|
@@ -268,9 +305,18 @@ export function glassStack(el, config) {
|
|
|
268
305
|
gl.uniform1f(uniform('uReflect'), c.reflectivity);
|
|
269
306
|
gl.uniform1f(uniform('uOpacity'), Math.max(0, Math.min(1, c.opacity)));
|
|
270
307
|
gl.uniform2fv(uniform('uLightDir'), lightVector(c.lightAngle));
|
|
308
|
+
gl.uniform1i(uniform('uPhoto'), 2);
|
|
309
|
+
gl.uniform1f(uniform('uHasPhoto'), 0);
|
|
310
|
+
gl.uniform1f(uniform('uDim'), c.dim);
|
|
311
|
+
gl.uniform1f(uniform('uSoft'), Math.max(0, Math.min(5, c.soften)));
|
|
312
|
+
gl.uniform1f(uniform('uDisperse'), Math.max(0, Math.min(1, c.dispersion)) * 0.25);
|
|
313
|
+
return () => gl.deleteTexture(photo);
|
|
271
314
|
},
|
|
272
315
|
resize({ gl, uniform, canvas }) {
|
|
273
316
|
const aspect = canvas.width / canvas.height;
|
|
317
|
+
boxAspect = aspect;
|
|
318
|
+
gl.uniform1f(uniform('uAspect'), aspect);
|
|
319
|
+
gl.uniform2fv(uniform('uCover'), coverScale(photoAspect, aspect));
|
|
274
320
|
const layout = stackLayout(c, aspect);
|
|
275
321
|
gl.uniform2f(uniform('uCentre'), layout.centre.x * aspect, 1 - layout.centre.y);
|
|
276
322
|
gl.uniform1f(uniform('uEdge'), layout.edge);
|
|
@@ -309,7 +355,29 @@ export function glassStack(el, config) {
|
|
|
309
355
|
tilt.x += (goal.x - tilt.x) * ease;
|
|
310
356
|
tilt.y += (goal.y - tilt.y) * ease;
|
|
311
357
|
gl.uniform2f(uniform('uTilt'), tilt.x, tilt.y);
|
|
358
|
+
if (photo) {
|
|
359
|
+
gl.activeTexture(gl.TEXTURE2);
|
|
360
|
+
gl.bindTexture(gl.TEXTURE_2D, photo);
|
|
361
|
+
}
|
|
312
362
|
},
|
|
313
363
|
});
|
|
314
|
-
|
|
364
|
+
if (stage && c.backdrop) {
|
|
365
|
+
loadImage(c.backdrop)
|
|
366
|
+
.then((image) => {
|
|
367
|
+
if (!alive) return;
|
|
368
|
+
const { gl, uniform, program } = stage;
|
|
369
|
+
gl.activeTexture(gl.TEXTURE2);
|
|
370
|
+
photo = imageTexture(gl, image, { mipmap: true });
|
|
371
|
+
photoAspect = image.naturalWidth / image.naturalHeight;
|
|
372
|
+
gl.useProgram(program);
|
|
373
|
+
gl.uniform1f(uniform('uHasPhoto'), 1);
|
|
374
|
+
gl.uniform2fv(uniform('uCover'), coverScale(photoAspect, boxAspect));
|
|
375
|
+
stage.redraw();
|
|
376
|
+
})
|
|
377
|
+
.catch((error) => console.warn('[effects-astro]', error));
|
|
378
|
+
}
|
|
379
|
+
return () => {
|
|
380
|
+
alive = false;
|
|
381
|
+
stop();
|
|
382
|
+
};
|
|
315
383
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** Where the light is, and the quiet area, as the other fields take them. */
|
|
2
|
+
type Quiet = { x?: number; y?: number; radius?: number; depth?: number } | null;
|
|
3
|
+
|
|
4
|
+
export interface LensFlareConfig {
|
|
5
|
+
/** The ground, painted everywhere. Default '#030406'. */
|
|
6
|
+
background?: string;
|
|
7
|
+
/** The streak's colour (the lens coating's): blue for the classic, amber or teal for others. Default '#4f8dff'. */
|
|
8
|
+
streak?: string;
|
|
9
|
+
/** The ghosts' main tint; they also take the streak's and a green one. Default '#ffb36b'. */
|
|
10
|
+
ghost?: string;
|
|
11
|
+
/** The ground's glow round the source. Default the streak's, deep. */
|
|
12
|
+
haze?: string;
|
|
13
|
+
/** Where the light is, 0–1 from the top left. Default [0.24, 0.32]. */
|
|
14
|
+
source?: [number, number];
|
|
15
|
+
/** How long the streak reaches each way, in widths. Default 1.6. */
|
|
16
|
+
length?: number;
|
|
17
|
+
/** The streak's half-height, a share of the height. Default 0.006. */
|
|
18
|
+
width?: number;
|
|
19
|
+
/** How bright it all is. Default 1. */
|
|
20
|
+
power?: number;
|
|
21
|
+
/** How many ghosts, 0–6. Default 5. */
|
|
22
|
+
ghosts?: number;
|
|
23
|
+
/** How far the source wanders, a share of the height. Default 0.06. */
|
|
24
|
+
drift?: number;
|
|
25
|
+
/** How fast it wanders, cycles a second. Default 0.05. */
|
|
26
|
+
rate?: number;
|
|
27
|
+
/** How much the source flickers. Default 0.04. */
|
|
28
|
+
flicker?: number;
|
|
29
|
+
/** Fine grain; 0 for none. Default 0.035. */
|
|
30
|
+
grain?: number;
|
|
31
|
+
/** Where its wandering starts. Default 0. */
|
|
32
|
+
seed?: number;
|
|
33
|
+
/** Plays every clock faster or slower; 0 for one still frame. Default 1. */
|
|
34
|
+
speed?: number;
|
|
35
|
+
/** An area dimmed: centre (0–1), radius (share of the height), depth (0–1). */
|
|
36
|
+
quiet?: Quiet;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export declare const FLARE_DEFAULTS: Required<Omit<LensFlareConfig, 'haze' | 'speed' | 'quiet'>>;
|
|
40
|
+
export declare const MAX_GHOSTS: number;
|
|
41
|
+
export declare function flareSource(config: LensFlareConfig, aspect: number, time?: number): [number, number];
|
|
42
|
+
export declare function flareGhosts(config: LensFlareConfig, aspect: number, time?: number): { x: number; y: number; radius: number }[];
|
|
43
|
+
export declare function streakAt(config: LensFlareConfig, x: number, y: number, aspect: number, time?: number): number;
|
|
44
|
+
export declare function flareStill(config: LensFlareConfig): string;
|
|
45
|
+
export declare function lensFlare(el: HTMLElement, config: LensFlareConfig): () => void;
|