@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naanouff/w3dts-path-tracer",
3
- "version": "0.1.18",
3
+ "version": "0.1.19",
4
4
  "description": "Path tracing with denoiser for the W3DTS WebGPU engine.",
5
5
  "keywords": [
6
6
  "path-tracing",
@@ -47,7 +47,8 @@
47
47
  "typescript": "^5.5.4",
48
48
  "vite": "^5.4.21",
49
49
  "vite-plugin-dts": "^4.0.3",
50
- "vitest": "^3.2.7"
50
+ "vitest": "^3.2.7",
51
+ "@naanouff/w3dts-lightmapper": "0.0.1"
51
52
  },
52
53
  "scripts": {
53
54
  "build": "vite build",
@@ -64,6 +64,14 @@ struct PathTracerMaterial {
64
64
  transmission: vec4<f32>,
65
65
  attenuation: vec4<f32>,
66
66
  extra: vec4<f32>,
67
+ openpbrBase: vec4<f32>,
68
+ openpbrCoat: vec4<f32>,
69
+ openpbrCoatExt: vec4<f32>,
70
+ openpbrThinFilmFuzz: vec4<f32>,
71
+ openpbrFuzzColor: vec4<f32>,
72
+ openpbrSubsurface: vec4<f32>,
73
+ openpbrSubsurfaceExt: vec4<f32>,
74
+ openpbrTransScatter: vec4<f32>,
67
75
  }
68
76
 
69
77
  @group(0) @binding(0) var<uniform> frame: FrameUniforms;
@@ -57,11 +57,11 @@ struct PathTracerInstance {
57
57
  _pad: u32,
58
58
  }
59
59
 
60
- /** One material record (80 bytes, must match CPU PATH_TRACER_MATERIAL_FLOATS / PATH_TRACER_MATERIAL_FIELD). */
60
+ /** One material record (208 bytes, must match CPU PATH_TRACER_MATERIAL_FLOATS / PATH_TRACER_MATERIAL_FIELD). */
61
61
  struct PathTracerMaterial {
62
62
  /** x texSlot (255 = none), y bsdfKind, z envSlot, w alpha (0 opaque, >0 mask cutoff, <0 blend) */
63
63
  header: vec4<f32>,
64
- /** x specularFactor, yzw specularColor */
64
+ /** x specularFactor, yzw specularColor (OpenPBR: specular_weight + specular_color / F82) */
65
65
  specular: vec4<f32>,
66
66
  /** x transmission, y opacity, z ior, w dispersion */
67
67
  transmission: vec4<f32>,
@@ -69,6 +69,22 @@ struct PathTracerMaterial {
69
69
  attenuation: vec4<f32>,
70
70
  /** x clearcoat, y clearcoatRoughness, z catcherReflect, w shadowCatcherCutoff */
71
71
  extra: vec4<f32>,
72
+ /** OpenPBR: x base_weight, y base_diffuse_roughness, z specular_roughness_anisotropy, w tangent_rotation */
73
+ openpbrBase: vec4<f32>,
74
+ /** OpenPBR: xyz coat_color, w coat_ior */
75
+ openpbrCoat: vec4<f32>,
76
+ /** OpenPBR: x coat_darkening, y coat_roughness_anisotropy, z thin_walled, w thin_film_weight */
77
+ openpbrCoatExt: vec4<f32>,
78
+ /** OpenPBR: x thin_film_thickness_nm, y thin_film_ior, z fuzz_weight, w fuzz_roughness */
79
+ openpbrThinFilmFuzz: vec4<f32>,
80
+ /** OpenPBR: xyz fuzz_color, w subsurface_weight */
81
+ openpbrFuzzColor: vec4<f32>,
82
+ /** OpenPBR: xyz subsurface_color, w subsurface_radius */
83
+ openpbrSubsurface: vec4<f32>,
84
+ /** OpenPBR: xyz subsurface_radius_scale, w subsurface_scatter_anisotropy */
85
+ openpbrSubsurfaceExt: vec4<f32>,
86
+ /** OpenPBR: xyz transmission_scatter, w transmission_scatter_anisotropy */
87
+ openpbrTransScatter: vec4<f32>,
72
88
  }
73
89
 
74
90
  @group(0) @binding(0) var<uniform> frame: FrameUniforms;
@@ -527,8 +543,12 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
527
543
  roughness = mr.y;
528
544
  }
529
545
  let emissive = eval_surface_emissive(matSlot, hitBu, hitBv, uvs, uvs1, emit0);
530
- let baseColor = eval_surface_albedo(matSlot, hitBu, hitBv, uvs, baseF);
546
+ var baseColor = eval_surface_albedo(matSlot, hitBu, hitBv, uvs, baseF);
531
547
  let bsdfKind = u32(hitMat.header.y + 0.5);
548
+ // OpenPBR: triangle factors are raw; apply base_weight here (maps stay unbaked).
549
+ if (bsdfKind == BSDF_OPENPBR) {
550
+ baseColor *= hitMat.openpbrBase.x;
551
+ }
532
552
  if (bsdfKind == BSDF_UNLIT) {
533
553
  if ((featureFlags & FLAG_UNLIT_AS_DIFFUSE) == 0u) {
534
554
  if (bounce == 0u) {
@@ -598,7 +618,11 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
598
618
  let transmission = material_transmission_opacity(hitMat).x;
599
619
  let rSel = rand2(globalPixel, frameIdx * 131u + bounce * 9u + 17u);
600
620
  if (material_is_dielectric(hitMat, metallic) && rSel.x < transmission) {
601
- if (!frontFace) {
621
+ // OpenPBR thin-walled: no volume path / Beer–Lambert (entry≈exit).
622
+ let openPbrThinWalled =
623
+ bsdfKind == BSDF_OPENPBR && hitMat.openpbrCoatExt.z > 0.5;
624
+ var volumeHitDist = hitDist;
625
+ if (!frontFace && !openPbrThinWalled) {
602
626
  throughput *= beer_lambert(hitMat.attenuation.xyz, hitMat.attenuation.w, hitDist);
603
627
  }
604
628
  var ior = max(hitMat.transmission.z, 1.0);
@@ -639,7 +663,28 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
639
663
  let ll = smith_lambda(max(abs(dot(N, dielDir)), 1e-4), aD);
640
664
  throughput *= (1.0 + lv) / (1.0 + lv + ll);
641
665
  }
642
- currentRd = normalize(dielDir);
666
+ dielDir = normalize(dielDir);
667
+ // OpenPBR transmission_scatter: single HG event along the chord (honey / ketchup volume).
668
+ if (
669
+ !frontFace &&
670
+ bsdfKind == BSDF_OPENPBR &&
671
+ !openPbrThinWalled &&
672
+ !PREVIEW_MODE
673
+ ) {
674
+ let uSc = rand2(globalPixel, frameIdx * 307u + bounce * 41u + 11u);
675
+ let scEvt = openpbr_volume_scatter_event(
676
+ dielDir,
677
+ hitMat.openpbrTransScatter.xyz,
678
+ hitMat.openpbrTransScatter.w,
679
+ volumeHitDist,
680
+ uSc
681
+ );
682
+ if (scEvt.w > 0.5) {
683
+ dielDir = scEvt.xyz;
684
+ throughput *= openpbr_volume_scatter_albedo(hitMat.openpbrTransScatter.xyz);
685
+ }
686
+ }
687
+ currentRd = dielDir;
643
688
  carryPdf = false;
644
689
  continue;
645
690
  }
@@ -649,7 +694,10 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
649
694
  let safeCcRough = max(hitMat.extra.y, 0.04);
650
695
  let dielectricF0 = dielectric_f0_specular_ext(hitMat);
651
696
  let F0 = pbr_f0(baseColor, metallic, dielectricF0);
652
- let F90 = pbr_f90(hitMat, metallic);
697
+ var F90 = pbr_f90(hitMat, metallic);
698
+ if (bsdfKind == BSDF_OPENPBR) {
699
+ F90 = openpbr_adjust_f90(hitMat, metallic, F0, F90);
700
+ }
653
701
  let V = normalize(-currentRd);
654
702
 
655
703
  // GI bounces already darken occluded points. The extra AO rays only stand in when GI is off.
@@ -694,8 +742,8 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
694
742
  L = sample_cone_around(L, sunAngularRadius, globalPixel, frameIdx * 401u + bounce * 7u);
695
743
  let vis0 = shadow_transmittance(shadowOrigin, L, count, useBvh, useTlas, bvhRoot, bvhNodeCount, sceneFlags, lightT);
696
744
  let vis1 = shadow_transmittance(shadowOrigin, L2, count, useBvh, useTlas, bvhRoot, bvhNodeCount, sceneFlags, lightT);
697
- let f0 = layered_opaque_f_cos(N, V, L, F0, F90, baseColor, metallic, safeRough, clearcoat, safeCcRough);
698
- let f1 = layered_opaque_f_cos(N, V, L2, F0, F90, baseColor, metallic, safeRough, clearcoat, safeCcRough);
745
+ let f0 = shade_opaque_f_cos(hitMat, bsdfKind, N, V, L, F0, F90, baseColor, metallic, safeRough, clearcoat, safeCcRough);
746
+ let f1 = shade_opaque_f_cos(hitMat, bsdfKind, N, V, L2, F0, F90, baseColor, metallic, safeRough, clearcoat, safeCcRough);
699
747
  direct += 0.5 * (f0 * vis0 + f1 * vis1) * lightRadiance;
700
748
  sawDirectional = true;
701
749
  continue;
@@ -727,7 +775,7 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
727
775
  lightRadiance *= att;
728
776
  lightT = dist - 0.004;
729
777
  }
730
- let fLit = layered_opaque_f_cos(N, V, L, F0, F90, baseColor, metallic, safeRough, clearcoat, safeCcRough);
778
+ let fLit = shade_opaque_f_cos(hitMat, bsdfKind, N, V, L, F0, F90, baseColor, metallic, safeRough, clearcoat, safeCcRough);
731
779
  if (shadowsOn != 0u) {
732
780
  let vis = shadow_transmittance(shadowOrigin, L, count, useBvh, useTlas, bvhRoot, bvhNodeCount, sceneFlags, lightT);
733
781
  direct += fLit * vis * lightRadiance;
@@ -740,16 +788,22 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
740
788
  let envRand = rand2(globalPixel, frameIdx * 307u + bounce * 19u + 9u);
741
789
  let envSamp = sample_env(pathEnv, envRand.x, envRand.y);
742
790
  if (envSamp.pdf > 1e-6 && dot(N, envSamp.dir) > 0.0) {
743
- let pdfB = layered_bsdf_mixture_pdf(
744
- N, V, envSamp.dir, safeRough, safeCcRough, lobeProbs.x, lobeProbs.y, lobeProbs.z
791
+ let pdfB = select(
792
+ layered_bsdf_mixture_pdf(
793
+ N, V, envSamp.dir, safeRough, safeCcRough, lobeProbs.x, lobeProbs.y, lobeProbs.z
794
+ ),
795
+ openpbr_bsdf_mixture_pdf(
796
+ hitMat, N, V, envSamp.dir, safeRough, safeCcRough, lobeProbs.x, lobeProbs.y, lobeProbs.z
797
+ ),
798
+ bsdfKind == BSDF_OPENPBR
745
799
  );
746
800
  let mis = envSamp.pdf / max(envSamp.pdf + pdfB, 1e-6);
747
801
  var envVis = vec3<f32>(1.0);
748
802
  if (shadowsOn != 0u) {
749
803
  envVis = shadow_transmittance(shadowOrigin, envSamp.dir, count, useBvh, useTlas, bvhRoot, bvhNodeCount, sceneFlags, 1e10);
750
804
  }
751
- direct += layered_opaque_f_cos(
752
- N, V, envSamp.dir, F0, F90, baseColor, metallic, safeRough, clearcoat, safeCcRough
805
+ direct += shade_opaque_f_cos(
806
+ hitMat, bsdfKind, N, V, envSamp.dir, F0, F90, baseColor, metallic, safeRough, clearcoat, safeCcRough
753
807
  ) * env_equirect(pathEnv, envSamp.dir) * envVis * mis / envSamp.pdf;
754
808
  }
755
809
  let softCount = min(u32(pathTracerEnv[3]), 4u);
@@ -776,16 +830,16 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
776
830
  softVis = shadow_transmittance(shadowOrigin, Ls, count, useBvh, useTlas, bvhRoot, bvhNodeCount, sceneFlags, 1e10);
777
831
  }
778
832
  let softLi = pathTracerEnv[sb + 3u] * vec3<f32>(pathTracerEnv[sb + 4u], pathTracerEnv[sb + 5u], pathTracerEnv[sb + 6u]) * mask;
779
- direct += layered_opaque_f_cos(
780
- N, V, Ls, F0, F90, baseColor, metallic, safeRough, clearcoat, safeCcRough
833
+ direct += shade_opaque_f_cos(
834
+ hitMat, bsdfKind, N, V, Ls, F0, F90, baseColor, metallic, safeRough, clearcoat, safeCcRough
781
835
  ) * softLi * softVis * omega;
782
836
  }
783
837
  } else if (env_layer_count() == 0u && (featureFlags & FLAG_ENV_CUBEMAP) != 0u) {
784
838
  let envDir = cosine_sample_hemisphere(N, globalPixel, bounce * 31u + 13u, frameIdx);
785
839
  let pdfEnv = max(dot(N, envDir), 0.0) / PI;
786
840
  if (pdfEnv > 1e-6) {
787
- let fEnv = layered_opaque_f_cos(
788
- N, V, envDir, F0, F90, baseColor, metallic, safeRough, clearcoat, safeCcRough
841
+ let fEnv = shade_opaque_f_cos(
842
+ hitMat, bsdfKind, N, V, envDir, F0, F90, baseColor, metallic, safeRough, clearcoat, safeCcRough
789
843
  );
790
844
  if (PREVIEW_MODE) {
791
845
  direct += fEnv * env_sample(envDir, envIblIntensity) / pdfEnv;
@@ -796,8 +850,14 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
796
850
  }
797
851
  var mis = 1.0;
798
852
  if (enableGI != 0u) {
799
- let pdfB = layered_bsdf_mixture_pdf(
800
- N, V, envDir, safeRough, safeCcRough, lobeProbs.x, lobeProbs.y, lobeProbs.z
853
+ let pdfB = select(
854
+ layered_bsdf_mixture_pdf(
855
+ N, V, envDir, safeRough, safeCcRough, lobeProbs.x, lobeProbs.y, lobeProbs.z
856
+ ),
857
+ openpbr_bsdf_mixture_pdf(
858
+ hitMat, N, V, envDir, safeRough, safeCcRough, lobeProbs.x, lobeProbs.y, lobeProbs.z
859
+ ),
860
+ bsdfKind == BSDF_OPENPBR
801
861
  );
802
862
  mis = mis_balance_power(pdfEnv, pdfB);
803
863
  }
@@ -832,7 +892,11 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
832
892
  } else if (enableReflection != 0u && rnd.x < lobeProbs.x + lobeProbs.y) {
833
893
  if (glossyCount >= glossyLimit || opaqueCount >= opaqueLimit) { break; }
834
894
  glossyCount += 1u;
835
- if ((featureFlags & FLAG_GGX_REFLECTION) != 0u) {
895
+ if (bsdfKind == BSDF_OPENPBR) {
896
+ newDir = openpbr_sample_specular(
897
+ hitMat, N, V, safeRough, globalPixel, frameIdx + bounce * 19u
898
+ );
899
+ } else if ((featureFlags & FLAG_GGX_REFLECTION) != 0u) {
836
900
  newDir = sample_reflect_ggx(N, V, safeRough, globalPixel, frameIdx + bounce * 19u);
837
901
  } else {
838
902
  let R = normalize(reflect(-currentRd, N));
@@ -847,15 +911,40 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
847
911
  diffuseCount += 1u;
848
912
  newDir = cosine_sample_hemisphere(N, globalPixel, bounce, frameIdx);
849
913
  }
850
- let pdfMix = layered_bsdf_mixture_pdf(
851
- N, V, newDir, safeRough, safeCcRough, lobeProbs.x, lobeProbs.y, lobeProbs.z
914
+ let pdfMix = select(
915
+ layered_bsdf_mixture_pdf(
916
+ N, V, newDir, safeRough, safeCcRough, lobeProbs.x, lobeProbs.y, lobeProbs.z
917
+ ),
918
+ openpbr_bsdf_mixture_pdf(
919
+ hitMat, N, V, newDir, safeRough, safeCcRough, lobeProbs.x, lobeProbs.y, lobeProbs.z
920
+ ),
921
+ bsdfKind == BSDF_OPENPBR
852
922
  );
853
923
  if (dot(N, newDir) <= 0.0 || pdfMix <= 1e-6) { break; }
854
- let fTotalCos = layered_opaque_f_cos(
855
- N, V, newDir, F0, F90, baseColor, metallic, safeRough, clearcoat, safeCcRough
924
+ let fTotalCos = shade_opaque_f_cos(
925
+ hitMat, bsdfKind, N, V, newDir, F0, F90, baseColor, metallic, safeRough, clearcoat, safeCcRough
856
926
  );
857
927
  throughput *= min(fTotalCos / pdfMix, vec3<f32>(16.0));
858
- currentRo = hitPos + N * surface_bias(length(hitPos));
928
+ // OpenPBR SSS single-scatter preview: offset into the volume on diffuse exits.
929
+ var nextOrigin = hitPos + N * surface_bias(length(hitPos));
930
+ if (bsdfKind == BSDF_OPENPBR && rnd.x >= lobeProbs.x + lobeProbs.y) {
931
+ let sssW = clamp(hitMat.openpbrFuzzColor.w, 0.0, 1.0);
932
+ let thinW = hitMat.openpbrCoatExt.z > 0.5;
933
+ if (sssW > 1e-3 && !thinW && !PREVIEW_MODE) {
934
+ let radius = max(hitMat.openpbrSubsurface.w, 1e-3);
935
+ let uSss = rand2(globalPixel, frameIdx * 411u + bounce * 31u + 9u);
936
+ let depth = -log(max(1.0 - uSss.x * sssW, 1e-4)) * radius * 0.35;
937
+ nextOrigin = hitPos - N * min(depth, radius);
938
+ let scale = hitMat.openpbrSubsurfaceExt.xyz;
939
+ let sigma = 1.0 / max(radius * scale, vec3<f32>(1e-3));
940
+ throughput *= mix(
941
+ vec3<f32>(1.0),
942
+ hitMat.openpbrSubsurface.xyz * exp(-sigma * depth),
943
+ sssW
944
+ );
945
+ }
946
+ }
947
+ currentRo = nextOrigin;
859
948
  currentRd = newDir;
860
949
  carryPdf = true;
861
950
  carryN = N;