reze-engine 0.68.14 → 0.68.16
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/dist/engine.d.ts +78 -0
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +126 -11
- package/dist/graph/registry.d.ts.map +1 -1
- package/dist/graph/registry.js +4 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/shaders/lights.d.ts +1 -1
- package/dist/shaders/lights.d.ts.map +1 -1
- package/dist/shaders/lights.js +89 -6
- package/dist/shaders/materials/common.d.ts.map +1 -1
- package/dist/shaders/materials/common.js +2 -1
- package/dist/shaders/materials/nodes.d.ts +1 -1
- package/dist/shaders/materials/nodes.d.ts.map +1 -1
- package/dist/shaders/materials/nodes.js +182 -13
- package/dist/shaders/passes/composite.d.ts.map +1 -1
- package/dist/shaders/passes/composite.js +59 -4
- package/package.json +1 -1
- package/src/engine.ts +152 -11
- package/src/graph/registry.ts +4 -1
- package/src/index.ts +2 -0
- package/src/shaders/lights.ts +89 -6
- package/src/shaders/materials/common.ts +2 -1
- package/src/shaders/materials/nodes.ts +182 -13
- package/src/shaders/passes/composite.ts +59 -4
package/src/engine.ts
CHANGED
|
@@ -491,6 +491,19 @@ export type SceneAovs = {
|
|
|
491
491
|
exposure: number
|
|
492
492
|
}
|
|
493
493
|
|
|
494
|
+
/** The shape of a renderer's pixel filter (Engine.setPixelFilter): Cycles'
|
|
495
|
+
* default Blackman-Harris, or its box. */
|
|
496
|
+
export type PixelFilterType = "blackman-harris" | "box"
|
|
497
|
+
|
|
498
|
+
/** A local reflection probe (Engine.setReflectionProbe): where it is captured
|
|
499
|
+
* from and the box it projects onto, world space (PMX units). A surface
|
|
500
|
+
* outside the box reflects the sky. */
|
|
501
|
+
export type ReflectionProbeRegion = {
|
|
502
|
+
center: readonly [number, number, number] | readonly number[]
|
|
503
|
+
min: readonly [number, number, number] | readonly number[]
|
|
504
|
+
max: readonly [number, number, number] | readonly number[]
|
|
505
|
+
}
|
|
506
|
+
|
|
494
507
|
/** The light arriving at one point (Engine.probeLight). Colours are linear
|
|
495
508
|
* light, before the surface's albedo: the sun's is its colour times strength,
|
|
496
509
|
* unshadowed and unfaced — multiply by sunShadow and max(sunFacing, 0). */
|
|
@@ -1086,6 +1099,8 @@ interface ModelInstance {
|
|
|
1086
1099
|
* hiddenMaterials so a morph switching a part off never clobbers the user's
|
|
1087
1100
|
* own visibility toggle, and vice versa. */
|
|
1088
1101
|
morphHiddenMaterials: Set<string>
|
|
1102
|
+
/** Materials the reflection probe leaves out (setMaterialInProbe). */
|
|
1103
|
+
probeHiddenMaterials: Set<string>
|
|
1089
1104
|
/** Material-morph targets, or null when the model has no type-8 morphs. */
|
|
1090
1105
|
materialMorphTargets: MaterialMorphTarget[] | null
|
|
1091
1106
|
/** The same targets by PMX material index, so a named offset is one lookup. */
|
|
@@ -1970,6 +1985,16 @@ export type SceneLight = {
|
|
|
1970
1985
|
* room without lighting up its shiny floor. Omitted or true: both.
|
|
1971
1986
|
*/
|
|
1972
1987
|
specular?: boolean
|
|
1988
|
+
/**
|
|
1989
|
+
* A TUBE: the lamp is the segment position ± this half-axis (engine units)
|
|
1990
|
+
* rather than a point — a neon strip, a glowing line. Its light is the
|
|
1991
|
+
* segment's and its highlight a band along it; `near` is the tube's radius,
|
|
1992
|
+
* `radius` its reach from the segment. With an `aim` (square to the tube)
|
|
1993
|
+
* it is a glowing strip facing that way, Lambertian — the cosine exact, so
|
|
1994
|
+
* `angle`, `innerAngle` and `cookie` are not read; without one it glows every
|
|
1995
|
+
* way. Omitted or zero: a point (or spot).
|
|
1996
|
+
*/
|
|
1997
|
+
tube?: XYZ
|
|
1973
1998
|
}
|
|
1974
1999
|
|
|
1975
2000
|
/**
|
|
@@ -2796,6 +2821,12 @@ export class Engine {
|
|
|
2796
2821
|
/** The frame the current run of requests began — the cap's start. */
|
|
2797
2822
|
private probeAskedFrame = 0
|
|
2798
2823
|
private probeFrame = 0
|
|
2824
|
+
/** Off: no capture, and every reader falls back to the sky — see
|
|
2825
|
+
* setReflectionProbe. */
|
|
2826
|
+
private probeEnabled = true
|
|
2827
|
+
/** A local probe's capture point and box, world space — see
|
|
2828
|
+
* setReflectionProbe. Null: the stage's middle and bounds. */
|
|
2829
|
+
private probeRegion: ReflectionProbeRegion | null = null
|
|
2799
2830
|
private probeCapture: {
|
|
2800
2831
|
msTargets: GPUTexture[]
|
|
2801
2832
|
depth: GPUTexture
|
|
@@ -3336,6 +3367,58 @@ export class Engine {
|
|
|
3336
3367
|
return this.grain
|
|
3337
3368
|
}
|
|
3338
3369
|
|
|
3370
|
+
/** The pixel filter: its width in pixels (0 off) and the 1D weights of
|
|
3371
|
+
* offsets 0, ±1, ±2 it comes to — see setPixelFilter. */
|
|
3372
|
+
private pixelFilter = { width: 0, type: "blackman-harris" as PixelFilterType, weights: [1, 0, 0] as [number, number, number] }
|
|
3373
|
+
|
|
3374
|
+
/**
|
|
3375
|
+
* A renderer's pixel filter, over the rendered scene: how Cycles builds a
|
|
3376
|
+
* pixel from the light around it rather than from its own square alone.
|
|
3377
|
+
* Off (width 0) the engine's pixels are their own box, which is what it has
|
|
3378
|
+
* always drawn.
|
|
3379
|
+
*
|
|
3380
|
+
* Cycles samples each pixel through a Blackman-Harris window twice the
|
|
3381
|
+
* filter width across (1.5 px by default, so 3 px), or a box that width
|
|
3382
|
+
* across. It is invisible on ordinary surfaces and decides what a thing
|
|
3383
|
+
* smaller than a pixel and far brighter than white looks like: a 3 cm ember
|
|
3384
|
+
* glowing at twenty times white spreads into the pixels around it and reads
|
|
3385
|
+
* as a coloured square, and a neon edge gets its coloured fringe. The engine
|
|
3386
|
+
* resolves its samples to a box first, so the filter here is that window
|
|
3387
|
+
* integrated over each neighbour's square — the nearest staircase to it —
|
|
3388
|
+
* applied to the scene's HDR light before the view transform, coverage
|
|
3389
|
+
* alongside, so the sky behind it composites as before. Up to 2.5 px wide
|
|
3390
|
+
* (5×5 taps), one more pass of reads in the composite, none when off.
|
|
3391
|
+
*/
|
|
3392
|
+
setPixelFilter(width: number, type: PixelFilterType = "blackman-harris"): void {
|
|
3393
|
+
const w = Number.isFinite(width) ? Math.min(Math.max(width, 0), 2.5) : 0
|
|
3394
|
+
// The window's full span in pixels and its shape over u in 0..1.
|
|
3395
|
+
const span = type === "box" ? w : w * 2
|
|
3396
|
+
const shape = (u: number) =>
|
|
3397
|
+
type === "box"
|
|
3398
|
+
? 1
|
|
3399
|
+
: 0.35875 - 0.48829 * Math.cos(2 * Math.PI * u) + 0.14128 * Math.cos(4 * Math.PI * u) - 0.01168 * Math.cos(6 * Math.PI * u)
|
|
3400
|
+
const weights: [number, number, number] = [1, 0, 0]
|
|
3401
|
+
if (span > 1e-3) {
|
|
3402
|
+
// Each offset's square, [d − ½, d + ½], integrated over the window.
|
|
3403
|
+
const n = 64
|
|
3404
|
+
for (let d = 0; d < 3; d++) {
|
|
3405
|
+
let sum = 0
|
|
3406
|
+
for (let i = 0; i < n; i++) {
|
|
3407
|
+
const x = d - 0.5 + (i + 0.5) / n
|
|
3408
|
+
if (Math.abs(x) < span / 2) sum += shape(x / span + 0.5) / n
|
|
3409
|
+
}
|
|
3410
|
+
weights[d] = sum
|
|
3411
|
+
}
|
|
3412
|
+
const total = weights[0] + 2 * weights[1] + 2 * weights[2]
|
|
3413
|
+
for (let d = 0; d < 3; d++) weights[d] /= total
|
|
3414
|
+
}
|
|
3415
|
+
this.pixelFilter = { width: w, type, weights }
|
|
3416
|
+
if (this.device && this.compositeUniformBuffer) this.writeCompositeViewUniforms()
|
|
3417
|
+
}
|
|
3418
|
+
getPixelFilter(): Readonly<{ width: number; type: PixelFilterType }> {
|
|
3419
|
+
return { width: this.pixelFilter.width, type: this.pixelFilter.type }
|
|
3420
|
+
}
|
|
3421
|
+
|
|
3339
3422
|
setViewTransformOptions(patch: Partial<ViewTransformOptions>): void {
|
|
3340
3423
|
const v = this.viewTransform
|
|
3341
3424
|
if (patch.exposure !== undefined) v.exposure = patch.exposure
|
|
@@ -3423,6 +3506,11 @@ export class Engine {
|
|
|
3423
3506
|
// The soft curve's power (viewU[15].x), the scene's own (SceneSetting._contrast).
|
|
3424
3507
|
// Its own vec4: viewU[11..14] are the cast's positions whenever an effect runs.
|
|
3425
3508
|
u[60] = v.contrast
|
|
3509
|
+
// The pixel filter's 1D weights for offsets 0, ±1, ±2 (viewU[15].yzw);
|
|
3510
|
+
// 1, 0, 0 is off — see setPixelFilter.
|
|
3511
|
+
u[61] = this.pixelFilter.weights[0]
|
|
3512
|
+
u[62] = this.pixelFilter.weights[1]
|
|
3513
|
+
u[63] = this.pixelFilter.weights[2]
|
|
3426
3514
|
// ── Grade (viewU[7..9]) ── The UI's three tonal COLORS map to ASC CDL here,
|
|
3427
3515
|
// on the CPU, so the shader only ever sees slope/offset/power. Mid-gray is
|
|
3428
3516
|
// neutral in all three; the signed distance from it is the amount.
|
|
@@ -11007,6 +11095,25 @@ export class Engine {
|
|
|
11007
11095
|
this.probeDirty = true
|
|
11008
11096
|
}
|
|
11009
11097
|
|
|
11098
|
+
/** Whether the stage reflects its own capture (the default) or the sky. Off
|
|
11099
|
+
* is for a stage whose sky was measured from inside it — a Blender export's
|
|
11100
|
+
* world — where a capture lit without the lamps' shadows comes out too
|
|
11101
|
+
* bright on a shiny floor.
|
|
11102
|
+
*
|
|
11103
|
+
* With a region the probe is LOCAL, as a room's box probe is in Unity:
|
|
11104
|
+
* captured from region.center, box-projected onto region.min..max, and a
|
|
11105
|
+
* surface outside that box reflects the sky — a tunnel's glossy walls
|
|
11106
|
+
* mirror its neon while the floor around it keeps the world. Null: the
|
|
11107
|
+
* stage's middle and bounds, every surface. */
|
|
11108
|
+
setReflectionProbe(enabled: boolean, region: ReflectionProbeRegion | null = null): void {
|
|
11109
|
+
const same = (a: ReflectionProbeRegion | null, b: ReflectionProbeRegion | null) =>
|
|
11110
|
+
a === b || (!!a && !!b && [0, 1, 2].every((k) => a.center[k] === b.center[k] && a.min[k] === b.min[k] && a.max[k] === b.max[k]))
|
|
11111
|
+
if (this.probeEnabled === enabled && same(this.probeRegion, region)) return
|
|
11112
|
+
this.probeEnabled = enabled
|
|
11113
|
+
this.probeRegion = region && [region.center, region.min, region.max].every((v) => v.length >= 3 && v.every(Number.isFinite)) ? region : null
|
|
11114
|
+
this.captureReflectionProbe()
|
|
11115
|
+
}
|
|
11116
|
+
|
|
11010
11117
|
/** The stage's world-space bounds: every stage model's material boxes
|
|
11011
11118
|
* through its placement. Null without a stage. */
|
|
11012
11119
|
private stageBounds(): { min: [number, number, number]; max: [number, number, number] } | null {
|
|
@@ -11043,15 +11150,17 @@ export class Engine {
|
|
|
11043
11150
|
const settled = this.probeFrame - this.probeDirtyFrame >= Engine.PROBE_SETTLE_FRAMES
|
|
11044
11151
|
if (!settled && this.probeFrame - this.probeAskedFrame < Engine.PROBE_MAX_WAIT_FRAMES) return
|
|
11045
11152
|
this.probeDirty = false
|
|
11046
|
-
const
|
|
11153
|
+
const region = this.probeRegion
|
|
11154
|
+
const bounds = !this.probeEnabled ? null : region ? { min: region.min, max: region.max } : this.stageBounds()
|
|
11047
11155
|
if (!bounds) {
|
|
11048
|
-
// No stage
|
|
11156
|
+
// No stage, or the probe is off: it goes dark and every reader falls
|
|
11157
|
+
// back to the sky.
|
|
11049
11158
|
this.probeData.fill(0)
|
|
11050
11159
|
this.device.queue.writeBuffer(this.probeUniformBuffer, 0, this.probeData)
|
|
11051
11160
|
return
|
|
11052
11161
|
}
|
|
11053
11162
|
try {
|
|
11054
|
-
this.renderProbe(bounds)
|
|
11163
|
+
this.renderProbe(bounds, region?.center ?? null)
|
|
11055
11164
|
} catch (e) {
|
|
11056
11165
|
console.warn("[reze-engine] reflection probe capture failed:", e)
|
|
11057
11166
|
}
|
|
@@ -11179,19 +11288,25 @@ export class Engine {
|
|
|
11179
11288
|
* the MMD convention, else their bottom) — where a game puts the probe of a
|
|
11180
11289
|
* room the player stands in. The box it projects onto is the bounds.
|
|
11181
11290
|
*
|
|
11291
|
+
* A LOCAL probe (setReflectionProbe's region) is captured from the point
|
|
11292
|
+
* it names and projects onto its own box; a surface outside that box
|
|
11293
|
+
* reflects the sky, as Unity's box probes fall back to the skybox past
|
|
11294
|
+
* their bounds.
|
|
11295
|
+
*
|
|
11182
11296
|
* WHAT: the stage alone, in its own looks, lit by the scene's sun, lamps
|
|
11183
11297
|
* and sky as they stand. The cast is not in it (a baked probe never holds
|
|
11184
11298
|
* the characters), nor are effects.
|
|
11185
11299
|
*/
|
|
11186
|
-
private renderProbe(
|
|
11300
|
+
private renderProbe(
|
|
11301
|
+
bounds: { min: readonly number[]; max: readonly number[] },
|
|
11302
|
+
at: readonly number[] | null,
|
|
11303
|
+
): void {
|
|
11187
11304
|
const cap = this.ensureProbeCapture()
|
|
11188
11305
|
const { min, max } = bounds
|
|
11189
11306
|
const floor = min[1] <= 0 && max[1] >= 0 ? 0 : min[1]
|
|
11190
|
-
const eye: [number, number, number] =
|
|
11191
|
-
|
|
11192
|
-
Math.min(floor + 12, (min[1] + max[1]) * 0.5),
|
|
11193
|
-
(min[2] + max[2]) * 0.5,
|
|
11194
|
-
]
|
|
11307
|
+
const eye: [number, number, number] = at
|
|
11308
|
+
? [at[0], at[1], at[2]]
|
|
11309
|
+
: [(min[0] + max[0]) * 0.5, Math.min(floor + 12, (min[1] + max[1]) * 0.5), (min[2] + max[2]) * 0.5]
|
|
11195
11310
|
const span = Math.hypot(max[0] - min[0], max[1] - min[1], max[2] - min[2])
|
|
11196
11311
|
const near = 0.25
|
|
11197
11312
|
const far = Math.max(span * 2, 100)
|
|
@@ -11314,7 +11429,8 @@ export class Engine {
|
|
|
11314
11429
|
for (let f = 0; f < 6; f++) run(face(this.probeTexture, m, f), cap.filterPipeline, cap.filterGroups[m * 6 + f])
|
|
11315
11430
|
this.device.queue.submit([encoder.finish()])
|
|
11316
11431
|
|
|
11317
|
-
|
|
11432
|
+
// boxMax.w: 1 for a local probe, whose outside reflects the sky.
|
|
11433
|
+
this.probeData.set([eye[0], eye[1], eye[2], 1, min[0], min[1], min[2], 1, max[0], max[1], max[2], at ? 1 : 0])
|
|
11318
11434
|
this.device.queue.writeBuffer(this.probeUniformBuffer, 0, this.probeData)
|
|
11319
11435
|
}
|
|
11320
11436
|
|
|
@@ -11877,6 +11993,12 @@ export class Engine {
|
|
|
11877
11993
|
// rewrites the lamps when it lands).
|
|
11878
11994
|
const cookie = l.cookie !== undefined ? this.cookieLayers.get(l.cookie) : undefined
|
|
11879
11995
|
this.lightsData[b + 15] = cookie !== undefined ? cookie + 1 : 0
|
|
11996
|
+
// A tube's half-axis; zero for a point (a non-finite one is a point too)
|
|
11997
|
+
const tube = l.tube && [l.tube.x, l.tube.y, l.tube.z].every(Number.isFinite) && l.kind !== "directional" ? l.tube : null
|
|
11998
|
+
this.lightsData[b + 16] = tube ? tube.x : 0
|
|
11999
|
+
this.lightsData[b + 17] = tube ? tube.y : 0
|
|
12000
|
+
this.lightsData[b + 18] = tube ? tube.z : 0
|
|
12001
|
+
this.lightsData[b + 19] = 0
|
|
11880
12002
|
}
|
|
11881
12003
|
// The grid, from the records as STORED — normalised aim, clamped reach, the
|
|
11882
12004
|
// cosines in f32 — so it indexes exactly what the shader will evaluate.
|
|
@@ -11884,7 +12006,10 @@ export class Engine {
|
|
|
11884
12006
|
list.map((_, i) => {
|
|
11885
12007
|
const b = LIGHT_HEADER + i * LIGHT_STRIDE
|
|
11886
12008
|
const d = this.lightsData
|
|
11887
|
-
|
|
12009
|
+
// a tube reaches its radius from any point of its segment: past the
|
|
12010
|
+
// middle by its half-length, and a cone is no bound along it
|
|
12011
|
+
const half = Math.hypot(d[b + 16], d[b + 17], d[b + 18])
|
|
12012
|
+
return { x: d[b], y: d[b + 1], z: d[b + 2], radius: d[b + 3] + half, ax: d[b + 8], ay: d[b + 9], az: d[b + 10], cosOuter: half > 0 ? -1 : d[b + 11] }
|
|
11888
12013
|
}),
|
|
11889
12014
|
)
|
|
11890
12015
|
this.lightHeader.set(grid.origin, 4)
|
|
@@ -13688,6 +13813,20 @@ export class Engine {
|
|
|
13688
13813
|
else inst.hiddenMaterials.add(materialName)
|
|
13689
13814
|
}
|
|
13690
13815
|
|
|
13816
|
+
/**
|
|
13817
|
+
* Whether the reflection probe captures a material. Off for a glowing strip
|
|
13818
|
+
* whose light a TUBE LAMP already carries (SceneLight.tube): the lamp draws
|
|
13819
|
+
* its reflection in every glossy surface, exactly, and the probe's picture
|
|
13820
|
+
* of the same strip would put it there a second time. Re-captures the probe.
|
|
13821
|
+
*/
|
|
13822
|
+
setMaterialInProbe(modelName: string, materialName: string, inProbe: boolean): void {
|
|
13823
|
+
const inst = this.modelInstances.get(modelName)
|
|
13824
|
+
if (!inst || inst.probeHiddenMaterials.has(materialName) === !inProbe) return
|
|
13825
|
+
if (inProbe) inst.probeHiddenMaterials.delete(materialName)
|
|
13826
|
+
else inst.probeHiddenMaterials.add(materialName)
|
|
13827
|
+
this.captureReflectionProbe()
|
|
13828
|
+
}
|
|
13829
|
+
|
|
13691
13830
|
toggleMaterialVisible(modelName: string, materialName: string): void {
|
|
13692
13831
|
const inst = this.modelInstances.get(modelName)
|
|
13693
13832
|
if (!inst) return
|
|
@@ -15357,6 +15496,7 @@ export class Engine {
|
|
|
15357
15496
|
skinMatricesDirty: true,
|
|
15358
15497
|
hiddenMaterials: new Set(),
|
|
15359
15498
|
morphHiddenMaterials: new Set(),
|
|
15499
|
+
probeHiddenMaterials: new Set(),
|
|
15360
15500
|
materialMorphTargets: null,
|
|
15361
15501
|
materialMorphByIndex: null,
|
|
15362
15502
|
physics,
|
|
@@ -19121,6 +19261,7 @@ export class Engine {
|
|
|
19121
19261
|
if (only && (only === "hair") !== this.isHairDraw(inst, draw)) continue
|
|
19122
19262
|
// The probe draws directly, past the cull: honour the switches it holds.
|
|
19123
19263
|
if (view.args === "probe" && !this.shouldRenderDrawCall(inst, draw)) continue
|
|
19264
|
+
if (view.args === "probe" && inst.probeHiddenMaterials.has(draw.materialName)) continue
|
|
19124
19265
|
if (!bound) {
|
|
19125
19266
|
pass.setBindGroup(0, view.perFrame)
|
|
19126
19267
|
pass.setBindGroup(1, inst.mainPerInstanceBindGroup)
|
package/src/graph/registry.ts
CHANGED
|
@@ -734,12 +734,15 @@ export const NODE_REGISTRY: Record<string, NodeSpec> = {
|
|
|
734
734
|
occlusion: F(1),
|
|
735
735
|
emission: C([0, 0, 0]),
|
|
736
736
|
alpha: F(1),
|
|
737
|
+
/** 1: the lamps' and sun's highlights rise toward white at grazing
|
|
738
|
+
* (Schlick), as Blender's Principled; 0 is URP's fixed reflectance. */
|
|
739
|
+
fresnel: F(0),
|
|
737
740
|
normal: { type: "vector", contextDefault: "n" },
|
|
738
741
|
},
|
|
739
742
|
outputs: { color: "color", alpha: "float" },
|
|
740
743
|
outputSelect: { color: ".rgb", alpha: ".a" },
|
|
741
744
|
emit: (a) =>
|
|
742
|
-
`urp_lit(${a.base_color}, ${a.metallic}, ${a.smoothness}, ${a.specular}, ${a.occlusion}, ${a.emission}, ${a.alpha}, ` +
|
|
745
|
+
`urp_lit(${a.base_color}, ${a.metallic}, ${a.smoothness}, ${a.specular}, ${a.occlusion}, ${a.emission}, ${a.alpha}, ${a.fresnel}, ` +
|
|
743
746
|
`${a.normal}, l, v, sun, amb, shadow, input.worldPos, true)`,
|
|
744
747
|
takesLight: true,
|
|
745
748
|
},
|
package/src/index.ts
CHANGED
package/src/shaders/lights.ts
CHANGED
|
@@ -23,7 +23,7 @@ import { subjectMaskApi } from "./cast-api"
|
|
|
23
23
|
// [4..6] grid origin (f32) [7] 1 / cell edge (f32)
|
|
24
24
|
// [8..10] grid dims (u32) [12..19] the OUTSIDE mask (u32 bits)
|
|
25
25
|
//
|
|
26
|
-
// then MAX_LIGHTS records of
|
|
26
|
+
// then MAX_LIGHTS records of 20 floats — five vec4s:
|
|
27
27
|
//
|
|
28
28
|
// [0..2] position, world space [3] radius
|
|
29
29
|
// [4..6] colour PREMULTIPLIED by intensity [7] type: bits, 1 spot, 2 diffuse only
|
|
@@ -32,6 +32,8 @@ import { subjectMaskApi } from "./cast-api"
|
|
|
32
32
|
// [13] the rendering layers it does NOT reach (u32 bits)
|
|
33
33
|
// [14] its bulb: the distance inside which the inverse square is held flat
|
|
34
34
|
// (0 = RZ_LAMP_NEAR) [15] its cookie: layer + 1, 0 none
|
|
35
|
+
// [16..18] a TUBE's half-axis, world space: the lamp is the segment
|
|
36
|
+
// position ± this, (0,0,0) for a point [19] unused
|
|
35
37
|
//
|
|
36
38
|
// then the grid: LIGHT_MASK_WORDS words of lamp bits per cell.
|
|
37
39
|
//
|
|
@@ -64,7 +66,7 @@ import { textureApi } from "./texture-api"
|
|
|
64
66
|
* outside mask, five vec4s — see the layout above. */
|
|
65
67
|
export const LIGHT_HEADER = 20
|
|
66
68
|
/** Floats per light — see the layout above. */
|
|
67
|
-
export const LIGHT_STRIDE =
|
|
69
|
+
export const LIGHT_STRIDE = 20
|
|
68
70
|
/**
|
|
69
71
|
* The cap. The loop below runs per fragment over the lights a scene HAS, so
|
|
70
72
|
* this bounds the worst case rather than the ordinary one, and the buffer is
|
|
@@ -252,6 +254,10 @@ fn lightEmitMain(@builtin(global_invocation_id) gid: vec3u) {
|
|
|
252
254
|
_rzLightsOut[b + 13u] = 0.0;
|
|
253
255
|
_rzLightsOut[b + 14u] = select(0.0, max(l.near, 0.0), finite && l.near == l.near);
|
|
254
256
|
_rzLightsOut[b + 15u] = 0.0;
|
|
257
|
+
_rzLightsOut[b + 16u] = 0.0;
|
|
258
|
+
_rzLightsOut[b + 17u] = 0.0;
|
|
259
|
+
_rzLightsOut[b + 18u] = 0.0;
|
|
260
|
+
_rzLightsOut[b + 19u] = 0.0;
|
|
255
261
|
}
|
|
256
262
|
`
|
|
257
263
|
}
|
|
@@ -299,9 +305,9 @@ fn rzLightPos(i: u32) -> vec3f { return _rzLightVec(i, 0u).xyz; }
|
|
|
299
305
|
fn rzLightRadius(i: u32) -> f32 { return _rzLightVec(i, 0u).w; }
|
|
300
306
|
|
|
301
307
|
/** Light i's colour, already multiplied by its intensity. */
|
|
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. */
|
|
308
|
+
fn rzLightColor(i: u32) -> vec3f { return _rzLightVec(i, 1u).xyz; }
|
|
309
|
+
/** 0 for a lamp that lights diffuse only (type bit 2), else 1 — the weight on
|
|
310
|
+
* its highlight. */
|
|
305
311
|
fn rzLightSpecular(i: u32) -> f32 { return select(1.0, 0.0, (u32(max(_rzLightVec(i, 1u).w, 0.0)) & 2u) != 0u); }
|
|
306
312
|
|
|
307
313
|
/** Where light i points, away from itself. The zero vector for a point light. */
|
|
@@ -335,6 +341,68 @@ fn rzLightCookie(i: u32, toLight: vec3f) -> vec3f {
|
|
|
335
341
|
* a soft sphere, which a single global bulb cannot spell. */
|
|
336
342
|
fn rzLightNear(i: u32) -> f32 { let r = _rzLightVec(i, 3u).z; return select(RZ_LAMP_NEAR, r, r > 0.0); }
|
|
337
343
|
|
|
344
|
+
/** A tube lamp's half-axis: the lamp is the segment rzLightPos ± this. The
|
|
345
|
+
* zero vector for a point or a spot, which is every lamp but a tube. */
|
|
346
|
+
fn rzLightTube(i: u32) -> vec3f { return _rzLightVec(i, 4u).xyz; }
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* A TUBE: a glowing strip, a neon line — light along a segment, not from a
|
|
350
|
+
* point. A row of points stood in for one puts a dot of highlight per point on
|
|
351
|
+
* a glossy wall where the strip's reflection is a band, and lights the panel
|
|
352
|
+
* beside the strip as if from half a metre away.
|
|
353
|
+
*
|
|
354
|
+
* Its irradiance is the segment's, its total intensity spread along it, per
|
|
355
|
+
* unit of that intensity (far off it is n·l/d², the point lamp's):
|
|
356
|
+
*
|
|
357
|
+
* - WITH AN AIM, a glowing strip facing it — Lambertian, cos θₑ at every
|
|
358
|
+
* point of it. Since the aim is square to the segment, cos θₑ/r is hₑ/r²
|
|
359
|
+
* all along it (hₑ the height of p over the strip's plane), and the
|
|
360
|
+
* integral of hₑ·n·(x − p)/r⁴ along it is closed: with t along the axis
|
|
361
|
+
* and D the distance off it, ∫(A + B·t)/(D² + t²)² dt =
|
|
362
|
+
* A·(t/(2D²(D² + t²)) + atan(t/D)/(2D³)) − B/(2(D² + t²)). Exact for a
|
|
363
|
+
* thin strip (within 0.4% of the integral over a 2 × 0.1 m face); a spot
|
|
364
|
+
* cone's fit to the cosine gave a quarter too much 30° off its axis.
|
|
365
|
+
* - Without one, a line glowing every way (Picott's, as Karis gives it):
|
|
366
|
+
* (n·L0/|L0| + n·L1/|L1|) / (|L0||L1| + L0·L1), the ends L0 and L1 from p;
|
|
367
|
+
* beside a long line at h it is 2/(h·length).
|
|
368
|
+
*
|
|
369
|
+
* The bulb floors D (2·bulb² the Picott denominator), as max(d², bulb²)
|
|
370
|
+
* floors a point. Clamped at zero: both assume the whole segment above p's
|
|
371
|
+
* horizon.
|
|
372
|
+
*/
|
|
373
|
+
fn _rzTubeIrradiance(p: vec3f, n: vec3f, c: vec3f, h: vec3f, aim: vec3f, bulb: f32) -> f32 {
|
|
374
|
+
if (dot(aim, aim) > 0.5) {
|
|
375
|
+
let len = length(h);
|
|
376
|
+
let u = h / max(len, 1e-6);
|
|
377
|
+
let w = c - p;
|
|
378
|
+
let wu = dot(w, u);
|
|
379
|
+
let wp = w - u * wu;
|
|
380
|
+
let he = -dot(aim, w);
|
|
381
|
+
if (he <= 0.0) { return 0.0; }
|
|
382
|
+
let D2 = max(dot(wp, wp), bulb * bulb);
|
|
383
|
+
let D = sqrt(D2);
|
|
384
|
+
let A = dot(n, wp);
|
|
385
|
+
let B = dot(n, u);
|
|
386
|
+
let t1 = wu + len;
|
|
387
|
+
let t0 = wu - len;
|
|
388
|
+
let f1 = A * (t1 / (2.0 * D2 * (D2 + t1 * t1)) + atan(t1 / D) / (2.0 * D2 * D)) - B / (2.0 * (D2 + t1 * t1));
|
|
389
|
+
let f0 = A * (t0 / (2.0 * D2 * (D2 + t0 * t0)) + atan(t0 / D) / (2.0 * D2 * D)) - B / (2.0 * (D2 + t0 * t0));
|
|
390
|
+
return max(he * (f1 - f0) / max(2.0 * len, 1e-6), 0.0);
|
|
391
|
+
}
|
|
392
|
+
let L0 = c - h - p;
|
|
393
|
+
let L1 = c + h - p;
|
|
394
|
+
let l0 = max(length(L0), 1e-4);
|
|
395
|
+
let l1 = max(length(L1), 1e-4);
|
|
396
|
+
let e = (dot(n, L0) / l0 + dot(n, L1) / l1) / max(l0 * l1 + dot(L0, L1), 2.0 * bulb * bulb);
|
|
397
|
+
return max(e, 0.0);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** The point of a tube nearest p: where its reach, cone and cookie are read. */
|
|
401
|
+
fn _rzTubeNearest(p: vec3f, c: vec3f, h: vec3f) -> vec3f {
|
|
402
|
+
let t = clamp(dot(p - c, h) / max(dot(h, h), 1e-8), -1.0, 1.0);
|
|
403
|
+
return c + h * t;
|
|
404
|
+
}
|
|
405
|
+
|
|
338
406
|
/** The cosines a spot fades between: x its outer edge, y its inner one. A point
|
|
339
407
|
* light stores (-1, -1), which saturates the cone term to 1. */
|
|
340
408
|
fn rzLightCone(i: u32) -> vec2f { return vec2f(_rzLightVec(i, 2u).w, _rzLightVec(i, 3u).x); }
|
|
@@ -379,8 +447,23 @@ fn _rzLightOne(i: u32, p: vec3f, n: vec3f) -> vec3f {
|
|
|
379
447
|
let dist = length(d);
|
|
380
448
|
// Out of reach before anything else is computed. The grid is conservative —
|
|
381
449
|
// a cell's bit means the lamp CAN reach part of it — so this still runs.
|
|
382
|
-
|
|
450
|
+
// a tube's reach is from its segment: past it by its half-length
|
|
451
|
+
let h = rzLightTube(i);
|
|
452
|
+
if (dist >= pr.w + length(h)) { return vec3f(0.0); }
|
|
383
453
|
let toLight = d / max(dist, 1e-4);
|
|
454
|
+
// A tube (see _rzTubeIrradiance): its reach from the point of it nearest
|
|
455
|
+
// p, its light the segment's whole — an aimed one's cosine included, so it
|
|
456
|
+
// takes no cone and no cookie.
|
|
457
|
+
if (dot(h, h) > 0.0) {
|
|
458
|
+
let q = _rzTubeNearest(p, pr.xyz, h);
|
|
459
|
+
let qd = length(q - p);
|
|
460
|
+
if (qd >= pr.w) { return vec3f(0.0); }
|
|
461
|
+
let qt = clamp(qd / max(pr.w, 1e-4), 0.0, 1.0);
|
|
462
|
+
let qt2 = qt * qt;
|
|
463
|
+
let qw = 1.0 - qt2 * qt2;
|
|
464
|
+
let e = _rzTubeIrradiance(p, n, pr.xyz, h, rzLightAim(i), rzLightNear(i));
|
|
465
|
+
return rzLightColor(i) * (e * qw * qw);
|
|
466
|
+
}
|
|
384
467
|
// Facing the light, and nothing behind it. No wrap or half-lambert: this
|
|
385
468
|
// layer adds light, and a wrapped term would lift the shadow side, which is
|
|
386
469
|
// the ramp's business and not this one's.
|
|
@@ -241,7 +241,8 @@ struct LightVP { viewProj: array<mat4x4f, ${SHADOW_CASCADES.length}>, };
|
|
|
241
241
|
|
|
242
242
|
/** The stage's reflection probe (Engine.captureReflectionProbe): where it was
|
|
243
243
|
* captured (xyz) and its strength (w); the box it projects onto, min with the
|
|
244
|
-
* on-flag in w, then max
|
|
244
|
+
* on-flag in w, then max with the local flag in w (1: outside the box the
|
|
245
|
+
* sky stands in). Off (boxMin.w 0) the sky stands in everywhere. */
|
|
245
246
|
struct ProbeUniforms {
|
|
246
247
|
center: vec4f,
|
|
247
248
|
boxMin: vec4f,
|