@luma.gl/shaders 10.0.0-alpha.10 → 10.0.0-alpha.11

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.
Files changed (89) hide show
  1. package/dist/dist.dev.js +1776 -58
  2. package/dist/dist.min.js +1394 -147
  3. package/dist/index.cjs +35 -0
  4. package/dist/index.cjs.map +3 -3
  5. package/dist/index.d.ts +17 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +9 -0
  8. package/dist/index.js.map +1 -1
  9. package/dist/modules/lighting/clustered-light-grid/clustered-light-grid.cjs +40 -0
  10. package/dist/modules/lighting/clustered-light-grid/clustered-light-grid.d.ts +6 -0
  11. package/dist/modules/lighting/clustered-light-grid/clustered-light-grid.d.ts.map +1 -0
  12. package/dist/modules/lighting/clustered-light-grid/clustered-light-grid.js +15 -0
  13. package/dist/modules/lighting/clustered-light-grid/clustered-light-grid.js.map +1 -0
  14. package/dist/modules/lighting/contact-shadow/contact-shadow.cjs +348 -0
  15. package/dist/modules/lighting/contact-shadow/contact-shadow.d.ts +188 -0
  16. package/dist/modules/lighting/contact-shadow/contact-shadow.d.ts.map +1 -0
  17. package/dist/modules/lighting/contact-shadow/contact-shadow.js +307 -0
  18. package/dist/modules/lighting/contact-shadow/contact-shadow.js.map +1 -0
  19. package/dist/modules/lighting/pbr-ambient-occlusion/pbr-ambient-occlusion.cjs +87 -0
  20. package/dist/modules/lighting/pbr-ambient-occlusion/pbr-ambient-occlusion.d.ts +21 -0
  21. package/dist/modules/lighting/pbr-ambient-occlusion/pbr-ambient-occlusion.d.ts.map +1 -0
  22. package/dist/modules/lighting/pbr-ambient-occlusion/pbr-ambient-occlusion.js +61 -0
  23. package/dist/modules/lighting/pbr-ambient-occlusion/pbr-ambient-occlusion.js.map +1 -0
  24. package/dist/modules/lighting/pbr-contact-shadow/pbr-contact-shadow.cjs +87 -0
  25. package/dist/modules/lighting/pbr-contact-shadow/pbr-contact-shadow.d.ts +21 -0
  26. package/dist/modules/lighting/pbr-contact-shadow/pbr-contact-shadow.d.ts.map +1 -0
  27. package/dist/modules/lighting/pbr-contact-shadow/pbr-contact-shadow.js +61 -0
  28. package/dist/modules/lighting/pbr-contact-shadow/pbr-contact-shadow.js.map +1 -0
  29. package/dist/modules/lighting/pbr-material/pbr-material-glsl.cjs +33 -11
  30. package/dist/modules/lighting/pbr-material/pbr-material-glsl.d.ts +2 -2
  31. package/dist/modules/lighting/pbr-material/pbr-material-glsl.d.ts.map +1 -1
  32. package/dist/modules/lighting/pbr-material/pbr-material-glsl.js +33 -11
  33. package/dist/modules/lighting/pbr-material/pbr-material-glsl.js.map +1 -1
  34. package/dist/modules/lighting/pbr-material/pbr-material-wgsl.cjs +33 -11
  35. package/dist/modules/lighting/pbr-material/pbr-material-wgsl.d.ts +1 -1
  36. package/dist/modules/lighting/pbr-material/pbr-material-wgsl.d.ts.map +1 -1
  37. package/dist/modules/lighting/pbr-material/pbr-material-wgsl.js +33 -11
  38. package/dist/modules/lighting/pbr-material/pbr-material-wgsl.js.map +1 -1
  39. package/dist/modules/lighting/pbr-material/pbr-material.d.ts +3 -3
  40. package/dist/modules/lighting/pbr-shadow/pbr-shadow.cjs +97 -0
  41. package/dist/modules/lighting/pbr-shadow/pbr-shadow.d.ts +15 -0
  42. package/dist/modules/lighting/pbr-shadow/pbr-shadow.d.ts.map +1 -0
  43. package/dist/modules/lighting/pbr-shadow/pbr-shadow.js +69 -0
  44. package/dist/modules/lighting/pbr-shadow/pbr-shadow.js.map +1 -0
  45. package/dist/modules/lighting/pbr-surface/pbr-surface.cjs +114 -0
  46. package/dist/modules/lighting/pbr-surface/pbr-surface.d.ts +15 -0
  47. package/dist/modules/lighting/pbr-surface/pbr-surface.d.ts.map +1 -0
  48. package/dist/modules/lighting/pbr-surface/pbr-surface.js +88 -0
  49. package/dist/modules/lighting/pbr-surface/pbr-surface.js.map +1 -0
  50. package/dist/modules/lighting/prefiltered-environment/prefiltered-environment.cjs +379 -0
  51. package/dist/modules/lighting/prefiltered-environment/prefiltered-environment.d.ts +41 -0
  52. package/dist/modules/lighting/prefiltered-environment/prefiltered-environment.d.ts.map +1 -0
  53. package/dist/modules/lighting/prefiltered-environment/prefiltered-environment.js +345 -0
  54. package/dist/modules/lighting/prefiltered-environment/prefiltered-environment.js.map +1 -0
  55. package/dist/modules/lighting/shadow/shadow-glsl.cjs +228 -0
  56. package/dist/modules/lighting/shadow/shadow-glsl.d.ts +3 -0
  57. package/dist/modules/lighting/shadow/shadow-glsl.d.ts.map +1 -0
  58. package/dist/modules/lighting/shadow/shadow-glsl.js +204 -0
  59. package/dist/modules/lighting/shadow/shadow-glsl.js.map +1 -0
  60. package/dist/modules/lighting/shadow/shadow-types.cjs +15 -0
  61. package/dist/modules/lighting/shadow/shadow-types.d.ts +63 -0
  62. package/dist/modules/lighting/shadow/shadow-types.d.ts.map +1 -0
  63. package/dist/modules/lighting/shadow/shadow-types.js +5 -0
  64. package/dist/modules/lighting/shadow/shadow-types.js.map +1 -0
  65. package/dist/modules/lighting/shadow/shadow.cjs +458 -0
  66. package/dist/modules/lighting/shadow/shadow.d.ts +117 -0
  67. package/dist/modules/lighting/shadow/shadow.d.ts.map +1 -0
  68. package/dist/modules/lighting/shadow/shadow.js +450 -0
  69. package/dist/modules/lighting/shadow/shadow.js.map +1 -0
  70. package/dist/modules/lighting/unlit-material/unlit-material.cjs +60 -0
  71. package/dist/modules/lighting/unlit-material/unlit-material.d.ts +9 -0
  72. package/dist/modules/lighting/unlit-material/unlit-material.d.ts.map +1 -0
  73. package/dist/modules/lighting/unlit-material/unlit-material.js +40 -0
  74. package/dist/modules/lighting/unlit-material/unlit-material.js.map +1 -0
  75. package/package.json +3 -3
  76. package/src/index.ts +48 -0
  77. package/src/modules/lighting/clustered-light-grid/clustered-light-grid.ts +17 -0
  78. package/src/modules/lighting/contact-shadow/contact-shadow.ts +365 -0
  79. package/src/modules/lighting/pbr-ambient-occlusion/pbr-ambient-occlusion.ts +78 -0
  80. package/src/modules/lighting/pbr-contact-shadow/pbr-contact-shadow.ts +78 -0
  81. package/src/modules/lighting/pbr-material/pbr-material-glsl.ts +33 -11
  82. package/src/modules/lighting/pbr-material/pbr-material-wgsl.ts +33 -11
  83. package/src/modules/lighting/pbr-shadow/pbr-shadow.ts +85 -0
  84. package/src/modules/lighting/pbr-surface/pbr-surface.ts +103 -0
  85. package/src/modules/lighting/prefiltered-environment/prefiltered-environment.ts +370 -0
  86. package/src/modules/lighting/shadow/shadow-glsl.ts +204 -0
  87. package/src/modules/lighting/shadow/shadow-types.ts +68 -0
  88. package/src/modules/lighting/shadow/shadow.ts +536 -0
  89. package/src/modules/lighting/unlit-material/unlit-material.ts +44 -0
@@ -1268,7 +1268,9 @@ fn calculateFinalColor(pbrInfo: PBRInfo, lightColor: vec3<f32>) -> vec3<f32> {
1268
1268
  }
1269
1269
 
1270
1270
  // Visibility for the first four spotlights; environment and other lights are unaffected.
1271
- fn pbr_filterColorWithSpotShadows(vertexColor: vec4<f32>, spotVisibility: vec4f) -> vec4<f32> {
1271
+ var<private> pbr_ambientVisibility: f32 = 1.0;
1272
+
1273
+ fn pbr_filterColorWithShadows(vertexColor: vec4<f32>, directionalVisibility: f32, pointVisibility: vec4f, spotVisibility: vec4f) -> vec4<f32> {
1272
1274
  let baseColorUV = getMaterialUV(pbrMaterial.baseColorUVSet, pbrMaterial.baseColorUVTransform);
1273
1275
  let metallicRoughnessUV = getMaterialUV(
1274
1276
  pbrMaterial.metallicRoughnessUVSet,
@@ -1361,7 +1363,7 @@ fn pbr_filterColorWithSpotShadows(vertexColor: vec4<f32>, spotVisibility: vec4f)
1361
1363
  color = baseColor.rgb;
1362
1364
  #ifdef HAS_LIGHTMAP
1363
1365
  if (pbrMaterial.lightmapMapEnabled != 0u) {
1364
- color *= lightmapIrradiance / M_PI;
1366
+ color *= lightmapIrradiance / M_PI * pbr_ambientVisibility;
1365
1367
  #ifdef HAS_OCCLUSIONMAP
1366
1368
  if (pbrMaterial.occlusionMapEnabled != 0) {
1367
1369
  let ao = textureSample(pbr_occlusionSampler, pbr_occlusionSamplerSampler, occlusionUV).r;
@@ -1464,12 +1466,12 @@ fn pbr_filterColorWithSpotShadows(vertexColor: vec4<f32>, spotVisibility: vec4f)
1464
1466
 
1465
1467
  #ifdef USE_LIGHTS
1466
1468
  PBRInfo_setAmbientLight(&pbrInfo);
1467
- color += calculateFinalColor(pbrInfo, lighting.ambientColor);
1469
+ color += calculateFinalColor(pbrInfo, lighting.ambientColor * pbr_ambientVisibility);
1468
1470
 
1469
1471
  for (var i = 0; i < lighting.directionalLightCount; i++) {
1470
1472
  if (i < lighting.directionalLightCount) {
1471
1473
  PBRInfo_setDirectionalLight(&pbrInfo, -lighting_getDirectionalLight(i).direction);
1472
- color += calculateFinalColor(pbrInfo, lighting_getDirectionalLight(i).color);
1474
+ color += calculateFinalColor(pbrInfo, lighting_getDirectionalLight(i).color * select(1.0, clamp(directionalVisibility, 0.0, 1.0), i == 0));
1473
1475
  }
1474
1476
  }
1475
1477
 
@@ -1480,7 +1482,7 @@ fn pbr_filterColorWithSpotShadows(vertexColor: vec4<f32>, spotVisibility: vec4f)
1480
1482
  lighting_getPointLight(i),
1481
1483
  distance(lighting_getPointLight(i).position, fragmentInputs.pbr_vPosition)
1482
1484
  );
1483
- color += calculateFinalColor(pbrInfo, lighting_getPointLight(i).color / attenuation);
1485
+ color += calculateFinalColor(pbrInfo, lighting_getPointLight(i).color * select(1.0, clamp(pointVisibility[min(i, 3)], 0.0, 1.0), i < 4) / attenuation);
1484
1486
  }
1485
1487
  }
1486
1488
 
@@ -1509,7 +1511,8 @@ fn pbr_filterColorWithSpotShadows(vertexColor: vec4<f32>, spotVisibility: vec4f)
1509
1511
  }
1510
1512
  #endif
1511
1513
 
1512
- #ifdef USE_IBL
1514
+ let indirectBaseColor = color;
1515
+ #ifdef USE_IBL
1513
1516
  if (pbrMaterial.IBLenabled != 0) {
1514
1517
  color += getIBLContribution(pbrInfo, n, reflection);
1515
1518
  }
@@ -1517,6 +1520,7 @@ fn pbr_filterColorWithSpotShadows(vertexColor: vec4<f32>, spotVisibility: vec4f)
1517
1520
 
1518
1521
  // The pinned KHR_lightmap draft explicitly defines c_diff = (1 - metallic) * baseColor.
1519
1522
  color += baseColor.rgb * (1.0 - metallic) * lightmapIrradiance / M_PI;
1523
+ color = indirectBaseColor + (color - indirectBaseColor) * pbr_ambientVisibility;
1520
1524
 
1521
1525
  #ifdef HAS_OCCLUSIONMAP
1522
1526
  if (pbrMaterial.occlusionMapEnabled != 0) {
@@ -1815,7 +1819,7 @@ fn pbr_filterColorWithSpotShadows(vertexColor: vec4<f32>, spotVisibility: vec4f)
1815
1819
  PBRInfo_setAmbientLight(&pbrInfo);
1816
1820
  color += calculateMaterialLightColor(
1817
1821
  pbrInfo,
1818
- lighting.ambientColor,
1822
+ lighting.ambientColor * pbr_ambientVisibility,
1819
1823
  clearcoatNormal,
1820
1824
  clearcoatFactor,
1821
1825
  clearcoatRoughness,
@@ -1831,7 +1835,7 @@ fn pbr_filterColorWithSpotShadows(vertexColor: vec4<f32>, spotVisibility: vec4f)
1831
1835
  PBRInfo_setDirectionalLight(&pbrInfo, -lighting_getDirectionalLight(i).direction);
1832
1836
  color += calculateMaterialLightColor(
1833
1837
  pbrInfo,
1834
- lighting_getDirectionalLight(i).color,
1838
+ lighting_getDirectionalLight(i).color * select(1.0, clamp(directionalVisibility, 0.0, 1.0), i == 0),
1835
1839
  clearcoatNormal,
1836
1840
  clearcoatFactor,
1837
1841
  clearcoatRoughness,
@@ -1842,7 +1846,7 @@ fn pbr_filterColorWithSpotShadows(vertexColor: vec4<f32>, spotVisibility: vec4f)
1842
1846
  );
1843
1847
  color += calculateDiffuseTransmissionLight(
1844
1848
  pbrInfo,
1845
- lighting_getDirectionalLight(i).color,
1849
+ lighting_getDirectionalLight(i).color * select(1.0, clamp(directionalVisibility, 0.0, 1.0), i == 0),
1846
1850
  diffuseTransmissionColor,
1847
1851
  diffuseTransmission,
1848
1852
  multiscatterColor,
@@ -1861,7 +1865,7 @@ fn pbr_filterColorWithSpotShadows(vertexColor: vec4<f32>, spotVisibility: vec4f)
1861
1865
  );
1862
1866
  color += calculateMaterialLightColor(
1863
1867
  pbrInfo,
1864
- lighting_getPointLight(i).color / attenuation,
1868
+ lighting_getPointLight(i).color * select(1.0, clamp(pointVisibility[min(i, 3)], 0.0, 1.0), i < 4) / attenuation,
1865
1869
  clearcoatNormal,
1866
1870
  clearcoatFactor,
1867
1871
  clearcoatRoughness,
@@ -1872,7 +1876,7 @@ fn pbr_filterColorWithSpotShadows(vertexColor: vec4<f32>, spotVisibility: vec4f)
1872
1876
  );
1873
1877
  color += calculateDiffuseTransmissionLight(
1874
1878
  pbrInfo,
1875
- lighting_getPointLight(i).color / attenuation,
1879
+ lighting_getPointLight(i).color * select(1.0, clamp(pointVisibility[min(i, 3)], 0.0, 1.0), i < 4) / attenuation,
1876
1880
  diffuseTransmissionColor,
1877
1881
  diffuseTransmission,
1878
1882
  multiscatterColor,
@@ -1941,6 +1945,7 @@ fn pbr_filterColorWithSpotShadows(vertexColor: vec4<f32>, spotVisibility: vec4f)
1941
1945
  }
1942
1946
  #endif
1943
1947
 
1948
+ let indirectBaseColor = color;
1944
1949
  // Calculate lighting contribution from image based lighting source (IBL)
1945
1950
  #ifdef USE_IBL
1946
1951
  if (pbrMaterial.IBLenabled != 0) {
@@ -1975,6 +1980,8 @@ fn pbr_filterColorWithSpotShadows(vertexColor: vec4<f32>, spotVisibility: vec4f)
1975
1980
  (1.0 - diffuseTransmission) * baseLayerEnergy * lightmapIrradiance / M_PI;
1976
1981
  #endif
1977
1982
 
1983
+ color = indirectBaseColor + (color - indirectBaseColor) * pbr_ambientVisibility;
1984
+
1978
1985
  // Apply optional PBR terms for additional (optional) shading
1979
1986
  #ifdef HAS_OCCLUSIONMAP
1980
1987
  if (pbrMaterial.occlusionMapEnabled != 0) {
@@ -2037,6 +2044,21 @@ fn pbr_filterColorWithSpotShadows(vertexColor: vec4<f32>, spotVisibility: vec4f)
2037
2044
  return vec4<f32>(applySceneColorManagement(color), alpha);
2038
2045
  }
2039
2046
 
2047
+ // Depth-only passes use the same base-color alpha, vertex color and UV transform.
2048
+ fn pbr_discardMaskedFragment(vertexColor: vec4f) {
2049
+ #ifdef ALPHA_CUTOFF
2050
+ var alpha = pbrMaterial.baseColorFactor.a * vertexColor.a;
2051
+ #ifdef HAS_BASECOLORMAP
2052
+ alpha *= textureSample(pbr_baseColorSampler, pbr_baseColorSamplerSampler, getMaterialUV(pbrMaterial.baseColorUVSet, pbrMaterial.baseColorUVTransform)).a;
2053
+ #endif
2054
+ if (alpha < pbrMaterial.alphaCutoff) {discard;}
2055
+ #endif
2056
+ }
2057
+
2058
+ fn pbr_filterColorWithSpotShadows(vertexColor: vec4f, spotVisibility: vec4f) -> vec4f {
2059
+ return pbr_filterColorWithShadows(vertexColor, 1.0, vec4f(1.0), spotVisibility);
2060
+ }
2061
+
2040
2062
  fn pbr_filterColor(vertexColor: vec4<f32>) -> vec4<f32> {
2041
2063
  return pbr_filterColorWithSpotShadows(vertexColor, vec4f(1.0));
2042
2064
  }
@@ -0,0 +1,85 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
3
+
4
+ import type {NumberArray16} from '@math.gl/core';
5
+ import type {ShaderModule} from '../../../lib/shader-module/shader-module';
6
+ import {pbrMaterial} from '../pbr-material/pbr-material';
7
+ import {shadow} from '../shadow/shadow';
8
+
9
+ export type PBRShadowProps = {
10
+ /** Receiver camera view matrix, used for directional cascade selection. */
11
+ viewMatrix?: Readonly<NumberArray16>;
12
+ /** Toggle receiving while retaining allocated maps and bindings. Defaults to true. */
13
+ enabled?: boolean;
14
+ };
15
+
16
+ export type PBRShadowUniforms = {
17
+ viewMatrix: Readonly<NumberArray16>;
18
+ enabled: number;
19
+ };
20
+
21
+ /** Applies shadow visibility to matching direct-light slots in the canonical PBR shader. */
22
+ export const pbrShadow: ShaderModule<PBRShadowProps, PBRShadowUniforms> = {
23
+ name: 'pbrShadow',
24
+ dependencies: [pbrMaterial, shadow],
25
+ bindingLayout: [{name: 'pbrShadow', group: 0}],
26
+ uniformTypes: {viewMatrix: 'mat4x4<f32>', enabled: 'i32'},
27
+ defaultUniforms: {
28
+ viewMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
29
+ enabled: 1
30
+ },
31
+ getUniforms(properties = {}) {
32
+ return {
33
+ ...(properties.viewMatrix ? {viewMatrix: properties.viewMatrix} : {}),
34
+ ...(properties.enabled === undefined ? {} : {enabled: Number(properties.enabled)})
35
+ };
36
+ },
37
+ source: /* wgsl */ `
38
+ struct PBRShadowUniforms {
39
+ viewMatrix: mat4x4f,
40
+ enabled: i32,
41
+ };
42
+ @group(0) @binding(auto) var<uniform> pbrShadow: PBRShadowUniforms;
43
+ fn pbrShadow_filterColorWithDirectionalContact(vertexColor: vec4f, worldPosition: vec3f, worldNormal: vec3f, contactVisibility: f32) -> vec4f {
44
+ var directionalVisibility = 1.0;
45
+ var pointVisibility = vec4f(1.0);
46
+ var spotVisibility = vec4f(1.0);
47
+ if (pbrShadow.enabled != 0) {
48
+ let normal = normalize(worldNormal);
49
+ let viewDepth = -(pbrShadow.viewMatrix * vec4f(worldPosition, 1.0)).z;
50
+ directionalVisibility = shadow_getDirectionalFactor(worldPosition, normal, viewDepth);
51
+ for (var index = 0; index < 4; index++) {
52
+ pointVisibility[index] = shadow_getPointFactor(index, worldPosition, normal);
53
+ spotVisibility[index] = shadow_getSpotFactor(index, worldPosition, normal);
54
+ }
55
+ }
56
+ return pbr_filterColorWithShadows(vertexColor, min(directionalVisibility, contactVisibility), pointVisibility, spotVisibility);
57
+ }
58
+ fn pbrShadow_filterColor(vertexColor: vec4f, worldPosition: vec3f, worldNormal: vec3f) -> vec4f {
59
+ return pbrShadow_filterColorWithDirectionalContact(vertexColor, worldPosition, worldNormal, 1.0);
60
+ }`,
61
+
62
+ fs: /* glsl */ `
63
+ layout(std140) uniform pbrShadowUniforms {
64
+ mat4 viewMatrix;
65
+ int enabled;
66
+ } pbrShadow;
67
+ vec4 pbrShadow_filterColorWithDirectionalContact(vec4 vertexColor, vec3 worldPosition, vec3 worldNormal, float contactVisibility) {
68
+ float directionalVisibility = 1.0;
69
+ vec4 pointVisibility = vec4(1.0);
70
+ vec4 spotVisibility = vec4(1.0);
71
+ if (pbrShadow.enabled != 0) {
72
+ vec3 normal = normalize(worldNormal);
73
+ float viewDepth = -(pbrShadow.viewMatrix * vec4(worldPosition, 1.0)).z;
74
+ directionalVisibility = shadow_getDirectionalFactor(worldPosition, normal, viewDepth);
75
+ for (int index = 0; index < 4; index++) {
76
+ pointVisibility[index] = shadow_getPointFactor(index, worldPosition, normal);
77
+ spotVisibility[index] = shadow_getSpotFactor(index, worldPosition, normal);
78
+ }
79
+ }
80
+ return pbr_filterColorWithShadows(vertexColor, min(directionalVisibility, contactVisibility), pointVisibility, spotVisibility);
81
+ }
82
+ vec4 pbrShadow_filterColor(vec4 vertexColor, vec3 worldPosition, vec3 worldNormal) {
83
+ return pbrShadow_filterColorWithDirectionalContact(vertexColor, worldPosition, worldNormal, 1.0);
84
+ }`
85
+ };
@@ -0,0 +1,103 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
3
+
4
+ import type {NumberArray16} from '@math.gl/core';
5
+ import type {ShaderModule} from '../../../lib/shader-module/shader-module';
6
+ import {pbrMaterial} from '../pbr-material/pbr-material';
7
+
8
+ export type PBRSurfaceProps = {
9
+ /** Rigid world-to-view camera transform. Defaults to identity. */
10
+ viewMatrix?: Readonly<NumberArray16>;
11
+ /** Include canonical normal/bump maps instead of geometric normals. Defaults to false. */
12
+ useMaterialNormals?: boolean;
13
+ };
14
+
15
+ export type PBRSurfaceUniforms = {
16
+ viewMatrix: Readonly<NumberArray16>;
17
+ useMaterialNormals: number;
18
+ };
19
+
20
+ /** Encodes view-space PBR normals in RGB; alpha one marks covered surface pixels. */
21
+ export const pbrSurface: ShaderModule<PBRSurfaceProps, PBRSurfaceUniforms> = {
22
+ name: 'pbrSurface',
23
+ dependencies: [pbrMaterial],
24
+ bindingLayout: [{name: 'pbrSurface', group: 0}],
25
+ uniformTypes: {viewMatrix: 'mat4x4<f32>', useMaterialNormals: 'i32'},
26
+ defaultUniforms: {
27
+ viewMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
28
+ useMaterialNormals: 0
29
+ },
30
+ getUniforms(properties = {}) {
31
+ return {
32
+ ...(properties.viewMatrix ? {viewMatrix: properties.viewMatrix} : {}),
33
+ ...(properties.useMaterialNormals === undefined
34
+ ? {}
35
+ : {useMaterialNormals: Number(properties.useMaterialNormals)})
36
+ };
37
+ },
38
+ source: /* wgsl */ `
39
+ struct PBRSurfaceUniforms {
40
+ viewMatrix: mat4x4f,
41
+ useMaterialNormals: i32,
42
+ };
43
+ @group(0) @binding(auto) var<uniform> pbrSurface: PBRSurfaceUniforms;
44
+ fn pbrSurface_getColor() -> vec4f {
45
+ #ifdef HAS_NORMALS
46
+ var normal = normalize(fragmentInputs.pbr_vNormal);
47
+ #ifdef DOUBLE_SIDED
48
+ if (!pbr_frontFacing) {normal = -normal;}
49
+ #endif
50
+ #else
51
+ var normal = normalize(cross(dpdy(fragmentInputs.pbr_vPosition), dpdx(fragmentInputs.pbr_vPosition)));
52
+ #endif
53
+ #ifdef HAS_NORMALMAP
54
+ if (pbrSurface.useMaterialNormals != 0) {
55
+ let coordinates = getMaterialUV(pbrMaterial.normalUVSet, pbrMaterial.normalUVTransform);
56
+ normal = getNormal(getTBN(coordinates), coordinates);
57
+ }
58
+ #else
59
+ #ifdef HAS_BUMPMAP
60
+ if (pbrSurface.useMaterialNormals != 0) {
61
+ let coordinates = getMaterialUV(pbrMaterial.normalUVSet, pbrMaterial.normalUVTransform);
62
+ normal = getNormal(getTBN(coordinates), coordinates);
63
+ }
64
+ #endif
65
+ #endif
66
+ let viewNormal = normalize((pbrSurface.viewMatrix * vec4f(normal, 0.0)).xyz);
67
+ return vec4f(viewNormal * 0.5 + vec3f(0.5), 1.0);
68
+ }`,
69
+ fs: /* glsl */ `
70
+ layout(std140) uniform pbrSurfaceUniforms {
71
+ mat4 viewMatrix;
72
+ int useMaterialNormals;
73
+ } pbrSurface;
74
+ vec4 pbrSurface_getColor() {
75
+ #ifdef HAS_NORMALS
76
+ #ifdef HAS_TANGENTS
77
+ vec3 normal = normalize(pbr_vTBN[2]);
78
+ #else
79
+ vec3 normal = normalize(pbr_vNormal);
80
+ #endif
81
+ #ifdef DOUBLE_SIDED
82
+ if (!gl_FrontFacing) normal = -normal;
83
+ #endif
84
+ #else
85
+ vec3 normal = normalize(cross(dFdx(pbr_vPosition), dFdy(pbr_vPosition)));
86
+ #endif
87
+ #ifdef HAS_NORMALMAP
88
+ if (pbrSurface.useMaterialNormals != 0) {
89
+ vec2 coordinates = getMaterialUV(pbrMaterial.normalUVSet, pbrMaterial.normalUVTransform);
90
+ normal = getNormal(getTBN(coordinates), coordinates);
91
+ }
92
+ #else
93
+ #ifdef HAS_BUMPMAP
94
+ if (pbrSurface.useMaterialNormals != 0) {
95
+ vec2 coordinates = getMaterialUV(pbrMaterial.normalUVSet, pbrMaterial.normalUVTransform);
96
+ normal = getNormal(getTBN(coordinates), coordinates);
97
+ }
98
+ #endif
99
+ #endif
100
+ vec3 viewNormal = normalize((pbrSurface.viewMatrix * vec4(normal, 0.0)).xyz);
101
+ return vec4(viewNormal * 0.5 + 0.5, 1.0);
102
+ }`
103
+ };