reze-engine 0.69.5 → 0.69.7

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.
@@ -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 for
74
- * enum, a bone's NAME for bone; `#rrggbb` for a colour, which the host
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
@@ -171,6 +176,12 @@ export type EffectDirectives = {
171
176
  * shows. Whole while the effect plays at all. Null without.
172
177
  */
173
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
174
185
  /**
175
186
  * How long ONE firing of this effect lasts, in seconds. 0 = undeclared.
176
187
  *
@@ -206,6 +217,7 @@ const SPEC = {
206
217
  mirror: 0,
207
218
  stepped: 0,
208
219
  xray: 4,
220
+ font: "rest",
209
221
  duration: 1,
210
222
  ground: "rest",
211
223
  } as const
@@ -286,6 +298,7 @@ export function parseDirectives(wgsl: string): DirectiveResult {
286
298
  mirror: false,
287
299
  stepped: false,
288
300
  xray: null,
301
+ font: null,
289
302
  duration: 0,
290
303
  }
291
304
  const errors: string[] = []
@@ -402,6 +415,26 @@ export function parseDirectives(wgsl: string): DirectiveResult {
402
415
  d.params.push({ name, kind: "bone", value: rest[0], ...labels })
403
416
  return
404
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
+ }
405
438
  if (kind === "enum") {
406
439
  // Each choice is `id` or `id|中文`; a leading `*` marks the default,
407
440
  // else the first is.
@@ -458,7 +491,7 @@ export function parseDirectives(wgsl: string): DirectiveResult {
458
491
  d.params.push({ name, kind: "float", value: v, ...(lo !== null && hi !== null ? { min: lo, max: hi } : {}), ...labels })
459
492
  return
460
493
  }
461
- errors.push(`${at}: #param kind must be float, int, color, vec3, point, bone, bool or enum`)
494
+ errors.push(`${at}: #param kind must be float, int, color, vec3, point, bone, bool, enum, text or lag`)
462
495
  return
463
496
  }
464
497
  case "halfres":
@@ -536,6 +569,18 @@ export function parseDirectives(wgsl: string): DirectiveResult {
536
569
  case "xray":
537
570
  d.xray = { x: args[0], y: args[1], width: args[2], height: args[3] }
538
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
+ }
539
584
  case "duration": {
540
585
  // SECONDS, like every other time a directive states (see #dissolve).
541
586
  // The document above works in frames and converts once; an author
@@ -571,6 +616,13 @@ export function parseDirectives(wgsl: string): DirectiveResult {
571
616
  }
572
617
  }
573
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
+
574
626
  // Duplicates are an author editing one line and forgetting another; last-wins
575
627
  // is a coin toss they never see resolved.
576
628
  const names = new Set<string>()
@@ -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
+ }