reze-engine 0.69.4 → 0.69.6
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/dist/engine.d.ts +50 -4
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +321 -18
- package/dist/graph/slots.d.ts.map +1 -1
- package/dist/graph/slots.js +6 -2
- package/dist/shaders/directives.d.ts +32 -4
- package/dist/shaders/directives.d.ts.map +1 -1
- package/dist/shaders/directives.js +55 -1
- package/dist/shaders/materials/common.d.ts +10 -1
- package/dist/shaders/materials/common.d.ts.map +1 -1
- package/dist/shaders/materials/common.js +24 -1
- package/dist/shaders/passes/depth-prepass.d.ts.map +1 -1
- package/dist/shaders/passes/depth-prepass.js +4 -2
- package/dist/shaders/passes/outline.d.ts.map +1 -1
- package/dist/shaders/passes/outline.js +7 -1
- package/dist/shaders/passes/shadow.d.ts +1 -1
- package/dist/shaders/passes/shadow.d.ts.map +1 -1
- package/dist/shaders/text-api.d.ts +32 -0
- package/dist/shaders/text-api.d.ts.map +1 -0
- package/dist/shaders/text-api.js +132 -0
- package/package.json +1 -1
- package/src/engine.ts +329 -25
- package/src/graph/slots.ts +6 -2
- package/src/shaders/directives.ts +78 -5
- package/src/shaders/materials/common.ts +25 -1
- package/src/shaders/passes/depth-prepass.ts +4 -2
- package/src/shaders/passes/outline.ts +7 -1
- package/src/shaders/text-api.ts +144 -0
|
@@ -69,11 +69,16 @@ export type EffectParamOption = { id: string; labelZh?: string }
|
|
|
69
69
|
*/
|
|
70
70
|
export type EffectParamDecl = {
|
|
71
71
|
name: string
|
|
72
|
-
kind: "float" | "color" | "vec3" | "bool" | "enum" | "int" | "point" | "bone"
|
|
73
|
-
/** Numbers for float/vec3/int/point, 0 or 1 for bool, the option's index
|
|
74
|
-
* enum, a bone's NAME for bone; `#rrggbb` for a
|
|
75
|
-
* converts. */
|
|
72
|
+
kind: "float" | "color" | "vec3" | "bool" | "enum" | "int" | "point" | "bone" | "text" | "lag"
|
|
73
|
+
/** Numbers for float/vec3/int/point/lag, 0 or 1 for bool, the option's index
|
|
74
|
+
* for enum, a bone's NAME for bone, the words for text; `#rrggbb` for a
|
|
75
|
+
* colour, which the host converts. */
|
|
76
76
|
value: number | [number, number, number] | string
|
|
77
|
+
/** lag only: the float dial it trails, how many seconds it holds after that
|
|
78
|
+
* dial falls, and how much it then drains per second. */
|
|
79
|
+
source?: string
|
|
80
|
+
hold?: number
|
|
81
|
+
rate?: number
|
|
77
82
|
/** float and int only, and only when the author gave a range. */
|
|
78
83
|
min?: number
|
|
79
84
|
max?: number
|
|
@@ -162,6 +167,21 @@ export type EffectDirectives = {
|
|
|
162
167
|
* the engine's STEPPED_DIALS.
|
|
163
168
|
*/
|
|
164
169
|
stepped: boolean
|
|
170
|
+
/**
|
|
171
|
+
* `#xray X Y WIDTH HEIGHT` — a see-through window held in front of the
|
|
172
|
+
* lens, as X-ray glasses are: a rectangle on the frame, four float dials in
|
|
173
|
+
* fractions of the frame — X and Y its offset from the frame's centre (Y
|
|
174
|
+
* up), WIDTH and HEIGHT its size. Behind it, the materials the host marks on the characters it is aimed
|
|
175
|
+
* at (setXrayMaterials — their clothes) are not drawn, so what is under them
|
|
176
|
+
* shows. Whole while the effect plays at all. Null without.
|
|
177
|
+
*/
|
|
178
|
+
xray: { x: string; y: string; width: string; height: string } | null
|
|
179
|
+
/**
|
|
180
|
+
* `#font <css font, without the size> [tracking <em>]` — the face every
|
|
181
|
+
* `#param text` dial of this effect is set in, e.g. `#font 500 Georgia, serif
|
|
182
|
+
* tracking 0.12`. Null sets them in the platform's serif.
|
|
183
|
+
*/
|
|
184
|
+
font: { css: string; tracking: number } | null
|
|
165
185
|
/**
|
|
166
186
|
* How long ONE firing of this effect lasts, in seconds. 0 = undeclared.
|
|
167
187
|
*
|
|
@@ -196,6 +216,8 @@ const SPEC = {
|
|
|
196
216
|
dissolve: 0,
|
|
197
217
|
mirror: 0,
|
|
198
218
|
stepped: 0,
|
|
219
|
+
xray: 4,
|
|
220
|
+
font: "rest",
|
|
199
221
|
duration: 1,
|
|
200
222
|
ground: "rest",
|
|
201
223
|
} as const
|
|
@@ -275,6 +297,8 @@ export function parseDirectives(wgsl: string): DirectiveResult {
|
|
|
275
297
|
ground: null,
|
|
276
298
|
mirror: false,
|
|
277
299
|
stepped: false,
|
|
300
|
+
xray: null,
|
|
301
|
+
font: null,
|
|
278
302
|
duration: 0,
|
|
279
303
|
}
|
|
280
304
|
const errors: string[] = []
|
|
@@ -391,6 +415,26 @@ export function parseDirectives(wgsl: string): DirectiveResult {
|
|
|
391
415
|
d.params.push({ name, kind: "bone", value: rest[0], ...labels })
|
|
392
416
|
return
|
|
393
417
|
}
|
|
418
|
+
if (kind === "text") {
|
|
419
|
+
// Words, set by the engine into a picture the field module reads
|
|
420
|
+
// through rzText_NAME(uv). The default is the rest of the line up to
|
|
421
|
+
// its note, spaces kept as one.
|
|
422
|
+
d.params.push({ name, kind: "text", value: rest.join(" "), ...labels })
|
|
423
|
+
return
|
|
424
|
+
}
|
|
425
|
+
if (kind === "lag") {
|
|
426
|
+
// A float dial trailing another: what a game's health bar draws as
|
|
427
|
+
// the chunk just lost. Derived by the engine, so a host draws no
|
|
428
|
+
// control for it.
|
|
429
|
+
const hold = num(rest[1])
|
|
430
|
+
const rate = num(rest[2])
|
|
431
|
+
if (rest.length !== 3 || !/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(rest[0]) || hold === null || hold < 0 || rate === null || rate <= 0) {
|
|
432
|
+
errors.push(`${at}: #param lag ${name} takes the dial it trails, a hold in seconds and a drain per second, like "HEALTH 0.8 0.6"`)
|
|
433
|
+
return
|
|
434
|
+
}
|
|
435
|
+
d.params.push({ name, kind: "lag", value: 0, source: rest[0], hold, rate, ...labels })
|
|
436
|
+
return
|
|
437
|
+
}
|
|
394
438
|
if (kind === "enum") {
|
|
395
439
|
// Each choice is `id` or `id|中文`; a leading `*` marks the default,
|
|
396
440
|
// else the first is.
|
|
@@ -447,7 +491,7 @@ export function parseDirectives(wgsl: string): DirectiveResult {
|
|
|
447
491
|
d.params.push({ name, kind: "float", value: v, ...(lo !== null && hi !== null ? { min: lo, max: hi } : {}), ...labels })
|
|
448
492
|
return
|
|
449
493
|
}
|
|
450
|
-
errors.push(`${at}: #param kind must be float, int, color, vec3, point, bone, bool or
|
|
494
|
+
errors.push(`${at}: #param kind must be float, int, color, vec3, point, bone, bool, enum, text or lag`)
|
|
451
495
|
return
|
|
452
496
|
}
|
|
453
497
|
case "halfres":
|
|
@@ -522,6 +566,21 @@ export function parseDirectives(wgsl: string): DirectiveResult {
|
|
|
522
566
|
case "stepped":
|
|
523
567
|
d.stepped = true
|
|
524
568
|
return
|
|
569
|
+
case "xray":
|
|
570
|
+
d.xray = { x: args[0], y: args[1], width: args[2], height: args[3] }
|
|
571
|
+
return
|
|
572
|
+
case "font": {
|
|
573
|
+
const words = args.join(" ")
|
|
574
|
+
const m = /^(.*?)(?:\s+tracking\s+(-?[\d.]+))?$/.exec(words)
|
|
575
|
+
const css = (m?.[1] ?? "").trim()
|
|
576
|
+
const tracking = m?.[2] !== undefined ? num(m[2]) : 0
|
|
577
|
+
if (!css || tracking === null) {
|
|
578
|
+
errors.push(`${at}: #font takes a CSS font without its size, like "500 Georgia, serif", and optionally "tracking 0.1"`)
|
|
579
|
+
return
|
|
580
|
+
}
|
|
581
|
+
d.font = { css, tracking }
|
|
582
|
+
return
|
|
583
|
+
}
|
|
525
584
|
case "duration": {
|
|
526
585
|
// SECONDS, like every other time a directive states (see #dissolve).
|
|
527
586
|
// The document above works in frames and converts once; an author
|
|
@@ -550,6 +609,20 @@ export function parseDirectives(wgsl: string): DirectiveResult {
|
|
|
550
609
|
}
|
|
551
610
|
})
|
|
552
611
|
|
|
612
|
+
// #xray names four dials, which have to be there and be numbers.
|
|
613
|
+
if (d.xray) {
|
|
614
|
+
for (const name of [d.xray.x, d.xray.y, d.xray.width, d.xray.height]) {
|
|
615
|
+
if (d.params.find((p) => p.name === name)?.kind !== "float") errors.push(`#xray: ${name} has to be a #param float`)
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// A lag trails a number that has to be there.
|
|
620
|
+
for (const p of d.params) {
|
|
621
|
+
if (p.kind !== "lag") continue
|
|
622
|
+
const src = d.params.find((q) => q.name === p.source)
|
|
623
|
+
if (src?.kind !== "float" && src?.kind !== "int") errors.push(`#param lag ${p.name}: ${p.source} has to be a #param float or int`)
|
|
624
|
+
}
|
|
625
|
+
|
|
553
626
|
// Duplicates are an author editing one line and forgetting another; last-wins
|
|
554
627
|
// is a coin toss they never see resolved.
|
|
555
628
|
const names = new Set<string>()
|
|
@@ -96,7 +96,30 @@ fn rzJellyVertex(p: vec3f, joints: vec4<u32>, nw: vec4f) -> vec3f {
|
|
|
96
96
|
/** A model's ObjectLight as WGSL declares it (see the layout below). One copy
|
|
97
97
|
* for every pass that binds it, so their layouts cannot drift. */
|
|
98
98
|
export const OBJECT_LIGHT_STRUCT_WGSL =
|
|
99
|
-
"struct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f, jelly: array<vec4f, 12>, bloom: vec4f }"
|
|
99
|
+
"struct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f, jelly: array<vec4f, 12>, bloom: vec4f, xray: array<vec4f, 4>, xrayMask: array<vec4u, 16> }"
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The see-through window (#xray): a rectangle on the frame, in pixels —
|
|
103
|
+
* xray[0] is (left, top, right, bottom), xray[3].x is 1 while a window is open
|
|
104
|
+
* on this model — and which of the model's materials it cuts: a bit a
|
|
105
|
+
* material, by its 1-based draw index (materialId), up to 512. Tested against
|
|
106
|
+
* the fragment's own position, so it sits where the lens is, whatever moves
|
|
107
|
+
* behind it. Declared after the pass's own `objectLight` binding.
|
|
108
|
+
*/
|
|
109
|
+
export const XRAY_WGSL = /* wgsl */ `
|
|
110
|
+
fn rz_xray_inside(fp: vec4f) -> bool {
|
|
111
|
+
if (objectLight.xray[3].x < 0.5) { return false; }
|
|
112
|
+
let r = objectLight.xray[0];
|
|
113
|
+
return fp.x >= r.x && fp.y >= r.y && fp.x <= r.z && fp.y <= r.w;
|
|
114
|
+
}
|
|
115
|
+
fn rz_xray_cut(fp: vec4f, materialId: f32) -> bool {
|
|
116
|
+
let i = u32(materialId);
|
|
117
|
+
if (i >= 512u) { return false; }
|
|
118
|
+
let word = objectLight.xrayMask[i / 128u][(i / 32u) % 4u];
|
|
119
|
+
if (((word >> (i % 32u)) & 1u) == 0u) { return false; }
|
|
120
|
+
return rz_xray_inside(fp);
|
|
121
|
+
}
|
|
122
|
+
`
|
|
100
123
|
|
|
101
124
|
export const COMMON_BINDINGS_WGSL = /* wgsl */ `
|
|
102
125
|
|
|
@@ -297,6 +320,7 @@ fn _rzCastShadow(wp: vec3f) -> f32 {
|
|
|
297
320
|
// outlines write.
|
|
298
321
|
${OBJECT_LIGHT_STRUCT_WGSL}
|
|
299
322
|
@group(1) @binding(1) var<uniform> objectLight: ObjectLight;
|
|
323
|
+
${XRAY_WGSL}
|
|
300
324
|
|
|
301
325
|
${JELLY_VERTEX_WGSL}
|
|
302
326
|
/** The light arriving at a surface of this object facing n: its own ambient
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
// attachment, and that answer does not exist at import time. The constant could
|
|
28
28
|
// not have taken the outputs at all, which is most of why it did not have them.
|
|
29
29
|
|
|
30
|
-
import { DISSOLVE_WGSL, JELLY_VERTEX_WGSL } from "../materials/common"
|
|
30
|
+
import { DISSOLVE_WGSL, JELLY_VERTEX_WGSL, OBJECT_LIGHT_STRUCT_WGSL, XRAY_WGSL } from "../materials/common"
|
|
31
31
|
import { sceneFsOutWgsl, sceneIdPadWgsl } from "./scene-contract"
|
|
32
32
|
|
|
33
33
|
export function transparentDepthPrepassWgsl(): string {
|
|
@@ -55,8 +55,9 @@ struct MaterialUniforms {
|
|
|
55
55
|
@group(1) @binding(0) var<storage, read> skinMats: array<mat4x4f>;
|
|
56
56
|
// The model's ObjectLight, for its jelly eyes: depth goes down where the colour
|
|
57
57
|
// pass will draw the iris.
|
|
58
|
-
|
|
58
|
+
${OBJECT_LIGHT_STRUCT_WGSL}
|
|
59
59
|
@group(1) @binding(1) var<uniform> objectLight: ObjectLight;
|
|
60
|
+
${XRAY_WGSL}
|
|
60
61
|
${JELLY_VERTEX_WGSL}
|
|
61
62
|
@group(2) @binding(0) var diffuseTexture: texture_2d<f32>;
|
|
62
63
|
@group(2) @binding(1) var<uniform> material: MaterialUniforms;
|
|
@@ -118,6 +119,7 @@ override CUTOFF: f32 = 0.5;
|
|
|
118
119
|
// her: you would see sky through her, which is the failure this line exists
|
|
119
120
|
// to prevent.
|
|
120
121
|
if (material.dissolve < 0.9995 && rz_dissolve_threshold(in.restPos) > material.dissolve) { discard; }
|
|
122
|
+
if (rz_xray_cut(in.position, material.materialId)) { discard; }
|
|
121
123
|
var out: PrepassOut;
|
|
122
124
|
out.color = vec4f(0.0);
|
|
123
125
|
out.mask = vec4f(0.0);
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
// a string baked at import cannot know what the device said.
|
|
18
18
|
|
|
19
19
|
import { sceneFsOutWgsl, sceneIdPadWgsl } from "./scene-contract"
|
|
20
|
-
import { DISSOLVE_WGSL, OBJECT_LIGHT_STRUCT_WGSL } from "../materials/common"
|
|
20
|
+
import { DISSOLVE_WGSL, OBJECT_LIGHT_STRUCT_WGSL, XRAY_WGSL } from "../materials/common"
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* Where `dissolve` sits in the outline's own uniform, in bytes.
|
|
@@ -79,6 +79,9 @@ struct MaterialUniforms {
|
|
|
79
79
|
* material's own alpha still holds — a line its author made sheer stays so. */
|
|
80
80
|
colorOverride: vec4f,
|
|
81
81
|
shadowColor: vec4f,
|
|
82
|
+
/** x: the material's 1-based draw index, as the colour pass's materialId —
|
|
83
|
+
* what lets the see-through window cut a garment's line and keep hers. */
|
|
84
|
+
xray: vec4f,
|
|
82
85
|
};
|
|
83
86
|
|
|
84
87
|
struct OutlineLight { direction: vec4f, color: vec4f, };
|
|
@@ -91,6 +94,7 @@ struct OutlineLights { ambientColor: vec4f, lights: array<OutlineLight, 4>, };
|
|
|
91
94
|
@group(1) @binding(0) var<storage, read> skinMats: array<mat4x4f>;
|
|
92
95
|
${OBJECT_LIGHT_STRUCT_WGSL}
|
|
93
96
|
@group(1) @binding(1) var<uniform> objectLight: ObjectLight;
|
|
97
|
+
${XRAY_WGSL}
|
|
94
98
|
@group(2) @binding(0) var<uniform> material: MaterialUniforms;
|
|
95
99
|
@group(2) @binding(1) var diffuseTexture: texture_2d<f32>;
|
|
96
100
|
/** A look's outline tint (ShaderGraph.outline.tint): one of its images, white
|
|
@@ -269,6 +273,8 @@ ${sceneFsOutWgsl({ name: "FSOut", aux: "mask" })}
|
|
|
269
273
|
// bind-pose position — three passes, one rule, or they disagree about which
|
|
270
274
|
// pieces are still there.
|
|
271
275
|
if (material.dissolve < 0.9995 && rz_dissolve_threshold(input.restPos) > material.dissolve) { discard; }
|
|
276
|
+
// The see-through window cuts the lines of what it cuts, and keeps hers.
|
|
277
|
+
if (rz_xray_cut(input.position, material.xray.x)) { discard; }
|
|
272
278
|
let tex = textureSample(diffuseTexture, edgeSampler, input.uv);
|
|
273
279
|
let texA = tex.a;
|
|
274
280
|
if (texA < 0.05) {
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// Words an effect is handed — `#param text NAME <default words>`.
|
|
2
|
+
//
|
|
3
|
+
// The engine sets every string an effect's text dials can show (each dial's
|
|
4
|
+
// value and every key on its track) into ONE picture per effect, a row a
|
|
5
|
+
// string, and a dial's slot in the params block holds the rect of the string it
|
|
6
|
+
// shows now. Changing the words, by hand or by a key, is a rect write; the
|
|
7
|
+
// picture is redrawn only when a string it has never held arrives.
|
|
8
|
+
//
|
|
9
|
+
// Canvas2D, as the lyric atlas: it sets every script the platform does, CJK
|
|
10
|
+
// included, which a glyph atlas cannot promise. The look — fill, edge, glow —
|
|
11
|
+
// is the author's, in WGSL, from the coverage rzText_NAME answers.
|
|
12
|
+
//
|
|
13
|
+
// FIELD MODULE ONLY, where the picture is bound. Every other module the file is
|
|
14
|
+
// spliced into gets stubs answering zero, so one effect file compiles in every
|
|
15
|
+
// mount it has.
|
|
16
|
+
|
|
17
|
+
import type { LyricRect } from "./lyrics-api"
|
|
18
|
+
|
|
19
|
+
/** The font size the rows are set at, in texels. A boss's name drawn 60 pixels
|
|
20
|
+
* tall on a 4K frame is still minified from this. */
|
|
21
|
+
export const EFFECT_TEXT_PX = 96
|
|
22
|
+
const PAD = 8
|
|
23
|
+
const MAX_W = 4096
|
|
24
|
+
const MAX_H = 8192
|
|
25
|
+
|
|
26
|
+
export type EffectTextFont = { css: string; tracking: number } | null
|
|
27
|
+
|
|
28
|
+
/** The CSS font for a size: `#font`'s words with the size put where CSS wants
|
|
29
|
+
* it — after style, variant and weight, before the family. */
|
|
30
|
+
export function effectTextFont(font: EffectTextFont, px: number): string {
|
|
31
|
+
const css = font?.css ?? "serif"
|
|
32
|
+
const words = css.split(/\s+/)
|
|
33
|
+
let k = 0
|
|
34
|
+
while (k < words.length - 1 && /^(normal|italic|oblique|small-caps|bold|bolder|lighter|\d{3})$/i.test(words[k])) k++
|
|
35
|
+
return [...words.slice(0, k), `${px}px`, words.slice(k).join(" ")].join(" ").trim()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type Ctx2D = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D
|
|
39
|
+
|
|
40
|
+
function canvas2d(w: number, h: number): { canvas: HTMLCanvasElement | OffscreenCanvas; ctx: Ctx2D } | null {
|
|
41
|
+
if (typeof OffscreenCanvas !== "undefined") {
|
|
42
|
+
const canvas = new OffscreenCanvas(w, h)
|
|
43
|
+
const ctx = canvas.getContext("2d")
|
|
44
|
+
if (ctx) return { canvas, ctx }
|
|
45
|
+
}
|
|
46
|
+
if (typeof document === "undefined") return null
|
|
47
|
+
const canvas = document.createElement("canvas")
|
|
48
|
+
canvas.width = w
|
|
49
|
+
canvas.height = h
|
|
50
|
+
const ctx = canvas.getContext("2d")
|
|
51
|
+
return ctx ? { canvas, ctx } : null
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type EffectTextSheet = {
|
|
55
|
+
source: HTMLCanvasElement | OffscreenCanvas
|
|
56
|
+
width: number
|
|
57
|
+
height: number
|
|
58
|
+
/** Where each string sits: u0, vTop, u1, vBottom, in 0..1. */
|
|
59
|
+
rects: Map<string, LyricRect>
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Set `strings` into one sheet, a row each. Every row is the font's full box
|
|
64
|
+
* (its ascent and descent, not the string's own ink), so two strings in one
|
|
65
|
+
* face have one height and their aspects compare. A string too wide for the
|
|
66
|
+
* sheet is set smaller in its own row rather than cut. Null without a canvas.
|
|
67
|
+
*/
|
|
68
|
+
export function rasterEffectText(strings: readonly string[], font: EffectTextFont): EffectTextSheet | null {
|
|
69
|
+
const list = [...new Set(strings.filter((s) => s.length > 0))]
|
|
70
|
+
const probe = canvas2d(1, 1)
|
|
71
|
+
if (!probe) return null
|
|
72
|
+
const css = effectTextFont(font, EFFECT_TEXT_PX)
|
|
73
|
+
const tracking = (font?.tracking ?? 0) * EFFECT_TEXT_PX
|
|
74
|
+
const setUp = (ctx: Ctx2D, px: number) => {
|
|
75
|
+
ctx.font = px === EFFECT_TEXT_PX ? css : effectTextFont(font, px)
|
|
76
|
+
if ("letterSpacing" in ctx) (ctx as { letterSpacing: string }).letterSpacing = `${(tracking * px) / EFFECT_TEXT_PX}px`
|
|
77
|
+
ctx.textBaseline = "alphabetic"
|
|
78
|
+
ctx.textAlign = "left"
|
|
79
|
+
}
|
|
80
|
+
setUp(probe.ctx, EFFECT_TEXT_PX)
|
|
81
|
+
const box = probe.ctx.measureText("Hg国")
|
|
82
|
+
const ascent = Math.ceil(box.fontBoundingBoxAscent || EFFECT_TEXT_PX * 0.9)
|
|
83
|
+
const descent = Math.ceil(box.fontBoundingBoxDescent || EFFECT_TEXT_PX * 0.25)
|
|
84
|
+
const rowH = ascent + descent + PAD * 2
|
|
85
|
+
const rows = list.map((s) => {
|
|
86
|
+
// Tracking adds after every glyph, the last one too; trim it off the box.
|
|
87
|
+
const w = probe.ctx.measureText(s).width - tracking
|
|
88
|
+
const px = w + PAD * 2 > MAX_W ? Math.floor((EFFECT_TEXT_PX * (MAX_W - PAD * 2)) / w) : EFFECT_TEXT_PX
|
|
89
|
+
return { s, px, w: Math.min(MAX_W, Math.ceil((w * px) / EFFECT_TEXT_PX) + PAD * 2) }
|
|
90
|
+
})
|
|
91
|
+
const width = Math.max(1, ...rows.map((r) => r.w))
|
|
92
|
+
const height = Math.max(1, Math.min(MAX_H, rows.length * rowH))
|
|
93
|
+
const sheet = canvas2d(width, height)
|
|
94
|
+
if (!sheet) return null
|
|
95
|
+
const { ctx } = sheet
|
|
96
|
+
ctx.clearRect(0, 0, width, height)
|
|
97
|
+
ctx.fillStyle = "#ffffff"
|
|
98
|
+
const rects = new Map<string, LyricRect>()
|
|
99
|
+
rows.forEach((r, i) => {
|
|
100
|
+
const top = i * rowH
|
|
101
|
+
if (top + rowH > height) return
|
|
102
|
+
setUp(ctx, r.px)
|
|
103
|
+
// A smaller row stays centred on the same baseline band.
|
|
104
|
+
const base = top + rowH / 2 + ((ascent - descent) * r.px) / (2 * EFFECT_TEXT_PX)
|
|
105
|
+
ctx.fillText(r.s, PAD, base)
|
|
106
|
+
rects.set(r.s, [0, top / height, r.w / width, (top + rowH) / height])
|
|
107
|
+
})
|
|
108
|
+
return { source: sheet.canvas, width, height, rects }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* rzText_NAME(uv) and rzTextAspect_NAME() for each text dial. uv runs 0..1
|
|
113
|
+
* across the string's own box, y up, like rzLyricText; the answer is glyph
|
|
114
|
+
* coverage. textureSampleLevel, so it is legal after any branch.
|
|
115
|
+
*/
|
|
116
|
+
export function effectTextWgsl(names: readonly string[], real: boolean, binding: number, sampler: string): string {
|
|
117
|
+
if (names.length === 0) return ""
|
|
118
|
+
if (!real) {
|
|
119
|
+
return names
|
|
120
|
+
.map((n) => `fn rzText_${n}(uv: vec2f) -> f32 { return 0.0; }\nfn rzTextAspect_${n}() -> f32 { return 1.0; }\n`)
|
|
121
|
+
.join("")
|
|
122
|
+
}
|
|
123
|
+
return (
|
|
124
|
+
/* wgsl */ `
|
|
125
|
+
@group(0) @binding(${binding}) var _rzEffectText: texture_2d<f32>;
|
|
126
|
+
|
|
127
|
+
fn _rzTextAt(r: vec4f, uv: vec2f) -> f32 {
|
|
128
|
+
if (r.z <= r.x || uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) { return 0.0; }
|
|
129
|
+
let at = vec2f(mix(r.x, r.z, uv.x), mix(r.w, r.y, uv.y));
|
|
130
|
+
return textureSampleLevel(_rzEffectText, ${sampler}, at, 0.0).r;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
fn _rzTextAspect(r: vec4f) -> f32 {
|
|
134
|
+
let h = r.w - r.y;
|
|
135
|
+
if (h <= 0.0) { return 1.0; }
|
|
136
|
+
let dim = vec2f(textureDimensions(_rzEffectText));
|
|
137
|
+
return ((r.z - r.x) * dim.x) / (h * dim.y);
|
|
138
|
+
}
|
|
139
|
+
` +
|
|
140
|
+
names
|
|
141
|
+
.map((n) => `fn rzText_${n}(uv: vec2f) -> f32 { return _rzTextAt(params.${n}, uv); }\nfn rzTextAspect_${n}() -> f32 { return _rzTextAspect(params.${n}); }\n`)
|
|
142
|
+
.join("")
|
|
143
|
+
)
|
|
144
|
+
}
|