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.
@@ -1289,15 +1289,147 @@ fn pmx_sphere_map(base: vec3f, strength: f32, N: vec3f) -> vec3f {
1289
1289
  // (_rzLampN, not the mapped N) and in the order rzLightsDiffuse sums it, so the
1290
1290
  // epilogue's rzLightsDiffuseOnce is that function's exact answer without a
1291
1291
  // second pass over the grid. See lights.ts.
1292
- struct _RzLampPair { d: vec3f, s: vec3f };
1292
+ //
1293
+ // Each highlight also comes out as Blender's Principled draws it (.p, and .q
1294
+ // weighted by its rise toward grazing, _rzGrazing), for a closure that asks
1295
+ // for it (urp_lit's fresnel): its reflectance is then f0·p + (1 − f0)·q per
1296
+ // lamp. URP's own
1297
+ // lobe is not that with a Fresnel added — its V·F shortcut,
1298
+ // 1/(max(0.1, L·H²)(4a + 2)), already rises at grazing, and Schlick on top of
1299
+ // it counted the rise twice (a third too bright in a glossy floor's
1300
+ // reflection of a strip).
1301
+ struct _RzLampPair { d: vec3f, s: vec3f, p: vec3f, q: vec3f };
1302
+ var<private> _rzLampPrincipled: vec3f = vec3f(0.0);
1303
+ var<private> _rzLampPrincipledGrazing: vec3f = vec3f(0.0);
1304
+
1305
+ /**
1306
+ * How far a Principled surface's reflectance rises from f0 toward 1 at this
1307
+ * pair of directions: the EXACT Fresnel of a dielectric at Principled's
1308
+ * default IOR 1.5 (f0 0.04), as Cycles evaluates it, put as
1309
+ * (F(L·H) − 0.04)/0.96 so f0 + (1 − f0)·this is that Fresnel. Schlick's
1310
+ * (1 − L·H)⁵ is a sixth low from 45° to 65°, a few percent high past 80°. For a metal (f0 its
1311
+ * colour) it stands in for Cycles' F82 tint, close where (1 − f0) is small.
1312
+ */
1313
+ fn _rzGrazing(l: vec3f, v: vec3f) -> f32 {
1314
+ let c = clamp(dot(l, normalize(l + v)), 1e-4, 1.0);
1315
+ let g = sqrt(1.25 + c * c); // n² − 1 + c², n = 1.5
1316
+ let a = (g - c) / (g + c);
1317
+ let b = (c * (g + c) - 1.0) / (c * (g - c) + 1.0);
1318
+ let f = 0.5 * a * a * (1.0 + b * b);
1319
+ return saturate((f - 0.04) / 0.96);
1320
+ }
1321
+
1322
+ /**
1323
+ * Cycles' microfacet lobe without its Fresnel: GGX's D and Smith's
1324
+ * height-correlated visibility, α = roughness², times π for this engine's
1325
+ * light units (LIGHT UNITS — URP's lobe carries the same π). Returns
1326
+ * π·D·V; the caller multiplies by N·L (or a tube's irradiance) and the
1327
+ * reflectance.
1328
+ */
1329
+ fn _rzGgxSmith(N: vec3f, L: vec3f, V: vec3f, roughness: f32) -> f32 {
1330
+ let a = max(roughness * roughness, 6.1035156e-5);
1331
+ let a2 = a * a;
1332
+ let H = normalize(L + V);
1333
+ let NH = saturate(dot(N, H));
1334
+ let NL = saturate(dot(N, L));
1335
+ let NV = max(dot(N, V), 1e-4);
1336
+ let d = NH * NH * (a2 - 1.0) + 1.0;
1337
+ let vis = 0.5 / max(NL * sqrt(NV * NV * (1.0 - a2) + a2) + NV * sqrt(NL * NL * (1.0 - a2) + a2), 1e-5);
1338
+ return a2 / max(d * d, 1e-8) * vis;
1339
+ }
1340
+
1341
+ /** One point of a tube's highlight, per unit of its length: (URP's lobe,
1342
+ * Principled's, Principled's grazing part), each × the strip's cosine there
1343
+ * (1 for a tube glowing every way), the surface's and 1/r². */
1344
+ fn _rzTubeSample(x: vec3f, p: vec3f, n: vec3f, v: vec3f, roughness: f32, facing: f32, aim: vec3f, bulb: f32) -> vec3f {
1345
+ let d = x - p;
1346
+ let rr = max(length(d), 1e-4);
1347
+ let l = d / rr;
1348
+ let ce = select(1.0, max(-dot(aim, l), 0.0), facing > 0.5);
1349
+ let cr = max(dot(n, l), 0.0);
1350
+ if (ce <= 0.0 || cr <= 0.0) { return vec3f(0.0); }
1351
+ let k = ce / max(rr * rr, bulb * bulb);
1352
+ let pp = _rzGgxSmith(n, l, v, roughness) * cr * k;
1353
+ return vec3f(bsdf_urp(n, l, v, cr, roughness) * k, pp, pp * _rzGrazing(l, v));
1354
+ }
1355
+
1356
+ /**
1357
+ * A tube's diffuse and highlight (see _rzTubeIrradiance in lights.ts).
1358
+ *
1359
+ * THE HIGHLIGHT IS INTEGRATED ALONG IT: the lobe at each point of the strip,
1360
+ * with its cosine, the surface's and 1/r², by Gauss-Legendre over three pieces
1361
+ * split around where the reflected ray passes nearest the segment — 4 points
1362
+ * across the lobe's window there, 2 on either side. The window's half-width
1363
+ * is the lobe's own at that distance, 0.85·α·d/sin (the ray's angle to the
1364
+ * tube), plus the bulb. Against the exact integral over a 2 × 0.1 m strip
1365
+ * from twelve views it is within 5% at roughness 0.35 and 7% at 0.6 (a
1366
+ * quarter low at 0.15, where the lobe is narrower than the strip is wide).
1367
+ * Karis's representative point, the lobe's peak times the whole strip's
1368
+ * light, was 0.2 to 2 times it across one floor.
1369
+ */
1370
+ fn _rzTubeOne(i: u32, p: vec3f, n: vec3f, v: vec3f, roughness: f32, c: vec3f, h: vec3f, reach: f32) -> _RzLampPair {
1371
+ var out = _RzLampPair(vec3f(0.0), vec3f(0.0), vec3f(0.0), vec3f(0.0));
1372
+ let q = _rzTubeNearest(p, c, h);
1373
+ let qd = length(q - p);
1374
+ if (qd >= reach) { return out; }
1375
+ let t = clamp(qd / max(reach, 1e-4), 0.0, 1.0);
1376
+ let t2 = t * t;
1377
+ let window = 1.0 - t2 * t2;
1378
+ let bulb = rzLightNear(i);
1379
+ let aim = rzLightAim(i);
1380
+ let facing = select(0.0, 1.0, dot(aim, aim) > 0.5);
1381
+ let col = rzLightColor(i) * (window * window);
1382
+ out.d = col * _rzTubeIrradiance(p, _rzLampN, c, h, aim, bulb);
1383
+ if (rzLightSpecular(i) <= 0.0 || _rzTubeIrradiance(p, n, c, h, aim, bulb) <= 0.0) { return out; }
1384
+ let r = reflect(-v, n);
1385
+ let L0 = c - h - p;
1386
+ let Ld = 2.0 * h;
1387
+ let lenD = max(length(Ld), 1e-6);
1388
+ let rLd = dot(r, Ld);
1389
+ let s0 = clamp((dot(r, L0) * rLd - dot(L0, Ld)) / max(dot(Ld, Ld) - rLd * rLd, 1e-8), 0.0, 1.0);
1390
+ let dr = length(L0 + Ld * s0);
1391
+ let sinT = sqrt(max(1.0 - (rLd / lenD) * (rLd / lenD), 1e-4));
1392
+ let a = max(roughness * roughness, 6.1035156e-5);
1393
+ let w = min(0.85 * a * dr / (lenD * sinT) + bulb / lenD, 1.0);
1394
+ let lo = max(s0 - w, 0.0);
1395
+ let hi = min(s0 + w, 1.0);
1396
+ let base = c - h;
1397
+ var acc = vec3f(0.0);
1398
+ // the two side pieces, 2 points each (±1/√3, weight 1)
1399
+ let g2 = 0.5773503;
1400
+ if (lo > 1e-6) {
1401
+ let m = lo * 0.5;
1402
+ acc = acc + (_rzTubeSample(base + Ld * (m - m * g2), p, n, v, roughness, facing, aim, bulb)
1403
+ + _rzTubeSample(base + Ld * (m + m * g2), p, n, v, roughness, facing, aim, bulb)) * m;
1404
+ }
1405
+ if (hi < 1.0 - 1e-6) {
1406
+ let m = (1.0 - hi) * 0.5;
1407
+ let mid = hi + m;
1408
+ acc = acc + (_rzTubeSample(base + Ld * (mid - m * g2), p, n, v, roughness, facing, aim, bulb)
1409
+ + _rzTubeSample(base + Ld * (mid + m * g2), p, n, v, roughness, facing, aim, bulb)) * m;
1410
+ }
1411
+ // the lobe's window, 4 points
1412
+ let m = (hi - lo) * 0.5;
1413
+ let mid = lo + m;
1414
+ acc = acc + (_rzTubeSample(base + Ld * (mid - m * 0.8611363), p, n, v, roughness, facing, aim, bulb) * 0.3478548
1415
+ + _rzTubeSample(base + Ld * (mid - m * 0.3399810), p, n, v, roughness, facing, aim, bulb) * 0.6521452
1416
+ + _rzTubeSample(base + Ld * (mid + m * 0.3399810), p, n, v, roughness, facing, aim, bulb) * 0.6521452
1417
+ + _rzTubeSample(base + Ld * (mid + m * 0.8611363), p, n, v, roughness, facing, aim, bulb) * 0.3478548) * m;
1418
+ out.s = col * acc.x;
1419
+ out.p = col * acc.y;
1420
+ out.q = col * acc.z;
1421
+ return out;
1422
+ }
1293
1423
 
1294
1424
  fn _rzLampOne(i: u32, p: vec3f, n: vec3f, v: vec3f, roughness: f32) -> _RzLampPair {
1295
- var out = _RzLampPair(vec3f(0.0), vec3f(0.0));
1425
+ var out = _RzLampPair(vec3f(0.0), vec3f(0.0), vec3f(0.0), vec3f(0.0));
1296
1426
  if (!_rzLightReaches(i)) { return out; }
1297
1427
  let pr = _rzLightVec(i, 0u);
1298
1428
  let d = pr.xyz - p;
1299
1429
  let dist = length(d);
1300
- if (dist >= pr.w) { return out; }
1430
+ let h = rzLightTube(i);
1431
+ if (dist >= pr.w + length(h)) { return out; }
1432
+ if (dot(h, h) > 0.0) { return _rzTubeOne(i, p, n, v, roughness, pr.xyz, h, pr.w); }
1301
1433
  let toLight = d / max(dist, 1e-4);
1302
1434
  let ndlD = max(dot(_rzLampN, toLight), 0.0);
1303
1435
  let ndl = dot(n, toLight);
@@ -1311,12 +1443,17 @@ fn _rzLampOne(i: u32, p: vec3f, n: vec3f, v: vec3f, roughness: f32) -> _RzLampPa
1311
1443
  let aim = clamp((dot(-toLight, rzLightAim(i)) - cone.x) / max(cone.y - cone.x, 1e-4), 0.0, 1.0);
1312
1444
  let c = rzLightColor(i) * rzLightCookie(i, toLight);
1313
1445
  if (ndlD > 0.0) { out.d = c * (ndlD * falloff * aim * aim); }
1314
- if (ndl > 0.0) { out.s = c * (bsdf_urp(n, toLight, v, ndl, roughness) * falloff * aim * aim * rzLightSpecular(i)); }
1446
+ if (ndl > 0.0) {
1447
+ let k = falloff * aim * aim * rzLightSpecular(i);
1448
+ out.s = c * (bsdf_urp(n, toLight, v, ndl, roughness) * k);
1449
+ out.p = c * (_rzGgxSmith(n, toLight, v, roughness) * ndl * k);
1450
+ out.q = out.p * _rzGrazing(toLight, v);
1451
+ }
1315
1452
  return out;
1316
1453
  }
1317
1454
 
1318
1455
  fn _rzLampWord(bits0: u32, base: u32, p: vec3f, n: vec3f, v: vec3f, roughness: f32) -> _RzLampPair {
1319
- var acc = _RzLampPair(vec3f(0.0), vec3f(0.0));
1456
+ var acc = _RzLampPair(vec3f(0.0), vec3f(0.0), vec3f(0.0), vec3f(0.0));
1320
1457
  var bits = bits0;
1321
1458
  loop {
1322
1459
  if (bits == 0u) { break; }
@@ -1325,6 +1462,8 @@ fn _rzLampWord(bits0: u32, base: u32, p: vec3f, n: vec3f, v: vec3f, roughness: f
1325
1462
  let one = _rzLampOne(i, p, n, v, roughness);
1326
1463
  acc.d = acc.d + one.d;
1327
1464
  acc.s = acc.s + one.s;
1465
+ acc.p = acc.p + one.p;
1466
+ acc.q = acc.q + one.q;
1328
1467
  }
1329
1468
  return acc;
1330
1469
  }
@@ -1332,6 +1471,8 @@ fn _rzLampWord(bits0: u32, base: u32, p: vec3f, n: vec3f, v: vec3f, roughness: f
1332
1471
  fn rzLampsSpecular(p: vec3f, n: vec3f, v: vec3f, roughness: f32) -> vec3f {
1333
1472
  var d = vec3f(0.0);
1334
1473
  var s = vec3f(0.0);
1474
+ var pr = vec3f(0.0);
1475
+ var q = vec3f(0.0);
1335
1476
  let count = rzLightCount();
1336
1477
  let docs = _rzLightDocCount();
1337
1478
  if (docs > 0u) {
@@ -1346,12 +1487,18 @@ fn rzLampsSpecular(p: vec3f, n: vec3f, v: vec3f, roughness: f32) -> vec3f {
1346
1487
  let w7 = _rzLampWord(m.hi.w, 224u, p, n, v, roughness);
1347
1488
  d = d + w0.d + w1.d + w2.d + w3.d + w4.d + w5.d + w6.d + w7.d;
1348
1489
  s = s + w0.s + w1.s + w2.s + w3.s + w4.s + w5.s + w6.s + w7.s;
1490
+ pr = pr + w0.p + w1.p + w2.p + w3.p + w4.p + w5.p + w6.p + w7.p;
1491
+ q = q + w0.q + w1.q + w2.q + w3.q + w4.q + w5.q + w6.q + w7.q;
1349
1492
  }
1350
1493
  for (var i = docs; i < count; i = i + 1u) {
1351
1494
  let one = _rzLampOne(i, p, n, v, roughness);
1352
1495
  d = d + one.d;
1353
1496
  s = s + one.s;
1497
+ pr = pr + one.p;
1498
+ q = q + one.q;
1354
1499
  }
1500
+ _rzLampPrincipled = pr;
1501
+ _rzLampPrincipledGrazing = q;
1355
1502
  // The first closure's walk speaks for the fragment; a second principled in
1356
1503
  // the same graph shares p and the prelude normal, so its answer is the same.
1357
1504
  if (!_rzLampDiffuseSet) {
@@ -1374,10 +1521,14 @@ fn rzLampsSpecular(p: vec3f, n: vec3f, v: vec3f, roughness: f32) -> vec3f {
1374
1521
  // behind the surface gets neither term, as the game's step(N·L ≥ 0) has it.
1375
1522
 
1376
1523
  fn _rzAddLightOne(i: u32, p: vec3f, n: vec3f, v: vec3f, e: f32) -> _RzLampPair {
1377
- var out = _RzLampPair(vec3f(0.0), vec3f(0.0));
1524
+ var out = _RzLampPair(vec3f(0.0), vec3f(0.0), vec3f(0.0), vec3f(0.0));
1378
1525
  if (!_rzLightReaches(i)) { return out; }
1379
1526
  let pr = _rzLightVec(i, 0u);
1380
- let d = pr.xyz - p;
1527
+ // a tube is met at its nearest point here: a graph's Blinn-Phong glint has
1528
+ // no representative-point form
1529
+ let tube = rzLightTube(i);
1530
+ let at = select(pr.xyz, _rzTubeNearest(p, pr.xyz, tube), dot(tube, tube) > 0.0);
1531
+ let d = at - p;
1381
1532
  let dist = length(d);
1382
1533
  if (dist >= pr.w) { return out; }
1383
1534
  let toLight = d / max(dist, 1e-4);
@@ -1397,7 +1548,7 @@ fn _rzAddLightOne(i: u32, p: vec3f, n: vec3f, v: vec3f, e: f32) -> _RzLampPair {
1397
1548
  }
1398
1549
 
1399
1550
  fn _rzAddLightWord(bits0: u32, base: u32, p: vec3f, n: vec3f, v: vec3f, e: f32) -> _RzLampPair {
1400
- var acc = _RzLampPair(vec3f(0.0), vec3f(0.0));
1551
+ var acc = _RzLampPair(vec3f(0.0), vec3f(0.0), vec3f(0.0), vec3f(0.0));
1401
1552
  var bits = bits0;
1402
1553
  loop {
1403
1554
  if (bits == 0u) { break; }
@@ -1411,7 +1562,7 @@ fn _rzAddLightWord(bits0: u32, base: u32, p: vec3f, n: vec3f, v: vec3f, e: f32)
1411
1562
  }
1412
1563
 
1413
1564
  fn rz_additional_lights(p: vec3f, n: vec3f, v: vec3f, e: f32) -> _RzLampPair {
1414
- var acc = _RzLampPair(vec3f(0.0), vec3f(0.0));
1565
+ var acc = _RzLampPair(vec3f(0.0), vec3f(0.0), vec3f(0.0), vec3f(0.0));
1415
1566
  let count = rzLightCount();
1416
1567
  let docs = _rzLightDocCount();
1417
1568
  if (docs > 0u) {
@@ -1471,7 +1622,8 @@ fn rz_box_project(d: vec3f, p: vec3f, bmin: vec3f, bmax: vec3f, c: vec3f) -> vec
1471
1622
  /**
1472
1623
  * The scene's reflection: the stage's own reflection probe where the engine
1473
1624
  * captured one (Engine reflection probe — rendered from the stage's middle,
1474
- * GGX-prefiltered, box-projected to the stage's bounds), else the world's sky.
1625
+ * GGX-prefiltered, box-projected to the stage's bounds, or a local probe's own
1626
+ * point and box, outside which the sky stands in), else the world's sky.
1475
1627
  *
1476
1628
  * ROUGHNESS PICKS THE MIP ON UNITY'S CURVE, r·(1.7 − 0.7r)·6 on a cube of
1477
1629
  * seven levels (64² down to 1²) — URP's PerceptualRoughnessToMipmapLevel with
@@ -1480,6 +1632,9 @@ fn rz_box_project(d: vec3f, p: vec3f, bmin: vec3f, bmax: vec3f, c: vec3f) -> vec
1480
1632
  fn rzProbeSpecular(dir: vec3f, wp: vec3f, roughness: f32) -> vec3f {
1481
1633
  let r = clamp(roughness, 0.0, 1.0);
1482
1634
  if (probe.boxMin.w < 0.5) { return rzWorldSpecularLod(dir, r, 1.0); }
1635
+ // A local probe (boxMax.w 1) holds only its box: past it, the sky.
1636
+ let outside = any(wp <= probe.boxMin.xyz) || any(wp >= probe.boxMax.xyz);
1637
+ if (probe.boxMax.w > 0.5 && outside) { return rzWorldSpecularLod(dir, r, 1.0); }
1483
1638
  let levels = f32(textureNumLevels(probeCube) - 1u);
1484
1639
  let lod = min(r * (1.7 - 0.7 * r) * 6.0, levels);
1485
1640
  let d = rz_box_project(normalize(dir), wp, probe.boxMin.xyz, probe.boxMax.xyz, probe.center.xyz);
@@ -1506,9 +1661,15 @@ fn rzProbeSpecular(dir: vec3f, wp: vec3f, roughness: f32) -> vec3f {
1506
1661
  * shaded here by THIS surface's albedo and the epilogue adds none; as a layer
1507
1662
  * of a larger look it leaves them to the epilogue. The cast's shadow on a
1508
1663
  * stage multiplies the lit part.
1664
+ *
1665
+ * fresnel 1 draws the direct highlights as Blender's Principled does: GGX
1666
+ * with Smith's visibility and Fresnel's reflectance (_rzGrazing),
1667
+ * per light — a dark dielectric seen at a grazing angle mirrors a lamp several
1668
+ * times as strongly as head on. 0 (the default, every Unity stage) is URP's
1669
+ * lobe and reflectance, term for term; between them it mixes.
1509
1670
  */
1510
1671
  fn urp_lit(base: vec3f, metallic: f32, smoothness: f32, spec: f32, occlusion: f32, emission: vec3f, alpha: f32,
1511
- N: vec3f, L: vec3f, V: vec3f, sun_rgb: vec3f, amb_rgb: vec3f, shadow: f32, wp: vec3f,
1672
+ fresnel: f32, N: vec3f, L: vec3f, V: vec3f, sun_rgb: vec3f, amb_rgb: vec3f, shadow: f32, wp: vec3f,
1512
1673
  surface: bool) -> vec4f {
1513
1674
  let m = clamp(metallic, 0.0, 1.0);
1514
1675
  let sm = clamp(smoothness, 0.0, 1.0);
@@ -1529,8 +1690,16 @@ fn urp_lit(base: vec3f, metallic: f32, smoothness: f32, spec: f32, occlusion: f3
1529
1690
  _rzLampDiffuseTaken = true;
1530
1691
  }
1531
1692
  let ao = clamp(occlusion, 0.0, 1.0);
1532
- let direct = diffuse * (sun_rgb * (NL * shadow) + lampDiff)
1533
- + specular * (bsdf_urp(N, L, V, NL, r) * sun_rgb * shadow + lampSpec);
1693
+ let urpSpec = specular * (bsdf_urp(N, L, V, NL, r) * sun_rgb * shadow + lampSpec);
1694
+ var directSpec = urpSpec;
1695
+ let fz = clamp(fresnel, 0.0, 1.0);
1696
+ if (fz > 0.0) {
1697
+ let sunP = _rzGgxSmith(N, L, V, r) * NL * sun_rgb * shadow;
1698
+ let principled = specular * (sunP + _rzLampPrincipled)
1699
+ + (vec3f(1.0) - specular) * (sunP * _rzGrazing(L, V) + _rzLampPrincipledGrazing);
1700
+ directSpec = mix(urpSpec, principled, fz);
1701
+ }
1702
+ let direct = diffuse * (sun_rgb * (NL * shadow) + lampDiff) + directSpec;
1534
1703
  let indirect = diffuse * amb_rgb * ao
1535
1704
  + rzProbeSpecular(reflect(-V, N), wp, r) * ao * urp_env_brdf(specular, r, sm, reflectivity, NV);
1536
1705
  var lit = direct + indirect;
@@ -1 +1 @@
1
- {"version":3,"file":"composite.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/composite.ts"],"names":[],"mappings":"AA+BA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;+BAgC+B;AAC/B;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,EAAE,CAIhG;AAID,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAEzG,KAAK,qBAAqB,GAAG;IAC3B,gFAAgF;IAChF,IAAI,EAAE,MAAM,CAAA;IACZ,kFAAkF;IAClF,UAAU,EAAE,MAAM,CAAA;IAClB,4DAA4D;IAC5D,aAAa,EAAE,OAAO,CAAA;IACtB,oEAAoE;IACpE,aAAa,EAAE,OAAO,CAAA;IACtB;6DACyD;IACzD,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,gFAAgF;IAChF,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,gEAAgE;IAChE,QAAQ,EAAE,MAAM,CAAA;IAChB;mFAC+E;IAC/E,GAAG,CAAC,EAAE,OAAO,CAAA;IACb;;;yEAGqE;IACrE,UAAU,EAAE,MAAM,CAAA;IAClB;;8EAE0E;IAC1E,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;CACjB,CAAA;AAuSD;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,gBAAgB,g6fAuI5B,CAAA;AA4MD,wBAAgB,oBAAoB,CAAC,MAAM,CAAC,EAAE,qBAAqB,GAAG,IAAI,GAAG,MAAM,CAclF;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,qBAAqB,GAAG,MAAM,CA2GtE;AAED,iFAAiF;AACjF,eAAO,MAAM,qBAAqB,QAA6B,CAAA"}
1
+ {"version":3,"file":"composite.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/composite.ts"],"names":[],"mappings":"AA+BA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;+BAgC+B;AAC/B;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,EAAE,CAIhG;AAID,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAEzG,KAAK,qBAAqB,GAAG;IAC3B,gFAAgF;IAChF,IAAI,EAAE,MAAM,CAAA;IACZ,kFAAkF;IAClF,UAAU,EAAE,MAAM,CAAA;IAClB,4DAA4D;IAC5D,aAAa,EAAE,OAAO,CAAA;IACtB,oEAAoE;IACpE,aAAa,EAAE,OAAO,CAAA;IACtB;6DACyD;IACzD,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,gFAAgF;IAChF,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,gEAAgE;IAChE,QAAQ,EAAE,MAAM,CAAA;IAChB;mFAC+E;IAC/E,GAAG,CAAC,EAAE,OAAO,CAAA;IACb;;;yEAGqE;IACrE,UAAU,EAAE,MAAM,CAAA;IAClB;;8EAE0E;IAC1E,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;CACjB,CAAA;AA2TD;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,gBAAgB,g6fAuI5B,CAAA;AA+OD,wBAAgB,oBAAoB,CAAC,MAAM,CAAC,EAAE,qBAAqB,GAAG,IAAI,GAAG,MAAM,CAclF;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,qBAAqB,GAAG,MAAM,CA2GtE;AAED,iFAAiF;AACjF,eAAO,MAAM,qBAAqB,QAA6B,CAAA"}
@@ -121,7 +121,8 @@ override APPLY_GAMMA: bool = true;
121
121
  // scene's own LUT (setStageGrade) — see _rzGradeScene() below.
122
122
  // viewU[10] = (camera world position, _) — refreshed with the basis above.
123
123
  // viewU[11..14] = the cast's positions (effects; count in viewU[10].w).
124
- // viewU[15] = (soft curve contrast, _, _, _).
124
+ // viewU[15] = (soft curve contrast, pixel filter weights for offsets 0, ±1, ±2)
125
+ // — 1, 0, 0 is no filter; see rzPixelFiltered.
125
126
  // invGamma = 1/gamma precomputed on CPU — avoids a per-pixel divide.
126
127
  @group(0) @binding(6) var bgEquirect: texture_2d<f32>;
127
128
  // The scene pass's own MSAA depth buffer, bound depth-only. NOT an extra
@@ -177,6 +178,25 @@ fn rzFieldMerge(top: vec4f, bot: vec4f) -> vec4f {
177
178
  return vec4f(top.rgb + bot.rgb * (1.0 - top.a), top.a + bot.a * (1.0 - top.a));
178
179
  }
179
180
 
181
+ /** The scene's premultiplied HDR light and coverage through the pixel filter
182
+ * (Engine.setPixelFilter): separable weights viewU[15].yzw over offsets 0,
183
+ * ±1, ±2, the coverage filtered alongside so the sky still composites under
184
+ * it. Only called while a filter is on. */
185
+ fn rzPixelFiltered(coord: vec2<i32>) -> vec4f {
186
+ let sz = vec2<i32>(textureDimensions(hdrTex)) - vec2<i32>(1);
187
+ let w = array<f32, 3>(viewU[15].y, viewU[15].z, viewU[15].w);
188
+ let r = select(1, 2, w[2] > 0.0);
189
+ var acc = vec4f(0.0);
190
+ for (var dy = -r; dy <= r; dy++) {
191
+ for (var dx = -r; dx <= r; dx++) {
192
+ let p = clamp(coord + vec2<i32>(dx, dy), vec2<i32>(0), sz);
193
+ let k = w[abs(dx)] * w[abs(dy)];
194
+ acc += vec4f(textureLoad(hdrTex, p, 0).rgb, textureLoad(maskTex, p, 0).g) * k;
195
+ }
196
+ }
197
+ return acc;
198
+ }
199
+
180
200
  fn linearDepth(coord: vec2<i32>) -> f32 {
181
201
  let z = textureLoad(depthTex, coord, 0);
182
202
  // projA and projB are m[10] and m[14] of whatever projection the camera built,
@@ -537,8 +557,14 @@ const COMPOSITE_BODY = /* wgsl */ `
537
557
 
538
558
  @fragment fn fs(@builtin(position) fragCoord: vec4f) -> @location(0) vec4f {
539
559
  let coord = vec2<i32>(fragCoord.xy);
540
- let hdr = textureLoad(hdrTex, coord, 0);
541
- let alpha = textureLoad(maskTex, coord, 0).g;
560
+ var hdr = textureLoad(hdrTex, coord, 0);
561
+ var alpha = textureLoad(maskTex, coord, 0).g;
562
+ // A renderer's pixel filter, while one is set (viewU[15].y < 1).
563
+ if (viewU[15].y < 0.99999) {
564
+ let f = rzPixelFiltered(coord);
565
+ hdr = vec4f(f.rgb, hdr.a);
566
+ alpha = f.a;
567
+ }
542
568
  let a = max(alpha, 1e-6);
543
569
  let straight = hdr.rgb / a;
544
570
  let fullSz = vec2f(textureDimensions(hdrTex));
@@ -603,6 +629,9 @@ const COMPOSITE_BODY = /* wgsl */ `
603
629
 
604
630
  let exposed = sceneStraight * exp2(viewU[0].x);
605
631
  var disp = max(viewTransform(exposed), vec3f(0.0));
632
+ // The scene as formed before any grade: what the sky's light-first
633
+ // composite below measures itself against.
634
+ var formed = disp;
606
635
  // Grade the SCENE only, before the display gamma. Deliberately not applied to
607
636
  // the background: it keeps a picked background color exactly as picked, and —
608
637
  // load-bearing — leaves green-screen mode's key color unshifted so chroma
@@ -612,6 +641,7 @@ const COMPOSITE_BODY = /* wgsl */ `
612
641
  }
613
642
  if (APPLY_GAMMA) {
614
643
  disp = pow(disp, vec3f(viewU[0].y));
644
+ formed = pow(formed, vec3f(viewU[0].y));
615
645
  }
616
646
  // ── Film grain, on the SCENE ONLY ─────────────────────────────────────────
617
647
  //
@@ -634,6 +664,11 @@ const COMPOSITE_BODY = /* wgsl */ `
634
664
  let bg = viewU[2];
635
665
  var bgA = select(0.0, 1.0, bg.w > 0.5);
636
666
  var bgPm = bg.rgb * bgA; // premultiplied accumulator
667
+ // Mode 3's sky as LIGHT (scene-linear, exposed) and as formed, for the
668
+ // light-first composite after the layers below.
669
+ var skyLin = vec3f(0.0);
670
+ var skyFormed = vec3f(0.0);
671
+ var skyIsLight = false;
637
672
  // This pixel's world-space view ray, rebuilt from the camera basis — what the
638
673
  // equirect samples by, and what both effect mounts navigate by. The dome sits
639
674
  // at infinity (no parallax): PhotoDome-style, display-only. Hoisted out of the
@@ -654,11 +689,14 @@ const COMPOSITE_BODY = /* wgsl */ `
654
689
  // like a sun instead of clipping at texture white. bg.x carries the
655
690
  // world STRENGTH (the colour slot is dead in equirect modes). The
656
691
  // scene's grade stays scene-only, the documented rule above.
657
- var sky = max(viewTransform(bgPm * bg.x * exp2(viewU[0].x)), vec3f(0.0));
692
+ skyLin = bgPm * bg.x * exp2(viewU[0].x);
693
+ var sky = max(viewTransform(skyLin), vec3f(0.0));
658
694
  if (APPLY_GAMMA) {
659
695
  sky = pow(sky, vec3f(viewU[0].y));
660
696
  }
661
697
  bgPm = sky;
698
+ skyFormed = sky;
699
+ skyIsLight = true;
662
700
  }
663
701
  }
664
702
  BACKGROUND_CALL
@@ -667,6 +705,23 @@ const COMPOSITE_BODY = /* wgsl */ `
667
705
  // expression, because the foreground mount composites onto it.
668
706
  var outRgb = disp * sceneAlpha + bgPm * (1.0 - sceneAlpha);
669
707
  var outA = sceneAlpha + bgA * (1.0 - sceneAlpha);
708
+ // OVER A SKY OF LIGHT, LIGHT FIRST. Mode 3's sky is radiance, so where the
709
+ // scene only partly covers it — an antialiased edge, an ember smaller than a
710
+ // pixel, a glow the pixel filter spread — the film sees the two lights
711
+ // summed, as a renderer's camera does: the scene's light plus the sky's
712
+ // through what the scene leaves uncovered, then formed. Formed apart and
713
+ // mixed after, as the display-space composite above does, a thin edge at
714
+ // twenty times white is clipped to white first and then faded by its
715
+ // coverage to a grey dot. The difference is added on top of the layered
716
+ // frame, so the background effects drawn over the sky and the scene's grade
717
+ // stay as they were; with neither it is the summed light exactly.
718
+ if (skyIsLight && sceneAlpha < 0.99999) {
719
+ var summed = max(viewTransform(exposed * sceneAlpha + skyLin * (1.0 - sceneAlpha)), vec3f(0.0));
720
+ if (APPLY_GAMMA) {
721
+ summed = pow(summed, vec3f(viewU[0].y));
722
+ }
723
+ outRgb += summed - (formed * sceneAlpha + skyFormed * (1.0 - sceneAlpha));
724
+ }
670
725
  // Ribbons are NOT read here any more: they draw inside the scene pass, so
671
726
  // they are already in disp — tone mapped, and bloomed, which they never
672
727
  // were while this line existed. Sampling them here as well would draw them
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reze-engine",
3
- "version": "0.68.14",
3
+ "version": "0.68.16",
4
4
  "description": "A lightweight WebGPU engine for real-time 3D MMD/PMX model rendering",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",