reze-engine 0.68.13 → 0.68.15

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/engine.ts CHANGED
@@ -776,6 +776,15 @@ export type StageGradeLut = {
776
776
  data: Uint8Array
777
777
  }
778
778
 
779
+ /** A baked view transform — see shaders/view-lut.ts. */
780
+ export type ViewLut = {
781
+ /** Texels along each edge. */
782
+ size: number
783
+ /** size³ texels of 16-bit display values (0..65535), RGB, red fastest then
784
+ * green then blue, over VIEW_LUT_LOG2 on each axis. */
785
+ data: Uint16Array
786
+ }
787
+
779
788
  export type GizmoDragKind = "rotate" | "translate"
780
789
 
781
790
  export interface GizmoDragEvent {
@@ -1955,6 +1964,12 @@ export type SceneLight = {
1955
1964
  * along the cone the way a stage light throws a gobo. Ignored on a point.
1956
1965
  */
1957
1966
  cookie?: string
1967
+ /**
1968
+ * false: the lamp lights diffuse only and puts no highlight on anything — a
1969
+ * Blender light with glossy visibility off, which a stage rig uses to fill a
1970
+ * room without lighting up its shiny floor. Omitted or true: both.
1971
+ */
1972
+ specular?: boolean
1958
1973
  }
1959
1974
 
1960
1975
  /**
@@ -1970,6 +1985,16 @@ const ALL_LAYERS = 0xffffffff
1970
1985
  /** Where the directional slots' layer bits sit in the light uniform, in words. */
1971
1986
  const DIR_LAYERS_AT = 108
1972
1987
 
1988
+ /** A float as IEEE half bits — for the cubes uploaded as rgba16float. */
1989
+ function toHalf(v: number): number {
1990
+ if (!(v > 0)) return 0
1991
+ if (v >= 65504) return 0x7bff
1992
+ const e = Math.floor(Math.log2(v))
1993
+ if (e < -14) return Math.round(v / 2 ** -24) & 0x3ff
1994
+ const m = Math.round((v / 2 ** e - 1) * 1024)
1995
+ return m === 1024 ? ((e + 16) << 10) : ((e + 15) << 10) | m
1996
+ }
1997
+
1973
1998
  export class Engine {
1974
1999
  private static instance: Engine | null = null
1975
2000
 
@@ -2771,6 +2796,9 @@ export class Engine {
2771
2796
  /** The frame the current run of requests began — the cap's start. */
2772
2797
  private probeAskedFrame = 0
2773
2798
  private probeFrame = 0
2799
+ /** Off: no capture, and every reader falls back to the sky — see
2800
+ * setReflectionProbe. */
2801
+ private probeEnabled = true
2774
2802
  private probeCapture: {
2775
2803
  msTargets: GPUTexture[]
2776
2804
  depth: GPUTexture
@@ -2882,6 +2910,9 @@ export class Engine {
2882
2910
  private stageGradeCube: StageGradeLut | null = null
2883
2911
  private stageGradeTexture: GPUTexture | null = null
2884
2912
  private stageGradeFallback!: GPUTexture
2913
+ /** The baked view transform (setViewLut), or null for the built-in curves. */
2914
+ private viewLutTexture: GPUTexture | null = null
2915
+ private viewLutCube: ViewLut | null = null
2885
2916
  /** Bound at composite binding 7 when no effect (or a param-less one) is set. */
2886
2917
  private bgParamsDummyBuffer!: GPUBuffer
2887
2918
  private compositePipelineLayout!: GPUPipelineLayout
@@ -3203,6 +3234,48 @@ export class Engine {
3203
3234
  if (this.device && this.stageGradeFallback) this.uploadStageGrade()
3204
3235
  }
3205
3236
 
3237
+ /**
3238
+ * Show the frame through a view transform its own renderer baked — a stage
3239
+ * exported from Blender brings its AgX (or Filmic, or a Look) as a cube, and
3240
+ * while one is set it replaces the transform chosen in setViewTransformOptions;
3241
+ * exposure still applies before it. null goes back to the built-in curves.
3242
+ */
3243
+ setViewLut(lut: ViewLut | null): void {
3244
+ if (lut && (!Number.isInteger(lut.size) || lut.size < 2 || lut.size > 65 || lut.data.length !== lut.size ** 3 * 3)) {
3245
+ throw new Error(`setViewLut: ${lut.data.length} values is not a ${lut.size}³ RGB cube`)
3246
+ }
3247
+ this.viewLutCube = lut
3248
+ if (this.device && this.stageGradeFallback) this.uploadViewLut()
3249
+ }
3250
+
3251
+ private uploadViewLut(): void {
3252
+ const lut = this.viewLutCube
3253
+ this.viewLutTexture?.destroy()
3254
+ this.viewLutTexture = null
3255
+ if (lut) {
3256
+ const n = lut.size
3257
+ const texels = n ** 3
3258
+ const half = new Uint16Array(texels * 4)
3259
+ for (let i = 0; i < texels; i++) {
3260
+ half[i * 4] = toHalf(lut.data[i * 3] / 65535)
3261
+ half[i * 4 + 1] = toHalf(lut.data[i * 3 + 1] / 65535)
3262
+ half[i * 4 + 2] = toHalf(lut.data[i * 3 + 2] / 65535)
3263
+ half[i * 4 + 3] = 0x3c00
3264
+ }
3265
+ const tex = this.device!.createTexture({
3266
+ label: `view lut ${n}³`,
3267
+ size: [n, n, n],
3268
+ dimension: "3d",
3269
+ format: "rgba16float",
3270
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
3271
+ })
3272
+ this.device!.queue.writeTexture({ texture: tex }, half, { bytesPerRow: n * 8, rowsPerImage: n }, [n, n, n])
3273
+ this.viewLutTexture = tex
3274
+ }
3275
+ this.rebuildCompositeBindGroup()
3276
+ if (this.compositeUniformBuffer) this.writeCompositeViewUniforms()
3277
+ }
3278
+
3206
3279
  private uploadStageGrade(): void {
3207
3280
  const lut = this.stageGradeCube
3208
3281
  this.stageGradeTexture?.destroy()
@@ -3346,7 +3419,8 @@ export class Engine {
3346
3419
  // rebuilt per effect, so the compiled variant IS the flag.
3347
3420
  u[11] = this.backdropEquirectView ? 2 : showingWorld ? 3 : bg ? 1 : 0
3348
3421
  // Which display transform forms the frame (see viewTransform in composite.ts).
3349
- u[25] = Engine.VIEW_TRANSFORM_ID[v.transform] ?? 0
3422
+ // A baked view transform, while one is set, stands in for the chosen one.
3423
+ u[25] = this.viewLutTexture ? 4 : (Engine.VIEW_TRANSFORM_ID[v.transform] ?? 0)
3350
3424
  u[26] = this.canvas.width
3351
3425
  u[27] = this.canvas.height
3352
3426
  // The soft curve's power (viewU[15].x), the scene's own (SceneSetting._contrast).
@@ -4525,6 +4599,7 @@ export class Engine {
4525
4599
  { binding: 8, resource: this.depthReadView },
4526
4600
  { binding: 9, resource: { buffer: this.dofUniformBuffer } },
4527
4601
  { binding: 12, resource: (this.stageGradeTexture ?? this.stageGradeFallback).createView({ dimension: "3d" }) },
4602
+ { binding: 40, resource: (this.viewLutTexture ?? this.stageGradeFallback).createView({ dimension: "3d" }) },
4528
4603
  { binding: 11, resource: { buffer: this.castBuffer } },
4529
4604
  { binding: 13, resource: { buffer: this.audioBuffer } },
4530
4605
  { binding: 19, resource: { buffer: this.midiBuffer } },
@@ -4962,6 +5037,7 @@ export class Engine {
4962
5037
  { binding: 32, resource: layers[3] },
4963
5038
  { binding: 2, resource: this.bloomSampler },
4964
5039
  { binding: 12, resource: (this.stageGradeTexture ?? this.stageGradeFallback).createView({ dimension: "3d" }) },
5040
+ { binding: 40, resource: (this.viewLutTexture ?? this.stageGradeFallback).createView({ dimension: "3d" }) },
4965
5041
  { binding: 1, resource: bloom },
4966
5042
  { binding: 4, resource: this.maskResolveView },
4967
5043
  ],
@@ -8104,6 +8180,7 @@ export class Engine {
8104
8180
  // author ever wrote. Sharing the code meant sharing these.
8105
8181
  { binding: 2, visibility: GPUShaderStage.FRAGMENT, sampler: {} },
8106
8182
  { binding: 12, visibility: GPUShaderStage.FRAGMENT, texture: { sampleType: "float", viewDimension: "3d" } },
8183
+ { binding: 40, visibility: GPUShaderStage.FRAGMENT, texture: { sampleType: "float", viewDimension: "3d" } },
8107
8184
  // And the scene's COVERAGE and bloom, without which the tap cannot
8108
8185
  // reconstruct a pixel: the HDR target is premultiplied, so colour alone
8109
8186
  // reads a half-transparent ground as a dark opaque one.
@@ -8602,6 +8679,7 @@ export class Engine {
8602
8679
  usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
8603
8680
  })
8604
8681
  this.uploadStageGrade()
8682
+ this.uploadViewLut()
8605
8683
 
8606
8684
  // BEFORE the bind group below, which binds it. Full size from the start:
8607
8685
  // every material pipeline binds this, so sizing it to the light count would
@@ -9035,6 +9113,8 @@ export class Engine {
9035
9113
  // The scene's own grade cube (setStageGrade) — 1×1×1 stand-in while it
9036
9114
  // has none, which the flag bit keeps from ever being sampled.
9037
9115
  { binding: 12, visibility: GPUShaderStage.FRAGMENT, texture: { viewDimension: "3d" } },
9116
+ // The baked view transform (setViewLut) — the same stand-in while none.
9117
+ { binding: 40, visibility: GPUShaderStage.FRAGMENT, texture: { viewDimension: "3d" } },
9038
9118
  // The cast, for rzSubject/rzAnchor. Always bound so the base shader's
9039
9119
  // layout matches; the base shader simply never reads it.
9040
9120
  { binding: 11, visibility: GPUShaderStage.FRAGMENT, buffer: { type: "read-only-storage" } },
@@ -10930,6 +11010,16 @@ export class Engine {
10930
11010
  this.probeDirty = true
10931
11011
  }
10932
11012
 
11013
+ /** Whether the stage reflects its own capture (the default) or the sky. Off
11014
+ * is for a stage whose sky was measured from inside it — a Blender export's
11015
+ * world — where a capture lit without the lamps' shadows comes out too
11016
+ * bright on a shiny floor. */
11017
+ setReflectionProbe(enabled: boolean): void {
11018
+ if (this.probeEnabled === enabled) return
11019
+ this.probeEnabled = enabled
11020
+ this.captureReflectionProbe()
11021
+ }
11022
+
10933
11023
  /** The stage's world-space bounds: every stage model's material boxes
10934
11024
  * through its placement. Null without a stage. */
10935
11025
  private stageBounds(): { min: [number, number, number]; max: [number, number, number] } | null {
@@ -10966,9 +11056,10 @@ export class Engine {
10966
11056
  const settled = this.probeFrame - this.probeDirtyFrame >= Engine.PROBE_SETTLE_FRAMES
10967
11057
  if (!settled && this.probeFrame - this.probeAskedFrame < Engine.PROBE_MAX_WAIT_FRAMES) return
10968
11058
  this.probeDirty = false
10969
- const bounds = this.stageBounds()
11059
+ const bounds = this.probeEnabled ? this.stageBounds() : null
10970
11060
  if (!bounds) {
10971
- // No stage: the probe goes dark and every reader falls back to the sky.
11061
+ // No stage, or the probe is off: it goes dark and every reader falls
11062
+ // back to the sky.
10972
11063
  this.probeData.fill(0)
10973
11064
  this.device.queue.writeBuffer(this.probeUniformBuffer, 0, this.probeData)
10974
11065
  return
@@ -11780,7 +11871,8 @@ export class Engine {
11780
11871
  // nothing at all — a light that vanishes because its aim was left at the
11781
11872
  // default is worse than one that shines everywhere.
11782
11873
  const len = l.aim && l.kind !== "point" ? Math.hypot(l.aim.x, l.aim.y, l.aim.z) : 0
11783
- this.lightsData[b + 7] = len > 0 ? 1 : 0
11874
+ // Its type, as bits: 1 a spot, 2 diffuse only (no highlight).
11875
+ this.lightsData[b + 7] = (len > 0 ? 1 : 0) + (l.specular === false ? 2 : 0)
11784
11876
  this.lightsData[b + 8] = len > 0 ? l.aim!.x / len : 0
11785
11877
  this.lightsData[b + 9] = len > 0 ? l.aim!.y / len : 0
11786
11878
  this.lightsData[b + 10] = len > 0 ? l.aim!.z / len : 0
@@ -145,7 +145,11 @@ ${gate}
145
145
  // game keeps the scene's shadow under a character's own key.
146
146
  let rz_key = rzMainLight();
147
147
  let l = -light.lights[rz_key].direction.xyz;
148
- let sun = light.lights[rz_key].color.xyz * light.lights[rz_key].color.w;
148
+ // NO KEY WHERE NONE REACHES: rzMainLight falls back to the sun's slot, and
149
+ // a sun kept off this draw's layers — the cast's alone, beside a stage lit
150
+ // only by its own lamps — must not light it through the fallback.
151
+ let rz_keyed = (light.dirLayers[rz_key] & objectLight.layers.x) != 0u;
152
+ let sun = select(vec3f(0.0), light.lights[rz_key].color.xyz * light.lights[rz_key].color.w, rz_keyed);
149
153
  let shadow = sampleShadow(input.worldPos, n);
150
154
  // The ambient: this object's own where loaded data gave it one — the game
151
155
  // gives every renderer its own — else the world's flat colour or the HDRI's
package/src/index.ts CHANGED
@@ -31,6 +31,7 @@ export {
31
31
  type DissolveCycle,
32
32
  type EffectParamValue,
33
33
  type EffectResult,
34
+ type ViewLut,
34
35
  type CullDiagnostics,
35
36
  type MidiNote,
36
37
  } from "./engine"
@@ -232,3 +233,4 @@ export type { NativeLook, NativeMaterialSpec, NativeTexture } from "./unity/look
232
233
  export type { NativeShader, NativeShaderInfo } from "./unity/host"
233
234
  export { AOV_PIXEL_FIELDS, AOV_PIXEL_STRIDE } from "./shaders/passes/aov"
234
235
  export { GROUND_MATERIAL_ID, GROUND_OBJECT_ID } from "./shaders/passes/ground"
236
+ export { VIEW_LUT_LOG2 } from "./shaders/view-lut"
@@ -26,7 +26,7 @@ import { subjectMaskApi } from "./cast-api"
26
26
  // then MAX_LIGHTS records of 16 floats — four vec4s:
27
27
  //
28
28
  // [0..2] position, world space [3] radius
29
- // [4..6] colour PREMULTIPLIED by intensity [7] type
29
+ // [4..6] colour PREMULTIPLIED by intensity [7] type: bits, 1 spot, 2 diffuse only
30
30
  // [8..10] aim, unit, pointing away from the light [11] cos of the outer angle
31
31
  // [12] cos of the inner angle
32
32
  // [13] the rendering layers it does NOT reach (u32 bits)
@@ -299,7 +299,10 @@ fn rzLightPos(i: u32) -> vec3f { return _rzLightVec(i, 0u).xyz; }
299
299
  fn rzLightRadius(i: u32) -> f32 { return _rzLightVec(i, 0u).w; }
300
300
 
301
301
  /** Light i's colour, already multiplied by its intensity. */
302
- fn rzLightColor(i: u32) -> vec3f { return _rzLightVec(i, 1u).xyz; }
302
+ fn rzLightColor(i: u32) -> vec3f { return _rzLightVec(i, 1u).xyz; }
303
+ /** 0 for a lamp that lights diffuse only (type bit 2), else 1 — the weight on
304
+ * its highlight. */
305
+ fn rzLightSpecular(i: u32) -> f32 { return select(1.0, 0.0, (u32(max(_rzLightVec(i, 1u).w, 0.0)) & 2u) != 0u); }
303
306
 
304
307
  /** Where light i points, away from itself. The zero vector for a point light. */
305
308
  fn rzLightAim(i: u32) -> vec3f { return _rzLightVec(i, 2u).xyz; }
@@ -1312,7 +1312,7 @@ fn _rzLampOne(i: u32, p: vec3f, n: vec3f, v: vec3f, roughness: f32) -> _RzLampPa
1312
1312
  let aim = clamp((dot(-toLight, rzLightAim(i)) - cone.x) / max(cone.y - cone.x, 1e-4), 0.0, 1.0);
1313
1313
  let c = rzLightColor(i) * rzLightCookie(i, toLight);
1314
1314
  if (ndlD > 0.0) { out.d = c * (ndlD * falloff * aim * aim); }
1315
- if (ndl > 0.0) { out.s = c * (bsdf_urp(n, toLight, v, ndl, roughness) * falloff * aim * aim); }
1315
+ if (ndl > 0.0) { out.s = c * (bsdf_urp(n, toLight, v, ndl, roughness) * falloff * aim * aim * rzLightSpecular(i)); }
1316
1316
  return out;
1317
1317
  }
1318
1318
 
@@ -1393,7 +1393,7 @@ fn _rzAddLightOne(i: u32, p: vec3f, n: vec3f, v: vec3f, e: f32) -> _RzLampPair {
1393
1393
  let aim = clamp((dot(-toLight, rzLightAim(i)) - cone.x) / max(cone.y - cone.x, 1e-4), 0.0, 1.0);
1394
1394
  let reach = rzLightColor(i) * rzLightCookie(i, toLight) * (falloff * aim * aim);
1395
1395
  out.d = reach * ndl;
1396
- out.s = reach * pow(saturate(dot(n, normalize(toLight + v))), e);
1396
+ out.s = reach * pow(saturate(dot(n, normalize(toLight + v))), e) * rzLightSpecular(i);
1397
1397
  return out;
1398
1398
  }
1399
1399
 
@@ -12,6 +12,7 @@ import { textureApi } from "../texture-api"
12
12
  import { castDistanceApi, CAST_FIELD_DIV } from "./cast-distance"
13
13
  import { lyricsApi, lyricsTextApi } from "../lyrics-api"
14
14
  import { midiApi } from "../midi-api"
15
+ import { VIEW_LUT_LOG2 } from "../view-lut"
15
16
  import { gridReadApi } from "./grid"
16
17
  // Composite: HDR scene + bloom → view transform (soft / neutral / aces / none) → gamma → swapchain.
17
18
  // Bloom tint/intensity applied at combine (EEVEE treats them as combine-stage params, not prefilter).
@@ -174,6 +175,9 @@ override APPLY_GAMMA: bool = true;
174
175
  // The scene's own colour grade, as a cube — see _rzStageGrade. sRGB-encoded
175
176
  // storage, so what a sample returns is already linear.
176
177
  @group(0) @binding(12) var stageGradeLut: texture_3d<f32>;
178
+ // The view transform a stage was authored under, as its own renderer computes
179
+ // it (Engine.setViewLut) — scene-linear through a log2 shaper to display.
180
+ @group(0) @binding(40) var viewLut: texture_3d<f32>;
177
181
  // The cast, as data. Read through rzSubject/rzAnchor below — the LAYOUT IS NOT
178
182
  // STABLE and never will be, because it depends on what each effect declared.
179
183
  // Reading it directly is the one thing that would freeze it forever.
@@ -360,12 +364,22 @@ fn acesTransform(color: vec3f) -> vec3f {
360
364
  return _rzSrgbEncode3(clamp(rec709, vec3f(0.0), vec3f(1.0)));
361
365
  }
362
366
 
367
+ /** A baked view transform (mode 4): the colour's log2 between
368
+ * VIEW_LUT_LOG2 [lo, hi], a stop every (hi − lo)/(n − 1), looked up in the
369
+ * cube the stage's renderer filled — its display values, already encoded. */
370
+ fn _rzViewLut(c: vec3f) -> vec3f {
371
+ let n = f32(textureDimensions(viewLut).x);
372
+ let t = clamp((log2(max(c, vec3f(exp2(${VIEW_LUT_LOG2[0]})))) - ${VIEW_LUT_LOG2[0]}) / ${VIEW_LUT_LOG2[1] - VIEW_LUT_LOG2[0]}, vec3f(0.0), vec3f(1.0));
373
+ return textureSampleLevel(viewLut, bloomSamp, t * ((n - 1.0) / n) + 0.5 / n, 0.0).rgb;
374
+ }
375
+
363
376
  /** Which display transform, chosen per frame at viewU[6].y (0 soft, 1 none,
364
- * 2 neutral, 3 aces). A uniform branch rather than a pipeline variant: switching
377
+ * 2 neutral, 3 aces, 4 a baked cube). A uniform branch rather than a pipeline variant: switching
365
378
  * is rare, and every arm is cheap enough that specialising the pipeline would
366
379
  * buy nothing. */
367
380
  fn viewTransform(c: vec3f) -> vec3f {
368
381
  let mode = viewU[6].y;
382
+ if (mode > 3.5) { return _rzViewLut(c); }
369
383
  if (mode > 2.5) { return acesTransform(c); }
370
384
  if (mode > 1.5) { return neutralTransform(c); }
371
385
  if (mode > 0.5) { return _rzSrgbEncode3(c); }
@@ -676,7 +690,7 @@ const COMPOSITE_BODY = /* wgsl */ `
676
690
  // like a sun instead of clipping at texture white. bg.x carries the
677
691
  // world STRENGTH (the colour slot is dead in equirect modes). The
678
692
  // scene's grade stays scene-only, the documented rule above.
679
- var sky = max(viewTransform(bgPm * bg.x * viewU[0].x), vec3f(0.0));
693
+ var sky = max(viewTransform(bgPm * bg.x * exp2(viewU[0].x)), vec3f(0.0));
680
694
  if (APPLY_GAMMA) {
681
695
  sky = pow(sky, vec3f(viewU[0].y));
682
696
  }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * A view transform baked by the renderer a stage was made in — Blender's AgX,
3
+ * Filmic, a Look — so the app shows it as that renderer does rather than
4
+ * through the nearest curve it has (Engine.setViewLut).
5
+ *
6
+ * The cube's input is the exposed scene-linear colour, each channel through
7
+ * log2 between these stops — 2^-12.5 (darker than any display shows) to 2^7
8
+ * (past where every film curve has rolled off) — a texel every
9
+ * (hi - lo)/(n - 1) stops. Its texels are display values, encoded. An exporter
10
+ * fills it by asking its renderer's colour management for exactly these
11
+ * inputs, so the two must agree on the range: it is fixed here, not carried.
12
+ */
13
+ export const VIEW_LUT_LOG2: readonly [number, number] = [-12.5, 7]