procedural-paper 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +205 -2
- package/THIRD_PARTY_NOTICES.md +51 -0
- package/dist/cards.d.ts +42 -0
- package/dist/color.d.ts +5 -0
- package/dist/composition/assets.d.ts +7 -0
- package/dist/composition/cords.d.ts +43 -0
- package/dist/composition/filters.d.ts +1 -0
- package/dist/composition/fit.d.ts +58 -0
- package/dist/composition/index.d.ts +27 -0
- package/dist/composition/index.js +1247 -0
- package/dist/composition/index.js.map +1 -0
- package/dist/composition/model.d.ts +72 -0
- package/dist/composition/motion-spec.d.ts +98 -0
- package/dist/composition/motion.d.ts +36 -0
- package/dist/composition/motion.js +291 -0
- package/dist/composition/motion.js.map +1 -0
- package/dist/composition/nodes.d.ts +7 -0
- package/dist/composition/pen.d.ts +6 -0
- package/dist/composition/photos.d.ts +41 -0
- package/dist/composition/props.d.ts +32 -0
- package/dist/composition/react.d.ts +10 -0
- package/dist/composition/react.js +22 -0
- package/dist/composition/react.js.map +1 -0
- package/dist/composition/renderer.d.ts +77 -0
- package/dist/composition/rng.d.ts +11 -0
- package/dist/composition/scene.d.ts +77 -0
- package/dist/composition/unrotate.d.ts +3 -0
- package/dist/composition.css +78 -0
- package/dist/cpu/compose.d.ts +7 -0
- package/dist/cpu/field.d.ts +10 -0
- package/dist/cpu/noise.d.ts +7 -0
- package/dist/cpu/overlays.d.ts +12 -0
- package/dist/cpu/renderer.d.ts +16 -0
- package/dist/cpu/strands.d.ts +33 -0
- package/dist/dom.d.ts +110 -0
- package/dist/dom.js +65 -0
- package/dist/dom.js.map +1 -0
- package/dist/export.d.ts +20 -0
- package/dist/gl/renderer.d.ts +61 -0
- package/dist/gl/shaders.d.ts +10 -0
- package/dist/gl/sheet-shader.d.ts +1 -0
- package/dist/hash.d.ts +12 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.js +2415 -0
- package/dist/index.js.map +1 -0
- package/dist/params.d.ts +169 -0
- package/dist/presets.d.ts +11 -0
- package/dist/react.d.ts +20 -0
- package/dist/react.js +50 -0
- package/dist/react.js.map +1 -0
- package/dist/resolve.d.ts +125 -0
- package/dist/sheet.d.ts +167 -0
- package/dist/stocks.d.ts +12 -0
- package/dist/styles.css +13 -0
- package/licenses/Apache-2.0.txt +201 -0
- package/package.json +116 -4
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { CardPresetId, DeepPartial, PaperParams, SheetSpec } from '../index';
|
|
2
|
+
import type { CordSpec } from './cords';
|
|
3
|
+
import type { PropSpec } from './props';
|
|
4
|
+
import type { PhotoSpec, Box } from './photos';
|
|
5
|
+
import type { MotionSpec } from './motion-spec';
|
|
6
|
+
export interface CardInfo {
|
|
7
|
+
kind: string;
|
|
8
|
+
paper: string;
|
|
9
|
+
fonts: string[];
|
|
10
|
+
attachments: string[];
|
|
11
|
+
}
|
|
12
|
+
export type SheetLook = Omit<SheetSpec, 'width' | 'height' | 'pixelRatio'>;
|
|
13
|
+
export type Attach = ({
|
|
14
|
+
type: 'prop';
|
|
15
|
+
x: number;
|
|
16
|
+
y: number;
|
|
17
|
+
layer: 'front' | 'back';
|
|
18
|
+
prop: PropSpec;
|
|
19
|
+
label: string;
|
|
20
|
+
} | {
|
|
21
|
+
type: 'sheet';
|
|
22
|
+
x: number;
|
|
23
|
+
y: number;
|
|
24
|
+
w: number;
|
|
25
|
+
h: number;
|
|
26
|
+
angle: number;
|
|
27
|
+
layer: 'front' | 'back';
|
|
28
|
+
paper: string;
|
|
29
|
+
params?: DeepPartial<PaperParams>;
|
|
30
|
+
look: SheetLook;
|
|
31
|
+
label: string;
|
|
32
|
+
} | {
|
|
33
|
+
type: 'html';
|
|
34
|
+
layer: 'front' | 'back';
|
|
35
|
+
el: HTMLElement;
|
|
36
|
+
label?: string;
|
|
37
|
+
}
|
|
38
|
+
/** A string / ribbon; renders its on-top strands in front of the sheet and the tucked-under parts behind. */
|
|
39
|
+
| {
|
|
40
|
+
type: 'cord';
|
|
41
|
+
spec: CordSpec;
|
|
42
|
+
label: string;
|
|
43
|
+
}
|
|
44
|
+
/** A photo print: a small sheet with a graded photo in its `window` (print px), centred at (cx, cy). */
|
|
45
|
+
| {
|
|
46
|
+
type: 'photo';
|
|
47
|
+
cx: number;
|
|
48
|
+
cy: number;
|
|
49
|
+
w: number;
|
|
50
|
+
h: number;
|
|
51
|
+
angle: number;
|
|
52
|
+
layer: 'front' | 'back';
|
|
53
|
+
paper: string;
|
|
54
|
+
params?: DeepPartial<PaperParams>;
|
|
55
|
+
look: SheetLook;
|
|
56
|
+
photo: PhotoSpec;
|
|
57
|
+
window: Box;
|
|
58
|
+
label: string;
|
|
59
|
+
}) & {
|
|
60
|
+
/** Only joins two things on the card (a photo taped onto a note): not how the card itself is held. */
|
|
61
|
+
binds?: boolean;
|
|
62
|
+
};
|
|
63
|
+
export interface CardPlan {
|
|
64
|
+
preset?: CardPresetId;
|
|
65
|
+
w: number;
|
|
66
|
+
h: number;
|
|
67
|
+
rot: number;
|
|
68
|
+
paper: string;
|
|
69
|
+
params?: DeepPartial<PaperParams>;
|
|
70
|
+
look: SheetLook;
|
|
71
|
+
/** Ink, rulings and print, positioned in card-local px. Null for blank paper. */
|
|
72
|
+
ink: HTMLElement | null;
|
|
73
|
+
attach: Attach[];
|
|
74
|
+
info: CardInfo;
|
|
75
|
+
/** How the ink animates once the card is on the desk (motion.ts). */
|
|
76
|
+
motion?: MotionSpec;
|
|
77
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
.pc-composition { position: relative; display: inline-block; vertical-align: top; line-height: 1.4; font-size: 13px; font-family: Inter, system-ui, sans-serif; color: #f1ede4; }
|
|
2
|
+
.pc-composition *, .pc-composition .pc-card * { box-sizing: border-box; }
|
|
3
|
+
.pc-composition .pc-card { position: absolute; transform-origin: 0 0; opacity: 1; }
|
|
4
|
+
.pc-composition .pc-card { position: absolute; line-height: 1.4; font-size: 13px; }
|
|
5
|
+
.pc-composition .pc-card > .shadow, .pc-composition .pc-card > .back, .pc-composition .pc-card > .front, .pc-composition .pc-card > .paper { position: absolute; inset: 0; pointer-events: none; }
|
|
6
|
+
.pc-composition .pc-card .paper { isolation: isolate; }
|
|
7
|
+
.pc-composition .pc-card .paper > canvas, .pc-composition .pc-card .front > canvas, .pc-composition .pc-card .back > canvas, .pc-composition .pc-card .shadow > canvas { position: absolute; pointer-events: none; max-width: none; }
|
|
8
|
+
.pc-composition .pc-card .shade { mix-blend-mode: hard-light; }
|
|
9
|
+
.pc-composition .pc-card .lifted, .pc-composition .pc-card .sheen { display: none; }
|
|
10
|
+
.pc-composition .pc-card .layer { pointer-events: auto; }
|
|
11
|
+
.pc-composition .pc-card .layer { user-select: text; }
|
|
12
|
+
.pc-composition .pc-card .develop { position: absolute; background: linear-gradient(160deg, #3c4a4b, #2c3537 60%, #4a4a3e); pointer-events: none; opacity: 0; }
|
|
13
|
+
:where(.pc-composition .pc-card) .layer { position: absolute; inset: 0; }
|
|
14
|
+
:where(.pc-composition .pc-card) .adhesive { position: absolute; left: 0; right: 0; top: 0; height: 18%; background: linear-gradient(rgba(0, 0, 0, 0.035), rgba(0, 0, 0, 0.012)); mix-blend-mode: multiply; }
|
|
15
|
+
:where(.pc-composition .pc-card) .print, :where(.pc-composition .pc-card) .postmark { position: absolute; mix-blend-mode: multiply; }
|
|
16
|
+
:where(.pc-composition .pc-card) .postmark { opacity: 0.55; filter: var(--pc-f-stamp, url(#f-stamp)); }
|
|
17
|
+
:where(.pc-composition .pc-card) .prop { position: absolute; cursor: grab; touch-action: none; user-select: none; pointer-events: none; }
|
|
18
|
+
:where(.pc-composition .pc-card) .prop canvas { position: absolute; inset: 0; pointer-events: none; }
|
|
19
|
+
:where(.pc-composition .pc-card) .prop-hit { position: absolute; inset: 0; overflow: visible; pointer-events: none; }
|
|
20
|
+
:where(.pc-composition .pc-card) .prop-hit path { fill: transparent; pointer-events: fill; }
|
|
21
|
+
:where(.pc-composition .pc-card) .desk.grid .prop { opacity: 0; pointer-events: none; transition: opacity 0.3s; }
|
|
22
|
+
:where(.pc-composition .pc-card) .desk.grid .prop-hit path { pointer-events: none; }
|
|
23
|
+
:where(.pc-composition .pc-card) .ink { position: absolute; mix-blend-mode: multiply; overflow-wrap: anywhere; }
|
|
24
|
+
:where(.pc-composition .pc-card) .ink.light { mix-blend-mode: normal; opacity: 0.92; }
|
|
25
|
+
:where(.pc-composition .pc-card) .ink .t { text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 3px; }
|
|
26
|
+
:where(.pc-composition .pc-card) .ink .done { text-decoration: line-through; opacity: 0.75; }
|
|
27
|
+
:where(.pc-composition .pc-card) .ink .t.nodeco, :where(.pc-composition .pc-card) .ink .done.nodeco { text-decoration: none; }
|
|
28
|
+
:where(.pc-composition .pc-card) .card.motion [data-mv] { will-change: transform, opacity; }
|
|
29
|
+
:where(.pc-composition .pc-card) .ink .r { text-align: right; }
|
|
30
|
+
:where(.pc-composition .pc-card) .ink .dim { opacity: 0.8; }
|
|
31
|
+
:where(.pc-composition .pc-card) .ink .num { color: #a02424; }
|
|
32
|
+
:where(.pc-composition .pc-card) .ink.fit { overflow: visible; }
|
|
33
|
+
:where(.pc-composition .pc-card) .ink.typed span.w { white-space: nowrap; }
|
|
34
|
+
:where(.pc-composition .pc-card) .ink .pen { display: block; }
|
|
35
|
+
:where(.pc-composition .pc-card) .ink.typed span span { position: relative; }
|
|
36
|
+
:where(.pc-composition .pc-card) .doodle { position: absolute; mix-blend-mode: multiply; pointer-events: none; }
|
|
37
|
+
:where(.pc-composition .pc-card) .doodle svg { display: block; overflow: visible; }
|
|
38
|
+
:where(.pc-composition .pc-card) .paint { position: absolute; inset: 0; mix-blend-mode: multiply; }
|
|
39
|
+
:where(.pc-composition .pc-card) .stain { position: absolute; mix-blend-mode: multiply; pointer-events: none; }
|
|
40
|
+
:where(.pc-composition .pc-card) .rules { position: absolute; inset: 0; pointer-events: none; mix-blend-mode: multiply; }
|
|
41
|
+
:where(.pc-composition .pc-card) .rules.index { top: 37.5px; background: repeating-linear-gradient(180deg, transparent 0 18.8px, rgba(92, 140, 206, 0.42) 18.8px 20px); }
|
|
42
|
+
:where(.pc-composition .pc-card) .rules.index::before { content: ''; position: absolute; left: 0; right: 0; top: -1.5px; height: 1.5px; background: rgba(214, 74, 82, 0.6); }
|
|
43
|
+
:where(.pc-composition .pc-card) .rules.notebook {
|
|
44
|
+
background:
|
|
45
|
+
linear-gradient(90deg, transparent 38px, rgba(214, 74, 82, 0.5) 38px, rgba(214, 74, 82, 0.5) 39.3px, transparent 39.3px),
|
|
46
|
+
repeating-linear-gradient(180deg, transparent 0, transparent 20.8px, rgba(92, 140, 206, 0.38) 20.8px, rgba(92, 140, 206, 0.38) 22px) 0 34px / 100% 22px repeat-y;
|
|
47
|
+
}
|
|
48
|
+
:where(.pc-composition .pc-card) .rules.dots { background: radial-gradient(circle, rgba(90, 90, 100, 0.45) 1px, transparent 1.4px) 6px 8px / 22px 22px; }
|
|
49
|
+
:where(.pc-composition .pc-card) .rules.graph {
|
|
50
|
+
--g: 60, 140, 120;
|
|
51
|
+
background:
|
|
52
|
+
linear-gradient(rgba(var(--g), 0.4) 1px, transparent 1px) 0 0 / 50px 50px,
|
|
53
|
+
linear-gradient(90deg, rgba(var(--g), 0.4) 1px, transparent 1px) 0 0 / 50px 50px,
|
|
54
|
+
linear-gradient(rgba(var(--g), 0.2) 1px, transparent 1px) 0 0 / 10px 10px,
|
|
55
|
+
linear-gradient(90deg, rgba(var(--g), 0.2) 1px, transparent 1px) 0 0 / 10px 10px;
|
|
56
|
+
}
|
|
57
|
+
:where(.pc-composition .pc-card) .emboss { position: absolute; inset: 9px; border: 3px dotted rgba(0, 0, 0, 0.05); border-radius: 4px; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.35); pointer-events: none; }
|
|
58
|
+
:where(.pc-composition .pc-card) .receipt .c { text-align: center; }
|
|
59
|
+
:where(.pc-composition .pc-card) .receipt .b { font-weight: 700; }
|
|
60
|
+
:where(.pc-composition .pc-card) .receipt .li { display: flex; justify-content: space-between; gap: 8px; }
|
|
61
|
+
:where(.pc-composition .pc-card) .receipt .hr { border-top: 1px dashed currentColor; margin: 7px 0; opacity: 0.6; }
|
|
62
|
+
:where(.pc-composition .pc-card) .barcode { margin: 12px auto 0; opacity: 0.9; }
|
|
63
|
+
:where(.pc-composition .pc-card) .biz { display: flex; flex-direction: column; justify-content: flex-end; }
|
|
64
|
+
:where(.pc-composition .pc-card) .biz.press { text-shadow: 0 1px 0 rgba(255, 255, 255, 0.35); }
|
|
65
|
+
:where(.pc-composition .pc-card) .biz .nm { }
|
|
66
|
+
:where(.pc-composition .pc-card) .biz .rl { opacity: 0.8; }
|
|
67
|
+
:where(.pc-composition .pc-card) .biz .ct { opacity: 0.7; }
|
|
68
|
+
:where(.pc-composition .pc-card) .mono-mark { width: 34px; height: 34px; border: 1.5px solid currentColor; display: grid; place-items: center; font-weight: 700; font-size: 1.1em; letter-spacing: 0.06em; text-transform: uppercase; }
|
|
69
|
+
:where(.pc-composition .pc-card) .news .hl { font-size: 1.85em; font-weight: 700; line-height: 1.05; letter-spacing: -0.01em; }
|
|
70
|
+
:where(.pc-composition .pc-card) .news .by { margin: 6px 0 8px; padding-bottom: 6px; border-bottom: 1px solid currentColor; font-size: 0.85em; opacity: 0.8; }
|
|
71
|
+
:where(.pc-composition .pc-card) .news .cols { columns: 2; column-gap: 12px; text-align: justify; hyphens: auto; line-height: 1.32; opacity: 0.88; }
|
|
72
|
+
:where(.pc-composition .pc-card) .photo-edge { position: absolute; pointer-events: none; box-shadow: inset 0 0 0 0.5px rgba(0, 0, 0, 0.35), inset 0.6px 1px 1.5px rgba(0, 0, 0, 0.35); }
|
|
73
|
+
:where(.pc-composition .pc-card) .pick { position: absolute; pointer-events: none; }
|
|
74
|
+
:where(.pc-composition .pc-card) .photo-corner { position: absolute; width: 0; height: 0; pointer-events: none; }
|
|
75
|
+
:where(.pc-composition .pc-card) .photo-corner svg { position: absolute; left: -4px; top: -4px; overflow: visible; filter: drop-shadow(0.6px 1.2px 0.8px rgba(0, 0, 0, 0.45)); }
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
.pc-composition .pc-card { animation: none; transition: none; cursor: default; touch-action: auto; }
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { OutputMode } from '../params';
|
|
2
|
+
import type { ResolvedPaper } from '../resolve';
|
|
3
|
+
/**
|
|
4
|
+
* Turn albedo + height (+ central-difference slopes hx, hy) into an output pixel.
|
|
5
|
+
* Mirrors COMPOSE_FS in gl/shaders.ts. Writes rgb (0..1) into `out`.
|
|
6
|
+
*/
|
|
7
|
+
export declare function composePixel(r: ResolvedPaper, mode: OutputMode, ar: number, ag: number, ab: number, h: number, hx: number, hy: number, out: Float64Array): void;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ResolvedPaper } from '../resolve';
|
|
2
|
+
export interface BaseSample {
|
|
3
|
+
r: number;
|
|
4
|
+
g: number;
|
|
5
|
+
b: number;
|
|
6
|
+
height: number;
|
|
7
|
+
}
|
|
8
|
+
export declare function structureHeight(r: ResolvedPaper, u: number, v: number): number;
|
|
9
|
+
/** Base albedo + height at pixel centre (px, py). */
|
|
10
|
+
export declare function baseSample(r: ResolvedPaper, px: number, py: number, out: BaseSample): BaseSample;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const imod: (a: number, b: number) => number;
|
|
2
|
+
/** Periodic gradient noise, roughly in [-1, 1]. */
|
|
3
|
+
export declare function gnoise(x: number, y: number, perX: number, perY: number, seed: number): number;
|
|
4
|
+
/** Periodic fractal Brownian motion, normalised to roughly [-1, 1]. */
|
|
5
|
+
export declare function fbm(x: number, y: number, perX: number, perY: number, octaves: number, persistence: number, seed: number): number;
|
|
6
|
+
/** Periodic Worley noise. Writes [smooth F1, F2] into `out` (smooth-min F1 avoids creases). */
|
|
7
|
+
export declare function worley(x: number, y: number, perX: number, perY: number, seed: number, out: Float64Array): void;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ResolvedPaper } from '../resolve';
|
|
2
|
+
/** Periodic faceted height in [-1, 1]; p in lattice units. */
|
|
3
|
+
export declare function crumple(px: number, py: number, perX: number, perY: number, seed: number): number;
|
|
4
|
+
export interface OverlaySample {
|
|
5
|
+
/** Height to add. */
|
|
6
|
+
dh: number;
|
|
7
|
+
/** Colour multiplier applied after fibers and specks. */
|
|
8
|
+
mr: number;
|
|
9
|
+
mg: number;
|
|
10
|
+
mb: number;
|
|
11
|
+
}
|
|
12
|
+
export declare function overlaySample(r: ResolvedPaper, px: number, py: number, o: OverlaySample): OverlaySample;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure-JS renderer. Same algorithm as the WebGL2 path, but fibers and specks
|
|
3
|
+
* are splatted per feature (fast on CPU) instead of gathered per pixel.
|
|
4
|
+
*/
|
|
5
|
+
import type { OutputMode } from '../params';
|
|
6
|
+
import type { ResolvedPaper } from '../resolve';
|
|
7
|
+
export interface Fields {
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
/** Unlit albedo, rgb interleaved. */
|
|
11
|
+
albedo: Float32Array;
|
|
12
|
+
height_: Float32Array;
|
|
13
|
+
}
|
|
14
|
+
export declare function computeFields(r: ResolvedPaper): Fields;
|
|
15
|
+
/** Render RGBA8 maps for the requested outputs. */
|
|
16
|
+
export declare function renderCPU(r: ResolvedPaper, outputs: OutputMode[]): Record<string, Uint8ClampedArray>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ResolvedPaper } from '../resolve';
|
|
2
|
+
export declare const FIBER_SEGMENTS = 6;
|
|
3
|
+
export interface Fiber {
|
|
4
|
+
/** Polyline of FIBER_SEGMENTS + 1 points (x0, y0, x1, y1, ...) in pixel space. */
|
|
5
|
+
pts: Float64Array;
|
|
6
|
+
halfWidth: number;
|
|
7
|
+
alpha: number;
|
|
8
|
+
dark: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface Speck {
|
|
11
|
+
cx: number;
|
|
12
|
+
cy: number;
|
|
13
|
+
cos: number;
|
|
14
|
+
sin: number;
|
|
15
|
+
/** Major / minor radius in px. */
|
|
16
|
+
ra: number;
|
|
17
|
+
rb: number;
|
|
18
|
+
ph1: number;
|
|
19
|
+
ph2: number;
|
|
20
|
+
color: number;
|
|
21
|
+
alpha: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Fiber in slot `k` of cell `n` (unwrapped cell index; `w` is the wrapped one).
|
|
25
|
+
* Returns false if the slot is empty.
|
|
26
|
+
*/
|
|
27
|
+
export declare function fiberAt(r: ResolvedPaper, wx: number, wy: number, nx: number, ny: number, k: number, f: Fiber): boolean;
|
|
28
|
+
/** Coverage (0..1, before alpha) of a fiber at pixel centre (px, py). */
|
|
29
|
+
export declare function fiberCoverage(f: Fiber, px: number, py: number): number;
|
|
30
|
+
export declare function speckAt(r: ResolvedPaper, wx: number, wy: number, nx: number, ny: number, k: number, s: Speck): boolean;
|
|
31
|
+
export declare function speckCoverage(s: Speck, px: number, py: number): number;
|
|
32
|
+
export declare function newFiber(): Fiber;
|
|
33
|
+
export declare function newSpeck(): Speck;
|
package/dist/dom.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { PaperRenderer, type DeepPartial, type PaperParams, type SheetSpec } from './index';
|
|
2
|
+
import { type CardOptions, type CardRegion } from './cards';
|
|
3
|
+
export interface PaperSheetOptions {
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
/** Texture preset (card preset IDs are used by createPaperCard instead). */
|
|
7
|
+
preset?: string;
|
|
8
|
+
params?: DeepPartial<PaperParams>;
|
|
9
|
+
seed?: number;
|
|
10
|
+
sheet?: Omit<SheetSpec, 'width' | 'height'>;
|
|
11
|
+
/** Borrowed renderers are never disposed by the component. */
|
|
12
|
+
renderer?: PaperRenderer;
|
|
13
|
+
}
|
|
14
|
+
export interface PaperCardOptions extends CardOptions {
|
|
15
|
+
renderer?: PaperRenderer;
|
|
16
|
+
}
|
|
17
|
+
export interface PaperLayers {
|
|
18
|
+
base: HTMLCanvasElement;
|
|
19
|
+
shade: HTMLCanvasElement;
|
|
20
|
+
shadow: HTMLCanvasElement;
|
|
21
|
+
/** Mask includes the transparent sheet margin. No object URLs to release. */
|
|
22
|
+
mask: string;
|
|
23
|
+
maskPosition: string;
|
|
24
|
+
maskSize: string;
|
|
25
|
+
}
|
|
26
|
+
/** Shared layer compositor, also used by the desk's interaction adapter. */
|
|
27
|
+
export declare function renderPaperLayers(renderer: PaperRenderer, params: PaperParams, spec: SheetSpec): PaperLayers;
|
|
28
|
+
export interface PaperComponent<T> {
|
|
29
|
+
element: HTMLDivElement;
|
|
30
|
+
/** Append DOM nodes here; updates never replace caller-owned children. */
|
|
31
|
+
slots: Record<string, HTMLDivElement>;
|
|
32
|
+
update(options: Partial<T>): void;
|
|
33
|
+
dispose(): void;
|
|
34
|
+
}
|
|
35
|
+
export declare function regionStyle(r: CardRegion): Record<string, string>;
|
|
36
|
+
export declare function resolveSheetOptions(options: PaperSheetOptions): {
|
|
37
|
+
params: PaperParams;
|
|
38
|
+
sheet: {
|
|
39
|
+
width: number;
|
|
40
|
+
height: number;
|
|
41
|
+
seed: number;
|
|
42
|
+
relief?: {
|
|
43
|
+
crumples?: number;
|
|
44
|
+
crumpleSize?: number;
|
|
45
|
+
wrinkles?: number;
|
|
46
|
+
wrinkleSize?: number;
|
|
47
|
+
folds?: {
|
|
48
|
+
x?: number;
|
|
49
|
+
y?: number;
|
|
50
|
+
strength?: number;
|
|
51
|
+
offset?: number;
|
|
52
|
+
width?: number;
|
|
53
|
+
wear?: number;
|
|
54
|
+
};
|
|
55
|
+
curl?: {
|
|
56
|
+
amount: number;
|
|
57
|
+
radius?: number;
|
|
58
|
+
corners?: import("./sheet").Corner[];
|
|
59
|
+
sides?: import("./sheet").Side[];
|
|
60
|
+
};
|
|
61
|
+
cockle?: number;
|
|
62
|
+
cockleSize?: number;
|
|
63
|
+
bend?: number;
|
|
64
|
+
bendAngle?: number;
|
|
65
|
+
};
|
|
66
|
+
shadow?: false | {
|
|
67
|
+
strength?: number;
|
|
68
|
+
softness?: number;
|
|
69
|
+
offsetX?: number;
|
|
70
|
+
offsetY?: number;
|
|
71
|
+
lift?: number;
|
|
72
|
+
};
|
|
73
|
+
pixelRatio?: number;
|
|
74
|
+
margin?: number;
|
|
75
|
+
shape?: {
|
|
76
|
+
radius?: number;
|
|
77
|
+
chamfer?: number;
|
|
78
|
+
edges?: Partial<Record<import("./sheet").Side, import("./sheet").EdgeKind | import("./sheet").EdgeSpec>>;
|
|
79
|
+
rim?: number;
|
|
80
|
+
holes?: import("./sheet").HoleSpec[];
|
|
81
|
+
holeRows?: import("./sheet").HoleRowSpec[];
|
|
82
|
+
};
|
|
83
|
+
finish?: {
|
|
84
|
+
opacity?: number;
|
|
85
|
+
gloss?: number;
|
|
86
|
+
edgeWear?: number;
|
|
87
|
+
aging?: number;
|
|
88
|
+
agingColor?: string;
|
|
89
|
+
pattern?: {
|
|
90
|
+
kind: "stripes" | "dots" | "grid" | "checker";
|
|
91
|
+
color: string;
|
|
92
|
+
scale?: number;
|
|
93
|
+
angle?: number;
|
|
94
|
+
width?: number;
|
|
95
|
+
opacity?: number;
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
regions: {
|
|
100
|
+
content: {
|
|
101
|
+
x: number;
|
|
102
|
+
y: number;
|
|
103
|
+
width: number;
|
|
104
|
+
height: number;
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
export declare const paperInk: (params: PaperParams) => "#f4f1e8" | "#242323";
|
|
109
|
+
export declare const createPaperSheet: (options: PaperSheetOptions) => PaperComponent<PaperSheetOptions>;
|
|
110
|
+
export declare const createPaperCard: (options: PaperCardOptions) => PaperComponent<PaperCardOptions>;
|
package/dist/dom.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { luminance as v, parseColor as $, buildParams as E, resolveCard as C, PaperRenderer as N } from "./index.js";
|
|
2
|
+
function P(e, h, r) {
|
|
3
|
+
const t = e.renderSheet(h, r, ["base", "shade", "shadow"]), n = t.pixelRatio, c = t.margin / n, a = (p) => {
|
|
4
|
+
const d = document.createElement("canvas");
|
|
5
|
+
return d.width = t.width, d.height = t.height, d.className = `pp-${p}`, d.setAttribute("aria-hidden", "true"), d.style.cssText = `position:absolute;pointer-events:none;left:${-c}px;top:${-c}px;width:${t.width / n}px;height:${t.height / n}px`, d.getContext("2d").putImageData(new ImageData(t.maps[p].data, t.width, t.height), 0, 0), d;
|
|
6
|
+
}, m = a("base"), l = a("shade"), i = a("shadow");
|
|
7
|
+
return l.style.mixBlendMode = "hard-light", { base: m, shade: l, shadow: i, mask: `url(${l.toDataURL()})`, maskPosition: `${-c}px ${-c}px`, maskSize: `${t.width / n}px ${t.height / n}px` };
|
|
8
|
+
}
|
|
9
|
+
function I(e) {
|
|
10
|
+
return { left: `${e.x}px`, top: `${e.y}px`, width: `${e.width}px`, height: `${e.height}px` };
|
|
11
|
+
}
|
|
12
|
+
function R(e) {
|
|
13
|
+
if (![e.width, e.height].every((h) => Number.isFinite(h) && h > 0)) throw new RangeError("Paper dimensions must be positive finite numbers");
|
|
14
|
+
return { params: E(e.preset, e.params, e.seed), sheet: { seed: e.seed ?? 1, ...e.sheet, width: e.width, height: e.height }, regions: { content: { x: 0, y: 0, width: e.width, height: e.height } } };
|
|
15
|
+
}
|
|
16
|
+
const S = (e) => v($(e.color.base)) < 0.45 ? "#f4f1e8" : "#242323";
|
|
17
|
+
function x(e, h) {
|
|
18
|
+
const r = document.createElement("div");
|
|
19
|
+
r.className = "pp-paper";
|
|
20
|
+
const t = document.createElement("div");
|
|
21
|
+
t.className = "pp-shadow-layer";
|
|
22
|
+
const n = document.createElement("div");
|
|
23
|
+
n.className = "pp-surface";
|
|
24
|
+
const c = document.createElement("div");
|
|
25
|
+
c.className = "pp-base-layer";
|
|
26
|
+
const a = document.createElement("div");
|
|
27
|
+
a.className = "pp-content";
|
|
28
|
+
const m = document.createElement("div");
|
|
29
|
+
m.className = "pp-ruling", m.setAttribute("aria-hidden", "true");
|
|
30
|
+
const l = document.createElement("div");
|
|
31
|
+
l.className = "pp-shade-layer", a.append(m), n.append(c, a, l), r.append(t, n);
|
|
32
|
+
const i = {};
|
|
33
|
+
let p = e, d, u = !1;
|
|
34
|
+
function f(k) {
|
|
35
|
+
if (u) throw new Error("procedural-paper: component has been disposed");
|
|
36
|
+
p = { ...p, ...k };
|
|
37
|
+
const g = h ? C(p) : R(p), { params: w, sheet: b, regions: y } = g;
|
|
38
|
+
r.style.width = `${b.width}px`, r.style.height = `${b.height}px`, r.style.color = S(w), n.style.backgroundColor = w.color.base, m.dataset.ruling = "preset" in g ? g.preset.ruling ?? "" : "";
|
|
39
|
+
for (const [s, o] of Object.entries(i)) o.hidden = !Object.hasOwn(y, s);
|
|
40
|
+
for (const [s, o] of Object.entries(y))
|
|
41
|
+
i[s] || (i[s] = document.createElement("div"), i[s].className = "pp-slot", i[s].dataset.slot = s, a.append(i[s])), i[s].hidden = !1, Object.assign(i[s].style, I(o));
|
|
42
|
+
c.replaceChildren(), l.replaceChildren(), t.replaceChildren(), a.style.maskImage = a.style.webkitMaskImage = "", r.dataset.rendered = "fallback";
|
|
43
|
+
try {
|
|
44
|
+
const s = p.renderer ?? (d ??= new N());
|
|
45
|
+
if (!s.gl) return;
|
|
46
|
+
const o = P(s, w, { pixelRatio: Math.min(2, window.devicePixelRatio || 1), ...b });
|
|
47
|
+
c.append(o.base), l.append(o.shade), t.append(o.shadow), a.style.maskImage = a.style.webkitMaskImage = o.mask, a.style.maskPosition = o.maskPosition, a.style.maskSize = o.maskSize, a.style.maskRepeat = "no-repeat", n.style.backgroundColor = "transparent", r.dataset.rendered = "webgl2";
|
|
48
|
+
} catch (s) {
|
|
49
|
+
r.dispatchEvent(new CustomEvent("papererror", { detail: s }));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return f({}), { element: r, slots: i, update: f, dispose() {
|
|
53
|
+
u || (u = !0, d?.dispose(), r.remove());
|
|
54
|
+
} };
|
|
55
|
+
}
|
|
56
|
+
const j = (e) => x(e, !1), M = (e) => x(e, !0);
|
|
57
|
+
export {
|
|
58
|
+
M as createPaperCard,
|
|
59
|
+
j as createPaperSheet,
|
|
60
|
+
S as paperInk,
|
|
61
|
+
I as regionStyle,
|
|
62
|
+
P as renderPaperLayers,
|
|
63
|
+
R as resolveSheetOptions
|
|
64
|
+
};
|
|
65
|
+
//# sourceMappingURL=dom.js.map
|
package/dist/dom.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dom.js","sources":["../src/dom.ts"],"sourcesContent":["import { PaperRenderer, buildParams, luminance, parseColor, type DeepPartial, type PaperParams, type SheetSpec } from './index';\nimport { resolveCard, type CardOptions, type CardRegion } from './cards';\n\nexport interface PaperSheetOptions {\n width: number;\n height: number;\n /** Texture preset (card preset IDs are used by createPaperCard instead). */\n preset?: string;\n params?: DeepPartial<PaperParams>;\n seed?: number;\n sheet?: Omit<SheetSpec, 'width' | 'height'>;\n /** Borrowed renderers are never disposed by the component. */\n renderer?: PaperRenderer;\n}\nexport interface PaperCardOptions extends CardOptions { renderer?: PaperRenderer }\nexport interface PaperLayers {\n base: HTMLCanvasElement;\n shade: HTMLCanvasElement;\n shadow: HTMLCanvasElement;\n /** Mask includes the transparent sheet margin. No object URLs to release. */\n mask: string;\n maskPosition: string;\n maskSize: string;\n}\n/** Shared layer compositor, also used by the desk's interaction adapter. */\nexport function renderPaperLayers(renderer: PaperRenderer, params: PaperParams, spec: SheetSpec): PaperLayers {\n const result = renderer.renderSheet(params, spec, ['base', 'shade', 'shadow']);\n const k = result.pixelRatio, margin = result.margin / k;\n const make = (name: 'base' | 'shade' | 'shadow') => {\n const canvas = document.createElement('canvas');\n canvas.width = result.width; canvas.height = result.height;\n canvas.className = `pp-${name}`;\n canvas.setAttribute('aria-hidden', 'true');\n canvas.style.cssText = `position:absolute;pointer-events:none;left:${-margin}px;top:${-margin}px;width:${result.width / k}px;height:${result.height / k}px`;\n canvas.getContext('2d')!.putImageData(new ImageData(result.maps[name]!.data as Uint8ClampedArray<ArrayBuffer>, result.width, result.height), 0, 0);\n return canvas;\n };\n const base = make('base'), shade = make('shade'), shadow = make('shadow');\n shade.style.mixBlendMode = 'hard-light';\n return { base, shade, shadow, mask: `url(${shade.toDataURL()})`, maskPosition: `${-margin}px ${-margin}px`, maskSize: `${result.width / k}px ${result.height / k}px` };\n}\nexport interface PaperComponent<T> {\n element: HTMLDivElement;\n /** Append DOM nodes here; updates never replace caller-owned children. */\n slots: Record<string, HTMLDivElement>;\n update(options: Partial<T>): void;\n dispose(): void;\n}\nexport function regionStyle(r: CardRegion): Record<string, string> {\n return { left: `${r.x}px`, top: `${r.y}px`, width: `${r.width}px`, height: `${r.height}px` };\n}\nexport function resolveSheetOptions(options: PaperSheetOptions) {\n if (![options.width, options.height].every(n => Number.isFinite(n) && n > 0)) throw new RangeError('Paper dimensions must be positive finite numbers');\n return { params: buildParams(options.preset, options.params, options.seed), sheet: { seed: options.seed ?? 1, ...options.sheet, width: options.width, height: options.height }, regions: { content: { x: 0, y: 0, width: options.width, height: options.height } } };\n}\nexport const paperInk = (params: PaperParams) => luminance(parseColor(params.color.base)) < 0.45 ? '#f4f1e8' : '#242323';\n\nfunction create<T extends PaperSheetOptions | PaperCardOptions>(initial: T, isCard: boolean): PaperComponent<T> {\n const element = document.createElement('div');\n element.className = 'pp-paper';\n const shadow = document.createElement('div'); shadow.className = 'pp-shadow-layer';\n const surface = document.createElement('div'); surface.className = 'pp-surface';\n const base = document.createElement('div'); base.className = 'pp-base-layer';\n const content = document.createElement('div'); content.className = 'pp-content';\n const ruling = document.createElement('div'); ruling.className = 'pp-ruling'; ruling.setAttribute('aria-hidden', 'true');\n const shade = document.createElement('div'); shade.className = 'pp-shade-layer';\n content.append(ruling); surface.append(base, content, shade); element.append(shadow, surface);\n const slots: Record<string, HTMLDivElement> = {};\n let options = initial, owned: PaperRenderer | undefined, disposed = false;\n function update(patch: Partial<T>) {\n if (disposed) throw new Error('procedural-paper: component has been disposed');\n options = { ...options, ...patch };\n const resolved = isCard ? resolveCard(options as PaperCardOptions) : resolveSheetOptions(options as PaperSheetOptions);\n const { params, sheet, regions } = resolved;\n element.style.width = `${sheet.width}px`; element.style.height = `${sheet.height}px`;\n element.style.color = paperInk(params);\n surface.style.backgroundColor = params.color.base;\n ruling.dataset.ruling = 'preset' in resolved ? resolved.preset.ruling ?? '' : '';\n for (const [name, slot] of Object.entries(slots)) slot.hidden = !Object.hasOwn(regions, name);\n for (const [name, r] of Object.entries(regions)) {\n if (!slots[name]) { slots[name] = document.createElement('div'); slots[name].className = 'pp-slot'; slots[name].dataset.slot = name; content.append(slots[name]); }\n slots[name].hidden = false; Object.assign(slots[name].style, regionStyle(r));\n }\n base.replaceChildren(); shade.replaceChildren(); shadow.replaceChildren();\n content.style.maskImage = content.style.webkitMaskImage = '';\n element.dataset.rendered = 'fallback';\n try {\n const renderer = options.renderer ?? (owned ??= new PaperRenderer());\n if (!renderer.gl) return;\n const layers = renderPaperLayers(renderer, params, { pixelRatio: Math.min(2, window.devicePixelRatio || 1), ...sheet });\n base.append(layers.base); shade.append(layers.shade); shadow.append(layers.shadow);\n content.style.maskImage = content.style.webkitMaskImage = layers.mask;\n content.style.maskPosition = layers.maskPosition; content.style.maskSize = layers.maskSize; content.style.maskRepeat = 'no-repeat';\n surface.style.backgroundColor = 'transparent'; element.dataset.rendered = 'webgl2';\n } catch (error) {\n // Keep caller content readable if the GPU becomes unavailable.\n element.dispatchEvent(new CustomEvent('papererror', { detail: error }));\n }\n }\n update({});\n return { element, slots, update, dispose() { if (disposed) return; disposed = true; owned?.dispose(); element.remove(); } };\n}\nexport const createPaperSheet = (options: PaperSheetOptions): PaperComponent<PaperSheetOptions> => create(options, false);\nexport const createPaperCard = (options: PaperCardOptions): PaperComponent<PaperCardOptions> => create(options, true);\n"],"names":["renderPaperLayers","renderer","params","spec","result","k","margin","make","name","canvas","base","shade","shadow","regionStyle","r","resolveSheetOptions","options","n","buildParams","paperInk","luminance","parseColor","create","initial","isCard","element","surface","content","ruling","slots","owned","disposed","update","patch","resolved","resolveCard","sheet","regions","slot","PaperRenderer","layers","error","createPaperSheet","createPaperCard"],"mappings":"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"}
|
package/dist/export.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Output helpers that work in browsers, workers and Node 18+. */
|
|
2
|
+
export interface PixelMap {
|
|
3
|
+
width: number;
|
|
4
|
+
height: number;
|
|
5
|
+
/** RGBA8, rows top to bottom. */
|
|
6
|
+
data: Uint8ClampedArray;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Encode a map as PNG bytes. Grayscale maps (height/overlay) are written as
|
|
10
|
+
* 8-bit gray, others as RGB (paper is always opaque).
|
|
11
|
+
*/
|
|
12
|
+
export declare function encodePNG(map: PixelMap, opts?: {
|
|
13
|
+
grayscale?: boolean;
|
|
14
|
+
}): Promise<Uint8Array>;
|
|
15
|
+
export declare function toBlob(map: PixelMap): Promise<Blob>;
|
|
16
|
+
export declare function toDataURL(map: PixelMap): Promise<string>;
|
|
17
|
+
export declare function toImageData(map: PixelMap): ImageData;
|
|
18
|
+
export declare function toCanvas(map: PixelMap, canvas?: HTMLCanvasElement): HTMLCanvasElement;
|
|
19
|
+
/** CSS `background` value for a tiling paper (uses a blob: URL; revoke when done). */
|
|
20
|
+
export declare function toCSSBackground(map: PixelMap, displayScale?: number): Promise<string>;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { OutputMode } from '../params';
|
|
2
|
+
import type { ResolvedPaper } from '../resolve';
|
|
3
|
+
import type { ResolvedSheet, SheetOutput } from '../sheet';
|
|
4
|
+
export type GLCanvas = HTMLCanvasElement | OffscreenCanvas;
|
|
5
|
+
export interface PreviewView {
|
|
6
|
+
zoom?: number;
|
|
7
|
+
offsetX?: number;
|
|
8
|
+
offsetY?: number;
|
|
9
|
+
seams?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* WebGL2 renderer. Reuse one instance for interactive editing; it keeps its
|
|
13
|
+
* context, programs and textures alive between renders.
|
|
14
|
+
*/
|
|
15
|
+
export declare class GLPaperRenderer {
|
|
16
|
+
readonly canvas: GLCanvas;
|
|
17
|
+
readonly gl: WebGL2RenderingContext;
|
|
18
|
+
readonly maxSize: number;
|
|
19
|
+
private fieldProg;
|
|
20
|
+
private composeProg;
|
|
21
|
+
private displayProg;
|
|
22
|
+
private sheetProg;
|
|
23
|
+
private vao;
|
|
24
|
+
private fieldFormat;
|
|
25
|
+
private fieldTex;
|
|
26
|
+
private fieldSize;
|
|
27
|
+
private outTex;
|
|
28
|
+
private outSize;
|
|
29
|
+
private fbo;
|
|
30
|
+
/** Size of the texture produced by the last renderToTexture(). */
|
|
31
|
+
lastSize: [number, number];
|
|
32
|
+
static isSupported(): boolean;
|
|
33
|
+
constructor(canvas?: GLCanvas);
|
|
34
|
+
private program;
|
|
35
|
+
private u;
|
|
36
|
+
private ensureTex;
|
|
37
|
+
private setFieldUniforms;
|
|
38
|
+
private setComposeUniforms;
|
|
39
|
+
/** Render the 1px-padded field for the chunk at (ox, oy) of size (w, h). */
|
|
40
|
+
private renderField;
|
|
41
|
+
private composeInto;
|
|
42
|
+
/**
|
|
43
|
+
* Render the requested maps to RGBA8 pixel buffers (rows top to bottom).
|
|
44
|
+
* Large tiles are rendered in chunks, so sizes beyond MAX_TEXTURE_SIZE work.
|
|
45
|
+
*/
|
|
46
|
+
render(r: ResolvedPaper, outputs: OutputMode[], chunk?: number): Record<string, Uint8ClampedArray>;
|
|
47
|
+
/**
|
|
48
|
+
* Render one map into an internal GPU texture (no readback). Used for live
|
|
49
|
+
* previews together with drawPreview(). Size must fit MAX_TEXTURE_SIZE.
|
|
50
|
+
*/
|
|
51
|
+
renderToTexture(r: ResolvedPaper, mode: OutputMode): WebGLTexture;
|
|
52
|
+
/**
|
|
53
|
+
* Render a finite sheet (see sheet.ts): the paper field at the sheet's size,
|
|
54
|
+
* then the outline / relief / shadow pass. Returns RGBA8 buffers (straight
|
|
55
|
+
* alpha, rows top to bottom) of size (width + 2 margin) x (height + 2 margin).
|
|
56
|
+
*/
|
|
57
|
+
renderSheet(r: ResolvedPaper, s: ResolvedSheet, outputs: SheetOutput[]): Record<string, Uint8ClampedArray>;
|
|
58
|
+
/** Draw the last renderToTexture() result to the canvas, tiled infinitely. */
|
|
59
|
+
drawPreview(view?: PreviewView): void;
|
|
60
|
+
dispose(): void;
|
|
61
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GLSL ES 3.00 twins of src/cpu/*. Keep in sync with:
|
|
3
|
+
* hash.ts (pcg, hash2, u01), cpu/noise.ts, cpu/field.ts, cpu/strands.ts, cpu/compose.ts
|
|
4
|
+
*/
|
|
5
|
+
export declare const VS = "#version 300 es\nin vec2 aPos;\nvoid main() { gl_Position = vec4(aPos, 0.0, 1.0); }\n";
|
|
6
|
+
export declare const COMMON = "\nprecision highp float;\nprecision highp int;\nconst float TAU = 6.283185307179586;\n\nuint pcg(uint v) {\n uint state = v * 747796405u + 2891336453u;\n uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;\n return (word >> 22u) ^ word;\n}\nuint hash2(int x, int y, uint seed) { return pcg(uint(x) + pcg(uint(y) + pcg(seed))); }\nfloat u01(uint h) { return float(h >> 8u) * (1.0 / 16777216.0); }\nint imod(int a, int b) { return a - b * int(floor(float(a) / float(b))); }\nivec2 imod2(ivec2 a, ivec2 b) { return ivec2(imod(a.x, b.x), imod(a.y, b.y)); }\n\nfloat corner(ivec2 c, ivec2 per, uint seed, vec2 d) {\n ivec2 w = imod2(c, per);\n uint h = hash2(w.x, w.y, seed) & 31u;\n float a = float(h) * (TAU / 32.0);\n return cos(a) * d.x + sin(a) * d.y;\n}\nfloat fade(float t) { return t * t * t * (t * (t * 6.0 - 15.0) + 10.0); }\n\nfloat gnoise(vec2 p, ivec2 per, uint seed) {\n vec2 i = floor(p);\n vec2 f = p - i;\n ivec2 c = ivec2(i);\n float a = corner(c, per, seed, f);\n float b = corner(c + ivec2(1, 0), per, seed, f - vec2(1.0, 0.0));\n float cc = corner(c + ivec2(0, 1), per, seed, f - vec2(0.0, 1.0));\n float d = corner(c + ivec2(1, 1), per, seed, f - vec2(1.0, 1.0));\n vec2 u = vec2(fade(f.x), fade(f.y));\n float ab = mix(a, b, u.x);\n float cd = mix(cc, d, u.x);\n return mix(ab, cd, u.y) * 1.4142;\n}\n\nfloat fbm(vec2 p, ivec2 per, int octaves, float persistence, uint seed) {\n float sum = 0.0, amp = 1.0, norm = 0.0, f = 1.0;\n for (int o = 0; o < 12; o++) {\n if (o >= octaves) break;\n int fi = int(f);\n sum += amp * gnoise(p * f, per * fi, seed + uint(o));\n norm += amp;\n amp *= persistence;\n f *= 2.0;\n }\n return sum / norm;\n}\n\nvec2 worley(vec2 p, ivec2 per, uint seed) {\n ivec2 c = ivec2(floor(p));\n float f1 = 9.0, f2 = 9.0, acc = 0.0;\n for (int j = -1; j <= 1; j++) {\n for (int i = -1; i <= 1; i++) {\n ivec2 n = c + ivec2(i, j);\n ivec2 w = imod2(n, per);\n uint h = hash2(w.x, w.y, seed);\n vec2 pt = vec2(n) + vec2(u01(h), u01(pcg(h)));\n float d = length(pt - p);\n acc += exp(-10.0 * d);\n if (d < f1) { f2 = f1; f1 = d; } else if (d < f2) { f2 = d; }\n }\n }\n return vec2(max(0.0, -log(acc) / 10.0), f2);\n}\n";
|
|
7
|
+
/** Sheet-scale overlays; twins of cpu/overlays.ts. */
|
|
8
|
+
export declare const OVERLAYS = "\nvec3 crV[9];\nvoid crVertex(ivec2 n, ivec2 per, uint seed, int k) {\n ivec2 w = imod2(n, per);\n uint h = hash2(w.x, w.y, seed);\n float jx = u01(h); h = pcg(h);\n float jy = u01(h); h = pcg(h);\n crV[k] = vec3(vec2(n) + 0.5 + (vec2(jx, jy) - 0.5) * 0.7, u01(h) * 2.0 - 1.0);\n}\nbool crTri(vec2 p, int a, int b, int c, out float h) {\n vec2 v0 = crV[b].xy - crV[a].xy, v1 = crV[c].xy - crV[a].xy, v2 = p - crV[a].xy;\n float d = v0.x * v1.y - v1.x * v0.y;\n float u = (v2.x * v1.y - v1.x * v2.y) / d;\n float v = (v0.x * v2.y - v2.x * v0.y) / d;\n h = crV[a].z + u * (crV[b].z - crV[a].z) + v * (crV[c].z - crV[a].z);\n return u >= -1e-5 && v >= -1e-5 && u + v <= 1.0 + 1e-5;\n}\n// periodic faceted height in [-1, 1]; p in lattice units\nfloat crumple(vec2 p, ivec2 per, uint seed) {\n ivec2 c = ivec2(floor(p));\n for (int j = 0; j < 3; j++) for (int i = 0; i < 3; i++) crVertex(c + ivec2(i - 1, j - 1), per, seed, j * 3 + i);\n float h;\n for (int qy = 0; qy < 2; qy++) for (int qx = 0; qx < 2; qx++) {\n int a = qy * 3 + qx, b = a + 1, cc = a + 3, d = a + 4;\n ivec2 qw = imod2(c + ivec2(qx - 1, qy - 1), per);\n uint diag = hash2(qw.x, qw.y, seed + 7u) & 1u;\n if (diag == 0u) {\n if (crTri(p, a, b, d, h)) return h;\n if (crTri(p, a, d, cc, h)) return h;\n } else {\n if (crTri(p, a, b, cc, h)) return h;\n if (crTri(p, b, d, cc, h)) return h;\n }\n }\n return 0.0;\n}\nfloat foldAxis(float x, float L, int count, float offset, uint seed, float strength, float width, out float dist) {\n float s = L / float(count);\n float t = x / s - offset;\n float k = floor(t);\n float f = t - k;\n int k0 = imod(int(k), count);\n int k1 = imod(int(k) + 1, count);\n float h0 = u01(hash2(k0, 0, seed)) * 2.0 - 1.0;\n float h1 = u01(hash2(k1, 0, seed)) * 2.0 - 1.0;\n float panel = h0 + (h1 - h0) * f;\n float dn = min(f, 1.0 - f) * s;\n int kn = f < 0.5 ? k0 : k1;\n float sgn = u01(pcg(hash2(kn, 0, seed))) < 0.5 ? -1.0 : 1.0;\n float rr = max(0.0, 1.0 - dn / width);\n dist = dn;\n return strength * (panel * s * 0.12 + sgn * rr * rr * width * 0.3);\n}\n";
|
|
9
|
+
export declare const FIELD_FS = "#version 300 es\n\nprecision highp float;\nprecision highp int;\nconst float TAU = 6.283185307179586;\n\nuint pcg(uint v) {\n uint state = v * 747796405u + 2891336453u;\n uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;\n return (word >> 22u) ^ word;\n}\nuint hash2(int x, int y, uint seed) { return pcg(uint(x) + pcg(uint(y) + pcg(seed))); }\nfloat u01(uint h) { return float(h >> 8u) * (1.0 / 16777216.0); }\nint imod(int a, int b) { return a - b * int(floor(float(a) / float(b))); }\nivec2 imod2(ivec2 a, ivec2 b) { return ivec2(imod(a.x, b.x), imod(a.y, b.y)); }\n\nfloat corner(ivec2 c, ivec2 per, uint seed, vec2 d) {\n ivec2 w = imod2(c, per);\n uint h = hash2(w.x, w.y, seed) & 31u;\n float a = float(h) * (TAU / 32.0);\n return cos(a) * d.x + sin(a) * d.y;\n}\nfloat fade(float t) { return t * t * t * (t * (t * 6.0 - 15.0) + 10.0); }\n\nfloat gnoise(vec2 p, ivec2 per, uint seed) {\n vec2 i = floor(p);\n vec2 f = p - i;\n ivec2 c = ivec2(i);\n float a = corner(c, per, seed, f);\n float b = corner(c + ivec2(1, 0), per, seed, f - vec2(1.0, 0.0));\n float cc = corner(c + ivec2(0, 1), per, seed, f - vec2(0.0, 1.0));\n float d = corner(c + ivec2(1, 1), per, seed, f - vec2(1.0, 1.0));\n vec2 u = vec2(fade(f.x), fade(f.y));\n float ab = mix(a, b, u.x);\n float cd = mix(cc, d, u.x);\n return mix(ab, cd, u.y) * 1.4142;\n}\n\nfloat fbm(vec2 p, ivec2 per, int octaves, float persistence, uint seed) {\n float sum = 0.0, amp = 1.0, norm = 0.0, f = 1.0;\n for (int o = 0; o < 12; o++) {\n if (o >= octaves) break;\n int fi = int(f);\n sum += amp * gnoise(p * f, per * fi, seed + uint(o));\n norm += amp;\n amp *= persistence;\n f *= 2.0;\n }\n return sum / norm;\n}\n\nvec2 worley(vec2 p, ivec2 per, uint seed) {\n ivec2 c = ivec2(floor(p));\n float f1 = 9.0, f2 = 9.0, acc = 0.0;\n for (int j = -1; j <= 1; j++) {\n for (int i = -1; i <= 1; i++) {\n ivec2 n = c + ivec2(i, j);\n ivec2 w = imod2(n, per);\n uint h = hash2(w.x, w.y, seed);\n vec2 pt = vec2(n) + vec2(u01(h), u01(pcg(h)));\n float d = length(pt - p);\n acc += exp(-10.0 * d);\n if (d < f1) { f2 = f1; f1 = d; } else if (d < f2) { f2 = d; }\n }\n }\n return vec2(max(0.0, -log(acc) / 10.0), f2);\n}\n\n\nvec3 crV[9];\nvoid crVertex(ivec2 n, ivec2 per, uint seed, int k) {\n ivec2 w = imod2(n, per);\n uint h = hash2(w.x, w.y, seed);\n float jx = u01(h); h = pcg(h);\n float jy = u01(h); h = pcg(h);\n crV[k] = vec3(vec2(n) + 0.5 + (vec2(jx, jy) - 0.5) * 0.7, u01(h) * 2.0 - 1.0);\n}\nbool crTri(vec2 p, int a, int b, int c, out float h) {\n vec2 v0 = crV[b].xy - crV[a].xy, v1 = crV[c].xy - crV[a].xy, v2 = p - crV[a].xy;\n float d = v0.x * v1.y - v1.x * v0.y;\n float u = (v2.x * v1.y - v1.x * v2.y) / d;\n float v = (v0.x * v2.y - v2.x * v0.y) / d;\n h = crV[a].z + u * (crV[b].z - crV[a].z) + v * (crV[c].z - crV[a].z);\n return u >= -1e-5 && v >= -1e-5 && u + v <= 1.0 + 1e-5;\n}\n// periodic faceted height in [-1, 1]; p in lattice units\nfloat crumple(vec2 p, ivec2 per, uint seed) {\n ivec2 c = ivec2(floor(p));\n for (int j = 0; j < 3; j++) for (int i = 0; i < 3; i++) crVertex(c + ivec2(i - 1, j - 1), per, seed, j * 3 + i);\n float h;\n for (int qy = 0; qy < 2; qy++) for (int qx = 0; qx < 2; qx++) {\n int a = qy * 3 + qx, b = a + 1, cc = a + 3, d = a + 4;\n ivec2 qw = imod2(c + ivec2(qx - 1, qy - 1), per);\n uint diag = hash2(qw.x, qw.y, seed + 7u) & 1u;\n if (diag == 0u) {\n if (crTri(p, a, b, d, h)) return h;\n if (crTri(p, a, d, cc, h)) return h;\n } else {\n if (crTri(p, a, b, cc, h)) return h;\n if (crTri(p, b, d, cc, h)) return h;\n }\n }\n return 0.0;\n}\nfloat foldAxis(float x, float L, int count, float offset, uint seed, float strength, float width, out float dist) {\n float s = L / float(count);\n float t = x / s - offset;\n float k = floor(t);\n float f = t - k;\n int k0 = imod(int(k), count);\n int k1 = imod(int(k) + 1, count);\n float h0 = u01(hash2(k0, 0, seed)) * 2.0 - 1.0;\n float h1 = u01(hash2(k1, 0, seed)) * 2.0 - 1.0;\n float panel = h0 + (h1 - h0) * f;\n float dn = min(f, 1.0 - f) * s;\n int kn = f < 0.5 ? k0 : k1;\n float sgn = u01(pcg(hash2(kn, 0, seed))) < 0.5 ? -1.0 : 1.0;\n float rr = max(0.0, 1.0 - dn / width);\n dist = dn;\n return strength * (panel * s * 0.12 + sgn * rr * rr * width * 0.3);\n}\n\nuniform ivec2 uSize; // tile size\nuniform ivec2 uOrigin; // absolute pixel of this target's (0,0)\n\nuniform vec3 uBase, uTint;\nuniform float uTintAmount, uVariation, uGrainTone;\n\nuniform uint uMoSeed, uMoTintSeed, uMoWarpSeed;\nuniform ivec2 uMoPer; uniform int uMoOct; uniform float uMoWarp, uMoRelief;\n\nuniform uint uGrSeed; uniform ivec2 uGrPer; uniform int uGrOct; uniform float uGrRough, uGrStrength;\n\nuniform uint uCeSeed, uCeWarpSeed; uniform ivec2 uCePer; uniform float uCeStrength, uCeRidge, uCeWarp;\n\nuniform uint uSkSeed; uniform ivec2 uSkPer; uniform int uSkOct; uniform float uSkStrength;\n\nuniform int uStType; uniform uint uStSeed; uniform ivec2 uStPer;\nuniform float uStStrength, uStSharp, uStWarp; uniform bool uStVertical;\n\nuniform uint uFbSeed; uniform ivec2 uFbPer; uniform vec2 uFbCell; uniform float uFbCount;\nuniform float uFbLength, uFbWidth, uFbCurl, uFbDarkRatio, uFbOpacity, uFbRelief;\nuniform vec3 uFbDark, uFbLight;\n\nuniform uint uSpSeed; uniform ivec2 uSpPer; uniform vec2 uSpCell; uniform float uSpCount;\nuniform float uSpMin, uSpMax, uSpElong, uSpOpacity, uSpRelief;\nuniform vec3 uSpC0, uSpC1, uSpC2;\n\nuniform uint uCrSeed, uCrWarpSeed; uniform ivec2 uCrPer, uCrWPer; uniform float uCrAmp, uCrWAmp, uCrWarp;\nuniform uint uFoSeed; uniform ivec2 uFoCount; uniform float uFoOffset, uFoStrength, uFoWidth, uFoWear;\nuniform uint uSnSeed; uniform ivec2 uSnPer; uniform vec2 uSnCell; uniform float uSnDensity, uSnSize, uSnStrength, uSnRing; uniform vec3 uSnColor;\n\nout vec4 outField;\n\n// returns colour multiplier; adds relief to dh\nvec3 overlays(vec2 p, float u, float v, inout float dh) {\n vec3 mul = vec3(1.0);\n if (uCrAmp > 0.0 || uCrWAmp > 0.0) {\n float wu = u, wv = v;\n if (uCrWarp > 0.0) {\n float k = uCrWarp / (float(uCrPer.x + uCrPer.y) * 0.5);\n wu += k * gnoise(vec2(u, v) * vec2(uCrPer), uCrPer, uCrWarpSeed);\n wv += k * gnoise(vec2(u, v) * vec2(uCrPer) + vec2(5.2, 1.3), uCrPer, uCrWarpSeed);\n }\n if (uCrAmp > 0.0) dh += uCrAmp * crumple(vec2(wu, wv) * vec2(uCrPer), uCrPer, uCrSeed);\n if (uCrWAmp > 0.0) {\n dh += uCrWAmp * (crumple(vec2(wu, wv) * vec2(uCrWPer), uCrWPer, uCrSeed + 1u)\n + 0.5 * crumple(vec2(wu, wv) * vec2(uCrWPer * 2), uCrWPer * 2, uCrSeed + 2u));\n }\n }\n if (uFoStrength > 0.0 || uFoWear > 0.0) {\n float wear = 0.0, d;\n if (uFoCount.x > 0) {\n dh += foldAxis(p.x, float(uSize.x), uFoCount.x, uFoOffset, uFoSeed, uFoStrength, uFoWidth, d);\n wear = max(wear, exp(-(d / 1.3) * (d / 1.3)));\n }\n if (uFoCount.y > 0) {\n dh += foldAxis(p.y, float(uSize.y), uFoCount.y, uFoOffset, uFoSeed + 1u, uFoStrength, uFoWidth, d);\n wear = max(wear, exp(-(d / 1.3) * (d / 1.3)));\n }\n mul *= 1.0 - uFoWear * 0.22 * wear;\n }\n if (uSnDensity > 0.0 && uSnStrength > 0.0) {\n ivec2 c = ivec2(floor(p / uSnCell));\n float keep = 1.0;\n for (int j = -1; j <= 1; j++) for (int i = -1; i <= 1; i++) {\n ivec2 n = c + ivec2(i, j);\n ivec2 w = imod2(n, uSnPer);\n uint h = hash2(w.x, w.y, uSnSeed);\n if (u01(h) >= uSnDensity) continue;\n h = pcg(h); float r1 = u01(h);\n h = pcg(h); float r2 = u01(h);\n h = pcg(h); float r3 = u01(h);\n h = pcg(h); float r4 = u01(h);\n h = pcg(h); float r5 = u01(h);\n h = pcg(h); float r6 = u01(h);\n vec2 cen = (vec2(n) + 0.2 + 0.6 * vec2(r1, r2)) * uSnCell;\n float R = uSnSize * (0.55 + 0.55 * r3);\n vec2 dd = p - cen;\n float L = length(dd);\n if (L > R * 1.3) continue;\n float a = atan(dd.y, dd.x);\n float e = 1.0 + 0.035 * sin(3.0 * a + r4 * TAU) + 0.02 * sin(7.0 * a + r5 * TAU);\n float q = L / (R * e);\n float inside = clamp((1.0 - q) * R * e + 0.5, 0.0, 1.0);\n float t = (q - 0.985) / 0.03;\n float ring = exp(-t * t) * (0.35 + 0.65 * smoothstep(-0.4, 0.6, sin(a + r6 * TAU)));\n float alpha = clamp(inside * (0.1 + 0.14 * q * q) + ring * uSnRing, 0.0, 1.0) * uSnStrength;\n keep *= 1.0 - alpha;\n dh += 0.15 * ring * uSnStrength;\n }\n mul *= 1.0 + (uSnColor - 1.0) * (1.0 - keep);\n }\n return mul;\n}\n\nfloat structureHeight(float u, float v) {\n float a = uStVertical ? u : v;\n float b = uStVertical ? v : u;\n float mainLen = float(uStVertical ? uSize.x : uSize.y);\n float crossLen = float(uStVertical ? uSize.y : uSize.x);\n int wpa = max(1, int(round(float(uStPer.x) / 4.0)));\n int wpb = max(1, int(round(float(uStPer.y) / 4.0)));\n float A = a * float(uStPer.x) + uStWarp * 0.6 * gnoise(vec2(a * float(wpa), b * float(wpb)), ivec2(wpa, wpb), uStSeed);\n float B = b * float(uStPer.y);\n float sharp = uStSharp;\n if (uStType == 1) {\n float s = 0.5 + 0.5 * cos(TAU * A);\n return pow(s, 1.0 + 5.0 * sharp) * 2.0 - 1.0;\n } else if (uStType == 2) {\n float chain = pow(0.5 + 0.5 * cos(TAU * B), 8.0 + 60.0 * sharp);\n return 0.6 * cos(TAU * A) + 1.2 * chain - 0.1;\n } else if (uStType == 3) {\n float cellA = mainLen / float(uStPer.x);\n float cellB = crossLen / float(uStPer.y);\n float cm = min(cellA, cellB);\n int row = int(floor(A));\n float dmin = 9.0;\n for (int dr = -1; dr <= 1; dr++) {\n int rr = row + dr;\n float off = imod(rr, 2) == 1 ? 0.5 : 0.0;\n float col = floor(B - off);\n for (int dc = -1; dc <= 1; dc++) {\n float dy = (A - (float(rr) + 0.5)) * cellA;\n float dx = (B - (col + float(dc) + off + 0.5)) * cellB;\n dmin = min(dmin, sqrt(dx * dx + dy * dy) / cm);\n }\n }\n float edge = mix(0.45, 0.08, sharp);\n return smoothstep(0.35 - edge, 0.35 + edge, dmin) * 2.0 - 1.0;\n } else if (uStType == 4) {\n float x = cos(TAU * A) * cos(TAU * B);\n return sign(x) * pow(abs(x), 1.0 - 0.7 * sharp);\n }\n return 0.0;\n}\n\nuint slotHash(ivec2 w, uint seed, int k) { return pcg(hash2(w.x, w.y, seed) + uint(k) * 0x9e3779b9u); }\n\nfloat segDist2(vec2 p, vec2 a, vec2 b) {\n vec2 e = b - a, w = p - a;\n float t = clamp(dot(w, e) / (dot(e, e) + 1e-9), 0.0, 1.0);\n vec2 q = w - e * t;\n return dot(q, q);\n}\n\n// returns (darkT, lightT)\nvec2 fibers(vec2 p) {\n float darkT = 1.0, lightT = 1.0;\n if (uFbCount <= 0.0 || uFbOpacity <= 0.0) return vec2(1.0);\n ivec2 c = ivec2(floor(p / uFbCell));\n int slots = int(ceil(uFbCount));\n float reach = uFbLength * (0.5 + 0.75 * uFbCurl) + uFbWidth * 0.7 + 1.0;\n for (int j = -1; j <= 1; j++)\n for (int i = -1; i <= 1; i++) {\n ivec2 n = c + ivec2(i, j);\n ivec2 w = imod2(n, uFbPer);\n for (int k = 0; k < 32; k++) {\n if (k >= slots) break;\n uint h = slotHash(w, uFbSeed, k);\n if (u01(h) >= clamp(uFbCount - float(k), 0.0, 1.0)) continue;\n h = pcg(h); float r1 = u01(h);\n h = pcg(h); float r2 = u01(h);\n vec2 cen = (vec2(n) + vec2(r1, r2)) * uFbCell;\n if (length(p - cen) > reach) continue;\n h = pcg(h); float r3 = u01(h);\n h = pcg(h); float r4 = u01(h);\n h = pcg(h); float r5 = u01(h);\n h = pcg(h); float r6 = u01(h);\n h = pcg(h); float r7 = u01(h);\n h = pcg(h); float r8 = u01(h);\n h = pcg(h); float r9 = u01(h);\n float ang = r3 * TAU;\n vec2 d = vec2(cos(ang), sin(ang));\n vec2 nrm = vec2(-d.y, d.x);\n float L = uFbLength * (0.4 + 0.6 * r4);\n float c1 = (r5 * 2.0 - 1.0) * uFbCurl * L;\n float c2 = (r6 * 2.0 - 1.0) * uFbCurl * L;\n vec2 p0 = cen - d * L * 0.5;\n vec2 p3 = cen + d * L * 0.5;\n vec2 p1 = p0 + d * L / 3.0 + nrm * c1;\n vec2 p2 = p0 + d * 2.0 * L / 3.0 + nrm * c2;\n float d2 = 1e20;\n vec2 prev = p0;\n for (int s = 1; s <= 6; s++) {\n float t = float(s) / 6.0;\n float q = 1.0 - t;\n vec2 cur = q * q * q * p0 + 3.0 * q * q * t * p1 + 3.0 * q * t * t * p2 + t * t * t * p3;\n d2 = min(d2, segDist2(p, prev, cur));\n prev = cur;\n }\n float hw = uFbWidth * (0.6 + 0.8 * r8) * 0.5;\n float cov = clamp(hw + 0.5 - sqrt(d2), 0.0, 1.0);\n if (cov <= 0.0) continue;\n float alpha = uFbOpacity * (0.4 + 0.6 * r9);\n if (r7 < uFbDarkRatio) darkT *= 1.0 - cov * alpha; else lightT *= 1.0 - cov * alpha;\n }\n }\n return vec2(darkT, lightT);\n}\n\n// returns accumulated (rgb * a, a) and transmittance in .w of second\nvoid specks(vec2 p, out vec4 acc, out float T) {\n acc = vec4(0.0); T = 1.0;\n if (uSpCount <= 0.0 || uSpOpacity <= 0.0) return;\n ivec2 c = ivec2(floor(p / uSpCell));\n int slots = int(ceil(uSpCount));\n for (int j = -1; j <= 1; j++)\n for (int i = -1; i <= 1; i++) {\n ivec2 n = c + ivec2(i, j);\n ivec2 w = imod2(n, uSpPer);\n for (int k = 0; k < 32; k++) {\n if (k >= slots) break;\n uint h = slotHash(w, uSpSeed, k);\n if (u01(h) >= clamp(uSpCount - float(k), 0.0, 1.0)) continue;\n h = pcg(h); float r1 = u01(h);\n h = pcg(h); float r2 = u01(h);\n h = pcg(h); float r3 = u01(h);\n h = pcg(h); float r4 = u01(h);\n h = pcg(h); float r5 = u01(h);\n vec2 cen = (vec2(n) + vec2(r1, r2)) * uSpCell;\n float rad = uSpMin + (uSpMax - uSpMin) * r4 * r4 * r4;\n float rb = rad;\n float ra = rad * (1.0 + uSpElong * r5);\n vec2 dd = p - cen;\n if (length(dd) > 1.32 * ra + 0.66 * ra / rb + 1.0) continue;\n h = pcg(h); float r6 = u01(h);\n h = pcg(h); float r7 = u01(h);\n h = pcg(h); float r8 = u01(h);\n h = pcg(h); float r9 = u01(h);\n float ang = r3 * TAU;\n float cs = cos(ang), sn = sin(ang);\n vec2 q = vec2(dd.x * cs + dd.y * sn, -dd.x * sn + dd.y * cs);\n vec2 e = vec2(q.x / ra, q.y / rb);\n float a2 = atan(e.y, e.x);\n float irr = 1.0 + 0.12 * sin(3.0 * a2 + r8 * TAU) + 0.1 * sin(5.0 * a2 + r9 * TAU);\n float dn = length(e) / irr;\n float cov = clamp((1.0 - dn) * rb + 0.5, 0.0, 1.0);\n if (cov <= 0.0) continue;\n int ci = min(2, int(floor(r6 * 3.0)));\n vec3 col = ci == 0 ? uSpC0 : (ci == 1 ? uSpC1 : uSpC2);\n float a = cov * uSpOpacity * (0.5 + 0.5 * r7);\n T *= 1.0 - a;\n acc += vec4(col * a, a);\n }\n }\n}\n\nvoid main() {\n ivec2 ip = imod2(uOrigin + ivec2(gl_FragCoord.xy), uSize);\n vec2 p = vec2(ip) + 0.5;\n float u = p.x / float(uSize.x);\n float v = p.y / float(uSize.y);\n\n // mottle\n vec2 mp = vec2(u, v) * vec2(uMoPer);\n if (uMoWarp > 0.0) {\n float wx = fbm(mp, uMoPer, 3, 0.5, uMoWarpSeed);\n float wy = fbm(mp + vec2(5.2, 1.3), uMoPer, 3, 0.5, uMoWarpSeed);\n mp += uMoWarp * vec2(wx, wy);\n }\n float m = fbm(mp, uMoPer, uMoOct, 0.5, uMoSeed);\n float t = uTintAmount > 0.0 ? fbm(vec2(u, v) * vec2(uMoPer), uMoPer, uMoOct, 0.5, uMoTintSeed) : 0.0;\n\n float g = uGrStrength > 0.0 ? fbm(vec2(u, v) * vec2(uGrPer), uGrPer, uGrOct, uGrRough, uGrSeed) : 0.0;\n\n float hc = 0.0;\n if (uCeStrength > 0.0) {\n vec2 cp = vec2(u, v) * vec2(uCePer);\n if (uCeWarp > 0.0) {\n cp.x += uCeWarp * fbm(cp, uCePer, 2, 0.5, uCeWarpSeed);\n cp.y += uCeWarp * fbm(cp + vec2(3.7, 8.1), uCePer, 2, 0.5, uCeWarpSeed);\n }\n vec2 F = worley(cp, uCePer, uCeSeed);\n float dome = 0.5 - F.x;\n float ridge = F.y - F.x - 0.4;\n hc = mix(dome, ridge, uCeRidge) * 2.0;\n }\n\n float st = uSkStrength > 0.0 ? fbm(vec2(u, v) * vec2(uSkPer), uSkPer, uSkOct, 0.5, uSkSeed) : 0.0;\n float hs = uStStrength > 0.0 ? structureHeight(u, v) : 0.0;\n\n float surface = uGrStrength * g + uCeStrength * hc + uSkStrength * st + uStStrength * hs;\n float height = uMoRelief * m + surface;\n\n float ta = uTintAmount * clamp(0.5 + t, 0.0, 1.0);\n float tone = 1.0 + uVariation * 2.0 * m + uGrainTone * surface;\n vec3 col = mix(uBase, uTint, ta) * tone;\n\n vec2 fT = fibers(p);\n col = mix(col, uFbDark, 1.0 - fT.x);\n col = mix(col, uFbLight, 1.0 - fT.y);\n height += uFbRelief * (1.0 - fT.x * fT.y);\n\n vec4 acc; float T;\n specks(p, acc, T);\n if (acc.w > 0.0) {\n col = mix(col, acc.rgb / acc.w, 1.0 - T);\n height += uSpRelief * (1.0 - T);\n }\n\n vec3 mul = overlays(p, u, v, height);\n outField = vec4(col * mul, height);\n}\n";
|
|
10
|
+
export declare const COMPOSE_FS = "#version 300 es\nprecision highp float;\nprecision highp int;\nuniform highp sampler2D uField; // padded by 1px on every side\nuniform int uMode; // 0 color, 1 albedo, 2 overlay, 3 height, 4 normal\nuniform vec3 uLight;\nuniform float uDepth, uBaseLum, uHeightNorm, uBrightness, uContrast;\nuniform bool uFlipY;\nuniform bool uFlipOut; // flip vertically when drawing to the default framebuffer\nuniform int uOutHeight;\nout vec4 outColor;\n\nvoid main() {\n ivec2 ij = ivec2(gl_FragCoord.xy);\n if (uFlipOut) ij.y = uOutHeight - 1 - ij.y;\n ivec2 q = ij + 1;\n vec4 f = texelFetch(uField, q, 0);\n float hx = (texelFetch(uField, q + ivec2(1, 0), 0).a - texelFetch(uField, q - ivec2(1, 0), 0).a) * 0.5;\n float hy = (texelFetch(uField, q + ivec2(0, 1), 0).a - texelFetch(uField, q - ivec2(0, 1), 0).a) * 0.5;\n vec3 n = normalize(vec3(-hx * uDepth, -hy * uDepth, 1.0));\n\n if (uMode == 4) {\n outColor = vec4(n.x * 0.5 + 0.5, (uFlipY ? n.y : -n.y) * 0.5 + 0.5, n.z * 0.5 + 0.5, 1.0);\n return;\n }\n if (uMode == 3) {\n outColor = vec4(vec3(clamp(0.5 + 0.5 * f.a * uHeightNorm, 0.0, 1.0)), 1.0);\n return;\n }\n float shade = clamp(dot(n, uLight) / uLight.z, 0.0, 3.0);\n vec3 c = uMode == 1 ? f.rgb : f.rgb * shade;\n if (uMode == 2) {\n float lum = dot(c, vec3(0.2126, 0.7152, 0.0722));\n outColor = vec4(vec3(clamp((lum - uBaseLum) * 0.5 * uContrast + 0.5 + uBrightness, 0.0, 1.0)), 1.0);\n return;\n }\n outColor = vec4(clamp((c - 0.5) * uContrast + 0.5 + uBrightness, 0.0, 1.0), 1.0);\n}\n";
|