@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,136 @@
|
|
|
1
|
+
import { hexToRgb } from '../../utils/color.ts';
|
|
2
|
+
import { DOT_EFFECTS, DOT_SHAPES, getDotEffectId } from '../../utils/dotEffectSpecs.ts';
|
|
3
|
+
import type { ResolvedDotEffectOptions } from '../../utils/dotEffectSpecs.ts';
|
|
4
|
+
import type { UniformValues } from '../uniforms.ts';
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* Declare one trigger's settings; click, pulse, and rest all use the same shader evaluator.
|
|
8
|
+
*/
|
|
9
|
+
export function dotEffectDeclarations(prefix: string) {
|
|
10
|
+
return `
|
|
11
|
+
uniform int ${prefix}Effect;
|
|
12
|
+
uniform int ${prefix}Shape;
|
|
13
|
+
uniform float ${prefix}Radius;
|
|
14
|
+
uniform float ${prefix}Width;
|
|
15
|
+
uniform float ${prefix}Duration;
|
|
16
|
+
uniform float ${prefix}Strength;
|
|
17
|
+
uniform float ${prefix}Scale;
|
|
18
|
+
uniform float ${prefix}Opacity;
|
|
19
|
+
uniform float ${prefix}ColorOn;
|
|
20
|
+
uniform vec3 ${prefix}Color;
|
|
21
|
+
`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/*
|
|
25
|
+
* Create uniform slots with safe values, without requiring an enabled effect.
|
|
26
|
+
*/
|
|
27
|
+
export function createDotEffectUniforms(prefix: string): UniformValues {
|
|
28
|
+
return {
|
|
29
|
+
[`${prefix}Effect`]: { value: 0 },
|
|
30
|
+
[`${prefix}Shape`]: { value: 0 },
|
|
31
|
+
[`${prefix}Radius`]: { value: 1 },
|
|
32
|
+
[`${prefix}Width`]: { value: 1 },
|
|
33
|
+
[`${prefix}Duration`]: { value: 1 },
|
|
34
|
+
[`${prefix}Strength`]: { value: 0 },
|
|
35
|
+
[`${prefix}Scale`]: { value: 1 },
|
|
36
|
+
[`${prefix}Opacity`]: { value: 1 },
|
|
37
|
+
[`${prefix}ColorOn`]: { value: 0 },
|
|
38
|
+
[`${prefix}Color`]: { value: [1, 1, 1] },
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/*
|
|
43
|
+
* Apply resolved appearance and movement settings; only parameter changes do this work.
|
|
44
|
+
*/
|
|
45
|
+
export function setDotEffectUniforms(uniforms: UniformValues, prefix: string, options: ResolvedDotEffectOptions | null) {
|
|
46
|
+
uniforms[`${prefix}Effect`].value = getDotEffectId(options?.effect ?? 'swell');
|
|
47
|
+
uniforms[`${prefix}Shape`].value = DOT_SHAPES.indexOf(options?.shape ?? 'circle');
|
|
48
|
+
uniforms[`${prefix}Radius`].value = options?.radius ?? 1;
|
|
49
|
+
uniforms[`${prefix}Width`].value = options?.width ?? 1;
|
|
50
|
+
uniforms[`${prefix}Duration`].value = (options?.duration ?? 1000) / 1000;
|
|
51
|
+
uniforms[`${prefix}Strength`].value = options?.strength ?? 0;
|
|
52
|
+
uniforms[`${prefix}Scale`].value = options?.scale ?? 1;
|
|
53
|
+
uniforms[`${prefix}Opacity`].value = options?.opacity ?? 1;
|
|
54
|
+
uniforms[`${prefix}ColorOn`].value = options?.color ? 1 : 0;
|
|
55
|
+
uniforms[`${prefix}Color`].value = hexToRgb(options?.color ?? '#ffffff');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Stateless effect math, composed with GLSL_CONSTANTS for PI; no extra render pass or per-frame allocations. */
|
|
59
|
+
export const GLSL_DOT_EFFECTS = `
|
|
60
|
+
struct DotEffectSample {
|
|
61
|
+
vec2 offset;
|
|
62
|
+
float amount;
|
|
63
|
+
float tint;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/*
|
|
67
|
+
* Zero influence, including at the end of a timeline, so every effect returns home exactly.
|
|
68
|
+
*/
|
|
69
|
+
DotEffectSample noDotEffect() {
|
|
70
|
+
return DotEffectSample(vec2(0.0), 0.0, 0.0);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/*
|
|
74
|
+
* Resolve random from a seed that the trigger holds constant throughout its animation.
|
|
75
|
+
*/
|
|
76
|
+
int selectDotEffect(int effect, float seed) {
|
|
77
|
+
return effect < 0 ? min(${DOT_EFFECTS.length - 1}, int(floor(seed * ${DOT_EFFECTS.length}.0))) : effect;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/*
|
|
81
|
+
* A smooth rise and fall with zero slope at both ends.
|
|
82
|
+
*/
|
|
83
|
+
float dotEffectBump(float phase) {
|
|
84
|
+
float bump = sin(PI * clamp(phase, 0.0, 1.0));
|
|
85
|
+
return bump * bump;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/*
|
|
89
|
+
* Evaluate any effect relative to its origin. Triggers supply coordinates, a normalized
|
|
90
|
+
* lifetime, and a stable per-dot seed; movement is added to the cursor spring independently.
|
|
91
|
+
*/
|
|
92
|
+
DotEffectSample sampleDotEffect(int effect, vec2 away, float phase, float seed, float radius, float width, float strength) {
|
|
93
|
+
if (phase < 0.0 || phase >= 1.0) return noDotEffect();
|
|
94
|
+
float distance = length(away);
|
|
95
|
+
if (distance >= radius) return noDotEffect();
|
|
96
|
+
float fade = 1.0 - smoothstep(radius * 0.65, radius, distance);
|
|
97
|
+
float bump = dotEffectBump(phase);
|
|
98
|
+
DotEffectSample result = DotEffectSample(vec2(0.0), bump * fade, 0.0);
|
|
99
|
+
if (effect == ${getDotEffectId('ripple')} || effect == ${getDotEffectId('color_wave')}) {
|
|
100
|
+
float crest = phase * (radius + width);
|
|
101
|
+
result.amount = (1.0 - smoothstep(0.0, width, abs(distance - crest))) * fade;
|
|
102
|
+
result.tint = effect == ${getDotEffectId('color_wave')} ? 1.0 : 0.0;
|
|
103
|
+
} else if (effect == ${getDotEffectId('vortex')}) {
|
|
104
|
+
float angle = bump * fade * strength * 2.0 * PI;
|
|
105
|
+
float c = cos(angle);
|
|
106
|
+
float s = sin(angle);
|
|
107
|
+
result.offset = vec2(c * away.x - s * away.y, s * away.x + c * away.y) - away;
|
|
108
|
+
} else if (effect == ${getDotEffectId('magnet')}) {
|
|
109
|
+
result.offset = -away * min(0.95, strength * 2.0) * bump * fade;
|
|
110
|
+
} else if (effect == ${getDotEffectId('sparkle')}) {
|
|
111
|
+
float start = seed * 0.6;
|
|
112
|
+
float local = (phase - start) / 0.3;
|
|
113
|
+
result.amount = local > 0.0 && local < 1.0 ? dotEffectBump(local) * fade : 0.0;
|
|
114
|
+
result.tint = 1.0;
|
|
115
|
+
}
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/*
|
|
120
|
+
* A stable accent palette for color wave and sparkle when no explicit tint was supplied.
|
|
121
|
+
*/
|
|
122
|
+
vec3 dotEffectPalette(float seed) {
|
|
123
|
+
return 0.55 + 0.4 * cos(2.0 * PI * (seed + vec3(0.0, 0.33, 0.67)));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/*
|
|
127
|
+
* Composite a trigger's tint in premultiplied form; later triggers can override earlier ones.
|
|
128
|
+
*/
|
|
129
|
+
void addDotEffectTint(inout vec4 tint, DotEffectSample effectSample, vec3 color, float colorOn, float seed) {
|
|
130
|
+
float alpha = effectSample.amount * max(colorOn, effectSample.tint);
|
|
131
|
+
if (alpha <= 0.0) return;
|
|
132
|
+
vec3 target = colorOn > 0.5 ? color : dotEffectPalette(seed);
|
|
133
|
+
tint.rgb = tint.rgb * (1.0 - alpha) + target * alpha;
|
|
134
|
+
tint.a += (1.0 - tint.a) * alpha;
|
|
135
|
+
}
|
|
136
|
+
`;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { Geometry, Mesh, Program } from 'ogl';
|
|
2
|
+
import type { OGLRenderingContext, Texture } from 'ogl';
|
|
3
|
+
import { GLSL_HEADER, fragmentShader } from '../glsl.ts';
|
|
4
|
+
import { imageEffectShaderParts } from '../passes.ts';
|
|
5
|
+
import type { SceneSize, RGB } from '../types.ts';
|
|
6
|
+
import { setVec2, setVec3 } from '../uniforms.ts';
|
|
7
|
+
import type { ImageEffectInstance } from './imageEffects.ts';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Types
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** A rectangle of the canvas in CSS pixels, y down, relative to the canvas's top-left corner. */
|
|
14
|
+
export interface QuadRect {
|
|
15
|
+
x: number;
|
|
16
|
+
y: number;
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface EffectQuadDraw {
|
|
22
|
+
image: Texture;
|
|
23
|
+
/** Intrinsic image size in pixels, for the cover fit. */
|
|
24
|
+
imageSize: [number, number];
|
|
25
|
+
rect: QuadRect;
|
|
26
|
+
/** Current canvas size, as the scene received it from `resize`. */
|
|
27
|
+
canvas: SceneSize;
|
|
28
|
+
/** 0..1 effect strength; every effect is an exact no-op at 0. */
|
|
29
|
+
amount: number;
|
|
30
|
+
/** Pointer in the rectangle's own UV space (0..1, y up); only pointer-driven effects read it. */
|
|
31
|
+
pointer: [number, number];
|
|
32
|
+
/** Active seconds, for time-driven effects. */
|
|
33
|
+
time: number;
|
|
34
|
+
/** Corner radius in CSS pixels; the corners are anti-aliased and left transparent. */
|
|
35
|
+
radius: number;
|
|
36
|
+
/** 0..1 opacity of the drawn image over `backdrop`; the rectangle itself stays opaque. */
|
|
37
|
+
opacity: number;
|
|
38
|
+
/** Overall alpha of the image and its backdrop; defaults to 1. */
|
|
39
|
+
alpha?: number;
|
|
40
|
+
/** Color seen through a translucent image, as if the rectangle had that background. */
|
|
41
|
+
backdrop: RGB;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface EffectQuad {
|
|
45
|
+
program: Program;
|
|
46
|
+
/** Draw one image, cover-fit, into a rectangle of the canvas through the effects. Call once per rectangle per frame. */
|
|
47
|
+
draw: (options: EffectQuadDraw) => void;
|
|
48
|
+
dispose: () => void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Two triangles over the unit square; `position` doubles as the quad's UV.
|
|
52
|
+
const QUAD_POSITIONS = new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]);
|
|
53
|
+
const QUAD_INDICES = new Uint16Array([0, 1, 2, 2, 1, 3]);
|
|
54
|
+
|
|
55
|
+
/** Places the unit quad at `uRect` (x, y, width, height in clip space). */
|
|
56
|
+
const QUAD_VERTEX = `${GLSL_HEADER}
|
|
57
|
+
in vec2 position;
|
|
58
|
+
out vec2 vUv;
|
|
59
|
+
uniform vec4 uRect;
|
|
60
|
+
void main() {
|
|
61
|
+
vUv = position;
|
|
62
|
+
gl_Position = vec4(uRect.xy + position * uRect.zw, 0.0, 1.0);
|
|
63
|
+
}
|
|
64
|
+
`;
|
|
65
|
+
|
|
66
|
+
/*
|
|
67
|
+
* Draw a cover-fit image through image effects into any rectangle of a transparent canvas,
|
|
68
|
+
* with optional rounded corners, writing premultiplied color. One quad serves any number of
|
|
69
|
+
* rectangles: `draw` sets the per-rectangle uniforms and issues one draw call, so several
|
|
70
|
+
* images with the same effect list share a single program. The effects' `amount` is the
|
|
71
|
+
* `amount` given to each draw. Effects that keep per-host state sized to one canvas (`ripple`,
|
|
72
|
+
* `bokeh`) are not supported here; stateless effects such as `pixelate` and the color effects are.
|
|
73
|
+
*/
|
|
74
|
+
export function createEffectQuad(gl: OGLRenderingContext, effects: ImageEffectInstance[]): EffectQuad {
|
|
75
|
+
const parts = imageEffectShaderParts({ hoverEffects: effects });
|
|
76
|
+
const fragment = fragmentShader(
|
|
77
|
+
`
|
|
78
|
+
uniform float uRadius;
|
|
79
|
+
uniform float uOpacity;
|
|
80
|
+
uniform float uAlpha;
|
|
81
|
+
uniform vec3 uBackdrop;
|
|
82
|
+
void main() {
|
|
83
|
+
${parts.body}
|
|
84
|
+
// Signed distance to the rounded rectangle in drawing-buffer pixels, anti-aliased over one pixel.
|
|
85
|
+
vec2 corner = abs(vUv - 0.5) * uResolution - (uResolution * 0.5 - uRadius);
|
|
86
|
+
float dist = length(max(corner, 0.0)) + min(max(corner.x, corner.y), 0.0) - uRadius;
|
|
87
|
+
float coverage = 1.0 - clamp(dist + 0.5, 0.0, 1.0);
|
|
88
|
+
// Mix the image over its backdrop, then apply overall alpha to the premultiplied result.
|
|
89
|
+
coverage *= uAlpha;
|
|
90
|
+
fragColor = vec4(mix(uBackdrop, image, uOpacity) * coverage, coverage);
|
|
91
|
+
}
|
|
92
|
+
`,
|
|
93
|
+
parts.chunks,
|
|
94
|
+
);
|
|
95
|
+
const program = new Program(gl, {
|
|
96
|
+
vertex: QUAD_VERTEX,
|
|
97
|
+
fragment,
|
|
98
|
+
uniforms: {
|
|
99
|
+
...parts.uniforms,
|
|
100
|
+
uResolution: { value: [1, 1] },
|
|
101
|
+
uRect: { value: [0, 0, 1, 1] },
|
|
102
|
+
uRadius: { value: 0 },
|
|
103
|
+
uOpacity: { value: 1 },
|
|
104
|
+
uAlpha: { value: 1 },
|
|
105
|
+
uBackdrop: { value: [1, 1, 1] },
|
|
106
|
+
},
|
|
107
|
+
transparent: true,
|
|
108
|
+
depthTest: false,
|
|
109
|
+
depthWrite: false,
|
|
110
|
+
cullFace: false,
|
|
111
|
+
});
|
|
112
|
+
program.setBlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
113
|
+
const geometry = new Geometry(gl, {
|
|
114
|
+
position: { size: 2, data: QUAD_POSITIONS },
|
|
115
|
+
index: { data: QUAD_INDICES },
|
|
116
|
+
});
|
|
117
|
+
const mesh = new Mesh(gl, { geometry, program, frustumCulled: false });
|
|
118
|
+
const { uniforms } = program;
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
program,
|
|
122
|
+
draw({ image, imageSize, rect, canvas, amount, pointer, time, radius, opacity, alpha = 1, backdrop }) {
|
|
123
|
+
uniforms.tImage.value = image;
|
|
124
|
+
setVec2(uniforms.uImageSize, imageSize[0], imageSize[1]);
|
|
125
|
+
setVec2(uniforms.uResolution, rect.width * canvas.pixelRatio, rect.height * canvas.pixelRatio);
|
|
126
|
+
// CSS pixels (y down) to clip space (y up), with the quad's origin at its bottom-left corner.
|
|
127
|
+
const clipRect = uniforms.uRect.value as number[];
|
|
128
|
+
clipRect[0] = (rect.x / canvas.width) * 2 - 1;
|
|
129
|
+
clipRect[1] = 1 - ((rect.y + rect.height) / canvas.height) * 2;
|
|
130
|
+
clipRect[2] = (rect.width / canvas.width) * 2;
|
|
131
|
+
clipRect[3] = (rect.height / canvas.height) * 2;
|
|
132
|
+
uniforms.uEffectAmount.value = amount;
|
|
133
|
+
setVec2(uniforms.uPointer, pointer[0], pointer[1]);
|
|
134
|
+
uniforms.uTime.value = time;
|
|
135
|
+
uniforms.uRadius.value = radius * canvas.pixelRatio;
|
|
136
|
+
uniforms.uOpacity.value = opacity;
|
|
137
|
+
uniforms.uAlpha.value = alpha;
|
|
138
|
+
setVec3(uniforms.uBackdrop, backdrop);
|
|
139
|
+
gl.renderer.render({ scene: mesh, clear: false, sort: false, frustumCull: false, update: false });
|
|
140
|
+
},
|
|
141
|
+
dispose() {
|
|
142
|
+
geometry.remove();
|
|
143
|
+
program.remove();
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import type { OGLRenderingContext, Texture } from 'ogl';
|
|
2
|
+
import { disposeTexture } from '../chain.ts';
|
|
3
|
+
import { createImageTexture, createLinearTexture, createPixelTexture } from '../texture.ts';
|
|
4
|
+
import type { ImageTexture } from '../texture.ts';
|
|
5
|
+
import type { SceneSize } from '../types.ts';
|
|
6
|
+
import type { UniformValues } from '../uniforms.ts';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Effect regions: a per-pixel mask (0..1) that scales a group of image effects, so effects can
|
|
10
|
+
* cover part of the image instead of all of it. Every position accepts fixed coordinates or
|
|
11
|
+
* `'pointer'`, so the same shape can be static or follow the mouse. The shape is chosen by a
|
|
12
|
+
* uniform, not compiled in, so changing a region never recompiles the host shader.
|
|
13
|
+
*
|
|
14
|
+
* Coordinates are CSS-like fractions of the canvas: x 0 (left) → 1 (right), y 0 (top) → 1
|
|
15
|
+
* (bottom). Sizes, radii, and softness are fractions of the canvas HEIGHT, so circles stay
|
|
16
|
+
* circular at any aspect ratio.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** A fixed point, or the (eased) pointer position. */
|
|
20
|
+
export type EffectRegionAnchor = 'pointer' | [x: number, y: number];
|
|
21
|
+
|
|
22
|
+
/** Which side of a split line is affected: a named side, or degrees clockwise from "right". */
|
|
23
|
+
export type EffectRegionDirection = 'left' | 'right' | 'up' | 'down' | number;
|
|
24
|
+
|
|
25
|
+
interface EffectRegionBase {
|
|
26
|
+
/** Feather width of the edge, as a fraction of canvas height. */
|
|
27
|
+
softness?: number;
|
|
28
|
+
/** Affect everything outside the shape instead of inside it. */
|
|
29
|
+
invert?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type EffectRegion =
|
|
33
|
+
| ({
|
|
34
|
+
shape: 'circle';
|
|
35
|
+
center?: EffectRegionAnchor;
|
|
36
|
+
radius?: number;
|
|
37
|
+
} & EffectRegionBase)
|
|
38
|
+
| ({
|
|
39
|
+
shape: 'rect';
|
|
40
|
+
center?: EffectRegionAnchor;
|
|
41
|
+
size?: [width: number, height: number];
|
|
42
|
+
} & EffectRegionBase)
|
|
43
|
+
/** Everything on one side of a line through `at`. */
|
|
44
|
+
| ({
|
|
45
|
+
shape: 'split';
|
|
46
|
+
at?: EffectRegionAnchor;
|
|
47
|
+
direction?: EffectRegionDirection;
|
|
48
|
+
} & EffectRegionBase)
|
|
49
|
+
/** An SVG path (`d` attribute) drawn into a box; without `size` the box is the whole canvas. */
|
|
50
|
+
| ({
|
|
51
|
+
shape: 'path';
|
|
52
|
+
d: string;
|
|
53
|
+
viewBox?: [width: number, height: number];
|
|
54
|
+
position?: EffectRegionAnchor;
|
|
55
|
+
size?: number | [width: number, height: number];
|
|
56
|
+
} & EffectRegionBase)
|
|
57
|
+
/** A grayscale mask image drawn into a box; without `size` the box is the whole canvas. */
|
|
58
|
+
| ({
|
|
59
|
+
shape: 'image';
|
|
60
|
+
url: string;
|
|
61
|
+
position?: EffectRegionAnchor;
|
|
62
|
+
size?: number | [width: number, height: number];
|
|
63
|
+
} & EffectRegionBase);
|
|
64
|
+
|
|
65
|
+
export interface EffectRegionInstance {
|
|
66
|
+
/** Uniform declarations plus `float regionMask(vec2 uv, vec2 pointer, vec2 aspect)`. */
|
|
67
|
+
chunk: string;
|
|
68
|
+
uniforms: UniformValues;
|
|
69
|
+
/** Apply a new region (or none) by updating uniforms and mask textures only. */
|
|
70
|
+
setSpec: (spec: EffectRegion | null) => void;
|
|
71
|
+
resize: (size: SceneSize) => void;
|
|
72
|
+
dispose: () => void;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const REGION_SHAPES = {
|
|
76
|
+
all: 0,
|
|
77
|
+
circle: 1,
|
|
78
|
+
rect: 2,
|
|
79
|
+
split: 3,
|
|
80
|
+
texture: 4,
|
|
81
|
+
} as const;
|
|
82
|
+
const MASK_RASTER_SIZE = 1024;
|
|
83
|
+
|
|
84
|
+
const REGION_GLSL = `
|
|
85
|
+
uniform int uRegionShape;
|
|
86
|
+
uniform vec2 uRegionCenter;
|
|
87
|
+
uniform float uRegionTracksPointer;
|
|
88
|
+
uniform vec2 uRegionSize;
|
|
89
|
+
uniform vec2 uRegionNormal;
|
|
90
|
+
uniform float uRegionSoftness;
|
|
91
|
+
uniform float uRegionInvert;
|
|
92
|
+
uniform sampler2D tRegionMask;
|
|
93
|
+
float regionMask(vec2 uv, vec2 pointer, vec2 aspect) {
|
|
94
|
+
vec2 center = mix(uRegionCenter, pointer, uRegionTracksPointer);
|
|
95
|
+
vec2 p = (uv - center) * aspect;
|
|
96
|
+
float soft = max(uRegionSoftness, 0.0005);
|
|
97
|
+
float m = 1.0;
|
|
98
|
+
if (uRegionShape == 1) {
|
|
99
|
+
m = 1.0 - smoothstep(uRegionSize.x - soft, uRegionSize.x + soft, length(p));
|
|
100
|
+
} else if (uRegionShape == 2) {
|
|
101
|
+
vec2 d = abs(p) - uRegionSize * 0.5;
|
|
102
|
+
m = 1.0 - smoothstep(-soft, soft, max(d.x, d.y));
|
|
103
|
+
} else if (uRegionShape == 3) {
|
|
104
|
+
m = smoothstep(-soft, soft, dot(p, uRegionNormal));
|
|
105
|
+
} else if (uRegionShape == 4) {
|
|
106
|
+
vec2 box = uRegionSize.x > 0.0 ? uRegionSize : aspect;
|
|
107
|
+
vec2 maskUv = p / box + 0.5;
|
|
108
|
+
float inside = step(0.0, maskUv.x) * step(maskUv.x, 1.0) * step(0.0, maskUv.y) * step(maskUv.y, 1.0);
|
|
109
|
+
m = texture(tRegionMask, maskUv).r * inside;
|
|
110
|
+
}
|
|
111
|
+
return mix(m, 1.0 - m, uRegionInvert);
|
|
112
|
+
}
|
|
113
|
+
`;
|
|
114
|
+
|
|
115
|
+
/*
|
|
116
|
+
* Convert a CSS-like anchor (y down) into canvas UV (y up); `'pointer'` maps to the center
|
|
117
|
+
* placeholder and sets the tracking flag instead.
|
|
118
|
+
*/
|
|
119
|
+
function resolveAnchor(anchor: EffectRegionAnchor | undefined): {
|
|
120
|
+
center: [number, number];
|
|
121
|
+
tracks: number;
|
|
122
|
+
} {
|
|
123
|
+
if (anchor === 'pointer') return { center: [0.5, 0.5], tracks: 1 };
|
|
124
|
+
const [x, y] = anchor ?? [0.5, 0.5];
|
|
125
|
+
return { center: [x, 1 - y], tracks: 0 };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/*
|
|
129
|
+
* Turn a split direction into a unit normal in aspect-corrected, y-up space.
|
|
130
|
+
*/
|
|
131
|
+
function resolveNormal(direction: EffectRegionDirection | undefined): [number, number] {
|
|
132
|
+
switch (direction) {
|
|
133
|
+
case 'left':
|
|
134
|
+
return [-1, 0];
|
|
135
|
+
case 'up':
|
|
136
|
+
return [0, 1];
|
|
137
|
+
case 'down':
|
|
138
|
+
return [0, -1];
|
|
139
|
+
case undefined:
|
|
140
|
+
case 'right':
|
|
141
|
+
return [1, 0];
|
|
142
|
+
default: {
|
|
143
|
+
const radians = (direction * Math.PI) / 180;
|
|
144
|
+
// Degrees run clockwise on screen, which is counter-clockwise in y-up space.
|
|
145
|
+
return [Math.cos(radians), -Math.sin(radians)];
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/*
|
|
151
|
+
* Normalise a box size into [width, height] height-units, or [0, 0] meaning "cover the canvas".
|
|
152
|
+
*/
|
|
153
|
+
function resolveSize(size: number | [number, number] | undefined): [number, number] {
|
|
154
|
+
if (size === undefined) return [0, 0];
|
|
155
|
+
return typeof size === 'number' ? [size, size] : size;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/*
|
|
159
|
+
* Draw an SVG path into a black canvas as white, optionally feathered, sized for the box aspect.
|
|
160
|
+
*/
|
|
161
|
+
function rasterizePath(spec: Extract<EffectRegion, { shape: 'path' }>, boxAspect: number, featherPx: number) {
|
|
162
|
+
const width = MASK_RASTER_SIZE;
|
|
163
|
+
const height = Math.max(1, Math.round(width / boxAspect));
|
|
164
|
+
const canvas = document.createElement('canvas');
|
|
165
|
+
canvas.width = width;
|
|
166
|
+
canvas.height = height;
|
|
167
|
+
const context = canvas.getContext('2d');
|
|
168
|
+
if (!context) return canvas;
|
|
169
|
+
const [viewWidth, viewHeight] = spec.viewBox ?? [100, 100];
|
|
170
|
+
context.fillStyle = '#000';
|
|
171
|
+
context.fillRect(0, 0, width, height);
|
|
172
|
+
// Canvas filters are the cheap way to feather; browsers without them get a crisp edge.
|
|
173
|
+
if (featherPx > 0 && 'filter' in context) context.filter = `blur(${featherPx.toFixed(1)}px)`;
|
|
174
|
+
context.fillStyle = '#fff';
|
|
175
|
+
context.scale(width / viewWidth, height / viewHeight);
|
|
176
|
+
context.fill(new Path2D(spec.d));
|
|
177
|
+
return canvas;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/*
|
|
181
|
+
* Create a region whose uniforms plug into an image pass; starts as "all" (mask = 1 everywhere).
|
|
182
|
+
* An `image` region's mask decodes asynchronously: `onLoad` fires when it does, so a scene that
|
|
183
|
+
* renders on demand can ask for a frame, and loads that finish after `signal` aborts are dropped.
|
|
184
|
+
*/
|
|
185
|
+
export function createEffectRegion(
|
|
186
|
+
gl: OGLRenderingContext,
|
|
187
|
+
{ signal, onLoad }: { signal?: AbortSignal; onLoad?: () => void } = {},
|
|
188
|
+
): EffectRegionInstance {
|
|
189
|
+
const blank = createPixelTexture(gl, [255, 255, 255, 255]);
|
|
190
|
+
const uniforms: UniformValues = {
|
|
191
|
+
uRegionShape: { value: REGION_SHAPES.all },
|
|
192
|
+
uRegionCenter: { value: [0.5, 0.5] },
|
|
193
|
+
uRegionTracksPointer: { value: 0 },
|
|
194
|
+
uRegionSize: { value: [0, 0] },
|
|
195
|
+
uRegionNormal: { value: [1, 0] },
|
|
196
|
+
uRegionSoftness: { value: 0.02 },
|
|
197
|
+
uRegionInvert: { value: 0 },
|
|
198
|
+
tRegionMask: { value: blank },
|
|
199
|
+
};
|
|
200
|
+
let spec: EffectRegion | null = null;
|
|
201
|
+
let pathTexture: Texture | null = null;
|
|
202
|
+
let imageTexture: ImageTexture | null = null;
|
|
203
|
+
let canvasAspect = 1;
|
|
204
|
+
|
|
205
|
+
/*
|
|
206
|
+
* Drop any mask texture the previous spec owned.
|
|
207
|
+
*/
|
|
208
|
+
function releaseMasks() {
|
|
209
|
+
if (pathTexture) disposeTexture(gl, pathTexture);
|
|
210
|
+
pathTexture = null;
|
|
211
|
+
imageTexture?.dispose();
|
|
212
|
+
imageTexture = null;
|
|
213
|
+
uniforms.tRegionMask.value = blank;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/*
|
|
217
|
+
* Rasterize the current path spec for the current box aspect and upload it.
|
|
218
|
+
*/
|
|
219
|
+
function updatePathMask() {
|
|
220
|
+
if (spec?.shape !== 'path') return;
|
|
221
|
+
const box = resolveSize(spec.size);
|
|
222
|
+
const boxAspect = box[0] > 0 ? box[0] / box[1] : canvasAspect;
|
|
223
|
+
const boxHeight = box[0] > 0 ? box[1] : 1;
|
|
224
|
+
const featherPx = ((spec.softness ?? 0) / boxHeight) * (MASK_RASTER_SIZE / boxAspect);
|
|
225
|
+
const canvas = rasterizePath(spec, boxAspect, featherPx);
|
|
226
|
+
if (pathTexture) {
|
|
227
|
+
pathTexture.image = canvas;
|
|
228
|
+
pathTexture.needsUpdate = true;
|
|
229
|
+
} else {
|
|
230
|
+
pathTexture = createLinearTexture(gl, { image: canvas });
|
|
231
|
+
}
|
|
232
|
+
uniforms.tRegionMask.value = pathTexture;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/*
|
|
236
|
+
* Write shared uniforms for the spec, then set up whichever mask source it needs.
|
|
237
|
+
*/
|
|
238
|
+
function setSpec(nextSpec: EffectRegion | null) {
|
|
239
|
+
spec = nextSpec;
|
|
240
|
+
releaseMasks();
|
|
241
|
+
uniforms.uRegionSoftness.value = nextSpec?.softness ?? 0.02;
|
|
242
|
+
uniforms.uRegionInvert.value = nextSpec?.invert ? 1 : 0;
|
|
243
|
+
if (!nextSpec) {
|
|
244
|
+
uniforms.uRegionShape.value = REGION_SHAPES.all;
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const anchor = resolveAnchor(
|
|
248
|
+
nextSpec.shape === 'split'
|
|
249
|
+
? nextSpec.at
|
|
250
|
+
: nextSpec.shape === 'circle' || nextSpec.shape === 'rect'
|
|
251
|
+
? nextSpec.center
|
|
252
|
+
: nextSpec.position,
|
|
253
|
+
);
|
|
254
|
+
uniforms.uRegionCenter.value = anchor.center;
|
|
255
|
+
uniforms.uRegionTracksPointer.value = anchor.tracks;
|
|
256
|
+
switch (nextSpec.shape) {
|
|
257
|
+
case 'circle':
|
|
258
|
+
uniforms.uRegionShape.value = REGION_SHAPES.circle;
|
|
259
|
+
uniforms.uRegionSize.value = [nextSpec.radius ?? 0.3, 0];
|
|
260
|
+
break;
|
|
261
|
+
case 'rect':
|
|
262
|
+
uniforms.uRegionShape.value = REGION_SHAPES.rect;
|
|
263
|
+
uniforms.uRegionSize.value = nextSpec.size ?? [0.6, 0.6];
|
|
264
|
+
break;
|
|
265
|
+
case 'split':
|
|
266
|
+
uniforms.uRegionShape.value = REGION_SHAPES.split;
|
|
267
|
+
uniforms.uRegionNormal.value = resolveNormal(nextSpec.direction);
|
|
268
|
+
break;
|
|
269
|
+
case 'path':
|
|
270
|
+
uniforms.uRegionShape.value = REGION_SHAPES.texture;
|
|
271
|
+
uniforms.uRegionSize.value = resolveSize(nextSpec.size);
|
|
272
|
+
updatePathMask();
|
|
273
|
+
break;
|
|
274
|
+
case 'image': {
|
|
275
|
+
uniforms.uRegionShape.value = REGION_SHAPES.texture;
|
|
276
|
+
uniforms.uRegionSize.value = resolveSize(nextSpec.size);
|
|
277
|
+
imageTexture = createImageTexture(gl, { url: nextSpec.url, signal, onLoad });
|
|
278
|
+
// Transparent until decoded, so the region affects nothing rather than everything meanwhile.
|
|
279
|
+
uniforms.tRegionMask.value = imageTexture.texture;
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return {
|
|
286
|
+
chunk: REGION_GLSL,
|
|
287
|
+
uniforms,
|
|
288
|
+
setSpec,
|
|
289
|
+
resize({ width, height }) {
|
|
290
|
+
canvasAspect = width / Math.max(1, height);
|
|
291
|
+
// Only canvas-covering paths depend on the canvas aspect.
|
|
292
|
+
if (spec?.shape === 'path' && resolveSize(spec.size)[0] === 0) updatePathMask();
|
|
293
|
+
},
|
|
294
|
+
dispose() {
|
|
295
|
+
releaseMasks();
|
|
296
|
+
disposeTexture(gl, blank);
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
}
|