@naanouff/w3dts-path-tracer 0.1.18 → 0.1.19
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/PathTracerFrameUploadController.d.ts.map +1 -1
- package/dist/SceneToRayData.d.ts.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/openPbrBsdfReference.d.ts +55 -0
- package/dist/openPbrBsdfReference.d.ts.map +1 -0
- package/dist/openPbrMaterialParams.d.ts +55 -0
- package/dist/openPbrMaterialParams.d.ts.map +1 -0
- package/dist/pathTracerCoreCompat.d.ts +19 -0
- package/dist/pathTracerCoreCompat.d.ts.map +1 -0
- package/dist/pathTracerMaterialTable.d.ts +14 -3
- package/dist/pathTracerMaterialTable.d.ts.map +1 -1
- package/dist/pathTracerRuntimeConfig.d.ts +1 -1
- package/dist/pathTracerRuntimeConfig.d.ts.map +1 -1
- package/dist/w3dts-path-tracer.es.js +2065 -1860
- package/dist/w3dts-path-tracer.umd.js +3 -3
- package/package.json +3 -2
- package/shaders/hybrid_primary_aov.compute.wgsl +8 -0
- package/shaders/path_trace.compute.wgsl +114 -25
- package/shaders/path_trace_common.wgsl +449 -0
|
@@ -1187,6 +1187,58 @@ fn beer_lambert(attColor: vec3<f32>, attDist: f32, dist: f32) -> vec3<f32> {
|
|
|
1187
1187
|
return exp(-sigma * dist);
|
|
1188
1188
|
}
|
|
1189
1189
|
|
|
1190
|
+
/** Henyey–Greenstein phase sample around `forward` (`g` ∈ (−1,1)). */
|
|
1191
|
+
fn sample_henyey_greenstein(forward: vec3<f32>, g: f32, u: vec2<f32>) -> vec3<f32> {
|
|
1192
|
+
let axis = normalize(forward);
|
|
1193
|
+
let gg = clamp(g, -0.999, 0.999);
|
|
1194
|
+
var cosTheta: f32;
|
|
1195
|
+
if (abs(gg) < 1e-3) {
|
|
1196
|
+
cosTheta = 1.0 - 2.0 * u.x;
|
|
1197
|
+
} else {
|
|
1198
|
+
let sqr = (1.0 - gg * gg) / (1.0 - gg + 2.0 * gg * u.x);
|
|
1199
|
+
cosTheta = (1.0 + gg * gg - sqr * sqr) / (2.0 * gg);
|
|
1200
|
+
}
|
|
1201
|
+
cosTheta = clamp(cosTheta, -1.0, 1.0);
|
|
1202
|
+
let sinTheta = sqrt(max(0.0, 1.0 - cosTheta * cosTheta));
|
|
1203
|
+
let phi = 2.0 * PI * u.y;
|
|
1204
|
+
let basis = build_orthonormal_basis(axis);
|
|
1205
|
+
return normalize(
|
|
1206
|
+
basis[0] * (sinTheta * cos(phi)) + basis[1] * (sinTheta * sin(phi)) + axis * cosTheta
|
|
1207
|
+
);
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
/**
|
|
1211
|
+
* OpenPBR transmission_scatter single-event: probability 1−e^(−σ_s · dist),
|
|
1212
|
+
* then HG deflection + spectral scatter albedo. No-op when σ_s≈0.
|
|
1213
|
+
*/
|
|
1214
|
+
fn openpbr_volume_scatter_event(
|
|
1215
|
+
dir: vec3<f32>,
|
|
1216
|
+
scatterRgb: vec3<f32>,
|
|
1217
|
+
scatterG: f32,
|
|
1218
|
+
dist: f32,
|
|
1219
|
+
u: vec2<f32>,
|
|
1220
|
+
) -> vec4<f32> {
|
|
1221
|
+
let sigmaS = max(scatterRgb.x, max(scatterRgb.y, scatterRgb.z));
|
|
1222
|
+
if (sigmaS <= 1e-4 || dist <= 1e-6) {
|
|
1223
|
+
return vec4<f32>(dir, 0.0);
|
|
1224
|
+
}
|
|
1225
|
+
let pSc = 1.0 - exp(-sigmaS * dist);
|
|
1226
|
+
if (u.x > pSc) {
|
|
1227
|
+
return vec4<f32>(dir, 0.0);
|
|
1228
|
+
}
|
|
1229
|
+
let albedo = clamp(scatterRgb / sigmaS, vec3<f32>(0.0), vec3<f32>(1.0));
|
|
1230
|
+
let newDir = sample_henyey_greenstein(dir, scatterG, vec2<f32>(u.y, fract(u.x * 17.0)));
|
|
1231
|
+
// Pack albedo luminance into w as a marker; RGB applied by caller via xyz... use separate return.
|
|
1232
|
+
// w=1 means scattered; caller multiplies throughput by albedo.
|
|
1233
|
+
return vec4<f32>(newDir, 1.0);
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
fn openpbr_volume_scatter_albedo(scatterRgb: vec3<f32>) -> vec3<f32> {
|
|
1237
|
+
let sigmaS = max(scatterRgb.x, max(scatterRgb.y, scatterRgb.z));
|
|
1238
|
+
if (sigmaS <= 1e-4) { return vec3<f32>(1.0); }
|
|
1239
|
+
return clamp(scatterRgb / sigmaS, vec3<f32>(0.0), vec3<f32>(1.0));
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1190
1242
|
/** Cauchy IOR at `lambdaNm` from n_d (587.6 nm) and KHR dispersion (`20 / Abbe`). */
|
|
1191
1243
|
fn dispersive_ior(nd: f32, dispersion: f32, lambdaNm: f32) -> f32 {
|
|
1192
1244
|
if (dispersion <= 0.0) { return nd; }
|
|
@@ -1275,6 +1327,137 @@ fn sample_reflect_ggx(N: vec3<f32>, V: vec3<f32>, roughness: f32, pixel: vec2<u3
|
|
|
1275
1327
|
return sample_vndf_reflect(N, V, roughness, pixel, seed).xyz;
|
|
1276
1328
|
}
|
|
1277
1329
|
|
|
1330
|
+
/**
|
|
1331
|
+
* Anisotropic VNDF half-vector (Heitz 2018) in the OpenPBR tangent frame (T,B,N).
|
|
1332
|
+
* `at`/`ab` are GGX alphas (αx, αy), not perceptual roughness.
|
|
1333
|
+
*/
|
|
1334
|
+
fn sample_vndf_half_vector_aniso(
|
|
1335
|
+
N: vec3<f32>,
|
|
1336
|
+
V: vec3<f32>,
|
|
1337
|
+
T: vec3<f32>,
|
|
1338
|
+
B: vec3<f32>,
|
|
1339
|
+
at: f32,
|
|
1340
|
+
ab: f32,
|
|
1341
|
+
r: vec2<f32>,
|
|
1342
|
+
) -> vec3<f32> {
|
|
1343
|
+
let ax = max(at, 1e-4);
|
|
1344
|
+
let ay = max(ab, 1e-4);
|
|
1345
|
+
let Ve = normalize(vec3<f32>(
|
|
1346
|
+
ax * dot(V, T),
|
|
1347
|
+
ay * dot(V, B),
|
|
1348
|
+
max(dot(V, N), 1e-4)
|
|
1349
|
+
));
|
|
1350
|
+
let lensq = Ve.x * Ve.x + Ve.y * Ve.y;
|
|
1351
|
+
let T1 = select(
|
|
1352
|
+
vec3<f32>(1.0, 0.0, 0.0),
|
|
1353
|
+
vec3<f32>(-Ve.y, Ve.x, 0.0) / sqrt(lensq),
|
|
1354
|
+
lensq > 1e-7
|
|
1355
|
+
);
|
|
1356
|
+
let T2 = cross(Ve, T1);
|
|
1357
|
+
let rad = sqrt(clamp(r.x, 0.0, 1.0));
|
|
1358
|
+
let phi = 2.0 * PI * r.y;
|
|
1359
|
+
let t1 = rad * cos(phi);
|
|
1360
|
+
var t2 = rad * sin(phi);
|
|
1361
|
+
let s = 0.5 * (1.0 + Ve.z);
|
|
1362
|
+
t2 = (1.0 - s) * sqrt(max(0.0, 1.0 - t1 * t1)) + s * t2;
|
|
1363
|
+
let nhz = sqrt(max(0.0, 1.0 - t1 * t1 - t2 * t2));
|
|
1364
|
+
let Nh = t1 * T1 + t2 * T2 + nhz * Ve;
|
|
1365
|
+
let Ht = normalize(vec3<f32>(ax * Nh.x, ay * Nh.y, max(Nh.z, 1e-6)));
|
|
1366
|
+
return normalize(Ht.x * T + Ht.y * B + Ht.z * N);
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
fn sample_vndf_reflect_aniso(
|
|
1370
|
+
N: vec3<f32>,
|
|
1371
|
+
V: vec3<f32>,
|
|
1372
|
+
T: vec3<f32>,
|
|
1373
|
+
B: vec3<f32>,
|
|
1374
|
+
at: f32,
|
|
1375
|
+
ab: f32,
|
|
1376
|
+
pixel: vec2<u32>,
|
|
1377
|
+
seed: u32,
|
|
1378
|
+
) -> vec3<f32> {
|
|
1379
|
+
if (at < 1e-3 && ab < 1e-3) {
|
|
1380
|
+
return normalize(reflect(-V, N));
|
|
1381
|
+
}
|
|
1382
|
+
let H = sample_vndf_half_vector_aniso(N, V, T, B, at, ab, rand2(pixel, seed));
|
|
1383
|
+
return normalize(reflect(-V, H));
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
fn ggx_reflection_pdf_aniso(
|
|
1387
|
+
N: vec3<f32>,
|
|
1388
|
+
V: vec3<f32>,
|
|
1389
|
+
L: vec3<f32>,
|
|
1390
|
+
T: vec3<f32>,
|
|
1391
|
+
B: vec3<f32>,
|
|
1392
|
+
at: f32,
|
|
1393
|
+
ab: f32,
|
|
1394
|
+
) -> f32 {
|
|
1395
|
+
if (dot(N, L) <= 0.0) { return 0.0; }
|
|
1396
|
+
let NdotV = max(dot(N, V), 1e-4);
|
|
1397
|
+
let NdotL = max(dot(N, L), 1e-4);
|
|
1398
|
+
let hRaw = V + L;
|
|
1399
|
+
let hLen = length(hRaw);
|
|
1400
|
+
if (hLen < 1e-6) { return 0.0; }
|
|
1401
|
+
let H = hRaw / hLen;
|
|
1402
|
+
let D = distribution_ggx_aniso(N, H, T, B, at, ab);
|
|
1403
|
+
// G1(V) ≈ 1 / (1 + ΛV); approximate Λ via anisotropic lengths.
|
|
1404
|
+
let TdotV = dot(T, V);
|
|
1405
|
+
let BdotV = dot(B, V);
|
|
1406
|
+
let lenV = length(vec3<f32>(at * TdotV, ab * BdotV, NdotV));
|
|
1407
|
+
let lambdaV = 0.5 * (-1.0 + lenV / max(NdotV, 1e-4));
|
|
1408
|
+
let g1V = 1.0 / (1.0 + max(lambdaV, 0.0));
|
|
1409
|
+
return (D * g1V) / max(4.0 * NdotV, 1e-6);
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
/** Mixture PDF with optional OpenPBR specular anisotropy. */
|
|
1413
|
+
fn openpbr_bsdf_mixture_pdf(
|
|
1414
|
+
mat: PathTracerMaterial,
|
|
1415
|
+
N: vec3<f32>,
|
|
1416
|
+
V: vec3<f32>,
|
|
1417
|
+
L: vec3<f32>,
|
|
1418
|
+
roughness: f32,
|
|
1419
|
+
clearcoatRoughness: f32,
|
|
1420
|
+
pClearcoat: f32,
|
|
1421
|
+
pSpec: f32,
|
|
1422
|
+
pDiff: f32,
|
|
1423
|
+
) -> f32 {
|
|
1424
|
+
let NdotL = dot(N, L);
|
|
1425
|
+
if (NdotL <= 0.0) { return 0.0; }
|
|
1426
|
+
var pdf = pDiff * (NdotL / PI);
|
|
1427
|
+
let aniso = clamp(mat.openpbrBase.z, 0.0, 1.0);
|
|
1428
|
+
if (pSpec > 0.0) {
|
|
1429
|
+
if (aniso > 1e-4) {
|
|
1430
|
+
let axy = openpbr_alpha_xy(max(roughness, 0.04), aniso);
|
|
1431
|
+
let frame = openpbr_tangent_frame(N, mat.openpbrBase.w);
|
|
1432
|
+
pdf += pSpec * ggx_reflection_pdf_aniso(N, V, L, frame[0], frame[1], axy.x, axy.y);
|
|
1433
|
+
} else {
|
|
1434
|
+
pdf += pSpec * ggx_reflection_pdf(N, V, L, roughness);
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
if (pClearcoat > 0.0) {
|
|
1438
|
+
pdf += pClearcoat * ggx_reflection_pdf(N, V, L, clearcoatRoughness);
|
|
1439
|
+
}
|
|
1440
|
+
return pdf;
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
/** Sample OpenPBR base specular (aniso VNDF when needed). */
|
|
1444
|
+
fn openpbr_sample_specular(
|
|
1445
|
+
mat: PathTracerMaterial,
|
|
1446
|
+
N: vec3<f32>,
|
|
1447
|
+
V: vec3<f32>,
|
|
1448
|
+
roughness: f32,
|
|
1449
|
+
pixel: vec2<u32>,
|
|
1450
|
+
seed: u32,
|
|
1451
|
+
) -> vec3<f32> {
|
|
1452
|
+
let aniso = clamp(mat.openpbrBase.z, 0.0, 1.0);
|
|
1453
|
+
if (aniso > 1e-4) {
|
|
1454
|
+
let axy = openpbr_alpha_xy(max(roughness, 0.04), aniso);
|
|
1455
|
+
let frame = openpbr_tangent_frame(N, mat.openpbrBase.w);
|
|
1456
|
+
return sample_vndf_reflect_aniso(N, V, frame[0], frame[1], axy.x, axy.y, pixel, seed);
|
|
1457
|
+
}
|
|
1458
|
+
return sample_reflect_ggx(N, V, roughness, pixel, seed);
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1278
1461
|
struct AreaLightSample {
|
|
1279
1462
|
dir: vec3<f32>,
|
|
1280
1463
|
radiance: vec3<f32>,
|
|
@@ -1608,6 +1791,272 @@ fn emissive_nee(
|
|
|
1608
1791
|
const BACKPLATE = 80u;
|
|
1609
1792
|
const BSDF_SHADOW_CATCHER = 2u;
|
|
1610
1793
|
const BSDF_UNLIT = 3u;
|
|
1794
|
+
const BSDF_OPENPBR = 4u;
|
|
1795
|
+
|
|
1796
|
+
/** Schlick power at the F82 sample angle (cosθ = 1/7). */
|
|
1797
|
+
const OPENPBR_F82_SCHLICK = 0.509788576; // (6/7)^5
|
|
1798
|
+
|
|
1799
|
+
/** Convert OpenPBR (F0, F82) to a Schlick F90 that matches F82 at cosθ=1/7. */
|
|
1800
|
+
fn openpbr_schlick_f90_from_f82(f0: vec3<f32>, f82: vec3<f32>) -> vec3<f32> {
|
|
1801
|
+
return f0 + (f82 - f0) / OPENPBR_F82_SCHLICK;
|
|
1802
|
+
}
|
|
1803
|
+
|
|
1804
|
+
fn openpbr_dielectric_f0_scalar(ior: f32) -> f32 {
|
|
1805
|
+
let eta = max(ior, 1e-4);
|
|
1806
|
+
let t = (eta - 1.0) / (eta + 1.0);
|
|
1807
|
+
return t * t;
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1810
|
+
/**
|
|
1811
|
+
* OpenPBR αx/αy from roughness + anisotropy (aspect = √(1 − 0.9 a)).
|
|
1812
|
+
* a=0 → (r², r²). Used by the native Light Tracer path (not the Khronos remap).
|
|
1813
|
+
*/
|
|
1814
|
+
fn openpbr_alpha_xy(roughness: f32, anisotropy: f32) -> vec2<f32> {
|
|
1815
|
+
let r = clamp(roughness, 0.0, 1.0);
|
|
1816
|
+
let a = clamp(anisotropy, 0.0, 1.0);
|
|
1817
|
+
let alpha = max(r * r, 1e-4);
|
|
1818
|
+
let aspect = sqrt(max(1.0 - 0.9 * a, 1e-4));
|
|
1819
|
+
return vec2<f32>(alpha / aspect, alpha * aspect);
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
/** Tangent frame for OpenPBR anisotropy: orthonormal basis rotated around N. */
|
|
1823
|
+
fn openpbr_tangent_frame(N: vec3<f32>, tangentRotation: f32) -> mat3x3<f32> {
|
|
1824
|
+
let basis = build_orthonormal_basis(N);
|
|
1825
|
+
let ang = tangentRotation * 2.0 * PI;
|
|
1826
|
+
let c = cos(ang);
|
|
1827
|
+
let s = sin(ang);
|
|
1828
|
+
let T = normalize(c * basis[0] + s * basis[1]);
|
|
1829
|
+
let B = cross(N, T);
|
|
1830
|
+
return mat3x3<f32>(T, B, N);
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
fn distribution_ggx_aniso(
|
|
1834
|
+
N: vec3<f32>,
|
|
1835
|
+
H: vec3<f32>,
|
|
1836
|
+
T: vec3<f32>,
|
|
1837
|
+
B: vec3<f32>,
|
|
1838
|
+
at: f32,
|
|
1839
|
+
ab: f32,
|
|
1840
|
+
) -> f32 {
|
|
1841
|
+
let NdotH = max(dot(N, H), 0.0);
|
|
1842
|
+
let TdotH = dot(T, H);
|
|
1843
|
+
let BdotH = dot(B, H);
|
|
1844
|
+
let a2 = at * ab;
|
|
1845
|
+
let dx = ab * TdotH;
|
|
1846
|
+
let dy = at * BdotH;
|
|
1847
|
+
let dz = a2 * NdotH;
|
|
1848
|
+
let d2 = dx * dx + dy * dy + dz * dz;
|
|
1849
|
+
let b2 = a2 / max(d2, 1e-12);
|
|
1850
|
+
return a2 * b2 * b2 / PI;
|
|
1851
|
+
}
|
|
1852
|
+
|
|
1853
|
+
fn v_ggx_correlated_aniso(
|
|
1854
|
+
at: f32,
|
|
1855
|
+
ab: f32,
|
|
1856
|
+
T: vec3<f32>,
|
|
1857
|
+
B: vec3<f32>,
|
|
1858
|
+
N: vec3<f32>,
|
|
1859
|
+
V: vec3<f32>,
|
|
1860
|
+
L: vec3<f32>,
|
|
1861
|
+
NdotV: f32,
|
|
1862
|
+
NdotL: f32,
|
|
1863
|
+
) -> f32 {
|
|
1864
|
+
let TdotV = dot(T, V);
|
|
1865
|
+
let BdotV = dot(B, V);
|
|
1866
|
+
let TdotL = dot(T, L);
|
|
1867
|
+
let BdotL = dot(B, L);
|
|
1868
|
+
let lenV = length(vec3<f32>(at * TdotV, ab * BdotV, NdotV));
|
|
1869
|
+
let lenL = length(vec3<f32>(at * TdotL, ab * BdotL, NdotL));
|
|
1870
|
+
return 0.5 / max(NdotL * lenV + NdotV * lenL, 1e-5);
|
|
1871
|
+
}
|
|
1872
|
+
|
|
1873
|
+
/**
|
|
1874
|
+
* Thin-film preview (Belcour/Barla-inspired): modulate base F0 with 1st-order
|
|
1875
|
+
* Airy interference for RGB wavelengths. Full spectral OPD tables remain optional.
|
|
1876
|
+
*/
|
|
1877
|
+
fn openpbr_thin_film_modulate_f0(
|
|
1878
|
+
baseF0: vec3<f32>,
|
|
1879
|
+
filmWeight: f32,
|
|
1880
|
+
thicknessNm: f32,
|
|
1881
|
+
filmIor: f32,
|
|
1882
|
+
cosTheta: f32,
|
|
1883
|
+
) -> vec3<f32> {
|
|
1884
|
+
let w = clamp(filmWeight, 0.0, 1.0);
|
|
1885
|
+
if (w <= 1e-4 || thicknessNm <= 1e-3) { return baseF0; }
|
|
1886
|
+
let eta = max(filmIor, 1.0);
|
|
1887
|
+
let ct = clamp(cosTheta, 0.0, 1.0);
|
|
1888
|
+
let sin2t = (1.0 / (eta * eta)) * (1.0 - ct * ct);
|
|
1889
|
+
if (sin2t >= 1.0) {
|
|
1890
|
+
return mix(baseF0, vec3<f32>(1.0), w);
|
|
1891
|
+
}
|
|
1892
|
+
let cosT = sqrt(max(1.0 - sin2t, 0.0));
|
|
1893
|
+
let R0 = openpbr_dielectric_f0_scalar(eta);
|
|
1894
|
+
let R12 = R0 + (1.0 - R0) * pow(1.0 - ct, 5.0);
|
|
1895
|
+
let T121 = 1.0 - R12;
|
|
1896
|
+
let opd = 2.0 * eta * thicknessNm * cosT;
|
|
1897
|
+
let lambda = vec3<f32>(650.0, 550.0, 450.0);
|
|
1898
|
+
// Optical phase for path difference (Belcour/Barla OPD in nm).
|
|
1899
|
+
let phi = (2.0 * PI * opd) / lambda;
|
|
1900
|
+
let R23 = clamp(baseF0, vec3<f32>(1e-5), vec3<f32>(0.9999));
|
|
1901
|
+
let R123 = clamp(vec3<f32>(R12) * R23, vec3<f32>(1e-5), vec3<f32>(0.9999));
|
|
1902
|
+
let r123 = sqrt(R123);
|
|
1903
|
+
let Rs = (T121 * T121) * R23 / max(vec3<f32>(1.0) - R123, vec3<f32>(1e-4));
|
|
1904
|
+
// Airy series m=1,2 (cos terms) — Belcour preview without XYZ sensitivity tables.
|
|
1905
|
+
var I = vec3<f32>(R12) + Rs;
|
|
1906
|
+
var Cm = Rs - T121;
|
|
1907
|
+
Cm = Cm * r123;
|
|
1908
|
+
I = I + Cm * (2.0 * cos(phi));
|
|
1909
|
+
Cm = Cm * r123;
|
|
1910
|
+
I = I + Cm * (2.0 * cos(2.0 * phi));
|
|
1911
|
+
return mix(baseF0, clamp(I, vec3<f32>(0.0), vec3<f32>(1.5)), w);
|
|
1912
|
+
}
|
|
1913
|
+
|
|
1914
|
+
/** Charlie-like fuzz (Zeltner) lobe used above the coat — soft retro-reflection. */
|
|
1915
|
+
fn openpbr_fuzz_f_cos(
|
|
1916
|
+
N: vec3<f32>,
|
|
1917
|
+
V: vec3<f32>,
|
|
1918
|
+
L: vec3<f32>,
|
|
1919
|
+
fuzzWeight: f32,
|
|
1920
|
+
fuzzColor: vec3<f32>,
|
|
1921
|
+
fuzzRoughness: f32,
|
|
1922
|
+
) -> vec3<f32> {
|
|
1923
|
+
let w = clamp(fuzzWeight, 0.0, 1.0);
|
|
1924
|
+
if (w <= 1e-5) { return vec3<f32>(0.0); }
|
|
1925
|
+
let NdotL = max(dot(N, L), 0.0);
|
|
1926
|
+
let NdotV = max(dot(N, V), 1e-4);
|
|
1927
|
+
if (NdotL <= 0.0) { return vec3<f32>(0.0); }
|
|
1928
|
+
let r = clamp(fuzzRoughness, 0.02, 1.0);
|
|
1929
|
+
// Soft inv-Ndot lobe (Charlie-inspired) — energy scaled by weight × color.
|
|
1930
|
+
let inv = pow(max(1.0 - NdotL, 0.0), mix(0.5, 4.0, r));
|
|
1931
|
+
return fuzzColor * (w * inv * NdotV * NdotL / PI);
|
|
1932
|
+
}
|
|
1933
|
+
|
|
1934
|
+
/**
|
|
1935
|
+
* OpenPBR opaque stack on top of the Khronos layered lobe:
|
|
1936
|
+
* F82 metal tint, coat F0 from coat_ior + darkening, fuzz above coat,
|
|
1937
|
+
* Oren-Nayar-ish diffuse via base_diffuse_roughness, SSS albedo mix (preview).
|
|
1938
|
+
*/
|
|
1939
|
+
fn openpbr_layered_f_cos(
|
|
1940
|
+
mat: PathTracerMaterial,
|
|
1941
|
+
N: vec3<f32>,
|
|
1942
|
+
V: vec3<f32>,
|
|
1943
|
+
L: vec3<f32>,
|
|
1944
|
+
F0: vec3<f32>,
|
|
1945
|
+
F90: vec3<f32>,
|
|
1946
|
+
baseColor: vec3<f32>,
|
|
1947
|
+
metallic: f32,
|
|
1948
|
+
roughness: f32,
|
|
1949
|
+
clearcoat: f32,
|
|
1950
|
+
clearcoatRoughness: f32,
|
|
1951
|
+
) -> vec3<f32> {
|
|
1952
|
+
let NdotL = dot(N, L);
|
|
1953
|
+
let NdotV = max(dot(N, V), 1e-4);
|
|
1954
|
+
if (NdotL <= 0.0) { return vec3<f32>(0.0); }
|
|
1955
|
+
|
|
1956
|
+
let hRaw = L + V;
|
|
1957
|
+
let hLen = length(hRaw);
|
|
1958
|
+
if (hLen < 1e-6) { return vec3<f32>(0.0); }
|
|
1959
|
+
let H = hRaw / hLen;
|
|
1960
|
+
let VdotH = clamp(dot(V, H), 0.0, 1.0);
|
|
1961
|
+
let r = clamp(roughness, 0.04, 1.0);
|
|
1962
|
+
let m = clamp(metallic, 0.0, 1.0);
|
|
1963
|
+
|
|
1964
|
+
// Thin-film modulates the interface F0 (pearl / soap bubble).
|
|
1965
|
+
var F0eff = F0;
|
|
1966
|
+
let filmW = mat.openpbrCoatExt.w;
|
|
1967
|
+
if (filmW > 1e-4) {
|
|
1968
|
+
F0eff = openpbr_thin_film_modulate_f0(
|
|
1969
|
+
F0, filmW, mat.openpbrThinFilmFuzz.x, mat.openpbrThinFilmFuzz.y, VdotH
|
|
1970
|
+
);
|
|
1971
|
+
}
|
|
1972
|
+
|
|
1973
|
+
let aniso = clamp(mat.openpbrBase.z, 0.0, 1.0);
|
|
1974
|
+
var D: f32;
|
|
1975
|
+
var Vg: f32;
|
|
1976
|
+
if (aniso > 1e-4) {
|
|
1977
|
+
let axy = openpbr_alpha_xy(r, aniso);
|
|
1978
|
+
let frame = openpbr_tangent_frame(N, mat.openpbrBase.w);
|
|
1979
|
+
D = distribution_ggx_aniso(N, H, frame[0], frame[1], axy.x, axy.y);
|
|
1980
|
+
Vg = v_ggx_correlated_aniso(axy.x, axy.y, frame[0], frame[1], N, V, L, NdotV, NdotL);
|
|
1981
|
+
} else {
|
|
1982
|
+
D = distribution_ggx(N, H, r);
|
|
1983
|
+
Vg = v_ggx_correlated(NdotV, NdotL, r);
|
|
1984
|
+
}
|
|
1985
|
+
let F = fresnel_schlick_f90(VdotH, F0eff, F90);
|
|
1986
|
+
|
|
1987
|
+
let ab = ggx_directional_albedo_ab(NdotV, r);
|
|
1988
|
+
let eMs = 1.0 - clamp(ab.x + ab.y, 0.0, 1.0);
|
|
1989
|
+
let fAvg = F0eff + (F90 - F0eff) * (1.0 / 21.0);
|
|
1990
|
+
let msScale = vec3<f32>(1.0) / max(vec3<f32>(1.0) - fAvg * eMs, vec3<f32>(1e-4));
|
|
1991
|
+
let fSpec = D * Vg * F * msScale;
|
|
1992
|
+
let maxF = max(F.x, max(F.y, F.z));
|
|
1993
|
+
let kD = (1.0 - maxF) * (1.0 - m);
|
|
1994
|
+
|
|
1995
|
+
// EON preview: base_diffuse_roughness softens the Lambert cosine (full EON tables later).
|
|
1996
|
+
let diffRough = clamp(mat.openpbrBase.y, 0.0, 1.0);
|
|
1997
|
+
var diffAlbedo = baseColor;
|
|
1998
|
+
let sssW = clamp(mat.openpbrFuzzColor.w, 0.0, 1.0);
|
|
1999
|
+
if (sssW > 1e-4) {
|
|
2000
|
+
// Preview SSS: pull diffuse toward subsurface_color (random-walk later).
|
|
2001
|
+
diffAlbedo = mix(diffAlbedo, mat.openpbrSubsurface.xyz, sssW * 0.55);
|
|
2002
|
+
}
|
|
2003
|
+
let nlDiff = mix(NdotL, pow(NdotL, 1.0 + diffRough), diffRough);
|
|
2004
|
+
let diffTerm = diffAlbedo * (kD / PI) * nlDiff;
|
|
2005
|
+
var baseFCos = fSpec * NdotL + diffTerm;
|
|
2006
|
+
|
|
2007
|
+
let cc = clamp(clearcoat, 0.0, 1.0);
|
|
2008
|
+
if (cc > 1e-5) {
|
|
2009
|
+
let coatIor = max(mat.openpbrCoat.w, 1.0);
|
|
2010
|
+
let coatF0 = openpbr_dielectric_f0_scalar(coatIor);
|
|
2011
|
+
let rcc = clamp(clearcoatRoughness, 0.04, 1.0);
|
|
2012
|
+
let Dcc = distribution_ggx(N, H, rcc);
|
|
2013
|
+
let Vcc = v_ggx_correlated(NdotV, NdotL, rcc);
|
|
2014
|
+
let fccVh = coatF0 + (1.0 - coatF0) * pow(1.0 - VdotH, 5.0);
|
|
2015
|
+
let fccNv = coatF0 + (1.0 - coatF0) * pow(1.0 - clamp(NdotV, 0.0, 1.0), 5.0);
|
|
2016
|
+
let fccNl = coatF0 + (1.0 - coatF0) * pow(1.0 - clamp(NdotL, 0.0, 1.0), 5.0);
|
|
2017
|
+
let darken = clamp(mat.openpbrCoatExt.x, 0.0, 1.0);
|
|
2018
|
+
let coatTint = mix(vec3<f32>(1.0), mat.openpbrCoat.xyz, cc * darken);
|
|
2019
|
+
let baseAtten = (1.0 - cc * fccNv) * (1.0 - cc * fccNl);
|
|
2020
|
+
baseFCos = baseFCos * baseAtten * coatTint + vec3<f32>(cc * Dcc * Vcc * fccVh * NdotL);
|
|
2021
|
+
}
|
|
2022
|
+
|
|
2023
|
+
// Fuzz sits above the coat (OpenPBR stack order).
|
|
2024
|
+
let fuzzW = mat.openpbrThinFilmFuzz.z;
|
|
2025
|
+
let fuzzR = mat.openpbrThinFilmFuzz.w;
|
|
2026
|
+
baseFCos += openpbr_fuzz_f_cos(N, V, L, fuzzW, mat.openpbrFuzzColor.xyz, fuzzR);
|
|
2027
|
+
return baseFCos;
|
|
2028
|
+
}
|
|
2029
|
+
|
|
2030
|
+
/** Opaque f·cos dispatcher: OpenPBR stack vs Khronos layered. */
|
|
2031
|
+
fn shade_opaque_f_cos(
|
|
2032
|
+
mat: PathTracerMaterial,
|
|
2033
|
+
bsdfKind: u32,
|
|
2034
|
+
N: vec3<f32>,
|
|
2035
|
+
V: vec3<f32>,
|
|
2036
|
+
L: vec3<f32>,
|
|
2037
|
+
F0: vec3<f32>,
|
|
2038
|
+
F90: vec3<f32>,
|
|
2039
|
+
baseColor: vec3<f32>,
|
|
2040
|
+
metallic: f32,
|
|
2041
|
+
roughness: f32,
|
|
2042
|
+
clearcoat: f32,
|
|
2043
|
+
clearcoatRoughness: f32,
|
|
2044
|
+
) -> vec3<f32> {
|
|
2045
|
+
if (bsdfKind == BSDF_OPENPBR) {
|
|
2046
|
+
return openpbr_layered_f_cos(
|
|
2047
|
+
mat, N, V, L, F0, F90, baseColor, metallic, roughness, clearcoat, clearcoatRoughness
|
|
2048
|
+
);
|
|
2049
|
+
}
|
|
2050
|
+
return layered_opaque_f_cos(
|
|
2051
|
+
N, V, L, F0, F90, baseColor, metallic, roughness, clearcoat, clearcoatRoughness
|
|
2052
|
+
);
|
|
2053
|
+
}
|
|
2054
|
+
|
|
2055
|
+
/** Adjust metal F90 for OpenPBR F82-tint (specular.yzw). */
|
|
2056
|
+
fn openpbr_adjust_f90(mat: PathTracerMaterial, metallic: f32, F0: vec3<f32>, F90: vec3<f32>) -> vec3<f32> {
|
|
2057
|
+
if (metallic < 0.5) { return F90; }
|
|
2058
|
+
return openpbr_schlick_f90_from_f82(F0, clamp(mat.specular.yzw, vec3<f32>(0.0), vec3<f32>(1.0)));
|
|
2059
|
+
}
|
|
1611
2060
|
|
|
1612
2061
|
fn backplate_enabled() -> bool {
|
|
1613
2062
|
return pathTracerEnv[BACKPLATE] > 0.5;
|