rift-ds 1.24.0 → 1.26.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/README.md +5 -4
- package/chat/ChatThreads.d.ts +3 -1
- package/chat/ChatThreads.js +3 -1
- package/chat/ChatWidget.css +166 -15
- package/chat/ChatWidget.d.ts +6 -2
- package/chat/ChatWidget.js +17 -3
- package/components/ActionCard/ActionCard.d.ts +1 -1
- package/components/Attachment/Attachment.css +8 -0
- package/components/Attachment/AttachmentViewer.js +26 -5
- package/components/Attachment/fileTypes.d.ts +4 -0
- package/components/ContextMenu/ContextMenu.css +11 -0
- package/components/ContextMenu/ContextMenu.d.ts +1 -1
- package/components/ContextMenu/ContextMenu.js +3 -1
- package/components/CurrencyInput/CurrencyInput.css +12 -0
- package/components/CurrencyInput/CurrencyInput.d.ts +51 -0
- package/components/CurrencyInput/CurrencyInput.js +158 -0
- package/components/DataTable/columnTypes.js +4 -16
- package/components/DatePicker/DatePicker.d.ts +2 -0
- package/components/DatePicker/DatePicker.js +44 -131
- package/components/DatePicker/MonthGrid.d.ts +56 -0
- package/components/DatePicker/MonthGrid.js +65 -0
- package/components/DatePicker/calendar.d.ts +42 -0
- package/components/DatePicker/calendar.js +108 -0
- package/components/DateRangePicker/DateRangePicker.css +92 -0
- package/components/DateRangePicker/DateRangePicker.d.ts +86 -0
- package/components/DateRangePicker/DateRangePicker.js +294 -0
- package/components/Deadline/Deadline.css +82 -0
- package/components/Deadline/Deadline.d.ts +44 -0
- package/components/Deadline/Deadline.js +94 -0
- package/components/Deadline/deadlineStatus.d.ts +15 -0
- package/components/Deadline/deadlineStatus.js +13 -0
- package/components/DescriptionList/DescriptionList.css +184 -0
- package/components/DescriptionList/DescriptionList.d.ts +64 -0
- package/components/DescriptionList/DescriptionList.js +100 -0
- package/components/DescriptionList/DescriptionListCopy.d.ts +18 -0
- package/components/DescriptionList/DescriptionListCopy.js +45 -0
- package/components/DocumentViewer/DocumentViewer.css +188 -0
- package/components/DocumentViewer/DocumentViewer.d.ts +59 -0
- package/components/DocumentViewer/DocumentViewer.js +185 -0
- package/components/DropdownMenu/DropdownMenu.css +38 -0
- package/components/DropdownMenu/DropdownMenu.d.ts +4 -0
- package/components/DropdownMenu/DropdownMenu.js +11 -5
- package/components/DropdownMenu/ShortcutHint.d.ts +9 -0
- package/components/DropdownMenu/ShortcutHint.js +6 -0
- package/components/ErrorSummary/ErrorSummary.css +94 -0
- package/components/ErrorSummary/ErrorSummary.d.ts +34 -0
- package/components/ErrorSummary/ErrorSummary.js +70 -0
- package/components/ErrorSummary/fieldErrors.d.ts +36 -0
- package/components/ErrorSummary/fieldErrors.js +19 -0
- package/components/Field/Field.css +12 -0
- package/components/Field/Field.d.ts +10 -2
- package/components/Field/Field.js +12 -5
- package/components/Heading/Heading.css +129 -0
- package/components/Heading/Heading.d.ts +31 -0
- package/components/Heading/Heading.js +39 -0
- package/components/Input/Input.css +120 -0
- package/components/Input/Input.d.ts +20 -1
- package/components/Input/Input.js +70 -5
- package/components/InterruptCard/InterruptCard.d.ts +1 -1
- package/components/Link/Link.css +84 -0
- package/components/Link/Link.d.ts +43 -0
- package/components/Link/Link.js +74 -0
- package/components/List/List.css +173 -0
- package/components/List/List.d.ts +44 -0
- package/components/List/List.js +60 -0
- package/components/MaskedInput/MaskedInput.css +16 -0
- package/components/MaskedInput/MaskedInput.d.ts +57 -0
- package/components/MaskedInput/MaskedInput.js +142 -0
- package/components/MaskedInput/mask.d.ts +58 -0
- package/components/MaskedInput/mask.js +106 -0
- package/components/MessageCard/MessageCard.css +17 -6
- package/components/MessageCard/MessageCard.d.ts +1 -1
- package/components/ShaderScene/ShaderScene.css +19 -0
- package/components/ShaderScene/ShaderScene.d.ts +82 -0
- package/components/ShaderScene/ShaderScene.js +167 -0
- package/components/ShaderScene/assemble.d.ts +5 -0
- package/components/ShaderScene/assemble.js +76 -0
- package/components/ShaderScene/common.glsl.d.ts +6 -0
- package/components/ShaderScene/common.glsl.js +49 -0
- package/components/ShaderScene/passes.d.ts +2 -0
- package/components/ShaderScene/passes.js +93 -0
- package/components/ShaderScene/renderer.d.ts +39 -0
- package/components/ShaderScene/renderer.js +170 -0
- package/components/ShaderScene/scenes.d.ts +76 -0
- package/components/ShaderScene/scenes.js +752 -0
- package/components/ShaderScene/types.d.ts +83 -0
- package/components/ShaderScene/types.js +8 -0
- package/components/SignaturePad/SignaturePad.css +141 -0
- package/components/SignaturePad/SignaturePad.d.ts +68 -0
- package/components/SignaturePad/SignaturePad.js +287 -0
- package/components/SignaturePad/signature.d.ts +32 -0
- package/components/SignaturePad/signature.js +46 -0
- package/components/Text/Text.css +129 -0
- package/components/Text/Text.d.ts +36 -0
- package/components/Text/Text.js +36 -0
- package/components/ThreadPanel/ThreadPanel.css +29 -0
- package/components/ThreadPanel/ThreadPanel.d.ts +2 -0
- package/components/ThreadPanel/ThreadPanel.js +13 -2
- package/components/Widget/Widget.css +111 -0
- package/components/Widget/Widget.d.ts +15 -1
- package/components/Widget/Widget.js +63 -11
- package/components/Widget/WidgetDisclosure.d.ts +30 -0
- package/components/Widget/WidgetDisclosure.js +52 -0
- package/components/registry.json +111 -2
- package/components/registry.json.d.ts +111 -2
- package/components/registry.json.js +2 -2
- package/format/index.d.ts +15 -0
- package/format/index.js +17 -0
- package/format/money.d.ts +89 -0
- package/format/money.js +187 -0
- package/format/relative.d.ts +49 -0
- package/format/relative.js +50 -0
- package/index.d.ts +13 -0
- package/index.js +48 -1
- package/package.json +9 -2
- package/tokens/motion.d.ts +4 -0
- package/tokens/motion.js +4 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import { MOTION_RENDERER_WATCHDOG_MS } from "../../tokens/motion.js";
|
|
5
|
+
import { PASSES } from "./passes.js";
|
|
6
|
+
import { createRenderer, glyphFontFor, coloursFor } from "./renderer.js";
|
|
7
|
+
import { bakeThumbnails } from "./renderer.js";
|
|
8
|
+
import { SCENES } from "./scenes.js";
|
|
9
|
+
import { defaultsFor } from "./types.js";
|
|
10
|
+
import "./ShaderScene.css";
|
|
11
|
+
const MAX_PIXEL_RATIO = 1;
|
|
12
|
+
const FRAME_MS = 1e3 / 30;
|
|
13
|
+
const DEFAULT_SPEED = 1.6;
|
|
14
|
+
function ShaderScene({
|
|
15
|
+
scene = "horizon",
|
|
16
|
+
params,
|
|
17
|
+
passes,
|
|
18
|
+
speed = DEFAULT_SPEED,
|
|
19
|
+
liveRef,
|
|
20
|
+
enabled = true,
|
|
21
|
+
onStatusChange,
|
|
22
|
+
className = "",
|
|
23
|
+
ref,
|
|
24
|
+
...rest
|
|
25
|
+
}) {
|
|
26
|
+
const hostRef = React.useRef(null);
|
|
27
|
+
const canvasRef = React.useRef(null);
|
|
28
|
+
const definition = SCENES.find((s) => s.id === scene) ?? SCENES[0];
|
|
29
|
+
const merged = React.useMemo(
|
|
30
|
+
() => ({
|
|
31
|
+
speed,
|
|
32
|
+
scene: { ...defaultsFor(definition.params), ...params },
|
|
33
|
+
passes: Object.fromEntries(
|
|
34
|
+
PASSES.map((pass) => {
|
|
35
|
+
const setting = passes?.[pass.id];
|
|
36
|
+
return [
|
|
37
|
+
pass.id,
|
|
38
|
+
{
|
|
39
|
+
on: setting?.on ?? pass.on,
|
|
40
|
+
params: { ...defaultsFor(pass.params), ...setting?.params }
|
|
41
|
+
}
|
|
42
|
+
];
|
|
43
|
+
})
|
|
44
|
+
)
|
|
45
|
+
}),
|
|
46
|
+
[definition, params, passes, speed]
|
|
47
|
+
);
|
|
48
|
+
const mergedRef = React.useRef(merged);
|
|
49
|
+
React.useEffect(() => {
|
|
50
|
+
mergedRef.current = merged;
|
|
51
|
+
}, [merged]);
|
|
52
|
+
const [gl, setGl] = React.useState("pending");
|
|
53
|
+
const [gaveUp, setGaveUp] = React.useState(false);
|
|
54
|
+
React.useEffect(() => {
|
|
55
|
+
if (!enabled || gl !== "pending") return;
|
|
56
|
+
const timer = window.setTimeout(() => setGaveUp(true), MOTION_RENDERER_WATCHDOG_MS);
|
|
57
|
+
return () => window.clearTimeout(timer);
|
|
58
|
+
}, [enabled, gl]);
|
|
59
|
+
const status = !enabled || gl === "failed" ? "unavailable" : gl === "painted" ? "active" : gaveUp ? "unavailable" : "pending";
|
|
60
|
+
const notify = React.useRef(onStatusChange);
|
|
61
|
+
React.useEffect(() => {
|
|
62
|
+
notify.current = onStatusChange;
|
|
63
|
+
}, [onStatusChange]);
|
|
64
|
+
React.useEffect(() => {
|
|
65
|
+
notify.current?.(status);
|
|
66
|
+
}, [status]);
|
|
67
|
+
const setRef = (node) => {
|
|
68
|
+
hostRef.current = node;
|
|
69
|
+
if (typeof ref === "function") ref(node);
|
|
70
|
+
else if (ref) ref.current = node;
|
|
71
|
+
};
|
|
72
|
+
React.useEffect(() => {
|
|
73
|
+
if (!enabled) return;
|
|
74
|
+
const host = hostRef.current;
|
|
75
|
+
const canvas = canvasRef.current;
|
|
76
|
+
const context = canvas?.getContext("webgl2", { antialias: false, alpha: false });
|
|
77
|
+
const renderer = context ? createRenderer(context, definition, PASSES) : null;
|
|
78
|
+
if (!host || !canvas || !renderer) {
|
|
79
|
+
const failed = requestAnimationFrame(() => setGl("failed"));
|
|
80
|
+
return () => cancelAnimationFrame(failed);
|
|
81
|
+
}
|
|
82
|
+
const applyColours = () => {
|
|
83
|
+
renderer.setColours(coloursFor(host, definition));
|
|
84
|
+
renderer.setGlyphs(glyphFontFor(host));
|
|
85
|
+
};
|
|
86
|
+
let ratio = 1;
|
|
87
|
+
const fit = () => {
|
|
88
|
+
ratio = Math.min(definition.pixelRatio ?? MAX_PIXEL_RATIO, window.devicePixelRatio || 1);
|
|
89
|
+
const width = Math.max(1, Math.round(host.clientWidth * ratio));
|
|
90
|
+
const height = Math.max(1, Math.round(host.clientHeight * ratio));
|
|
91
|
+
if (canvas.width === width && canvas.height === height) return;
|
|
92
|
+
canvas.width = width;
|
|
93
|
+
canvas.height = height;
|
|
94
|
+
};
|
|
95
|
+
const still = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
96
|
+
let time = 20;
|
|
97
|
+
let last = performance.now();
|
|
98
|
+
let frame = 0;
|
|
99
|
+
let painted = false;
|
|
100
|
+
const draw = (now) => {
|
|
101
|
+
frame = requestAnimationFrame(draw);
|
|
102
|
+
if (painted && now - last < FRAME_MS - 1) return;
|
|
103
|
+
const values = liveRef?.current ?? mergedRef.current;
|
|
104
|
+
const dt = Math.min(0.1, (now - last) / 1e3);
|
|
105
|
+
last = now;
|
|
106
|
+
if (!still) time += dt * values.speed;
|
|
107
|
+
fit();
|
|
108
|
+
renderer.draw(values, time, canvas.width, canvas.height, ratio);
|
|
109
|
+
if (!painted) {
|
|
110
|
+
painted = true;
|
|
111
|
+
setGl("painted");
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const start = () => {
|
|
115
|
+
if (frame) return;
|
|
116
|
+
last = performance.now();
|
|
117
|
+
frame = requestAnimationFrame(draw);
|
|
118
|
+
};
|
|
119
|
+
const stop = () => {
|
|
120
|
+
cancelAnimationFrame(frame);
|
|
121
|
+
frame = 0;
|
|
122
|
+
};
|
|
123
|
+
applyColours();
|
|
124
|
+
start();
|
|
125
|
+
let mounted = true;
|
|
126
|
+
if (definition.glyphs) {
|
|
127
|
+
document.fonts?.ready.then(() => {
|
|
128
|
+
if (!mounted) return;
|
|
129
|
+
renderer.setGlyphs("");
|
|
130
|
+
renderer.setGlyphs(glyphFontFor(host));
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
const visibilityObserver = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(
|
|
134
|
+
(entries) => entries[entries.length - 1].isIntersecting ? start() : stop(),
|
|
135
|
+
{ rootMargin: "200px 0px" }
|
|
136
|
+
);
|
|
137
|
+
visibilityObserver?.observe(host);
|
|
138
|
+
const sizeObserver = new ResizeObserver(() => {
|
|
139
|
+
if (frame) return;
|
|
140
|
+
fit();
|
|
141
|
+
renderer.draw(liveRef?.current ?? mergedRef.current, time, canvas.width, canvas.height, ratio);
|
|
142
|
+
});
|
|
143
|
+
sizeObserver.observe(host);
|
|
144
|
+
const themeObserver = new MutationObserver(applyColours);
|
|
145
|
+
themeObserver.observe(document.documentElement, {
|
|
146
|
+
attributes: true,
|
|
147
|
+
attributeFilter: ["data-theme", "data-brand", "style"]
|
|
148
|
+
});
|
|
149
|
+
return () => {
|
|
150
|
+
mounted = false;
|
|
151
|
+
stop();
|
|
152
|
+
visibilityObserver?.disconnect();
|
|
153
|
+
sizeObserver.disconnect();
|
|
154
|
+
themeObserver.disconnect();
|
|
155
|
+
renderer.dispose();
|
|
156
|
+
};
|
|
157
|
+
}, [definition, liveRef, enabled]);
|
|
158
|
+
const classes = ["ds-shader-scene", className].filter(Boolean).join(" ");
|
|
159
|
+
return /* @__PURE__ */ jsx("div", { ...rest, ref: setRef, className: classes, "data-status": status, "aria-hidden": "true", children: enabled && /* @__PURE__ */ jsx("canvas", { ref: canvasRef, className: "ds-shader-scene__canvas" }) });
|
|
160
|
+
}
|
|
161
|
+
export {
|
|
162
|
+
PASSES as SHADER_PASSES,
|
|
163
|
+
SCENES as SHADER_SCENES,
|
|
164
|
+
ShaderScene,
|
|
165
|
+
defaultsFor as shaderSceneDefaults,
|
|
166
|
+
bakeThumbnails as shaderSceneStills
|
|
167
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Pass, Scene } from './types';
|
|
2
|
+
export declare const VERTEX = "#version 300 es\nvoid main() {\n vec2 p = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2);\n gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0);\n}\n";
|
|
3
|
+
/** Every float uniform the shader declares, for the canvas to set each frame. */
|
|
4
|
+
export declare function uniformNames(scene: Scene, passes: Pass[]): string[];
|
|
5
|
+
export declare function assemble(scene: Scene, passes: Pass[]): string;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { COMMON_GLSL } from "./common.glsl.js";
|
|
2
|
+
import { MAX_COLOURS } from "./types.js";
|
|
3
|
+
const VERTEX = (
|
|
4
|
+
/* glsl */
|
|
5
|
+
`#version 300 es
|
|
6
|
+
void main() {
|
|
7
|
+
vec2 p = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2);
|
|
8
|
+
gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0);
|
|
9
|
+
}
|
|
10
|
+
`
|
|
11
|
+
);
|
|
12
|
+
function uniformNames(scene, passes) {
|
|
13
|
+
return [
|
|
14
|
+
...scene.params.map((param) => `u_${param.key}`),
|
|
15
|
+
...passes.flatMap((pass) => [
|
|
16
|
+
`u_${pass.id}_on`,
|
|
17
|
+
...pass.params.map((param) => `u_${pass.id}_${param.key}`)
|
|
18
|
+
])
|
|
19
|
+
];
|
|
20
|
+
}
|
|
21
|
+
function assemble(scene, passes) {
|
|
22
|
+
const floats = uniformNames(scene, passes).map((name) => `uniform float ${name};`).join("\n");
|
|
23
|
+
const call = (stage, target) => passes.filter((pass) => pass.stage === stage).map((pass) => ` if (u_${pass.id}_on > 0.5) ${target} = pass_${pass.id}(${target}, uv);`).join("\n");
|
|
24
|
+
return (
|
|
25
|
+
/* glsl */
|
|
26
|
+
`#version 300 es
|
|
27
|
+
precision highp float;
|
|
28
|
+
|
|
29
|
+
uniform vec2 u_size;
|
|
30
|
+
uniform float u_time;
|
|
31
|
+
uniform vec3 u_ground; // the scene's ground: the page's, or a night scene's own
|
|
32
|
+
uniform vec3 u_page; // the page's ground, which the passes fade to
|
|
33
|
+
uniform float u_lightMode; // 1: a night scene laid onto a light page
|
|
34
|
+
uniform float u_ratio; // drawing-buffer pixels per CSS pixel
|
|
35
|
+
uniform vec3 u_col[${MAX_COLOURS}];
|
|
36
|
+
${scene.glyphs ? "uniform sampler2D u_glyphs;\nuniform float u_glyphCount;" : ""}
|
|
37
|
+
${floats}
|
|
38
|
+
|
|
39
|
+
out vec4 outColor;
|
|
40
|
+
|
|
41
|
+
${COMMON_GLSL}
|
|
42
|
+
|
|
43
|
+
${scene.glsl}
|
|
44
|
+
|
|
45
|
+
${passes.map((pass) => pass.glsl).join("\n")}
|
|
46
|
+
|
|
47
|
+
void main() {
|
|
48
|
+
vec2 uv = gl_FragCoord.xy / u_size;
|
|
49
|
+
vec3 lin = scene(uv, u_time);
|
|
50
|
+
/* A night scene on a light page: the light it adds over its own dark
|
|
51
|
+
ground is laid onto the page as a pale tint, its hue washed most of
|
|
52
|
+
the way to white and never more than a light wash, so a horizon reads
|
|
53
|
+
as a soft dawn on paper rather than a dark block on it. */
|
|
54
|
+
if (u_lightMode > 0.5) {
|
|
55
|
+
vec3 light = max(lin - u_ground, vec3(0.0));
|
|
56
|
+
float peak = max(light.r, max(light.g, light.b));
|
|
57
|
+
vec3 hue = light / max(peak, 1e-4);
|
|
58
|
+
vec3 pastel = mix(hue, vec3(1.0), 0.3);
|
|
59
|
+
float strength = clamp(pow(dot(light, vec3(0.2126, 0.7152, 0.0722)) * 4.0, 0.7), 0.0, 0.85);
|
|
60
|
+
lin = mix(u_page, u_page * pastel, strength);
|
|
61
|
+
}
|
|
62
|
+
${call("light", "lin")}
|
|
63
|
+
vec3 srgb = pow(max(lin, vec3(0.0)), vec3(1.0 / 2.2));
|
|
64
|
+
/* A one-step dither under everything, so wide ramps never band. */
|
|
65
|
+
srgb += (float(ihash(uvec2(gl_FragCoord.xy)) & 255u) / 255.0 - 0.5) / 255.0;
|
|
66
|
+
${call("srgb", "srgb")}
|
|
67
|
+
outColor = vec4(clamp(srgb, 0.0, 1.0), 1.0);
|
|
68
|
+
}
|
|
69
|
+
`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
export {
|
|
73
|
+
VERTEX,
|
|
74
|
+
assemble,
|
|
75
|
+
uniformNames
|
|
76
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The GLSL every scene and pass can call: ShaderField's integer hash (no
|
|
3
|
+
* fract(sin()) patterning, so no lines in the grain), value noise, fbm and
|
|
4
|
+
* the one-sided Gaussian the horizon is built from.
|
|
5
|
+
*/
|
|
6
|
+
export declare const COMMON_GLSL = "\nuint ihash(uvec2 p) {\n p = p * uvec2(73333u, 7777u) ^ (p.yx >> 3u);\n uint h = p.x ^ (p.y * 2654435761u);\n h ^= h >> 15u;\n h *= 2246822519u;\n h ^= h >> 13u;\n return h;\n}\n\nfloat rnd(ivec2 p) {\n return float(ihash(uvec2(p)) >> 8) * (1.0 / 16777216.0);\n}\n\nfloat vnoise(vec2 p) {\n vec2 i = floor(p);\n vec2 f = fract(p);\n vec2 u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);\n ivec2 c = ivec2(i) + 4096;\n return mix(\n mix(rnd(c), rnd(c + ivec2(1, 0)), u.x),\n mix(rnd(c + ivec2(0, 1)), rnd(c + ivec2(1, 1)), u.x),\n u.y\n );\n}\n\nfloat fbm(vec2 p) {\n float s = 0.0;\n float a = 0.5;\n for (int i = 0; i < 4; i++) {\n s += a * vnoise(p);\n p = p * 2.02 + vec2(17.0, 9.0);\n a *= 0.5;\n }\n return s;\n}\n\n/* A one-sided Gaussian: one sigma above zero, another below. */\nfloat lobe(float d, float above, float below) {\n float s = d > 0.0 ? above : below;\n return exp(-(d * d) / (2.0 * s * s));\n}\n";
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const COMMON_GLSL = (
|
|
2
|
+
/* glsl */
|
|
3
|
+
`
|
|
4
|
+
uint ihash(uvec2 p) {
|
|
5
|
+
p = p * uvec2(73333u, 7777u) ^ (p.yx >> 3u);
|
|
6
|
+
uint h = p.x ^ (p.y * 2654435761u);
|
|
7
|
+
h ^= h >> 15u;
|
|
8
|
+
h *= 2246822519u;
|
|
9
|
+
h ^= h >> 13u;
|
|
10
|
+
return h;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
float rnd(ivec2 p) {
|
|
14
|
+
return float(ihash(uvec2(p)) >> 8) * (1.0 / 16777216.0);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
float vnoise(vec2 p) {
|
|
18
|
+
vec2 i = floor(p);
|
|
19
|
+
vec2 f = fract(p);
|
|
20
|
+
vec2 u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
|
|
21
|
+
ivec2 c = ivec2(i) + 4096;
|
|
22
|
+
return mix(
|
|
23
|
+
mix(rnd(c), rnd(c + ivec2(1, 0)), u.x),
|
|
24
|
+
mix(rnd(c + ivec2(0, 1)), rnd(c + ivec2(1, 1)), u.x),
|
|
25
|
+
u.y
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
float fbm(vec2 p) {
|
|
30
|
+
float s = 0.0;
|
|
31
|
+
float a = 0.5;
|
|
32
|
+
for (int i = 0; i < 4; i++) {
|
|
33
|
+
s += a * vnoise(p);
|
|
34
|
+
p = p * 2.02 + vec2(17.0, 9.0);
|
|
35
|
+
a *= 0.5;
|
|
36
|
+
}
|
|
37
|
+
return s;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/* A one-sided Gaussian: one sigma above zero, another below. */
|
|
41
|
+
float lobe(float d, float above, float below) {
|
|
42
|
+
float s = d > 0.0 ? above : below;
|
|
43
|
+
return exp(-(d * d) / (2.0 * s * s));
|
|
44
|
+
}
|
|
45
|
+
`
|
|
46
|
+
);
|
|
47
|
+
export {
|
|
48
|
+
COMMON_GLSL
|
|
49
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const PASSES = [
|
|
2
|
+
{
|
|
3
|
+
id: "scrim",
|
|
4
|
+
label: "Scrim",
|
|
5
|
+
stage: "light",
|
|
6
|
+
on: false,
|
|
7
|
+
params: [{ key: "amount", label: "Amount", min: 0, max: 0.9, step: 0.01, default: 0.35 }],
|
|
8
|
+
glsl: (
|
|
9
|
+
/* glsl */
|
|
10
|
+
`
|
|
11
|
+
vec3 pass_scrim(vec3 c, vec2 uv) {
|
|
12
|
+
return mix(c, u_page, u_scrim_amount);
|
|
13
|
+
}`
|
|
14
|
+
)
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
id: "vignette",
|
|
18
|
+
label: "Vignette",
|
|
19
|
+
stage: "light",
|
|
20
|
+
on: false,
|
|
21
|
+
params: [
|
|
22
|
+
{ key: "amount", label: "Amount", min: 0, max: 1, step: 0.01, default: 0.5 },
|
|
23
|
+
{ key: "size", label: "Size", min: 0.2, max: 1.2, step: 0.01, default: 0.75 }
|
|
24
|
+
],
|
|
25
|
+
glsl: (
|
|
26
|
+
/* glsl */
|
|
27
|
+
`
|
|
28
|
+
vec3 pass_vignette(vec3 c, vec2 uv) {
|
|
29
|
+
vec2 q = (uv - 0.5) * vec2(u_size.x / u_size.y, 1.0);
|
|
30
|
+
float v = smoothstep(u_vignette_size, u_vignette_size * 0.35, length(q));
|
|
31
|
+
return mix(u_page, c, mix(1.0, v, u_vignette_amount));
|
|
32
|
+
}`
|
|
33
|
+
)
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
id: "fadeTop",
|
|
37
|
+
label: "Fade the top",
|
|
38
|
+
stage: "light",
|
|
39
|
+
on: true,
|
|
40
|
+
params: [
|
|
41
|
+
{ key: "start", label: "Starts at", min: 0.2, max: 0.95, step: 0.01, default: 0.43 },
|
|
42
|
+
{ key: "softness", label: "Softness", min: 0.3, max: 2, step: 0.01, default: 0.75 }
|
|
43
|
+
],
|
|
44
|
+
glsl: (
|
|
45
|
+
/* glsl */
|
|
46
|
+
`
|
|
47
|
+
vec3 pass_fadeTop(vec3 c, vec2 uv) {
|
|
48
|
+
float f = 1.0 - pow(smoothstep(u_fadeTop_start, 1.0, uv.y), u_fadeTop_softness);
|
|
49
|
+
return mix(u_page, c, f);
|
|
50
|
+
}`
|
|
51
|
+
)
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
id: "fadeBottom",
|
|
55
|
+
label: "Fade the bottom",
|
|
56
|
+
stage: "light",
|
|
57
|
+
on: true,
|
|
58
|
+
params: [
|
|
59
|
+
{ key: "from", label: "Solid below", min: 0, max: 0.4, step: 0.01, default: 0.04 },
|
|
60
|
+
{ key: "to", label: "Clear above", min: 0.05, max: 0.7, step: 0.01, default: 0.3 }
|
|
61
|
+
],
|
|
62
|
+
glsl: (
|
|
63
|
+
/* glsl */
|
|
64
|
+
`
|
|
65
|
+
vec3 pass_fadeBottom(vec3 c, vec2 uv) {
|
|
66
|
+
float f = smoothstep(u_fadeBottom_from, max(u_fadeBottom_to, u_fadeBottom_from + 0.01), uv.y);
|
|
67
|
+
return mix(u_page, c, f);
|
|
68
|
+
}`
|
|
69
|
+
)
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
id: "grain",
|
|
73
|
+
label: "Film grain",
|
|
74
|
+
stage: "srgb",
|
|
75
|
+
on: true,
|
|
76
|
+
params: [{ key: "amount", label: "Amount", min: 0, max: 0.4, step: 5e-3, default: 0.198 }],
|
|
77
|
+
glsl: (
|
|
78
|
+
/* glsl */
|
|
79
|
+
`
|
|
80
|
+
vec3 pass_grain(vec3 c, vec2 uv) {
|
|
81
|
+
uvec2 gp = uvec2(gl_FragCoord.xy) + uvec2(
|
|
82
|
+
uint(fract(u_time * 7.31) * 1024.0) * 7919u,
|
|
83
|
+
uint(fract(u_time * 3.17) * 1024.0) * 104729u
|
|
84
|
+
);
|
|
85
|
+
float gr = float(ihash(gp) & 255u) / 255.0 - 0.5;
|
|
86
|
+
return c + gr * u_grain_amount * 0.35;
|
|
87
|
+
}`
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
];
|
|
91
|
+
export {
|
|
92
|
+
PASSES
|
|
93
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Pass, Scene, Values } from './types';
|
|
2
|
+
export type Rgb = [number, number, number];
|
|
3
|
+
export type Colours = {
|
|
4
|
+
/** The ground the scene is painted against. */
|
|
5
|
+
ground: Rgb;
|
|
6
|
+
/** The page's own ground, which the passes fade to. */
|
|
7
|
+
page: Rgb;
|
|
8
|
+
/** A night scene on a light page, laid on as a pale tint. */
|
|
9
|
+
lightMode: boolean;
|
|
10
|
+
colours: Rgb[];
|
|
11
|
+
};
|
|
12
|
+
/** Resolves the page ground and a scene's tokens to linear RGB at `host`. */
|
|
13
|
+
export declare function readColours(host: HTMLElement, tokens: string[]): Colours;
|
|
14
|
+
/**
|
|
15
|
+
* A scene's colours for a surface: read from the cascade. A night-only
|
|
16
|
+
* scene on a light page computes its light against a dark ground of its
|
|
17
|
+
* own, and the shader lays that light onto the page as a pale tint
|
|
18
|
+
* (u_lightMode), so its masks still fade to the page and the theme stays
|
|
19
|
+
* light.
|
|
20
|
+
*/
|
|
21
|
+
export declare function coloursFor(host: HTMLElement, scene: Scene): Colours;
|
|
22
|
+
/** The face a scene's type is drawn in: the theme's code face at `host`. */
|
|
23
|
+
export declare function glyphFontFor(host: HTMLElement): string;
|
|
24
|
+
export declare function createRenderer(gl: WebGL2RenderingContext, scene: Scene, passes: Pass[]): {
|
|
25
|
+
setColours({ ground, page, lightMode, colours }: Colours): void;
|
|
26
|
+
/** Draws a type scene's ramp in `font`; a scene without one ignores it. */
|
|
27
|
+
setGlyphs(font: string): void;
|
|
28
|
+
draw(values: Values, time: number, width: number, height: number, ratio?: number): void;
|
|
29
|
+
dispose(): void;
|
|
30
|
+
} | null;
|
|
31
|
+
/**
|
|
32
|
+
* Paints each recipe once on a throwaway context and returns the pictures
|
|
33
|
+
* as data URLs, then releases the context, so the tiles cost no live GPU
|
|
34
|
+
* context of their own and the page keeps one.
|
|
35
|
+
*/
|
|
36
|
+
export declare function bakeThumbnails(host: HTMLElement, jobs: {
|
|
37
|
+
scene: Scene;
|
|
38
|
+
values: Values;
|
|
39
|
+
}[], passes: Pass[], width: number, height: number): string[];
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { assemble, uniformNames, VERTEX } from "./assemble.js";
|
|
2
|
+
import { MAX_COLOURS } from "./types.js";
|
|
3
|
+
const NIGHT_GROUND = [15e-4, 18e-4, 3e-3];
|
|
4
|
+
function readColours(host, tokens) {
|
|
5
|
+
const probe = document.createElement("span");
|
|
6
|
+
probe.style.display = "none";
|
|
7
|
+
host.appendChild(probe);
|
|
8
|
+
const dot = document.createElement("canvas");
|
|
9
|
+
dot.width = 1;
|
|
10
|
+
dot.height = 1;
|
|
11
|
+
const ctx = dot.getContext("2d", { willReadFrequently: true });
|
|
12
|
+
const lin = (c) => Math.pow(c / 255, 2.2);
|
|
13
|
+
const read = (token) => {
|
|
14
|
+
if (!ctx) return [0.2, 0.2, 0.2];
|
|
15
|
+
probe.style.color = `var(${token})`;
|
|
16
|
+
ctx.clearRect(0, 0, 1, 1);
|
|
17
|
+
ctx.fillStyle = getComputedStyle(probe).color;
|
|
18
|
+
ctx.fillRect(0, 0, 1, 1);
|
|
19
|
+
const [r, g, b] = ctx.getImageData(0, 0, 1, 1).data;
|
|
20
|
+
return [lin(r), lin(g), lin(b)];
|
|
21
|
+
};
|
|
22
|
+
const ground = read("--color-bg-page-primary");
|
|
23
|
+
const colours = tokens.slice(0, MAX_COLOURS).map(read);
|
|
24
|
+
probe.remove();
|
|
25
|
+
return { ground, page: ground, lightMode: false, colours };
|
|
26
|
+
}
|
|
27
|
+
function coloursFor(host, scene) {
|
|
28
|
+
const colours = readColours(host, scene.colours);
|
|
29
|
+
const pageIsDark = document.documentElement.getAttribute("data-theme") === "dark";
|
|
30
|
+
if (scene.mode === "dark" && !pageIsDark) {
|
|
31
|
+
colours.ground = NIGHT_GROUND;
|
|
32
|
+
colours.lightMode = true;
|
|
33
|
+
}
|
|
34
|
+
return colours;
|
|
35
|
+
}
|
|
36
|
+
function glyphFontFor(host) {
|
|
37
|
+
return getComputedStyle(host).getPropertyValue("--font-family-code").trim() || "monospace";
|
|
38
|
+
}
|
|
39
|
+
const GLYPH_W = 48;
|
|
40
|
+
const GLYPH_H = 80;
|
|
41
|
+
function drawGlyphs(ramp, font) {
|
|
42
|
+
const glyphs = [...ramp];
|
|
43
|
+
const strip = document.createElement("canvas");
|
|
44
|
+
strip.width = GLYPH_W * glyphs.length;
|
|
45
|
+
strip.height = GLYPH_H;
|
|
46
|
+
const ctx = strip.getContext("2d");
|
|
47
|
+
if (ctx) {
|
|
48
|
+
ctx.fillStyle = "#000";
|
|
49
|
+
ctx.fillRect(0, 0, strip.width, strip.height);
|
|
50
|
+
ctx.fillStyle = "#fff";
|
|
51
|
+
ctx.font = `700 ${GLYPH_H * 0.78}px ${font}`;
|
|
52
|
+
ctx.textAlign = "center";
|
|
53
|
+
ctx.textBaseline = "middle";
|
|
54
|
+
glyphs.forEach((glyph, i) => ctx.fillText(glyph, (i + 0.5) * GLYPH_W, GLYPH_H * 0.52));
|
|
55
|
+
}
|
|
56
|
+
return { strip, count: glyphs.length };
|
|
57
|
+
}
|
|
58
|
+
function compile(gl, type, source) {
|
|
59
|
+
const shader = gl.createShader(type);
|
|
60
|
+
if (!shader) return null;
|
|
61
|
+
gl.shaderSource(shader, source);
|
|
62
|
+
gl.compileShader(shader);
|
|
63
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
64
|
+
console.warn(gl.getShaderInfoLog(shader));
|
|
65
|
+
gl.deleteShader(shader);
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
return shader;
|
|
69
|
+
}
|
|
70
|
+
function valueOf(values, passes, name) {
|
|
71
|
+
for (const pass of passes) {
|
|
72
|
+
const prefix = `u_${pass.id}_`;
|
|
73
|
+
if (!name.startsWith(prefix)) continue;
|
|
74
|
+
const state = values.passes[pass.id];
|
|
75
|
+
const key = name.slice(prefix.length);
|
|
76
|
+
if (key === "on") return state?.on ? 1 : 0;
|
|
77
|
+
return state?.params[key] ?? 0;
|
|
78
|
+
}
|
|
79
|
+
return values.scene[name.slice(2)] ?? 0;
|
|
80
|
+
}
|
|
81
|
+
function createRenderer(gl, scene, passes) {
|
|
82
|
+
const vs = compile(gl, gl.VERTEX_SHADER, VERTEX);
|
|
83
|
+
const fs = compile(gl, gl.FRAGMENT_SHADER, assemble(scene, passes));
|
|
84
|
+
const program = gl.createProgram();
|
|
85
|
+
if (!vs || !fs || !program) return null;
|
|
86
|
+
gl.attachShader(program, vs);
|
|
87
|
+
gl.attachShader(program, fs);
|
|
88
|
+
gl.linkProgram(program);
|
|
89
|
+
gl.useProgram(program);
|
|
90
|
+
const vao = gl.createVertexArray();
|
|
91
|
+
gl.bindVertexArray(vao);
|
|
92
|
+
const at = (name) => gl.getUniformLocation(program, name);
|
|
93
|
+
const uSize = at("u_size");
|
|
94
|
+
const uTime = at("u_time");
|
|
95
|
+
const uGround = at("u_ground");
|
|
96
|
+
const uPage = at("u_page");
|
|
97
|
+
const uLightMode = at("u_lightMode");
|
|
98
|
+
const uCols = at("u_col");
|
|
99
|
+
const uRatio = at("u_ratio");
|
|
100
|
+
const uGlyphCount = at("u_glyphCount");
|
|
101
|
+
const floats = uniformNames(scene, passes).map((name) => [name, at(name)]);
|
|
102
|
+
const glyphTexture = scene.glyphs ? gl.createTexture() : null;
|
|
103
|
+
let glyphFont = "";
|
|
104
|
+
return {
|
|
105
|
+
setColours({ ground, page, lightMode, colours }) {
|
|
106
|
+
gl.uniform3fv(uGround, ground);
|
|
107
|
+
gl.uniform3fv(uPage, page);
|
|
108
|
+
gl.uniform1f(uLightMode, lightMode ? 1 : 0);
|
|
109
|
+
const flat = new Float32Array(MAX_COLOURS * 3);
|
|
110
|
+
colours.forEach((rgb, i) => flat.set(rgb, i * 3));
|
|
111
|
+
gl.uniform3fv(uCols, flat);
|
|
112
|
+
},
|
|
113
|
+
/** Draws a type scene's ramp in `font`; a scene without one ignores it. */
|
|
114
|
+
setGlyphs(font) {
|
|
115
|
+
if (!scene.glyphs || !glyphTexture || font === glyphFont) return;
|
|
116
|
+
glyphFont = font;
|
|
117
|
+
const { strip, count } = drawGlyphs(scene.glyphs, font);
|
|
118
|
+
gl.bindTexture(gl.TEXTURE_2D, glyphTexture);
|
|
119
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
|
|
120
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, strip);
|
|
121
|
+
gl.generateMipmap(gl.TEXTURE_2D);
|
|
122
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
|
|
123
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
124
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
125
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
126
|
+
gl.uniform1f(uGlyphCount, count);
|
|
127
|
+
},
|
|
128
|
+
draw(values, time, width, height, ratio = 1) {
|
|
129
|
+
gl.viewport(0, 0, width, height);
|
|
130
|
+
gl.uniform2f(uSize, width, height);
|
|
131
|
+
gl.uniform1f(uRatio, ratio);
|
|
132
|
+
gl.uniform1f(uTime, time);
|
|
133
|
+
for (const [name, location] of floats) gl.uniform1f(location, valueOf(values, passes, name));
|
|
134
|
+
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
135
|
+
},
|
|
136
|
+
dispose() {
|
|
137
|
+
gl.deleteTexture(glyphTexture);
|
|
138
|
+
gl.deleteVertexArray(vao);
|
|
139
|
+
gl.deleteProgram(program);
|
|
140
|
+
gl.deleteShader(vs);
|
|
141
|
+
gl.deleteShader(fs);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function bakeThumbnails(host, jobs, passes, width, height) {
|
|
146
|
+
const canvas = document.createElement("canvas");
|
|
147
|
+
canvas.width = width;
|
|
148
|
+
canvas.height = height;
|
|
149
|
+
const gl = canvas.getContext("webgl2", { antialias: false, alpha: false, preserveDrawingBuffer: true });
|
|
150
|
+
if (!gl) return jobs.map(() => "");
|
|
151
|
+
const urls = jobs.map(({ scene, values }) => {
|
|
152
|
+
const renderer = createRenderer(gl, scene, passes);
|
|
153
|
+
if (!renderer) return "";
|
|
154
|
+
renderer.setColours(coloursFor(host, scene));
|
|
155
|
+
renderer.setGlyphs(glyphFontFor(host));
|
|
156
|
+
renderer.draw(values, 20, width, height);
|
|
157
|
+
const url = canvas.toDataURL("image/jpeg", 0.85);
|
|
158
|
+
renderer.dispose();
|
|
159
|
+
return url;
|
|
160
|
+
});
|
|
161
|
+
gl.getExtension("WEBGL_lose_context")?.loseContext();
|
|
162
|
+
return urls;
|
|
163
|
+
}
|
|
164
|
+
export {
|
|
165
|
+
bakeThumbnails,
|
|
166
|
+
coloursFor,
|
|
167
|
+
createRenderer,
|
|
168
|
+
glyphFontFor,
|
|
169
|
+
readColours
|
|
170
|
+
};
|