@jsb188/gfx 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +522 -0
- package/elements/Shapes.helpers.ts +204 -0
- package/elements/Shapes.hooks.ts +171 -0
- package/elements/Shapes.tsx +505 -0
- package/elements/Shapes.types.ts +81 -0
- package/elements/Stack.helpers.ts +42 -0
- package/elements/Stack.tsx +143 -0
- package/elements/Text.helpers.ts +45 -0
- package/elements/Text.hooks.ts +197 -0
- package/elements/Text.tsx +98 -0
- package/libs/chain.ts +174 -0
- package/libs/dotGrid.helpers.ts +53 -0
- package/libs/dotGrid.ts +954 -0
- package/libs/effects/dotEffects.ts +136 -0
- package/libs/effects/effectQuad.ts +146 -0
- package/libs/effects/effectRegion.ts +299 -0
- package/libs/effects/imageEffects.ts +599 -0
- package/libs/effects/index.ts +8 -0
- package/libs/effects/ripple.ts +160 -0
- package/libs/glsl.ts +134 -0
- package/libs/passes.ts +374 -0
- package/libs/pointer.ts +151 -0
- package/libs/runtime.ts +329 -0
- package/libs/texture.ts +160 -0
- package/libs/types.ts +94 -0
- package/libs/uniforms.ts +32 -0
- package/main.ts +4 -0
- package/modules/Carousel.helpers.ts +316 -0
- package/modules/Carousel.hooks.ts +520 -0
- package/modules/Carousel.tsx +631 -0
- package/modules/Dots.tsx +269 -0
- package/modules/DotsBoard.helpers.ts +388 -0
- package/modules/DotsBoard.hooks.ts +144 -0
- package/modules/DotsBoard.strings.ts +649 -0
- package/modules/DotsBoard.types.ts +240 -0
- package/modules/DotsBoardCanvas.hooks.ts +49 -0
- package/modules/DotsBoardCanvas.tsx +190 -0
- package/modules/DotsBoardElements.tsx +330 -0
- package/modules/Gallery.helpers.ts +32 -0
- package/modules/Gallery.hooks.ts +149 -0
- package/modules/Gallery.tsx +194 -0
- package/modules/GfxCanvas.hooks.ts +12 -0
- package/modules/GfxCanvas.tsx +189 -0
- package/modules/Hero.tsx +90 -0
- package/modules/KineticHeader.helpers.ts +44 -0
- package/modules/KineticHeader.hooks.ts +206 -0
- package/modules/KineticHeader.tsx +305 -0
- package/modules/KineticHeader.types.ts +56 -0
- package/modules/ScrollSteps.hooks.ts +138 -0
- package/modules/ScrollSteps.tsx +98 -0
- package/package.json +52 -0
- package/scenes/Carousel.ts +277 -0
- package/scenes/DotsBoard.helpers.ts +12 -0
- package/scenes/DotsBoard.ts +245 -0
- package/scenes/Hero.ts +280 -0
- package/tsconfig.json +13 -0
- package/utils/align.ts +18 -0
- package/utils/color.ts +112 -0
- package/utils/dotEffectSpecs.ts +79 -0
- package/utils/dotGridSpecs.ts +469 -0
- package/utils/easing.ts +18 -0
- package/utils/imageEffectSpecs.ts +89 -0
- package/utils/motion.ts +88 -0
- package/utils/random.ts +22 -0
- package/utils/strings.ts +116 -0
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
import type { OGLRenderingContext, Texture } from 'ogl';
|
|
2
|
+
import { PassChain, createPassProgram } from '../chain.ts';
|
|
3
|
+
import { hexToRgb } from '../../utils/color.ts';
|
|
4
|
+
import { GLSL_COVER_UV, GLSL_HASH2, GLSL_LUMA, GLSL_ROTATE_2D, GLSL_VALUE_NOISE, GLSL_VORONOI, fragmentShader } from '../glsl.ts';
|
|
5
|
+
import { createBokehPass } from '../passes.ts';
|
|
6
|
+
import type { ImageEffectName, ImageEffectOptions, ImageEffectSpec } from '../../utils/imageEffectSpecs.ts';
|
|
7
|
+
import { RippleSimulation } from './ripple.ts';
|
|
8
|
+
import type { SceneFrame, SceneSize } from '../types.ts';
|
|
9
|
+
import type { UniformValues } from '../uniforms.ts';
|
|
10
|
+
|
|
11
|
+
export type {
|
|
12
|
+
ImageEffectName,
|
|
13
|
+
ImageEffectOptions,
|
|
14
|
+
ImageEffectSpec,
|
|
15
|
+
} from '../../utils/imageEffectSpecs.ts';
|
|
16
|
+
export { normalizeImageEffectSpecs } from '../../utils/imageEffectSpecs.ts';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Image effects: small GLSL snippets that an image pass (see `createImageLightCompositePass`)
|
|
20
|
+
* splices into its own shader, so any number of them cost one full-resolution pass. Each
|
|
21
|
+
* effect scales itself by `amount` (the eased hover value) and is an exact no-op at 0.
|
|
22
|
+
*
|
|
23
|
+
* Snippets run inside the host shader's `main()` with these names in scope:
|
|
24
|
+
* vec2 uv — screen UV being sampled (y up); UV snippets modify it in place
|
|
25
|
+
* vec3 image — sampled color; color snippets modify it in place
|
|
26
|
+
* vec2 pointer — eased pointer in screen UV
|
|
27
|
+
* float amount — eased hover amount, 0..1
|
|
28
|
+
* float time — active seconds
|
|
29
|
+
* vec2 aspect — vec2(width / height, 1.0), multiply UVs by it for circular distances
|
|
30
|
+
* vec2 texel — one drawing-buffer pixel in screen UV
|
|
31
|
+
* vec3 sampleImage(vec2 screenUv) — cover-fit image lookup
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
export interface ImageEffectInstance {
|
|
35
|
+
/** GLSL function chunks the snippets depend on; duplicates across effects are merged. */
|
|
36
|
+
chunks: string[];
|
|
37
|
+
/** Uniform declarations for this instance (already uniquely prefixed). */
|
|
38
|
+
declarations: string;
|
|
39
|
+
/** Statements that modify `uv` before the image is sampled. */
|
|
40
|
+
uvCode?: string;
|
|
41
|
+
/** Statements that assign `image` instead of the default single sample; at most one effect may provide this. */
|
|
42
|
+
sampleCode?: string;
|
|
43
|
+
/** Statements that modify `image` after sampling. */
|
|
44
|
+
colorCode?: string;
|
|
45
|
+
/** Uniform values, shared by reference with the host program so `update` can drive them. */
|
|
46
|
+
uniforms: UniformValues;
|
|
47
|
+
/** Called once per frame before the host renders, with the photo the host is presenting. */
|
|
48
|
+
update?: (frame: SceneFrame, image: ImageEffectImage) => void;
|
|
49
|
+
resize?: (size: SceneSize) => void;
|
|
50
|
+
dispose?: () => void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The host's current photo, for effects that pre-process it. */
|
|
54
|
+
export interface ImageEffectImage {
|
|
55
|
+
texture: Texture;
|
|
56
|
+
size: [number, number];
|
|
57
|
+
loaded: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type ImageEffectFactory<K extends ImageEffectName> = (
|
|
61
|
+
gl: OGLRenderingContext,
|
|
62
|
+
options: ImageEffectOptions[K],
|
|
63
|
+
prefix: string,
|
|
64
|
+
) => ImageEffectInstance;
|
|
65
|
+
|
|
66
|
+
type UniformType = 'float' | 'vec2' | 'vec3' | 'sampler2D';
|
|
67
|
+
|
|
68
|
+
/** A uniform to declare for one effect instance: its unprefixed name, GLSL type, and initial value. */
|
|
69
|
+
type UniformEntry = [name: string, type: UniformType, value: unknown];
|
|
70
|
+
|
|
71
|
+
/** Resolves an unprefixed uniform name to the instance's prefixed GLSL identifier. */
|
|
72
|
+
type UniformName = (name: string) => string;
|
|
73
|
+
|
|
74
|
+
/*
|
|
75
|
+
* Declare prefixed uniforms and return a lookup for their GLSL names.
|
|
76
|
+
*/
|
|
77
|
+
function defineUniforms(prefix: string, entries: UniformEntry[]) {
|
|
78
|
+
const uniforms: UniformValues = {};
|
|
79
|
+
const declarations = entries
|
|
80
|
+
.map(([name, type, value]) => {
|
|
81
|
+
uniforms[prefix + name] = { value };
|
|
82
|
+
return `uniform ${type} ${prefix + name};`;
|
|
83
|
+
})
|
|
84
|
+
.join('\n');
|
|
85
|
+
return { uniforms, declarations, u: (name: string) => prefix + name };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/*
|
|
89
|
+
* Assemble a stateless effect: declare its uniforms, then render each snippet template with
|
|
90
|
+
* the prefixed uniform names. Effects with GPU state of their own build on `defineUniforms`.
|
|
91
|
+
*/
|
|
92
|
+
function defineEffect(
|
|
93
|
+
prefix: string,
|
|
94
|
+
chunks: string[],
|
|
95
|
+
entries: UniformEntry[],
|
|
96
|
+
code: {
|
|
97
|
+
uv?: (u: UniformName) => string;
|
|
98
|
+
sample?: (u: UniformName) => string;
|
|
99
|
+
color?: (u: UniformName) => string;
|
|
100
|
+
},
|
|
101
|
+
): ImageEffectInstance {
|
|
102
|
+
const { uniforms, declarations, u } = defineUniforms(prefix, entries);
|
|
103
|
+
return {
|
|
104
|
+
chunks,
|
|
105
|
+
declarations,
|
|
106
|
+
uniforms,
|
|
107
|
+
uvCode: code.uv?.(u),
|
|
108
|
+
sampleCode: code.sample?.(u),
|
|
109
|
+
colorCode: code.color?.(u),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/*
|
|
114
|
+
* Build a color effect that blends the photo toward a transformed copy by `amount`.
|
|
115
|
+
*/
|
|
116
|
+
function colorMixEffect(prefix: string, chunks: string[], entries: UniformEntry[], expression: (u: UniformName) => string) {
|
|
117
|
+
return defineEffect(prefix, chunks, entries, { color: (u) => `image = mix(image, ${expression(u)}, amount);` });
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/*
|
|
121
|
+
* Build a noise-displacement effect from a preset; heat haze and liquid share the shader.
|
|
122
|
+
*/
|
|
123
|
+
function noiseDisplaceEffect(
|
|
124
|
+
prefix: string,
|
|
125
|
+
options: ImageEffectOptions['heatHaze'],
|
|
126
|
+
preset: { frequency: number; amplitude: number; speed: number; drift: [number, number] },
|
|
127
|
+
) {
|
|
128
|
+
return defineEffect(
|
|
129
|
+
prefix,
|
|
130
|
+
[GLSL_VALUE_NOISE, FX_NOISE_DISPLACE],
|
|
131
|
+
[
|
|
132
|
+
['Frequency', 'float', options.frequency ?? preset.frequency],
|
|
133
|
+
['Amplitude', 'float', options.amplitude ?? preset.amplitude],
|
|
134
|
+
['Speed', 'float', options.speed ?? preset.speed],
|
|
135
|
+
['Drift', 'vec2', preset.drift],
|
|
136
|
+
],
|
|
137
|
+
{
|
|
138
|
+
uv: (u) =>
|
|
139
|
+
`uv = fxNoiseDisplace(uv, aspect, ${u('Frequency')}, ${u('Drift')}, ${u('Amplitude')} * amount, time * ${u('Speed')});`,
|
|
140
|
+
},
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/*
|
|
145
|
+
* Build a barrel-family effect; the three names differ only in their default strength.
|
|
146
|
+
*/
|
|
147
|
+
function barrelEffect(prefix: string, strength: number) {
|
|
148
|
+
return defineEffect(prefix, [FX_BARREL], [['Strength', 'float', strength]], {
|
|
149
|
+
uv: (u) => `uv = fxBarrel(uv, aspect, ${u('Strength')} * amount);`,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* GLSL functions. They take everything through parameters so one copy serves every instance.
|
|
155
|
+
*/
|
|
156
|
+
|
|
157
|
+
const FX_WAVES = `
|
|
158
|
+
vec2 fxWaves(vec2 uv, float frequency, float amplitude, float phase) {
|
|
159
|
+
uv.x += sin(uv.y * frequency + phase) * amplitude;
|
|
160
|
+
uv.y += sin(uv.x * frequency * 0.8 + phase * 1.3) * amplitude;
|
|
161
|
+
return uv;
|
|
162
|
+
}
|
|
163
|
+
`;
|
|
164
|
+
|
|
165
|
+
const FX_NOISE_DISPLACE = `
|
|
166
|
+
vec2 fxNoiseDisplace(vec2 uv, vec2 aspect, float frequency, vec2 drift, float amplitude, float phase) {
|
|
167
|
+
vec2 n = valueNoise2(uv * aspect * frequency + drift * phase) - 0.5;
|
|
168
|
+
return uv + n * amplitude;
|
|
169
|
+
}
|
|
170
|
+
`;
|
|
171
|
+
|
|
172
|
+
const FX_LENS = `
|
|
173
|
+
vec2 fxLens(vec2 uv, vec2 pointer, vec2 aspect, float radius, float zoom, float amount) {
|
|
174
|
+
vec2 d = (uv - pointer) * aspect;
|
|
175
|
+
float inside = 1.0 - smoothstep(0.7, 1.0, length(d) / radius);
|
|
176
|
+
return pointer + d * mix(1.0, 1.0 / zoom, amount * inside) / aspect;
|
|
177
|
+
}
|
|
178
|
+
`;
|
|
179
|
+
|
|
180
|
+
const FX_GLASS = `
|
|
181
|
+
vec2 fxGlass(vec2 uv, vec2 pointer, vec2 aspect, float radius, float strength) {
|
|
182
|
+
vec2 d = (uv - pointer) * aspect;
|
|
183
|
+
float len = length(d);
|
|
184
|
+
float r = len / radius;
|
|
185
|
+
if (r >= 1.0) return uv;
|
|
186
|
+
float rim = smoothstep(0.0, 1.0, r) * (1.0 - r * r);
|
|
187
|
+
return uv - d / max(len, 1e-5) * rim * strength / aspect;
|
|
188
|
+
}
|
|
189
|
+
`;
|
|
190
|
+
|
|
191
|
+
const FX_BARREL = `
|
|
192
|
+
vec2 fxBarrel(vec2 uv, vec2 aspect, float k) {
|
|
193
|
+
vec2 p = (uv - 0.5) * 2.0 * aspect;
|
|
194
|
+
float r2 = dot(p, p);
|
|
195
|
+
// Pincushion (k < 0) is normalised so the corners stay put instead of sampling outside.
|
|
196
|
+
float norm = k < 0.0 ? 1.0 - k * dot(aspect, aspect) : 1.0;
|
|
197
|
+
p *= (1.0 - k * r2) / norm;
|
|
198
|
+
return p / (2.0 * aspect) + 0.5;
|
|
199
|
+
}
|
|
200
|
+
`;
|
|
201
|
+
|
|
202
|
+
const FX_PARALLAX = `
|
|
203
|
+
vec2 fxParallax(vec2 uv, vec2 pointer, float shift) {
|
|
204
|
+
float zoom = 1.0 + 2.0 * shift;
|
|
205
|
+
return (uv - 0.5) / zoom + 0.5 - (pointer - 0.5) * shift;
|
|
206
|
+
}
|
|
207
|
+
`;
|
|
208
|
+
|
|
209
|
+
const FX_SWIRL = `
|
|
210
|
+
vec2 fxSwirl(vec2 uv, vec2 pointer, vec2 aspect, float radius, float angle) {
|
|
211
|
+
vec2 d = (uv - pointer) * aspect;
|
|
212
|
+
float falloff = 1.0 - smoothstep(0.0, radius, length(d));
|
|
213
|
+
return pointer + (rotate2d(angle * falloff * falloff) * d) / aspect;
|
|
214
|
+
}
|
|
215
|
+
`;
|
|
216
|
+
|
|
217
|
+
const FX_KALEIDOSCOPE = `
|
|
218
|
+
vec2 fxKaleidoscope(vec2 uv, vec2 pointer, vec2 aspect, float segments) {
|
|
219
|
+
vec2 d = (uv - pointer) * aspect;
|
|
220
|
+
float segment = TWO_PI / segments;
|
|
221
|
+
float a = mod(atan(d.y, d.x), segment);
|
|
222
|
+
a = abs(a - segment * 0.5);
|
|
223
|
+
return pointer + vec2(cos(a), sin(a)) * length(d) / aspect;
|
|
224
|
+
}
|
|
225
|
+
`;
|
|
226
|
+
|
|
227
|
+
const FX_PIXELATE = `
|
|
228
|
+
vec2 fxPixelate(vec2 uv, vec2 aspect, float cell) {
|
|
229
|
+
if (cell < 1e-4) return uv;
|
|
230
|
+
return (floor(uv * aspect / cell) + 0.5) * cell / aspect;
|
|
231
|
+
}
|
|
232
|
+
`;
|
|
233
|
+
|
|
234
|
+
const FX_SHATTER = `
|
|
235
|
+
vec2 fxShatter(vec2 uv, vec2 aspect, float scale, float spread, float angle, float skew, float phase) {
|
|
236
|
+
vec2 st = (uv - 0.5) * aspect * 50.0 * scale;
|
|
237
|
+
st = st * rotate2d(angle * TWO_PI) * mix(vec2(1.0), vec2(1.0, 0.0), skew);
|
|
238
|
+
return uv + (voronoiPoint(st, phase) - 0.5) * 0.4 * spread;
|
|
239
|
+
}
|
|
240
|
+
`;
|
|
241
|
+
|
|
242
|
+
const FX_CHROMATIC = `
|
|
243
|
+
vec3 fxChromatic(vec2 uv, vec2 pointer, float strength) {
|
|
244
|
+
vec2 dir = (uv - pointer) * strength;
|
|
245
|
+
return vec3(sampleImage(uv + dir).r, sampleImage(uv).g, sampleImage(uv - dir).b);
|
|
246
|
+
}
|
|
247
|
+
`;
|
|
248
|
+
|
|
249
|
+
const FX_ZOOM_BLUR = `
|
|
250
|
+
vec3 fxZoomBlur(vec2 uv, vec2 pointer, float strength) {
|
|
251
|
+
vec3 acc = vec3(0.0);
|
|
252
|
+
for (int i = 0; i < 12; i++) {
|
|
253
|
+
acc += sampleImage(mix(uv, pointer, float(i) / 11.0 * strength));
|
|
254
|
+
}
|
|
255
|
+
return acc / 12.0;
|
|
256
|
+
}
|
|
257
|
+
`;
|
|
258
|
+
|
|
259
|
+
const FX_EDGES = `
|
|
260
|
+
float fxEdges(vec2 uv, vec2 texel) {
|
|
261
|
+
float tl = luma(sampleImage(uv + vec2(-texel.x, texel.y)));
|
|
262
|
+
float t = luma(sampleImage(uv + vec2(0.0, texel.y)));
|
|
263
|
+
float tr = luma(sampleImage(uv + texel));
|
|
264
|
+
float l = luma(sampleImage(uv - vec2(texel.x, 0.0)));
|
|
265
|
+
float r = luma(sampleImage(uv + vec2(texel.x, 0.0)));
|
|
266
|
+
float bl = luma(sampleImage(uv - texel));
|
|
267
|
+
float b = luma(sampleImage(uv - vec2(0.0, texel.y)));
|
|
268
|
+
float br = luma(sampleImage(uv + vec2(texel.x, -texel.y)));
|
|
269
|
+
float gx = -tl - 2.0 * l - bl + tr + 2.0 * r + br;
|
|
270
|
+
float gy = -bl - 2.0 * b - br + tl + 2.0 * t + tr;
|
|
271
|
+
return length(vec2(gx, gy));
|
|
272
|
+
}
|
|
273
|
+
`;
|
|
274
|
+
|
|
275
|
+
const FX_SEPIA = `
|
|
276
|
+
vec3 fxSepia(vec3 c) {
|
|
277
|
+
return vec3(
|
|
278
|
+
dot(c, vec3(0.393, 0.769, 0.189)),
|
|
279
|
+
dot(c, vec3(0.349, 0.686, 0.168)),
|
|
280
|
+
dot(c, vec3(0.272, 0.534, 0.131))
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
`;
|
|
284
|
+
|
|
285
|
+
const FX_HUE_ROTATE = `
|
|
286
|
+
vec3 fxHueRotate(vec3 c, float angle) {
|
|
287
|
+
const mat3 toYIQ = mat3(0.299, 0.596, 0.211, 0.587, -0.274, -0.523, 0.114, -0.322, 0.312);
|
|
288
|
+
const mat3 toRGB = mat3(1.0, 1.0, 1.0, 0.956, -0.272, -1.106, 0.621, -0.647, 1.703);
|
|
289
|
+
vec3 yiq = toYIQ * c;
|
|
290
|
+
yiq.yz = rotate2d(angle) * yiq.yz;
|
|
291
|
+
return clamp(toRGB * yiq, 0.0, 1.0);
|
|
292
|
+
}
|
|
293
|
+
`;
|
|
294
|
+
|
|
295
|
+
const FX_HALFTONE = `
|
|
296
|
+
vec3 fxHalftone(vec3 c, vec2 fragCoord, float cell, float angle, vec3 ink, vec3 paper) {
|
|
297
|
+
vec2 p = rotate2d(angle) * fragCoord / cell;
|
|
298
|
+
float d = length(fract(p) - 0.5) * 2.0;
|
|
299
|
+
float dotSize = (1.0 - luma(c)) * 1.25;
|
|
300
|
+
float edge = fwidth(d);
|
|
301
|
+
return mix(ink, paper, smoothstep(dotSize - edge, dotSize + edge, d));
|
|
302
|
+
}
|
|
303
|
+
`;
|
|
304
|
+
|
|
305
|
+
// Draws the host's photo cover-fit into a chain target so it can be blurred at low resolution.
|
|
306
|
+
const COVER_BLIT_FRAGMENT = fragmentShader(
|
|
307
|
+
`
|
|
308
|
+
uniform sampler2D tImage;
|
|
309
|
+
uniform vec2 uImageSize;
|
|
310
|
+
void main() {
|
|
311
|
+
fragColor = vec4(texture(tImage, coverUv(vUv, uResolution, uImageSize)).rgb, 1.0);
|
|
312
|
+
}
|
|
313
|
+
`,
|
|
314
|
+
[GLSL_COVER_UV],
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
const FX_RIPPLE = `
|
|
318
|
+
vec2 fxRippleGradient(sampler2D field, vec2 uv, vec2 fieldSize) {
|
|
319
|
+
vec2 step = 1.0 / fieldSize;
|
|
320
|
+
float left = texture(field, uv - vec2(step.x, 0.0)).r;
|
|
321
|
+
float right = texture(field, uv + vec2(step.x, 0.0)).r;
|
|
322
|
+
float down = texture(field, uv - vec2(0.0, step.y)).r;
|
|
323
|
+
float up = texture(field, uv + vec2(0.0, step.y)).r;
|
|
324
|
+
return vec2(right - left, up - down);
|
|
325
|
+
}
|
|
326
|
+
`;
|
|
327
|
+
|
|
328
|
+
const IMAGE_EFFECT_FACTORIES: {
|
|
329
|
+
[K in ImageEffectName]: ImageEffectFactory<K>;
|
|
330
|
+
} = {
|
|
331
|
+
ripple(gl, options, prefix) {
|
|
332
|
+
const simulation = new RippleSimulation(gl, options);
|
|
333
|
+
const { uniforms, declarations, u } = defineUniforms(prefix, [
|
|
334
|
+
['Field', 'sampler2D', simulation.texture],
|
|
335
|
+
['FieldSize', 'vec2', simulation.size],
|
|
336
|
+
['Strength', 'float', options.strength ?? 0.06],
|
|
337
|
+
['Shine', 'float', options.shine ?? 0.6],
|
|
338
|
+
]);
|
|
339
|
+
return {
|
|
340
|
+
chunks: [FX_RIPPLE],
|
|
341
|
+
declarations,
|
|
342
|
+
uniforms,
|
|
343
|
+
uvCode: `
|
|
344
|
+
vec2 ${prefix}Gradient = fxRippleGradient(${u('Field')}, uv, ${u('FieldSize')}) * amount;
|
|
345
|
+
uv += ${prefix}Gradient * ${u('Strength')};`,
|
|
346
|
+
colorCode: `image += (${prefix}Gradient.x + ${prefix}Gradient.y) * ${u('Shine')};`,
|
|
347
|
+
update({ pointer, elapsed }) {
|
|
348
|
+
simulation.update(pointer, pointer.hover, elapsed);
|
|
349
|
+
// The simulation ping-pongs, so the readable texture changes identity every step.
|
|
350
|
+
uniforms[u('Field')].value = simulation.texture;
|
|
351
|
+
},
|
|
352
|
+
resize({ width, height }) {
|
|
353
|
+
simulation.setSize(width, height);
|
|
354
|
+
uniforms[u('FieldSize')].value = simulation.size;
|
|
355
|
+
},
|
|
356
|
+
dispose: () => simulation.dispose(),
|
|
357
|
+
};
|
|
358
|
+
},
|
|
359
|
+
|
|
360
|
+
waves: (_gl, options, prefix) =>
|
|
361
|
+
defineEffect(
|
|
362
|
+
prefix,
|
|
363
|
+
[FX_WAVES],
|
|
364
|
+
[
|
|
365
|
+
['Frequency', 'float', options.frequency ?? 12],
|
|
366
|
+
['Amplitude', 'float', options.amplitude ?? 0.012],
|
|
367
|
+
['Speed', 'float', options.speed ?? 2],
|
|
368
|
+
],
|
|
369
|
+
{ uv: (u) => `uv = fxWaves(uv, ${u('Frequency')}, ${u('Amplitude')} * amount, time * ${u('Speed')});` },
|
|
370
|
+
),
|
|
371
|
+
|
|
372
|
+
heatHaze: (_gl, options, prefix) =>
|
|
373
|
+
noiseDisplaceEffect(prefix, options, { frequency: 14, amplitude: 0.006, speed: 1.5, drift: [0.2, -1] }),
|
|
374
|
+
|
|
375
|
+
liquid: (_gl, options, prefix) =>
|
|
376
|
+
noiseDisplaceEffect(prefix, options, { frequency: 2.5, amplitude: 0.02, speed: 0.5, drift: [0.7, 0.7] }),
|
|
377
|
+
|
|
378
|
+
lens: (_gl, options, prefix) =>
|
|
379
|
+
defineEffect(
|
|
380
|
+
prefix,
|
|
381
|
+
[FX_LENS],
|
|
382
|
+
[
|
|
383
|
+
['Radius', 'float', options.radius ?? 0.25],
|
|
384
|
+
['Zoom', 'float', options.zoom ?? 1.6],
|
|
385
|
+
],
|
|
386
|
+
{ uv: (u) => `uv = fxLens(uv, pointer, aspect, ${u('Radius')}, ${u('Zoom')}, amount);` },
|
|
387
|
+
),
|
|
388
|
+
|
|
389
|
+
glass: (_gl, options, prefix) =>
|
|
390
|
+
defineEffect(
|
|
391
|
+
prefix,
|
|
392
|
+
[FX_GLASS],
|
|
393
|
+
[
|
|
394
|
+
['Radius', 'float', options.radius ?? 0.22],
|
|
395
|
+
['Strength', 'float', options.strength ?? 0.05],
|
|
396
|
+
],
|
|
397
|
+
{ uv: (u) => `uv = fxGlass(uv, pointer, aspect, ${u('Radius')}, ${u('Strength')} * amount);` },
|
|
398
|
+
),
|
|
399
|
+
|
|
400
|
+
fisheye: (_gl, options, prefix) => barrelEffect(prefix, options.strength ?? 0.15),
|
|
401
|
+
barrel: (_gl, options, prefix) => barrelEffect(prefix, options.strength ?? 0.1),
|
|
402
|
+
pincushion: (_gl, options, prefix) => barrelEffect(prefix, options.strength ?? -0.1),
|
|
403
|
+
|
|
404
|
+
parallax: (_gl, options, prefix) =>
|
|
405
|
+
defineEffect(prefix, [FX_PARALLAX], [['Shift', 'float', options.shift ?? 0.04]], {
|
|
406
|
+
uv: (u) => `uv = fxParallax(uv, pointer, ${u('Shift')} * amount);`,
|
|
407
|
+
}),
|
|
408
|
+
|
|
409
|
+
swirl: (_gl, options, prefix) =>
|
|
410
|
+
defineEffect(
|
|
411
|
+
prefix,
|
|
412
|
+
[GLSL_ROTATE_2D, FX_SWIRL],
|
|
413
|
+
[
|
|
414
|
+
['Radius', 'float', options.radius ?? 0.35],
|
|
415
|
+
['Angle', 'float', options.angle ?? 1.2],
|
|
416
|
+
],
|
|
417
|
+
{ uv: (u) => `uv = fxSwirl(uv, pointer, aspect, ${u('Radius')}, ${u('Angle')} * amount);` },
|
|
418
|
+
),
|
|
419
|
+
|
|
420
|
+
kaleidoscope: (_gl, options, prefix) =>
|
|
421
|
+
defineEffect(prefix, [FX_KALEIDOSCOPE], [['Segments', 'float', options.segments ?? 6]], {
|
|
422
|
+
uv: (u) => `uv = mix(uv, fxKaleidoscope(uv, pointer, aspect, ${u('Segments')}), amount);`,
|
|
423
|
+
}),
|
|
424
|
+
|
|
425
|
+
shatter: (_gl, options, prefix) =>
|
|
426
|
+
defineEffect(
|
|
427
|
+
prefix,
|
|
428
|
+
[GLSL_HASH2, GLSL_ROTATE_2D, GLSL_VORONOI, FX_SHATTER],
|
|
429
|
+
[
|
|
430
|
+
['Scale', 'float', options.scale ?? 0.5],
|
|
431
|
+
['Spread', 'float', options.spread ?? 0.05],
|
|
432
|
+
['Angle', 'float', options.angle ?? -44 / 360],
|
|
433
|
+
['Skew', 'float', options.skew ?? 0.84],
|
|
434
|
+
['Speed', 'float', options.speed ?? 0.4],
|
|
435
|
+
],
|
|
436
|
+
{
|
|
437
|
+
uv: (u) =>
|
|
438
|
+
`uv = fxShatter(uv, aspect, ${u('Scale')}, ${u('Spread')} * amount, ${u('Angle')}, ${u('Skew')}, time * ${u('Speed')});`,
|
|
439
|
+
},
|
|
440
|
+
),
|
|
441
|
+
|
|
442
|
+
pixelate: (_gl, options, prefix) =>
|
|
443
|
+
defineEffect(prefix, [FX_PIXELATE], [['Size', 'float', options.size ?? 0.02]], {
|
|
444
|
+
uv: (u) => `uv = fxPixelate(uv, aspect, ${u('Size')} * amount);`,
|
|
445
|
+
}),
|
|
446
|
+
|
|
447
|
+
chromatic: (_gl, options, prefix) =>
|
|
448
|
+
defineEffect(prefix, [FX_CHROMATIC], [['Strength', 'float', options.strength ?? 0.02]], {
|
|
449
|
+
sample: (u) => `image = fxChromatic(uv, pointer, ${u('Strength')} * amount);`,
|
|
450
|
+
}),
|
|
451
|
+
|
|
452
|
+
bokeh(gl, options, prefix) {
|
|
453
|
+
const scale = options.scale ?? 0.5;
|
|
454
|
+
const chain = new PassChain(gl);
|
|
455
|
+
const blit = createPassProgram(gl, COVER_BLIT_FRAGMENT, {
|
|
456
|
+
tImage: { value: null },
|
|
457
|
+
uImageSize: { value: [1, 1] },
|
|
458
|
+
});
|
|
459
|
+
const blur = createBokehPass(gl, {
|
|
460
|
+
radius: options.radius ?? 0.005,
|
|
461
|
+
highlight: options.highlight ?? 80,
|
|
462
|
+
samples: options.samples ?? 40,
|
|
463
|
+
});
|
|
464
|
+
const { uniforms, declarations, u } = defineUniforms(prefix, [
|
|
465
|
+
['Blur', 'sampler2D', chain.output],
|
|
466
|
+
['Focus', 'float', options.focus ?? 0.25],
|
|
467
|
+
['Softness', 'float', options.softness ?? 0.3],
|
|
468
|
+
]);
|
|
469
|
+
// The photo is static, so the blur is rendered once per (texture, size) and then just read.
|
|
470
|
+
let blurredTexture: Texture | null = null;
|
|
471
|
+
return {
|
|
472
|
+
chunks: [],
|
|
473
|
+
declarations,
|
|
474
|
+
uniforms,
|
|
475
|
+
sampleCode: `image = mix(sampleImage(uv), texture(${u('Blur')}, uv).rgb, amount * smoothstep(${u('Focus')}, ${u('Focus')} + ${u('Softness')}, length((uv - pointer) * aspect)));`,
|
|
476
|
+
update({ pointer }, image) {
|
|
477
|
+
if (pointer.hoverAmount <= 0 || !image.loaded || blurredTexture === image.texture) return;
|
|
478
|
+
blit.uniforms.tImage.value = image.texture;
|
|
479
|
+
blit.uniforms.uImageSize.value = image.size;
|
|
480
|
+
chain.render([blit, blur]);
|
|
481
|
+
uniforms[u('Blur')].value = chain.output;
|
|
482
|
+
blurredTexture = image.texture;
|
|
483
|
+
},
|
|
484
|
+
resize({ width, height }) {
|
|
485
|
+
chain.setSize(width * scale, height * scale);
|
|
486
|
+
blurredTexture = null;
|
|
487
|
+
},
|
|
488
|
+
dispose() {
|
|
489
|
+
chain.dispose();
|
|
490
|
+
blit.remove();
|
|
491
|
+
blur.remove();
|
|
492
|
+
},
|
|
493
|
+
};
|
|
494
|
+
},
|
|
495
|
+
|
|
496
|
+
zoomBlur: (_gl, options, prefix) =>
|
|
497
|
+
defineEffect(prefix, [FX_ZOOM_BLUR], [['Strength', 'float', options.strength ?? 0.12]], {
|
|
498
|
+
sample: (u) => `image = fxZoomBlur(uv, pointer, ${u('Strength')} * amount);`,
|
|
499
|
+
}),
|
|
500
|
+
|
|
501
|
+
edges: (_gl, options, prefix) =>
|
|
502
|
+
colorMixEffect(
|
|
503
|
+
prefix,
|
|
504
|
+
[GLSL_LUMA, FX_EDGES],
|
|
505
|
+
[
|
|
506
|
+
['Color', 'vec3', hexToRgb(options.color ?? '#ffffff')],
|
|
507
|
+
['Strength', 'float', options.strength ?? 1],
|
|
508
|
+
],
|
|
509
|
+
(u) => `${u('Color')} * min(1.0, fxEdges(uv, texel) * ${u('Strength')})`,
|
|
510
|
+
),
|
|
511
|
+
|
|
512
|
+
grayscale: (_gl, _options, prefix) => colorMixEffect(prefix, [GLSL_LUMA], [], () => 'vec3(luma(image))'),
|
|
513
|
+
|
|
514
|
+
sepia: (_gl, _options, prefix) => colorMixEffect(prefix, [FX_SEPIA], [], () => 'fxSepia(image)'),
|
|
515
|
+
|
|
516
|
+
invert: (_gl, _options, prefix) => colorMixEffect(prefix, [], [], () => '1.0 - image'),
|
|
517
|
+
|
|
518
|
+
duotone: (_gl, options, prefix) =>
|
|
519
|
+
colorMixEffect(
|
|
520
|
+
prefix,
|
|
521
|
+
[GLSL_LUMA],
|
|
522
|
+
[
|
|
523
|
+
['Shadow', 'vec3', hexToRgb(options.shadow ?? '#1b0a2e')],
|
|
524
|
+
['Highlight', 'vec3', hexToRgb(options.highlight ?? '#ffb37a')],
|
|
525
|
+
],
|
|
526
|
+
(u) => `mix(${u('Shadow')}, ${u('Highlight')}, luma(image))`,
|
|
527
|
+
),
|
|
528
|
+
|
|
529
|
+
posterize: (_gl, options, prefix) =>
|
|
530
|
+
colorMixEffect(
|
|
531
|
+
prefix,
|
|
532
|
+
[],
|
|
533
|
+
[['Levels', 'float', options.levels ?? 5]],
|
|
534
|
+
(u) => `clamp(floor(image * ${u('Levels')}) / max(${u('Levels')} - 1.0, 1.0), 0.0, 1.0)`,
|
|
535
|
+
),
|
|
536
|
+
|
|
537
|
+
hueRotate: (_gl, options, prefix) =>
|
|
538
|
+
defineEffect(prefix, [GLSL_ROTATE_2D, FX_HUE_ROTATE], [['Angle', 'float', options.angle ?? 2]], {
|
|
539
|
+
color: (u) => `image = fxHueRotate(image, ${u('Angle')} * amount);`,
|
|
540
|
+
}),
|
|
541
|
+
|
|
542
|
+
colorReveal: (_gl, options, prefix) =>
|
|
543
|
+
colorMixEffect(
|
|
544
|
+
prefix,
|
|
545
|
+
[GLSL_LUMA],
|
|
546
|
+
[
|
|
547
|
+
['Radius', 'float', options.radius ?? 0.3],
|
|
548
|
+
['Softness', 'float', options.softness ?? 0.5],
|
|
549
|
+
],
|
|
550
|
+
(u) =>
|
|
551
|
+
`mix(vec3(luma(image)), image, 1.0 - smoothstep(${u('Radius')} * (1.0 - ${u('Softness')}), ${u('Radius')}, length((uv - pointer) * aspect)))`,
|
|
552
|
+
),
|
|
553
|
+
|
|
554
|
+
halftone: (_gl, options, prefix) =>
|
|
555
|
+
colorMixEffect(
|
|
556
|
+
prefix,
|
|
557
|
+
[GLSL_LUMA, GLSL_ROTATE_2D, FX_HALFTONE],
|
|
558
|
+
[
|
|
559
|
+
['Size', 'float', options.size ?? 8],
|
|
560
|
+
['Angle', 'float', options.angle ?? 0.4],
|
|
561
|
+
['Ink', 'vec3', hexToRgb(options.ink ?? '#111111')],
|
|
562
|
+
['Paper', 'vec3', hexToRgb(options.paper ?? '#f3ead9')],
|
|
563
|
+
],
|
|
564
|
+
(u) => `fxHalftone(image, gl_FragCoord.xy, ${u('Size')}, ${u('Angle')}, ${u('Ink')}, ${u('Paper')})`,
|
|
565
|
+
),
|
|
566
|
+
|
|
567
|
+
scanlines: (_gl, options, prefix) =>
|
|
568
|
+
defineEffect(
|
|
569
|
+
prefix,
|
|
570
|
+
[],
|
|
571
|
+
[
|
|
572
|
+
['Period', 'float', options.period ?? 4],
|
|
573
|
+
['Strength', 'float', options.strength ?? 0.35],
|
|
574
|
+
],
|
|
575
|
+
{ color: (u) => `image *= 1.0 - ${u('Strength')} * amount * (0.5 + 0.5 * sin(gl_FragCoord.y * PI / ${u('Period')}));` },
|
|
576
|
+
),
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
export const IMAGE_EFFECT_NAMES = Object.keys(IMAGE_EFFECT_FACTORIES) as ImageEffectName[];
|
|
580
|
+
|
|
581
|
+
/*
|
|
582
|
+
* Instantiate effects from specs. Each gets a unique uniform prefix (so the same effect can
|
|
583
|
+
* appear twice, and separate groups can use separate prefixes); the caller owns the instances
|
|
584
|
+
* and must dispose them with the host program.
|
|
585
|
+
*/
|
|
586
|
+
export function createImageEffects(gl: OGLRenderingContext, specs: ImageEffectSpec[], prefix = 'uFx'): ImageEffectInstance[] {
|
|
587
|
+
return specs.map((spec, index) => {
|
|
588
|
+
const { name, ...options } = spec;
|
|
589
|
+
const factory = IMAGE_EFFECT_FACTORIES[name] as ImageEffectFactory<ImageEffectName>;
|
|
590
|
+
return factory(gl, options as never, `${prefix}${index}`);
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/*
|
|
595
|
+
* Release every instance's GPU resources.
|
|
596
|
+
*/
|
|
597
|
+
export function disposeImageEffects(effects: ImageEffectInstance[]) {
|
|
598
|
+
for (const effect of effects) effect.dispose?.();
|
|
599
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Everything an image pass needs for photo effects, in one module so a scene can `import()` it
|
|
3
|
+
* lazily and pay for the effect code only when effects are actually requested. The effect quad
|
|
4
|
+
* is not included: a scene built around it (the carousel) imports it directly, so a hero that
|
|
5
|
+
* only splices effects into its composite never loads it.
|
|
6
|
+
*/
|
|
7
|
+
export * from './effectRegion.ts';
|
|
8
|
+
export * from './imageEffects.ts';
|