reze-engine 0.68.13 → 0.68.14

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
 
@@ -2882,6 +2907,9 @@ export class Engine {
2882
2907
  private stageGradeCube: StageGradeLut | null = null
2883
2908
  private stageGradeTexture: GPUTexture | null = null
2884
2909
  private stageGradeFallback!: GPUTexture
2910
+ /** The baked view transform (setViewLut), or null for the built-in curves. */
2911
+ private viewLutTexture: GPUTexture | null = null
2912
+ private viewLutCube: ViewLut | null = null
2885
2913
  /** Bound at composite binding 7 when no effect (or a param-less one) is set. */
2886
2914
  private bgParamsDummyBuffer!: GPUBuffer
2887
2915
  private compositePipelineLayout!: GPUPipelineLayout
@@ -3203,6 +3231,48 @@ export class Engine {
3203
3231
  if (this.device && this.stageGradeFallback) this.uploadStageGrade()
3204
3232
  }
3205
3233
 
3234
+ /**
3235
+ * Show the frame through a view transform its own renderer baked — a stage
3236
+ * exported from Blender brings its AgX (or Filmic, or a Look) as a cube, and
3237
+ * while one is set it replaces the transform chosen in setViewTransformOptions;
3238
+ * exposure still applies before it. null goes back to the built-in curves.
3239
+ */
3240
+ setViewLut(lut: ViewLut | null): void {
3241
+ if (lut && (!Number.isInteger(lut.size) || lut.size < 2 || lut.size > 65 || lut.data.length !== lut.size ** 3 * 3)) {
3242
+ throw new Error(`setViewLut: ${lut.data.length} values is not a ${lut.size}³ RGB cube`)
3243
+ }
3244
+ this.viewLutCube = lut
3245
+ if (this.device && this.stageGradeFallback) this.uploadViewLut()
3246
+ }
3247
+
3248
+ private uploadViewLut(): void {
3249
+ const lut = this.viewLutCube
3250
+ this.viewLutTexture?.destroy()
3251
+ this.viewLutTexture = null
3252
+ if (lut) {
3253
+ const n = lut.size
3254
+ const texels = n ** 3
3255
+ const half = new Uint16Array(texels * 4)
3256
+ for (let i = 0; i < texels; i++) {
3257
+ half[i * 4] = toHalf(lut.data[i * 3] / 65535)
3258
+ half[i * 4 + 1] = toHalf(lut.data[i * 3 + 1] / 65535)
3259
+ half[i * 4 + 2] = toHalf(lut.data[i * 3 + 2] / 65535)
3260
+ half[i * 4 + 3] = 0x3c00
3261
+ }
3262
+ const tex = this.device!.createTexture({
3263
+ label: `view lut ${n}³`,
3264
+ size: [n, n, n],
3265
+ dimension: "3d",
3266
+ format: "rgba16float",
3267
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
3268
+ })
3269
+ this.device!.queue.writeTexture({ texture: tex }, half, { bytesPerRow: n * 8, rowsPerImage: n }, [n, n, n])
3270
+ this.viewLutTexture = tex
3271
+ }
3272
+ this.rebuildCompositeBindGroup()
3273
+ if (this.compositeUniformBuffer) this.writeCompositeViewUniforms()
3274
+ }
3275
+
3206
3276
  private uploadStageGrade(): void {
3207
3277
  const lut = this.stageGradeCube
3208
3278
  this.stageGradeTexture?.destroy()
@@ -3346,7 +3416,8 @@ export class Engine {
3346
3416
  // rebuilt per effect, so the compiled variant IS the flag.
3347
3417
  u[11] = this.backdropEquirectView ? 2 : showingWorld ? 3 : bg ? 1 : 0
3348
3418
  // Which display transform forms the frame (see viewTransform in composite.ts).
3349
- u[25] = Engine.VIEW_TRANSFORM_ID[v.transform] ?? 0
3419
+ // A baked view transform, while one is set, stands in for the chosen one.
3420
+ u[25] = this.viewLutTexture ? 4 : (Engine.VIEW_TRANSFORM_ID[v.transform] ?? 0)
3350
3421
  u[26] = this.canvas.width
3351
3422
  u[27] = this.canvas.height
3352
3423
  // The soft curve's power (viewU[15].x), the scene's own (SceneSetting._contrast).
@@ -4525,6 +4596,7 @@ export class Engine {
4525
4596
  { binding: 8, resource: this.depthReadView },
4526
4597
  { binding: 9, resource: { buffer: this.dofUniformBuffer } },
4527
4598
  { binding: 12, resource: (this.stageGradeTexture ?? this.stageGradeFallback).createView({ dimension: "3d" }) },
4599
+ { binding: 40, resource: (this.viewLutTexture ?? this.stageGradeFallback).createView({ dimension: "3d" }) },
4528
4600
  { binding: 11, resource: { buffer: this.castBuffer } },
4529
4601
  { binding: 13, resource: { buffer: this.audioBuffer } },
4530
4602
  { binding: 19, resource: { buffer: this.midiBuffer } },
@@ -4962,6 +5034,7 @@ export class Engine {
4962
5034
  { binding: 32, resource: layers[3] },
4963
5035
  { binding: 2, resource: this.bloomSampler },
4964
5036
  { binding: 12, resource: (this.stageGradeTexture ?? this.stageGradeFallback).createView({ dimension: "3d" }) },
5037
+ { binding: 40, resource: (this.viewLutTexture ?? this.stageGradeFallback).createView({ dimension: "3d" }) },
4965
5038
  { binding: 1, resource: bloom },
4966
5039
  { binding: 4, resource: this.maskResolveView },
4967
5040
  ],
@@ -8104,6 +8177,7 @@ export class Engine {
8104
8177
  // author ever wrote. Sharing the code meant sharing these.
8105
8178
  { binding: 2, visibility: GPUShaderStage.FRAGMENT, sampler: {} },
8106
8179
  { binding: 12, visibility: GPUShaderStage.FRAGMENT, texture: { sampleType: "float", viewDimension: "3d" } },
8180
+ { binding: 40, visibility: GPUShaderStage.FRAGMENT, texture: { sampleType: "float", viewDimension: "3d" } },
8107
8181
  // And the scene's COVERAGE and bloom, without which the tap cannot
8108
8182
  // reconstruct a pixel: the HDR target is premultiplied, so colour alone
8109
8183
  // reads a half-transparent ground as a dark opaque one.
@@ -8602,6 +8676,7 @@ export class Engine {
8602
8676
  usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
8603
8677
  })
8604
8678
  this.uploadStageGrade()
8679
+ this.uploadViewLut()
8605
8680
 
8606
8681
  // BEFORE the bind group below, which binds it. Full size from the start:
8607
8682
  // every material pipeline binds this, so sizing it to the light count would
@@ -9035,6 +9110,8 @@ export class Engine {
9035
9110
  // The scene's own grade cube (setStageGrade) — 1×1×1 stand-in while it
9036
9111
  // has none, which the flag bit keeps from ever being sampled.
9037
9112
  { binding: 12, visibility: GPUShaderStage.FRAGMENT, texture: { viewDimension: "3d" } },
9113
+ // The baked view transform (setViewLut) — the same stand-in while none.
9114
+ { binding: 40, visibility: GPUShaderStage.FRAGMENT, texture: { viewDimension: "3d" } },
9038
9115
  // The cast, for rzSubject/rzAnchor. Always bound so the base shader's
9039
9116
  // layout matches; the base shader simply never reads it.
9040
9117
  { binding: 11, visibility: GPUShaderStage.FRAGMENT, buffer: { type: "read-only-storage" } },
@@ -11780,7 +11857,8 @@ export class Engine {
11780
11857
  // nothing at all — a light that vanishes because its aim was left at the
11781
11858
  // default is worse than one that shines everywhere.
11782
11859
  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
11860
+ // Its type, as bits: 1 a spot, 2 diffuse only (no highlight).
11861
+ this.lightsData[b + 7] = (len > 0 ? 1 : 0) + (l.specular === false ? 2 : 0)
11784
11862
  this.lightsData[b + 8] = len > 0 ? l.aim!.x / len : 0
11785
11863
  this.lightsData[b + 9] = len > 0 ? l.aim!.y / len : 0
11786
11864
  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]