reze-engine 0.70.3 → 0.70.5

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.
@@ -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 +
@@ -43,6 +43,8 @@ import { textureApi } from "../texture-api"
43
43
  // actually bounds this is the STEP: one invocation per texel per frame, each
44
44
  // taking a dozen samples, so doubling the side quadruples the work.
45
45
  export const GRID_MAX = 1024
46
+ /** Where the step reads the cast's distance field. Above the params (9). */
47
+ export const GRID_CAST_DISTANCE_BINDING = 10
46
48
  /** rgba16float, ping-ponged. Two of these is the whole memory cost. */
47
49
  export const SIM_FORMAT: GPUTextureFormat = "rgba16float"
48
50
 
@@ -174,6 +176,11 @@ fn rzGrid(uv: vec2f) -> vec4f { return rzGridPrev(uv); }
174
176
  sceneLightApi(false, 0, 0) +
175
177
  pointsApi(false) +
176
178
  textureApi(0) +
179
+ // The cast's silhouette distance, as the LAST frame left it: the step runs
180
+ // inside the scene pass, before this frame's flood. One frame behind is
181
+ // what a kernel that measures her and eases toward it wants anyway. An
182
+ // effect that reads no distance binds the 1x1 "unreachably far" fallback.
183
+ //
177
184
  // Inlined rather than imported from cast-distance, and NOT for tidiness.
178
185
  //
179
186
  // This module and composite.ts import each other — grid takes
@@ -183,10 +190,14 @@ fn rzGrid(uv: vec2f) -> vec4f { return rzGridPrev(uv); }
183
190
  // grid's own evaluation order, which is enough to change where a bundler
184
191
  // enters the cycle: Node's loader tolerated it and a browser one threw
185
192
  // "Cannot access 'grid' before initialization" at load.
186
- //
187
- // One line of WGSL is not worth an import into a module that is already in a
188
- // cycle. The other mounts import it normally — none of them is.
189
- "fn rzCastDistance(uv: vec2f) -> f32 { return 1.0e30; }\n" +
193
+ /* wgsl */ `
194
+ @group(0) @binding(${GRID_CAST_DISTANCE_BINDING}) var _rzCastDistTex: texture_2d<f32>;
195
+ /** Screen pixels to the cast's silhouette, last frame: 0 on her, positive outside. */
196
+ fn rzCastDistance(uv: vec2f) -> f32 {
197
+ let flipped = vec2f(clamp(uv.x, 0.0, 1.0), 1.0 - clamp(uv.y, 0.0, 1.0));
198
+ return textureSampleLevel(_rzCastDistTex, _rzGridSamp, flipped, 0.0).x;
199
+ }
200
+ ` +
190
201
  "\n// ── user effect (setEffect) ──\n" +
191
202
  wgsl +
192
203
  /* wgsl */ `
@@ -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
  `