@alexpeng/dsh-custom-plugin 0.11.0 → 0.12.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.
@@ -1,287 +1,132 @@
1
1
  /**
2
- * WebGL1 backdrop: one full-screen triangle, one fragment shader, no library.
2
+ * WebGL2 light-and-shadow backdrop, built on Paper Shaders.
3
3
  *
4
- * This is the shader tier of `div#vx-bg`. It shares the layer, the motion
5
- * attribute (`data-vx-bg-motion`), the tint (`--vx-bg-tint`), the visibility
6
- * rules and the one-way degradation ladder with the ASCII renderer, but it is
7
- * exclusive with it: the two never coexist, and leaving this tier calls
8
- * `WEBGL_lose_context` so the page never accumulates contexts.
4
+ * The shader tier used to be a hand-written full-screen triangle per style.
5
+ * It is now a thin wrapper around `@paper-design/shaders` (Apache-2.0,
6
+ * https://github.com/paper-design/shaders): each preset is one of their
7
+ * audited fragment shaders, mounted through `ShaderMount`, which owns the
8
+ * WebGL2 context, DPR/size handling, visibility pausing and teardown.
9
9
  *
10
- * The uniform contract is fixed (time, resolution, dpr, surface, ink, speed,
11
- * density, intensity, seed). Surface and ink are the same values the ASCII
12
- * tier uses, so both tiers read correctly on light/dark themes and every
13
- * palette; the ink keeps `inkFor`'s fixed lightness delta and nothing neon or
14
- * highly saturated enters the shader.
10
+ * The page tint (`--vx-bg-tint`) is the shader's background colour, so a
11
+ * palette/theme flip keeps the page hue and only moves the ink. The calm zone
12
+ * is a CSS `mask-image` built from the same geometry as the scene tier
13
+ * (`bg-focus.ts`), and the user's 浓度 knob is the wrapper's opacity: the
14
+ * field fades toward the tint without the shader being recompiled.
15
15
  *
16
- * `liquid` also reads the pointer through two extra vec4s (`u_pointer`,
17
- * `u_pulse`): the cursor becomes an eddy that turns the cloud, and a press
18
- * expands a ring from where it landed. `grain` declares `animated:false` and
19
- * never runs a frame loop, so it deliberately ignores both — a static speck
20
- * field that reacts to the mouse would not be static any more.
21
- *
22
- * Styles are self-declared static or animated. A style that does not depend on
23
- * `u_time` draws one frame and never schedules rAF; the caller never has to
24
- * sniff pixels to discover that (readPixels would force a sync far more
25
- * expensive than the frame it saves).
16
+ * A missing WebGL2 context makes `createShaderBackdrop` return null; the
17
+ * caller then falls back to the character tier, exactly like the old WebGL1
18
+ * gate did.
26
19
  * @module @alexpeng/dsh-custom-plugin/client/bg-shader
27
20
  */
28
- import { type BgFocusRect } from './bg-focus.ts';
29
- import { type BgPointerEventLike } from './bg-pointer.ts';
30
- /** Every shader style id. */
31
- export type ShaderStyle = 'liquid' | 'grain';
32
- /** Inclusive numeric bounds for the future settings panel. */
21
+ import type { ShaderMountUniforms } from '@paper-design/shaders';
22
+ import { FOCUS_FEATHER_PX, FOCUS_FLOOR, type BgFocusRect } from './bg-focus.ts';
23
+ /** Every shader style id. Each one is a different Paper Shaders fragment
24
+ * shader, not one field with different knobs. */
25
+ export type ShaderStyle = 'mesh' | 'smoke' | 'rays' | 'dither' | 'grain' | 'warp' | 'swirl';
26
+ /** Inclusive numeric bounds for the settings panel. */
33
27
  export interface ShaderRange {
34
28
  readonly min: number;
35
29
  readonly max: number;
36
30
  }
37
- /** Writable bounds per style; defaults sit inside these. */
31
+ /** Writable bounds per style. */
38
32
  export interface ShaderStyleRanges {
39
33
  readonly speed: ShaderRange;
40
- readonly density: ShaderRange;
41
34
  readonly intensity: ShaderRange;
42
- readonly seed: ShaderRange;
43
- readonly fps: ShaderRange;
35
+ readonly opacity: ShaderRange;
44
36
  }
45
- /** Tunables of one style. `animated: false` means "draw one frame, never
46
- * schedule rAF" — a physical property of the shader, not a runtime guess. */
37
+ /** Tunables of one style. `animated: false` means the shader only draws when
38
+ * a uniform changes; Paper's mount handles the rAF stop for speed 0. */
47
39
  export interface ShaderStyleSpec {
48
40
  readonly speed: number;
49
- readonly density: number;
50
41
  readonly intensity: number;
51
- readonly seed: number;
52
- readonly fps: number;
42
+ readonly opacity: number;
53
43
  readonly animated: boolean;
54
- /** Per-style multiplier on the theme's base ΔL (see `inkFor`). The user's
55
- * soft/standard/strong strength choice multiplies this again. */
56
- readonly inkScale: number;
57
44
  readonly ranges: ShaderStyleRanges;
58
45
  }
59
- /**
60
- * Two deliberately different pieces rather than one field with knobs:
61
- * `liquid` is a low-frequency domain-warped ink cloud that drifts; `grain` is
62
- * a fine, static film grain with a soft edge falloff. Both derive their ink
63
- * from the surface brightness, so they work on light, dark and every palette.
64
- */
46
+ /** The five shipped styles. `mesh` is the default: a soft, low-contrast field
47
+ * that leaves the copy alone; `dither` is the most graphic one. Defaults are
48
+ * the browser-harness values (1280×822, tint-matched colours, mask on). */
65
49
  export declare const SHADER_STYLES: Record<ShaderStyle, ShaderStyleSpec>;
66
- /** Resolve a stored preset id to a shader style. An ASCII id or any unknown
67
- * string is a stale cross-tier combination and falls back to the default. */
50
+ /** Default style for the shader tier. */
51
+ export declare const DEFAULT_SHADER: ShaderStyle;
52
+ /** Resolve a stored id to a style; ASCII ids and unknown strings fall back. */
68
53
  export declare function resolveShaderStyle(preset: string | undefined): ShaderStyle;
69
- /** Panel overrides for one shader style. Values are clamped into the style's
70
- * own `ranges` before they reach a uniform; `strength` is the user's
71
- * soft/standard/strong multiplier on the ink step (clamped to the ink band). */
72
- export type ShaderParams = Partial<Record<'speed' | 'density' | 'intensity' | 'fps' | 'strength', number>>;
73
- /**
74
- * Resolve overrides against one style. Missing keys fall back to the style
75
- * default, so a style switch never inherits a value it cannot honour. Returns
76
- * the base spec object itself when nothing is overridden.
77
- */
78
- export declare function resolveShaderSpec(style: ShaderStyle, params: ShaderParams): ShaderStyleSpec;
79
- /** Hard bounds the uniform contract promises, independent of a style. */
80
- export declare const SHADER_UNIFORM_RANGES: {
81
- readonly speed: {
82
- readonly min: 0;
83
- readonly max: 2;
84
- };
85
- readonly density: {
86
- readonly min: 0.5;
87
- readonly max: 2;
88
- };
89
- readonly intensity: {
90
- readonly min: 0;
91
- readonly max: 1;
92
- };
93
- readonly seed: {
94
- readonly min: 0;
95
- readonly max: 9999;
96
- };
97
- };
98
- /** Vertex shader for the single full-screen triangle (GLSL ES 1.00). */
99
- export declare const SHADER_VERTEX_SOURCE = "attribute vec2 a_pos;\nvarying vec2 v_uv;\nvoid main() {\n v_uv = a_pos * 0.5 + 0.5;\n gl_Position = vec4(a_pos, 0.0, 1.0);\n}";
100
- /**
101
- * Fragment shaders, one per style. Both end in `mix(u_surface, u_ink, …)` with
102
- * alpha 1: the layer is the opaque page background while it is mounted, which
103
- * is the same contract the ASCII canvas honours with its surface fill.
104
- */
105
- export declare const SHADER_FRAGMENT_SOURCE: Record<ShaderStyle, string>;
106
- /** Numeric values handed to the GPU for one draw. Plain data so the uniform
107
- * mapping is unit-testable without a GL context. */
108
- export interface ShaderUniformValues {
109
- time: number;
110
- width: number;
111
- height: number;
112
- dpr: number;
113
- surface: [number, number, number];
114
- ink: [number, number, number];
115
- speed: number;
116
- density: number;
117
- intensity: number;
118
- seed: number;
119
- /** `[x, y, w, h]` in CSS px; `w <= 0` disables the mask. */
120
- focus: [number, number, number, number];
121
- focusFeather: number;
122
- focusFloor: number;
123
- /** `[x, y, active, speed]` in CSS px; `active <= 0` disables the eddy. */
124
- pointer: [number, number, number, number];
125
- /** `[x, y, age, active]` in CSS px; `active <= 0` disables the ring. */
126
- pulse: [number, number, number, number];
127
- }
128
- /** One pointer sample for the shader. `speed` is 0..1 (already normalised). */
129
- export interface ShaderPointerInput {
130
- x: number;
131
- y: number;
132
- active: boolean;
133
- speed: number;
134
- }
135
- /** One press sample for the shader; `age` is seconds since the press. */
136
- export interface ShaderPulseInput {
137
- x: number;
138
- y: number;
139
- age: number;
140
- }
141
- /** How long a press ring is allowed to expand, in seconds. */
142
- export declare const SHADER_PULSE_LIFE = 2.6;
143
- /** Inputs for {@link shaderUniforms}; `dark` is derived from the surface. */
144
- export interface ShaderUniformInput {
145
- time: number;
146
- width: number;
147
- height: number;
148
- dpr: number;
149
- spec: ShaderStyleSpec;
150
- surface: string;
151
- /** Multiplier applied by the degradation ladder (L2). */
152
- densityScale?: number;
153
- /** Reading-column calm zone from the appearance painter. */
154
- focus?: BgFocusRect | null;
155
- /** Live pointer sample; absent means "no eddy". */
156
- pointer?: ShaderPointerInput | null;
157
- /** Live press sample; absent or expired means "no ring". */
158
- pulse?: ShaderPulseInput | null;
159
- }
160
- /**
161
- * Map runtime state onto the uniform contract. Visibility time is clamped at
162
- * zero; resolution is CSS pixels (never backing pixels); dpr honours the same
163
- * 2× cap as the ASCII tier; density and the other knobs stay inside the
164
- * documented ranges even if a future panel or a hand-edited state file writes
165
- * something else. The ink is `inkFor(surface, dark)` so both tiers use exactly
166
- * the same lightness delta.
167
- */
168
- export declare function shaderUniforms(input: ShaderUniformInput): ShaderUniformValues;
169
- /** The GL call surface this renderer uses (kept structural for tests). */
170
- export interface ShaderGL {
171
- readonly VERTEX_SHADER: number;
172
- readonly FRAGMENT_SHADER: number;
173
- readonly COMPILE_STATUS: number;
174
- readonly LINK_STATUS: number;
175
- readonly ARRAY_BUFFER: number;
176
- readonly STATIC_DRAW: number;
177
- readonly FLOAT: number;
178
- readonly TRIANGLES: number;
179
- readonly COLOR_BUFFER_BIT: number;
180
- createShader(type: number): object | null;
181
- shaderSource(shader: object, source: string): void;
182
- compileShader(shader: object): void;
183
- getShaderParameter(shader: object, pname: number): boolean;
184
- getShaderInfoLog(shader: object): string | null;
185
- deleteShader(shader: object): void;
186
- createProgram(): object | null;
187
- attachShader(program: object, shader: object): void;
188
- linkProgram(program: object): void;
189
- getProgramParameter(program: object, pname: number): boolean;
190
- getProgramInfoLog(program: object): string | null;
191
- deleteProgram(program: object): void;
192
- useProgram(program: object | null): void;
193
- createBuffer(): object | null;
194
- bindBuffer(target: number, buffer: object | null): void;
195
- bufferData(target: number, data: Float32Array, usage: number): void;
196
- deleteBuffer(buffer: object): void;
197
- getAttribLocation(program: object, name: string): number;
198
- enableVertexAttribArray(index: number): void;
199
- vertexAttribPointer(index: number, size: number, type: number, normalized: boolean, stride: number, offset: number): void;
200
- getUniformLocation(program: object, name: string): object | null;
201
- uniform1f(location: object | null, value: number): void;
202
- uniform2f(location: object | null, x: number, y: number): void;
203
- uniform3f(location: object | null, x: number, y: number, z: number): void;
204
- uniform4f(location: object | null, x: number, y: number, z: number, w: number): void;
205
- viewport(x: number, y: number, width: number, height: number): void;
206
- drawArrays(mode: number, first: number, count: number): void;
207
- clearColor(r: number, g: number, b: number, a: number): void;
208
- clear(mask: number): void;
209
- getExtension(name: string): unknown;
210
- getParameter(pname: number): unknown;
211
- }
212
- /** WebGL context attributes the spec fixes. */
213
- export interface ShaderContextAttributes {
214
- readonly alpha: boolean;
215
- readonly antialias: boolean;
216
- readonly depth: boolean;
217
- readonly stencil: boolean;
218
- readonly powerPreference: 'low-power';
219
- }
220
- /** The canvas surface this renderer owns. */
221
- export interface ShaderCanvasLike {
222
- width: number;
223
- height: number;
224
- className: string;
225
- style: {
226
- width: string;
227
- height: string;
228
- };
229
- getContext(contextId: 'webgl', attributes?: ShaderContextAttributes): ShaderGL | null;
230
- addEventListener(type: 'webglcontextlost', listener: (event: ShaderContextLostEvent) => void): void;
231
- removeEventListener(type: 'webglcontextlost', listener: (event: ShaderContextLostEvent) => void): void;
232
- remove(): void;
233
- }
234
- /** The only context-loss field this renderer reads. */
235
- export interface ShaderContextLostEvent {
236
- preventDefault(): void;
237
- }
238
- /** The host element (`div#vx-bg`) as this renderer sees it. */
54
+ /** Panel overrides; every value is clamped into the style's own ranges. */
55
+ export interface ShaderParams {
56
+ readonly speed?: number;
57
+ readonly intensity?: number;
58
+ readonly opacity?: number;
59
+ /** Ink-strength multiplier from `BG_STRENGTH_SCALE`. */
60
+ readonly strength?: number;
61
+ }
62
+ /** Resolved values after merging overrides with the style defaults. */
63
+ export interface ShaderResolved {
64
+ readonly speed: number;
65
+ readonly intensity: number;
66
+ readonly opacity: number;
67
+ readonly strength: number;
68
+ }
69
+ export declare function resolveShaderSpec(style: ShaderStyle, params: ShaderParams): ShaderResolved;
70
+ /** The colour set one shader field is built from. All colours are derived from
71
+ * the page tint and the theme's ink, so the palette never turns neon. */
72
+ export interface ShaderPalette {
73
+ readonly back: string;
74
+ readonly ink: string;
75
+ readonly soft: string;
76
+ readonly mid: string;
77
+ readonly deep: string;
78
+ }
79
+ /** Blend two `#rrggbb` colours; `t = 0` is `a`, `t = 1` is `b`. */
80
+ export declare function mixHex(a: string, b: string, t: number): string;
81
+ /** Theme-aware colours for the shader tier. `strength` scales how far the
82
+ * field colours travel from the tint toward the ink. */
83
+ export declare function shaderPalette(tint: string, dark: boolean, strength: number): ShaderPalette;
84
+ /** Build one style's uniform set. Exported so a test can pin the mapping
85
+ * between the panel's 强度 knob and each shader's own parameter. */
86
+ export declare function shaderUniformsFor(style: ShaderStyle, palette: ShaderPalette, intensity: number): ShaderMountUniforms;
87
+ /** Host element (`div#vx-bg`) as the shader wrapper sees it. */
239
88
  export interface ShaderHostLike {
240
89
  readonly clientWidth: number;
241
90
  readonly clientHeight: number;
242
91
  getAttribute(name: string): string | null;
243
- appendChild(node: ShaderCanvasLike): unknown;
92
+ appendChild(node: ShaderElementLike): unknown;
244
93
  style: {
245
94
  getPropertyValue(name: string): string;
246
95
  };
247
96
  }
97
+ /** Wrapper element created inside the host. */
98
+ export interface ShaderElementLike {
99
+ readonly clientWidth: number;
100
+ readonly clientHeight: number;
101
+ style: Record<string, string>;
102
+ remove(): void;
103
+ }
248
104
  /** The slice of `Document` this renderer touches. */
249
105
  export interface ShaderDocLike {
250
- createElement(tag: string): ShaderCanvasLike;
251
- addEventListener(type: 'visibilitychange', listener: () => void): void;
252
- removeEventListener(type: 'visibilitychange', listener: () => void): void;
253
- readonly hidden: boolean;
106
+ createElement(tag: string): ShaderElementLike;
254
107
  }
255
- /** The slice of `MediaQueryList` this renderer touches. */
256
- export interface ShaderMediaQueryLike {
257
- readonly matches: boolean;
258
- addEventListener(type: 'change', listener: () => void): void;
259
- removeEventListener(type: 'change', listener: () => void): void;
260
- }
261
- /** The slice of `ResizeObserver` this renderer touches. */
262
- export interface ShaderResizeObserverLike {
263
- observe(target: unknown): void;
264
- disconnect(): void;
265
- }
266
- /** The slice of `Window` this renderer touches. Pointer listeners are optional
267
- * so a minimal test window without them still boots the tier (the eddy is
268
- * simply inert there). */
108
+ /** Kept for the appearance painter's structural typing; the mount owns window
109
+ * access, so only the pixel ratio is read here. */
269
110
  export interface ShaderWinLike {
270
- readonly devicePixelRatio: number;
271
- readonly innerWidth?: number;
272
- readonly innerHeight?: number;
273
- requestAnimationFrame(callback: (now: number) => void): number;
274
- cancelAnimationFrame(handle: number): void;
275
- matchMedia(query: string): ShaderMediaQueryLike;
276
- ResizeObserver?: new (callback: () => void) => ShaderResizeObserverLike;
277
- addEventListener?(type: string, listener: (event: BgPointerEventLike) => void, options?: {
278
- passive?: boolean;
279
- }): void;
280
- removeEventListener?(type: string, listener: (event: BgPointerEventLike) => void, options?: {
281
- passive?: boolean;
282
- }): void;
283
- }
284
- /** Observable renderer state; the live probe and tests read this. */
111
+ readonly devicePixelRatio?: number;
112
+ }
113
+ /** Backing-store pixel cap for the shader tier. A page background does not
114
+ * need the full 8.3M default; 4M keeps 1920×1080 crisp while roughly halving
115
+ * the fill cost on high-DPR screens. */
116
+ export declare const SHADER_MAX_PIXELS = 4000000;
117
+ /** Clamp the renderer's DPR to the device's own ratio (max 2×). Forcing 2× on
118
+ * a 1×/1.25× display only wastes fill rate for a soft background. */
119
+ export declare function resolveShaderDpr(win: ShaderWinLike): number;
120
+ /** The slice of a canvas event target the context-loss watcher needs. */
121
+ export interface ContextLossTarget {
122
+ addEventListener(type: string, listener: (event: Event) => void): void;
123
+ removeEventListener(type: string, listener: (event: Event) => void): void;
124
+ }
125
+ /** Watch for a lost GPU context. The handler prevents the browser's automatic
126
+ * restore (Paper's mount does not reinitialise its resources) and reports once;
127
+ * the returned function detaches the listener. */
128
+ export declare function attachContextLoss(target: ContextLossTarget, onLost: () => void): () => void;
129
+ /** Observable renderer state. */
285
130
  export interface ShaderBackdropStats {
286
131
  level: number;
287
132
  width: number;
@@ -290,7 +135,6 @@ export interface ShaderBackdropStats {
290
135
  alive: boolean;
291
136
  contextLost: boolean;
292
137
  style: ShaderStyle;
293
- /** Actual back-end string from `WEBGL_debug_renderer_info`, when exposed. */
294
138
  renderer: string | null;
295
139
  }
296
140
  /** Options accepted by {@link createShaderBackdrop}. */
@@ -299,31 +143,25 @@ export interface ShaderBackdropOptions {
299
143
  win: ShaderWinLike;
300
144
  host: ShaderHostLike;
301
145
  style?: ShaderStyle;
302
- /** Panel overrides for the boot style; clamps to its `ranges`. */
303
146
  params?: ShaderParams;
304
- /** Called once when the GPU drops this context. The caller falls back to
305
- * ASCII; this renderer never tries to recover on its own. */
147
+ /** Called once when the GPU drops this context; the caller falls back to the
148
+ * character tier. Paper's mount does not recover on its own. */
306
149
  onFallback?: (reason: 'context-lost') => void;
307
150
  }
308
151
  /** Handle owned by the appearance painter. */
309
152
  export interface ShaderBackdrop {
310
153
  sync(): void;
311
- /** Rebuild the program for another style. False means compile failed and
312
- * the previous style is still live, so the caller can fall back. */
154
+ /** Rebuild the mount for another style. False means WebGL2 creation failed
155
+ * and the caller should fall back to the character tier. */
313
156
  setStyle(style: ShaderStyle): boolean;
314
- /** Replace the panel overrides for the current style. Values are clamped
315
- * into that style's `ranges`; a style switch keeps the overrides and
316
- * re-clamps them. Repaints immediately so a dragging slider is visible. */
317
157
  setParams(params: ShaderParams): void;
318
158
  /** Move the calm zone (the reading column); `null` disables the mask. */
319
159
  setFocus(rect: BgFocusRect | null): void;
320
160
  destroy(): void;
321
161
  readonly stats: ShaderBackdropStats;
322
162
  }
323
- /**
324
- * Create the shader backdrop and attach its canvas. Returns null when WebGL1
325
- * is unavailable or the program does not compile; in that case the canvas is
326
- * removed and no context is left alive, and the caller falls back to ASCII and
327
- * tells the user.
328
- */
163
+ /** Create the shader tier. Returns null when WebGL2 is unavailable. */
329
164
  export declare function createShaderBackdrop(options: ShaderBackdropOptions): ShaderBackdrop | null;
165
+ /** Re-exported so the panel can mention the calm-zone constants without
166
+ * importing the focus module twice. */
167
+ export { FOCUS_FEATHER_PX, FOCUS_FLOOR };
@@ -13,6 +13,8 @@
13
13
  * 天青灰 leads the list: it is the install default and renders first among
14
14
  * the palette swatches, right after 无颜色. */
15
15
  export declare const PALETTE: Array<[string, string, string]>;
16
+ /** A surface counts as dark below this weighted brightness (0..255). */
17
+ export declare function isDarkSurface(hex: string): boolean;
16
18
  /** Parse a #rrggbb (or bare rrggbb) hex color; malformed input yields the
17
19
  * palette's neutral gray rather than throwing. */
18
20
  export declare function hexToRgb(hex: string): [number, number, number];