@luma.gl/shadertools 9.1.0-alpha.10 → 9.1.0-alpha.13

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 (51) hide show
  1. package/dist/dist.dev.js +195 -10
  2. package/dist/dist.min.js +241 -80
  3. package/dist/index.cjs +196 -10
  4. package/dist/index.cjs.map +3 -3
  5. package/dist/lib/shader-assembly/assemble-shaders.d.ts.map +1 -1
  6. package/dist/lib/shader-assembly/assemble-shaders.js +9 -8
  7. package/dist/lib/shader-module/shader-module-dependencies.d.ts.map +1 -1
  8. package/dist/lib/shader-module/shader-module-dependencies.js +1 -1
  9. package/dist/lib/shader-module/shader-module.d.ts +2 -2
  10. package/dist/lib/shader-module/shader-module.d.ts.map +1 -1
  11. package/dist/modules/engine/picking/picking.d.ts +5 -5
  12. package/dist/modules/engine/picking/picking.d.ts.map +1 -1
  13. package/dist/modules/lighting/gouraud-material/gouraud-material.d.ts +1 -0
  14. package/dist/modules/lighting/gouraud-material/gouraud-material.d.ts.map +1 -1
  15. package/dist/modules/lighting/lights/lighting-uniforms-glsl.d.ts +1 -1
  16. package/dist/modules/lighting/lights/lighting-uniforms-glsl.d.ts.map +1 -1
  17. package/dist/modules/lighting/lights/lighting-uniforms-glsl.js +1 -1
  18. package/dist/modules/lighting/lights/lighting-uniforms-wgsl.d.ts +2 -0
  19. package/dist/modules/lighting/lights/lighting-uniforms-wgsl.d.ts.map +1 -0
  20. package/dist/modules/lighting/lights/lighting-uniforms-wgsl.js +56 -0
  21. package/dist/modules/lighting/lights/lighting.d.ts +1 -0
  22. package/dist/modules/lighting/lights/lighting.d.ts.map +1 -1
  23. package/dist/modules/lighting/lights/lighting.js +5 -3
  24. package/dist/modules/lighting/no-material/dirlight.d.ts +4 -4
  25. package/dist/modules/lighting/no-material/dirlight.d.ts.map +1 -1
  26. package/dist/modules/lighting/no-material/dirlight.js +22 -11
  27. package/dist/modules/lighting/pbr-material/pbr-material.d.ts +1 -0
  28. package/dist/modules/lighting/pbr-material/pbr-material.d.ts.map +1 -1
  29. package/dist/modules/lighting/phong-material/phong-material.d.ts +2 -0
  30. package/dist/modules/lighting/phong-material/phong-material.d.ts.map +1 -1
  31. package/dist/modules/lighting/phong-material/phong-material.js +2 -0
  32. package/dist/modules/lighting/phong-material/phong-shaders-wgsl.d.ts +41 -0
  33. package/dist/modules/lighting/phong-material/phong-shaders-wgsl.d.ts.map +1 -0
  34. package/dist/modules/lighting/phong-material/phong-shaders-wgsl.js +130 -0
  35. package/dist/modules/math/fp64/fp64-utils.d.ts +3 -3
  36. package/dist/modules/math/fp64/fp64-utils.d.ts.map +1 -1
  37. package/dist/modules-webgl1/lighting/dirlight/dirlight.d.ts +2 -2
  38. package/dist/modules-webgl1/lighting/dirlight/dirlight.d.ts.map +1 -1
  39. package/package.json +5 -5
  40. package/src/lib/shader-assembly/assemble-shaders.ts +9 -8
  41. package/src/lib/shader-module/shader-module-dependencies.ts +1 -2
  42. package/src/lib/shader-module/shader-module.ts +10 -6
  43. package/src/modules/engine/picking/picking.ts +5 -5
  44. package/src/modules/lighting/lights/lighting-uniforms-glsl.ts +1 -1
  45. package/src/modules/lighting/lights/lighting-uniforms-wgsl.ts +57 -0
  46. package/src/modules/lighting/lights/lighting.ts +5 -3
  47. package/src/modules/lighting/no-material/dirlight.ts +24 -14
  48. package/src/modules/lighting/phong-material/phong-material.ts +2 -0
  49. package/src/modules/lighting/phong-material/phong-shaders-wgsl.ts +132 -0
  50. package/src/modules/math/fp64/fp64-utils.ts +3 -3
  51. package/src/modules-webgl1/lighting/dirlight/dirlight.ts +2 -2
package/dist/index.cjs CHANGED
@@ -620,6 +620,7 @@ function assembleGLSLShaderPair(options) {
620
620
  };
621
621
  }
622
622
  function assembleShaderWGSL(platformInfo, options) {
623
+ var _a;
623
624
  const {
624
625
  // id,
625
626
  source,
@@ -656,14 +657,14 @@ function assembleShaderWGSL(platformInfo, options) {
656
657
  mainInjections[key] = [injection];
657
658
  }
658
659
  }
659
- const modulesToInject = platformInfo.type !== "webgpu" ? modules : [];
660
+ const modulesToInject = modules;
660
661
  for (const module2 of modulesToInject) {
661
662
  if (log2) {
662
663
  checkShaderModuleDeprecations(module2, coreSource, log2);
663
664
  }
664
- const moduleSource = getShaderModuleSource(module2, stage);
665
+ const moduleSource = getShaderModuleSource(module2, "wgsl");
665
666
  assembledSource += moduleSource;
666
- const injections = module2.injections[stage];
667
+ const injections = ((_a = module2.injections) == null ? void 0 : _a[stage]) || {};
667
668
  for (const key in injections) {
668
669
  const match = /^(v|f)s:#([\w-]+)$/.exec(key);
669
670
  if (match) {
@@ -821,12 +822,16 @@ function getShaderModuleSource(module2, stage) {
821
822
  throw new Error("Shader module must have a name");
822
823
  }
823
824
  const moduleName = module2.name.toUpperCase().replace(/[^0-9a-z]/gi, "_");
824
- return `// ----- MODULE ${module2.name} ---------------
825
-
826
- #define MODULE_${moduleName}
827
- ${moduleSource}
825
+ let source = `// ----- MODULE ${module2.name} ---------------
828
826
 
829
827
  `;
828
+ if (stage !== "wgsl") {
829
+ source += `#define MODULE_${moduleName}
830
+ `;
831
+ }
832
+ source += `${moduleSource}
833
+ `;
834
+ return source;
830
835
  }
831
836
 
832
837
  // dist/lib/preprocessor/preprocessor.js
@@ -1690,7 +1695,7 @@ function getUniforms(opts = {}, prevUniforms) {
1690
1695
  }
1691
1696
 
1692
1697
  // dist/modules/lighting/lights/lighting-uniforms-glsl.js
1693
- var lightingUniforms = (
1698
+ var lightingUniformsGLSL = (
1694
1699
  /* glsl */
1695
1700
  `precision highp int;
1696
1701
 
@@ -1746,6 +1751,63 @@ float getPointLightAttenuation(PointLight pointLight, float distance) {
1746
1751
  `
1747
1752
  );
1748
1753
 
1754
+ // dist/modules/lighting/lights/lighting-uniforms-wgsl.js
1755
+ var lightingUniformsWGSL = (
1756
+ /* wgsl */
1757
+ `// #if (defined(SHADER_TYPE_FRAGMENT) && defined(LIGHTING_FRAGMENT)) || (defined(SHADER_TYPE_VERTEX) && defined(LIGHTING_VERTEX))
1758
+ struct AmbientLight {
1759
+ color: vec3<f32>,
1760
+ };
1761
+
1762
+ struct PointLight {
1763
+ color: vec3<f32>,
1764
+ position: vec3<f32>,
1765
+ attenuation: vec3<f32>, // 2nd order x:Constant-y:Linear-z:Exponential
1766
+ };
1767
+
1768
+ struct DirectionalLight {
1769
+ color: vec3<f32>,
1770
+ direction: vec3<f32>,
1771
+ };
1772
+
1773
+ struct lightingUniforms {
1774
+ enabled: i32,
1775
+ poightCount: i32,
1776
+ directionalLightCount: i32,
1777
+
1778
+ ambientColor: vec3<f32>,
1779
+
1780
+ // TODO - support multiple lights by uncommenting arrays below
1781
+ lightType: i32,
1782
+ lightColor: vec3<f32>,
1783
+ lightDirection: vec3<f32>,
1784
+ lightPosition: vec3<f32>,
1785
+ lightAttenuation: vec3<f32>,
1786
+
1787
+ // AmbientLight ambientLight;
1788
+ // PointLight pointLight[MAX_LIGHTS];
1789
+ // DirectionalLight directionalLight[MAX_LIGHTS];
1790
+ };
1791
+
1792
+ // Binding 0:1 is reserved for lighting (Note: could go into separate bind group as it is stable across draw calls)
1793
+ @binding(1) @group(0) var<uniform> lighting : lightingUniforms;
1794
+
1795
+ fn lighting_getPointLight(index: i32) -> PointLight {
1796
+ return PointLight(lighting.lightColor, lighting.lightPosition, lighting.lightAttenuation);
1797
+ }
1798
+
1799
+ fn lighting_getDirectionalLight(index: i32) -> DirectionalLight {
1800
+ return DirectionalLight(lighting.lightColor, lighting.lightDirection);
1801
+ }
1802
+
1803
+ fn getPointLightAttenuation(pointLight: PointLight, distance: f32) -> f32 {
1804
+ return pointLight.attenuation.x
1805
+ + pointLight.attenuation.y * distance
1806
+ + pointLight.attenuation.z * distance * distance;
1807
+ }
1808
+ `
1809
+ );
1810
+
1749
1811
  // dist/modules/lighting/lights/lighting.js
1750
1812
  var MAX_LIGHTS = 5;
1751
1813
  var COLOR_FACTOR = 255;
@@ -1789,8 +1851,9 @@ var lighting = {
1789
1851
  lightDirection: [1, 1, 1],
1790
1852
  lightAttenuation: [1, 1, 1]
1791
1853
  },
1792
- vs: lightingUniforms,
1793
- fs: lightingUniforms,
1854
+ source: lightingUniformsWGSL,
1855
+ vs: lightingUniformsGLSL,
1856
+ fs: lightingUniformsGLSL,
1794
1857
  getUniforms: getUniforms2
1795
1858
  };
1796
1859
  function getUniforms2(props, prevUniforms = {}) {
@@ -1868,6 +1931,36 @@ function convertColor(colorDef = {}) {
1868
1931
  }
1869
1932
 
1870
1933
  // dist/modules/lighting/no-material/dirlight.js
1934
+ var SOURCE_WGSL = (
1935
+ /* WGSL */
1936
+ `
1937
+ struct dirlightUniforms {
1938
+ lightDirection: vec3<f32>,
1939
+ };
1940
+
1941
+ alias DirlightNormal = vec3<f32>;
1942
+
1943
+ struct DirlightInputs {
1944
+ normal: DirlightNormal,
1945
+ };
1946
+
1947
+ @binding(1) @group(0) var<uniform> dirlight : dirlightUniforms;
1948
+
1949
+ // For vertex
1950
+ fn dirlight_setNormal(normal: vec3<f32>) -> DirlightNormal {
1951
+ return normalize(normal);
1952
+ }
1953
+
1954
+ // Returns color attenuated by angle from light source
1955
+ fn dirlight_filterColor(color: vec4<f32>, inputs: DirlightInputs) -> vec4<f32> {
1956
+ // TODO - fix default light direction
1957
+ // let lightDirection = dirlight.lightDirection;
1958
+ let lightDirection = vec3<f32>(1, 1, 1);
1959
+ let d: f32 = abs(dot(inputs.normal, normalize(lightDirection)));
1960
+ return vec4<f32>(color.rgb * d, color.a);
1961
+ }
1962
+ `
1963
+ );
1871
1964
  var VS_GLSL = (
1872
1965
  /* glsl */
1873
1966
  `out vec3 dirlight_vNormal;
@@ -1897,6 +1990,7 @@ var dirlight = {
1897
1990
  uniforms: {},
1898
1991
  name: "dirlight",
1899
1992
  dependencies: [],
1993
+ source: SOURCE_WGSL,
1900
1994
  vs: VS_GLSL,
1901
1995
  fs: FS_GLSL,
1902
1996
  // fragmentInputs: [
@@ -2047,6 +2141,97 @@ var gouraudMaterial = {
2047
2141
  }
2048
2142
  };
2049
2143
 
2144
+ // dist/modules/lighting/phong-material/phong-shaders-wgsl.js
2145
+ var PHONG_WGSL = (
2146
+ /* wgsl */
2147
+ `struct phongMaterialUniforms {
2148
+ ambient: f32,
2149
+ diffuse: f32,
2150
+ shininess: f32,
2151
+ specularColor: vec3<f32>,
2152
+ };
2153
+
2154
+ @binding(2) @group(0) var<uniform> material : phongMaterialUniforms;
2155
+
2156
+ fn lighting_getLightColor(surfaceColor: vec3<f32>, light_direction: vec3<f32>, view_direction: vec3<f32>, normal_worldspace: vec3<f32>, color: vec3<f32>) -> vec3<f32> {
2157
+ let halfway_direction: vec3<f32> = normalize(light_direction + view_direction);
2158
+ var lambertian: f32 = dot(light_direction, normal_worldspace);
2159
+ var specular: f32 = 0.0;
2160
+ if (lambertian > 0.0) {
2161
+ let specular_angle = max(dot(normal_worldspace, halfway_direction), 0.0);
2162
+ specular = pow(specular_angle, material.shininess);
2163
+ }
2164
+ lambertian = max(lambertian, 0.0);
2165
+ return (lambertian * material.diffuse * surfaceColor + specular * material.specularColor) * color;
2166
+ }
2167
+
2168
+ fn lighting_getLightColor2(surfaceColor: vec3<f32>, cameraPosition: vec3<f32>, position_worldspace: vec3<f32>, normal_worldspace: vec3<f32>) -> vec3<f32> {
2169
+ var lightColor: vec3<f32> = surfaceColor;
2170
+
2171
+ if (lighting.enabled == 0) {
2172
+ return lightColor;
2173
+ }
2174
+
2175
+ let view_direction: vec3<f32> = normalize(cameraPosition - position_worldspace);
2176
+ lightColor = material.ambient * surfaceColor * lighting.ambientColor;
2177
+
2178
+ if (lighting.lightType == 0) {
2179
+ let pointLight: PointLight = lighting_getPointLight(0);
2180
+ let light_position_worldspace: vec3<f32> = pointLight.position;
2181
+ let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
2182
+ lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
2183
+ } else if (lighting.lightType == 1) {
2184
+ var directionalLight: DirectionalLight = lighting_getDirectionalLight(0);
2185
+ lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
2186
+ }
2187
+
2188
+ return lightColor;
2189
+ /*
2190
+ for (int i = 0; i < MAX_LIGHTS; i++) {
2191
+ if (i >= lighting.pointLightCount) {
2192
+ break;
2193
+ }
2194
+ PointLight pointLight = lighting.pointLight[i];
2195
+ vec3 light_position_worldspace = pointLight.position;
2196
+ vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
2197
+ lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
2198
+ }
2199
+
2200
+ for (int i = 0; i < MAX_LIGHTS; i++) {
2201
+ if (i >= lighting.directionalLightCount) {
2202
+ break;
2203
+ }
2204
+ DirectionalLight directionalLight = lighting.directionalLight[i];
2205
+ lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
2206
+ }
2207
+ */
2208
+ }
2209
+
2210
+ fn lighting_getSpecularLightColor(cameraPosition: vec3<f32>, position_worldspace: vec3<f32>, normal_worldspace: vec3<f32>) -> vec3<f32>{
2211
+ var lightColor = vec3<f32>(0, 0, 0);
2212
+ let surfaceColor = vec3<f32>(0, 0, 0);
2213
+
2214
+ if (lighting.enabled == 0) {
2215
+ let view_direction = normalize(cameraPosition - position_worldspace);
2216
+
2217
+ switch (lighting.lightType) {
2218
+ case 0, default: {
2219
+ let pointLight: PointLight = lighting_getPointLight(0);
2220
+ let light_position_worldspace: vec3<f32> = pointLight.position;
2221
+ let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
2222
+ lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
2223
+ }
2224
+ case 1: {
2225
+ let directionalLight: DirectionalLight = lighting_getDirectionalLight(0);
2226
+ lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
2227
+ }
2228
+ }
2229
+ }
2230
+ return lightColor;
2231
+ }
2232
+ `
2233
+ );
2234
+
2050
2235
  // dist/modules/lighting/phong-material/phong-shaders-glsl.js
2051
2236
  var PHONG_VS = (
2052
2237
  /* glsl */
@@ -2154,6 +2339,7 @@ var phongMaterial = {
2154
2339
  name: "phong-lighting",
2155
2340
  dependencies: [lighting],
2156
2341
  // Note these are switched between phong and gouraud
2342
+ source: PHONG_WGSL,
2157
2343
  vs: PHONG_VS,
2158
2344
  fs: PHONG_FS,
2159
2345
  defines: {