reze-engine 0.70.2 → 0.70.4

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/src/model.ts CHANGED
@@ -75,12 +75,15 @@ const BLINK_PAIRS: [string, string][] = [
75
75
  const BLINK_CLOSE = 0.09
76
76
  const BLINK_HOLD = 0.04
77
77
  const BLINK_OPEN = 0.2
78
- /** How often a blink is doubled, and how soon the second follows (at speed 1).
79
- * Start to start: 0.6 s leaves the eyes clearly open for about a quarter
80
- * second between the two. At 0.4 the second began while the first was still
81
- * opening, and the pair read as one long, stuttering blink. */
82
- const BLINK_DOUBLE = 0.15
83
- const BLINK_DOUBLE_GAP = 0.6
78
+ /** How often a blink is doubled, how soon the second follows (start to start,
79
+ * at speed 1) and how much quicker both blinks of the pair run. A double is a
80
+ * flutter: two quick blinks with a glimpse of open eye between, about half a
81
+ * second in all. Two ordinary blinks 0.6 s apart read as just two blinks, and
82
+ * at 15% one came every half minute, so nobody saw it. At 1.8x a blink takes
83
+ * 0.18 s, which leaves the eyes open about 0.12 s before the second. */
84
+ const BLINK_DOUBLE = 0.3
85
+ const BLINK_DOUBLE_GAP = 0.3
86
+ const BLINK_DOUBLE_PACE = 1.8
84
87
 
85
88
  /** What setAutoBlink asks for. */
86
89
  export interface AutoBlinkOptions {
@@ -1362,6 +1365,8 @@ export class Model {
1362
1365
  count: number
1363
1366
  /** Whether the current blink is the first of a double. */
1364
1367
  double: boolean
1368
+ /** Whether it is either blink of a double: those run quicker. */
1369
+ pair: boolean
1365
1370
  options: Required<AutoBlinkOptions>
1366
1371
  } | null = null
1367
1372
 
@@ -1411,6 +1416,7 @@ export class Model {
1411
1416
  next: blinkGap(0, opts.interval),
1412
1417
  count: 0,
1413
1418
  double: false,
1419
+ pair: false,
1414
1420
  options: opts,
1415
1421
  }
1416
1422
  return true
@@ -1447,6 +1453,7 @@ export class Model {
1447
1453
  b.start = -1
1448
1454
  b.count = 0
1449
1455
  b.double = false
1456
+ b.pair = false
1450
1457
  b.next = blinkGap(0, interval)
1451
1458
  }
1452
1459
  // A loop, so a long jump forward lands on the blink it is in, not on each one passed.
@@ -1454,10 +1461,12 @@ export class Model {
1454
1461
  b.start = b.next
1455
1462
  b.count++
1456
1463
  // Now and then a second blink follows straight after the first — never a third.
1457
- b.double = double && !b.double && blinkHash(b.count * 7 + 3) < BLINK_DOUBLE
1464
+ const second = b.double
1465
+ b.double = double && !second && blinkHash(b.count * 7 + 3) < BLINK_DOUBLE
1466
+ b.pair = b.double || second
1458
1467
  b.next = b.start + (b.double ? BLINK_DOUBLE_GAP / speed : blinkGap(b.count, interval))
1459
1468
  }
1460
- const w = blinkCurve((b.clock - b.start) * speed)
1469
+ const w = blinkCurve((b.clock - b.start) * speed * (b.pair ? BLINK_DOUBLE_PACE : 1))
1461
1470
  let changed = false
1462
1471
  for (let i = 0; i < b.morphs.length; i++) {
1463
1472
  if (weights[b.morphs[i]] !== w) {
@@ -269,11 +269,15 @@ fn fs(@builtin(position) pos: vec4f) -> @location(0) f32 {
269
269
  * holding a very large number, so an effect keyed on it draws nothing instead of
270
270
  * failing to compile. Same rule the grid's accessor follows, and the id
271
271
  * accessors before it: an author should never have to guard a name.
272
+ *
273
+ * `sampler` names a sampler the module already declares. It used to declare
274
+ * its own at the grid sampler's binding, which WGSL accepts until one entry
275
+ * point uses both — an effect calling rzGrid AND rzCastDistance then failed to
276
+ * install with "multiple variables that use the same resource binding".
272
277
  */
273
- export function castDistanceApi(group: number, tex: number, samp: number, scale: number): string {
278
+ export function castDistanceApi(group: number, tex: number, sampler: string, scale: number): string {
274
279
  return /* wgsl */ `
275
280
  @group(${group}) @binding(${tex}) var _rzCastDistTex: texture_2d<f32>;
276
- @group(${group}) @binding(${samp}) var _rzCastDistSamp: sampler;
277
281
 
278
282
  /**
279
283
  * Distance from this point to the nearest pixel the CAST drew, in SCREEN pixels.
@@ -301,7 +305,7 @@ export function castDistanceApi(group: number, tex: number, samp: number, scale:
301
305
  */
302
306
  fn rzCastDistance(uv: vec2f) -> f32 {
303
307
  let flipped = vec2f(clamp(uv.x, 0.0, 1.0), 1.0 - clamp(uv.y, 0.0, 1.0));
304
- let d = textureSampleLevel(_rzCastDistTex, _rzCastDistSamp, flipped, 0.0).x;
308
+ let d = textureSampleLevel(_rzCastDistTex, ${sampler}, flipped, 0.0).x;
305
309
  return d * ${scale > 0 ? scale : 1}.0;
306
310
  }
307
311
  `
@@ -843,6 +843,14 @@ export function buildCompositeShader(effect?: CompositeEffectSource | null): str
843
843
  body
844
844
  }
845
845
 
846
+ /** The field pass's light: the materials' light uniform, the world's sky and a
847
+ * mip sampler for it — rzSunDir, rzSunColor, rzWorldAmbient, rzWorldSpecular.
848
+ * The sky is the field layout's sixteenth sampled texture, which is WebGPU's
849
+ * default per-stage cap, so the sun's shadow atlas is not bound here. */
850
+ export const FIELD_LIGHT_BINDING = 34
851
+ export const FIELD_SKY_BINDING = 35
852
+ export const FIELD_SKY_SAMPLER_BINDING = 36
853
+
846
854
  /**
847
855
  * The field pass: the user's background/foreground mounts at half resolution,
848
856
  * into two rgba16f targets the composite bilinearly upsamples.
@@ -890,13 +898,16 @@ export function buildFieldShader(effect: CompositeEffectSource): string {
890
898
  viewportApi("viewU[6].w") +
891
899
  trailSlotsApi(effect.trailCount) +
892
900
  idApi(effect.ids === true, 0, 23) +
893
- sceneLightApi(false, 0, 0) +
901
+ sceneLightApi(true, 0, FIELD_LIGHT_BINDING, {
902
+ shadow: false,
903
+ sky: { texture: FIELD_SKY_BINDING, sampler: FIELD_SKY_SAMPLER_BINDING },
904
+ }) +
894
905
  pointsApi(false) +
895
906
  textureApi(0) +
896
907
  // Distance to the cast, in SCREEN pixels: the field is half-res, so a field
897
908
  // texel is CAST_FIELD_DIV of them and the accessor scales on the way out.
898
909
  // An author writes the width they mean and never learns how it is built.
899
- castDistanceApi(0, 26, 18, CAST_FIELD_DIV) +
910
+ castDistanceApi(0, 26, "_rzGridSamp", CAST_FIELD_DIV) +
900
911
  sceneTapApi(0, 27, 28, [29, 30, 31, 32]) +
901
912
  "\n// ── user effect (setEffect) ──\n" +
902
913
  effect.paramsDecl +
@@ -114,6 +114,12 @@ export const SCENE_LIGHT_API_BINDINGS = 4
114
114
  * `rzWorldAmbient(n)`: the world's light arriving at a surface facing `n` —
115
115
  * the materials' own ambient, so an effect's shade can match hers.
116
116
  *
117
+ * `rzSunDir()` points toward the sun; `rzSunColor()` is the light it brings,
118
+ * the materials' colour × intensity. `rzWorldSpecular(dir, roughness)` is the
119
+ * sky seen along `dir` — the HDRI at the materials' own blur for that
120
+ * roughness, or the ambient by direction where the world has no picture or
121
+ * the module has no sky bound. All scene-linear.
122
+ *
117
123
  * With `on`, declares four bindings from `binding`:
118
124
  * +0 the light uniform (ambient, the sun — direction, and in .w the shadow
119
125
  * switch — and the HDRI's irradiance)
@@ -126,23 +132,67 @@ export const SCENE_LIGHT_API_BINDINGS = 4
126
132
  * own N·L. The point is nudged toward the sun instead, which lifts a blade's
127
133
  * root off a stage floor that casts into the same map.
128
134
  */
129
- export function sceneLightApi(on: boolean, group: number, binding: number): string {
135
+ export function sceneLightApi(
136
+ on: boolean,
137
+ group: number,
138
+ binding: number,
139
+ opts: {
140
+ /** Bind the shadow block, atlas and comparison sampler at binding+1..+3.
141
+ * Off, only the light uniform is bound and rzShadow answers lit. */
142
+ shadow?: boolean
143
+ /** The world's sky (equirect, prefiltered by roughness) and a mip sampler. */
144
+ sky?: { texture: number; sampler: number }
145
+ } = {},
146
+ ): string {
130
147
  if (!on) {
131
148
  return /* wgsl */ `
132
149
  fn rzShadow(p: vec3f) -> f32 { return 1.0; }
133
150
  fn rzWorldAmbient(n: vec3f) -> vec3f { return vec3f(0.0); }
134
151
  fn rzWorldAmbientAvg() -> vec3f { return vec3f(0.0); }
152
+ fn rzSunDir() -> vec3f { return vec3f(0.0, 1.0, 0.0); }
153
+ fn rzSunColor() -> vec3f { return vec3f(0.0); }
154
+ fn rzWorldSpecular(dir: vec3f, roughness: f32) -> vec3f { return vec3f(0.0); }
135
155
  `
136
156
  }
137
- const n = SHADOW_CASCADES.length
138
- return /* wgsl */ `
157
+ const shadow = opts.shadow ?? true
158
+ const sky = opts.sky
159
+ ? /* wgsl */ `
160
+ @group(${group}) @binding(${opts.sky.texture}) var _rzSkyTex: texture_2d<f32>;
161
+ @group(${group}) @binding(${opts.sky.sampler}) var _rzSkySamp: sampler;
162
+ fn rzWorldSpecular(dir: vec3f, roughness: f32) -> vec3f {
163
+ // The materials' lookup (nodes.ts), restated against this module's names:
164
+ // 2 in sh[0].w means an HDRI is installed; otherwise the ambient by
165
+ // direction is the honest blurred sky, and the bound 1x1 is never read.
166
+ if (_rzLight.sh[0].w < 1.5) { return rzWorldAmbient(normalize(dir)); }
167
+ let d = normalize(dir);
168
+ let u = 0.5 + atan2(d.x, d.z) * 0.15915494309;
169
+ let v = 0.5 - asin(clamp(d.y, -1.0, 1.0)) * 0.31830988618;
170
+ let levels = f32(textureNumLevels(_rzSkyTex) - 1u);
171
+ let lod = clamp(sqrt(clamp(roughness, 0.0, 1.0)) * levels, 0.0, levels);
172
+ return textureSampleLevel(_rzSkyTex, _rzSkySamp, vec2f(u, v), lod).rgb * _rzLight.ambientColor.w;
173
+ }
174
+ `
175
+ : /* wgsl */ `
176
+ fn rzWorldSpecular(dir: vec3f, roughness: f32) -> vec3f { return rzWorldAmbient(normalize(dir)); }
177
+ `
178
+ const sun = /* wgsl */ `
179
+ fn rzSunDir() -> vec3f { return normalize(-_rzLight.lights[0].direction.xyz); }
180
+ fn rzSunColor() -> vec3f { return _rzLight.lights[0].color.rgb * _rzLight.lights[0].color.w; }
181
+ `
182
+ const head = /* wgsl */ `
139
183
  // The light uniform, as the materials lay it out; lights[0] is the sun.
140
184
  struct _RzSceneLight { direction: vec4f, color: vec4f, }
141
185
  struct _RzSceneLightU { ambientColor: vec4f, lights: array<_RzSceneLight, 4>, sh: array<vec4f, 9>, }
186
+ @group(${group}) @binding(${binding}) var<uniform> _rzLight: _RzSceneLightU;
187
+ `
188
+ if (!shadow) {
189
+ return head + sun + sky + "\nfn rzShadow(p: vec3f) -> f32 { return 1.0; }\n" + worldAmbientWgsl("_rzLight")
190
+ }
191
+ const n = SHADOW_CASCADES.length
192
+ return head + sun + sky + /* wgsl */ `
142
193
  // The cascades' matrices, inner to outer, then every caster in one sphere
143
194
  // (x, y, z, radius): radius 0 = nothing casts, negative = do not test.
144
195
  struct _RzShadowBlock { viewProj: array<mat4x4f, ${n}>, casters: vec4f, }
145
- @group(${group}) @binding(${binding}) var<uniform> _rzLight: _RzSceneLightU;
146
196
  @group(${group}) @binding(${binding + 1}) var<uniform> _rzShadowVP: _RzShadowBlock;
147
197
  @group(${group}) @binding(${binding + 2}) var _rzShadowAtlas: texture_depth_2d;
148
198
  @group(${group}) @binding(${binding + 3}) var _rzShadowCmp: sampler_comparison;
@@ -201,9 +201,19 @@ fn rzSceneHit(uv: vec2f) -> bool {
201
201
  * is the identity, so writing it out is both safer and equivalent.
202
202
  */
203
203
  fn rzSceneDisplay(uv: vec2f) -> vec3f {
204
- var c = max(viewTransform(rzScene(uv) * exp2(viewU[0].x)), vec3f(0.0));
205
- if (viewU[9].w > 0.5) { c = _rzGradeScene(c); }
206
- return pow(max(c, vec3f(0.0)), vec3f(viewU[0].y));
204
+ return rzDisplay(rzScene(uv));
205
+ }
206
+
207
+ /**
208
+ * Scene-linear light to what the screen shows, the way the scene's own pixels
209
+ * get there: exposure, the view transform, the grade, the output gamma. For an
210
+ * effect that shades in light (rzWorldSpecular, rzSunColor) and returns
211
+ * display colour, so a highlight it draws rolls off exactly as hers do.
212
+ */
213
+ fn rzDisplay(c: vec3f) -> vec3f {
214
+ var o = max(viewTransform(c * exp2(viewU[0].x)), vec3f(0.0));
215
+ if (viewU[9].w > 0.5) { o = _rzGradeScene(o); }
216
+ return pow(max(o, vec3f(0.0)), vec3f(viewU[0].y));
207
217
  }
208
218
 
209
219
  /**
@@ -277,6 +287,7 @@ fn rzSceneFar() -> f32 { return 1e9; }
277
287
  fn rzSceneHit(uv: vec2f) -> bool { return false; }
278
288
  fn rzSceneAlpha(uv: vec2f) -> f32 { return 0.0; }
279
289
  fn rzSceneDisplay(uv: vec2f) -> vec3f { return vec3f(0.0); }
290
+ fn rzDisplay(c: vec3f) -> vec3f { return c; }
280
291
  fn rzSceneFrame(uv: vec2f) -> vec3f { return vec3f(0.0); }
281
292
  fn rzBackground() -> vec4f { return vec4f(0.0); }
282
293
  `