@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
@@ -0,0 +1,130 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ export const PHONG_WGSL = /* wgsl */ `\
5
+ struct phongMaterialUniforms {
6
+ ambient: f32,
7
+ diffuse: f32,
8
+ shininess: f32,
9
+ specularColor: vec3<f32>,
10
+ };
11
+
12
+ @binding(2) @group(0) var<uniform> material : phongMaterialUniforms;
13
+
14
+ fn lighting_getLightColor(surfaceColor: vec3<f32>, light_direction: vec3<f32>, view_direction: vec3<f32>, normal_worldspace: vec3<f32>, color: vec3<f32>) -> vec3<f32> {
15
+ let halfway_direction: vec3<f32> = normalize(light_direction + view_direction);
16
+ var lambertian: f32 = dot(light_direction, normal_worldspace);
17
+ var specular: f32 = 0.0;
18
+ if (lambertian > 0.0) {
19
+ let specular_angle = max(dot(normal_worldspace, halfway_direction), 0.0);
20
+ specular = pow(specular_angle, material.shininess);
21
+ }
22
+ lambertian = max(lambertian, 0.0);
23
+ return (lambertian * material.diffuse * surfaceColor + specular * material.specularColor) * color;
24
+ }
25
+
26
+ fn lighting_getLightColor2(surfaceColor: vec3<f32>, cameraPosition: vec3<f32>, position_worldspace: vec3<f32>, normal_worldspace: vec3<f32>) -> vec3<f32> {
27
+ var lightColor: vec3<f32> = surfaceColor;
28
+
29
+ if (lighting.enabled == 0) {
30
+ return lightColor;
31
+ }
32
+
33
+ let view_direction: vec3<f32> = normalize(cameraPosition - position_worldspace);
34
+ lightColor = material.ambient * surfaceColor * lighting.ambientColor;
35
+
36
+ if (lighting.lightType == 0) {
37
+ let pointLight: PointLight = lighting_getPointLight(0);
38
+ let light_position_worldspace: vec3<f32> = pointLight.position;
39
+ let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
40
+ lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
41
+ } else if (lighting.lightType == 1) {
42
+ var directionalLight: DirectionalLight = lighting_getDirectionalLight(0);
43
+ lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
44
+ }
45
+
46
+ return lightColor;
47
+ /*
48
+ for (int i = 0; i < MAX_LIGHTS; i++) {
49
+ if (i >= lighting.pointLightCount) {
50
+ break;
51
+ }
52
+ PointLight pointLight = lighting.pointLight[i];
53
+ vec3 light_position_worldspace = pointLight.position;
54
+ vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
55
+ lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
56
+ }
57
+
58
+ for (int i = 0; i < MAX_LIGHTS; i++) {
59
+ if (i >= lighting.directionalLightCount) {
60
+ break;
61
+ }
62
+ DirectionalLight directionalLight = lighting.directionalLight[i];
63
+ lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
64
+ }
65
+ */
66
+ }
67
+
68
+ fn lighting_getSpecularLightColor(cameraPosition: vec3<f32>, position_worldspace: vec3<f32>, normal_worldspace: vec3<f32>) -> vec3<f32>{
69
+ var lightColor = vec3<f32>(0, 0, 0);
70
+ let surfaceColor = vec3<f32>(0, 0, 0);
71
+
72
+ if (lighting.enabled == 0) {
73
+ let view_direction = normalize(cameraPosition - position_worldspace);
74
+
75
+ switch (lighting.lightType) {
76
+ case 0, default: {
77
+ let pointLight: PointLight = lighting_getPointLight(0);
78
+ let light_position_worldspace: vec3<f32> = pointLight.position;
79
+ let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
80
+ lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
81
+ }
82
+ case 1: {
83
+ let directionalLight: DirectionalLight = lighting_getDirectionalLight(0);
84
+ lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
85
+ }
86
+ }
87
+ }
88
+ return lightColor;
89
+ }
90
+ `;
91
+ // TODO - handle multiple lights
92
+ /**
93
+ for (int i = 0; i < MAX_LIGHTS; i++) {
94
+ if (i >= lighting.pointLightCount) {
95
+ break;
96
+ }
97
+ PointLight pointLight = lighting_getPointLight(i);
98
+ vec3 light_position_worldspace = pointLight.position;
99
+ vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
100
+ lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
101
+ }
102
+
103
+ for (int i = 0; i < MAX_LIGHTS; i++) {
104
+ if (i >= lighting.directionalLightCount) {
105
+ break;
106
+ }
107
+ PointLight pointLight = lighting_getDirectionalLight(i);
108
+ lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
109
+ }
110
+ }
111
+ /**
112
+ for (int i = 0; i < MAX_LIGHTS; i++) {
113
+ if (i >= lighting.pointLightCount) {
114
+ break;
115
+ }
116
+ PointLight pointLight = lighting_getPointLight(i);
117
+ vec3 light_position_worldspace = pointLight.position;
118
+ vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
119
+ lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
120
+ }
121
+
122
+ for (int i = 0; i < MAX_LIGHTS; i++) {
123
+ if (i >= lighting.directionalLightCount) {
124
+ break;
125
+ }
126
+ PointLight pointLight = lighting_getDirectionalLight(i);
127
+ lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
128
+ }
129
+ }
130
+ */
@@ -1,4 +1,4 @@
1
- import type { NumberArray } from '@math.gl/types';
1
+ import type { NumericArray } from '@math.gl/types';
2
2
  /**
3
3
  * Calculate WebGL 64 bit float
4
4
  * @param a - the input float number
@@ -6,7 +6,7 @@ import type { NumberArray } from '@math.gl/types';
6
6
  * @param startIndex - the index in the output array to fill from. Default 0.
7
7
  * @returns - the fp64 representation of the input number
8
8
  */
9
- export declare function fp64ify(a: number, out?: NumberArray, startIndex?: number): NumberArray;
9
+ export declare function fp64ify(a: number, out?: NumericArray, startIndex?: number): NumericArray;
10
10
  /**
11
11
  * Calculate the low part of a WebGL 64 bit float
12
12
  * @param a the input float number
@@ -18,5 +18,5 @@ export declare function fp64LowPart(a: number): number;
18
18
  * @param matrix the input matrix
19
19
  * @returns the fp64 representation of the input matrix
20
20
  */
21
- export declare function fp64ifyMatrix4(matrix: NumberArray): Float32Array;
21
+ export declare function fp64ifyMatrix4(matrix: NumericArray): Float32Array;
22
22
  //# sourceMappingURL=fp64-utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fp64-utils.d.ts","sourceRoot":"","sources":["../../../../src/modules/math/fp64/fp64-utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,gBAAgB,CAAC;AAEhD;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,GAAE,WAAgB,EAAE,UAAU,GAAE,MAAU,GAAG,WAAW,CAM7F;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,WAAW,GAAG,YAAY,CAUhE"}
1
+ {"version":3,"file":"fp64-utils.d.ts","sourceRoot":"","sources":["../../../../src/modules/math/fp64/fp64-utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAEjD;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,GAAE,YAAiB,EAAE,UAAU,GAAE,MAAU,GAAG,YAAY,CAM/F;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,YAAY,GAAG,YAAY,CAUjE"}
@@ -1,7 +1,7 @@
1
1
  import { ShaderModule } from "../../../lib/shader-module/shader-module.js";
2
- import type { NumberArray } from "../../../types.js";
2
+ import type { NumericArray } from "../../../types.js";
3
3
  export type DirlightOptions = {
4
- lightDirection?: NumberArray;
4
+ lightDirection?: NumericArray;
5
5
  };
6
6
  /**
7
7
  * Cheap lighting - single directional light, single dot product, one uniform
@@ -1 +1 @@
1
- {"version":3,"file":"dirlight.d.ts","sourceRoot":"","sources":["../../../../src/modules-webgl1/lighting/dirlight/dirlight.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,YAAY,EAAC,oDAAiD;AACtE,OAAO,KAAK,EAAC,WAAW,EAAC,0BAAuB;AAKhD,MAAM,MAAM,eAAe,GAAG;IAC5B,cAAc,CAAC,EAAE,WAAW,CAAC;CAC9B,CAAC;AA4BF;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,YAMtB,CAAC"}
1
+ {"version":3,"file":"dirlight.d.ts","sourceRoot":"","sources":["../../../../src/modules-webgl1/lighting/dirlight/dirlight.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,YAAY,EAAC,oDAAiD;AACtE,OAAO,KAAK,EAAC,YAAY,EAAC,0BAAuB;AAKjD,MAAM,MAAM,eAAe,GAAG;IAC5B,cAAc,CAAC,EAAE,YAAY,CAAC;CAC/B,CAAC;AA4BF;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,YAMtB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luma.gl/shadertools",
3
- "version": "9.1.0-alpha.10",
3
+ "version": "9.1.0-alpha.13",
4
4
  "description": "Shader module system for luma.gl",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -46,12 +46,12 @@
46
46
  "prepublishOnly": "npm run build-minified-bundle && npm run build-dev-bundle"
47
47
  },
48
48
  "peerDependencies": {
49
- "@luma.gl/core": "^9.0.0-beta"
49
+ "@luma.gl/core": "9.1.0-alpha.10"
50
50
  },
51
51
  "dependencies": {
52
- "@math.gl/core": "^4.0.0",
53
- "@math.gl/types": "^4.0.0",
52
+ "@math.gl/core": "4.1.0-alpha.3",
53
+ "@math.gl/types": "4.1.0-alpha.3",
54
54
  "wgsl_reflect": "^1.0.1"
55
55
  },
56
- "gitHead": "f419cdc284e87b553df60af49d2888ac7dbbf288"
56
+ "gitHead": "c2c641d67a5aec97467de13b0e3d8f9307ba03c2"
57
57
  }
@@ -224,17 +224,17 @@ export function assembleShaderWGSL(platformInfo: PlatformInfo, options: Assemble
224
224
  }
225
225
 
226
226
  // TODO - hack until shadertool modules support WebGPU
227
- const modulesToInject = platformInfo.type !== 'webgpu' ? modules : [];
227
+ const modulesToInject = modules;
228
228
 
229
229
  for (const module of modulesToInject) {
230
230
  if (log) {
231
231
  checkShaderModuleDeprecations(module, coreSource, log);
232
232
  }
233
- const moduleSource = getShaderModuleSource(module, stage);
233
+ const moduleSource = getShaderModuleSource(module, 'wgsl');
234
234
  // Add the module source, and a #define that declares it presence
235
235
  assembledSource += moduleSource;
236
236
 
237
- const injections = module.injections[stage];
237
+ const injections = module.injections?.[stage] || {};
238
238
  for (const key in injections) {
239
239
  const match = /^(v|f)s:#([\w-]+)$/.exec(key);
240
240
  if (match) {
@@ -496,14 +496,15 @@ export function getShaderModuleSource(
496
496
  throw new Error('Shader module must have a name');
497
497
  }
498
498
  const moduleName = module.name.toUpperCase().replace(/[^0-9a-z]/gi, '_');
499
- return `\
499
+ let source = `\
500
500
  // ----- MODULE ${module.name} ---------------
501
501
 
502
- #define MODULE_${moduleName}
503
- ${moduleSource}\
504
-
505
-
506
502
  `;
503
+ if (stage !== 'wgsl') {
504
+ source += `#define MODULE_${moduleName}\n`;
505
+ }
506
+ source += `${moduleSource}\n`;
507
+ return source;
507
508
  }
508
509
 
509
510
  /*
@@ -2,8 +2,7 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
- import {ShaderModule} from './shader-module';
6
- import {initializeShaderModules} from '../shader-module/shader-module';
5
+ import {ShaderModule, initializeShaderModules} from './shader-module';
7
6
 
8
7
  // import type {ShaderModule} from '../shader-module/shader-module';
9
8
 
@@ -2,16 +2,20 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
- import type {NumberArray} from '@math.gl/types';
5
+ import type {NumericArray} from '@math.gl/types';
6
6
  import {Sampler, Texture} from '@luma.gl/core';
7
7
  import type {UniformFormat} from '../../types';
8
- import {PropType, PropValidator} from '../filters/prop-types';
9
- import {ShaderInjection} from '../shader-assembly/shader-injections';
10
- import {makePropValidators, getValidatedProperties} from '../filters/prop-types';
11
- import {normalizeInjections} from '../shader-assembly/shader-injections';
8
+ import {
9
+ PropType,
10
+ PropValidator,
11
+ makePropValidators,
12
+ getValidatedProperties
13
+ } from '../filters/prop-types';
14
+ import {ShaderInjection, normalizeInjections} from '../shader-assembly/shader-injections';
12
15
 
13
16
  export type BindingValue = Buffer | Texture | Sampler;
14
- export type UniformValue = number | boolean | Readonly<NumberArray>; // Float32Array> | Readonly<Int32Array> | Readonly<Uint32Array> | Readonly<number[]>;
17
+ export type UniformValue = number | boolean | Readonly<NumericArray>;
18
+ // Float32Array> | Readonly<Int32Array> | Readonly<Uint32Array> | Readonly<number[]>;
15
19
 
16
20
  export type UniformInfo = {
17
21
  format?: UniformFormat;
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
- import {NumberArray} from '../../../types';
5
+ import {NumericArray} from '../../../types';
6
6
  import {ShaderModule} from '../../../lib/shader-module/shader-module';
7
7
 
8
8
  // cyan color
@@ -19,9 +19,9 @@ export type PickingProps = {
19
19
  /** Set to true when picking an attribute value instead of object index */
20
20
  isAttribute?: boolean;
21
21
  /** Set to a picking color to visually highlight that item, or `null` to explicitly clear **/
22
- highlightedObjectColor?: NumberArray | null;
22
+ highlightedObjectColor?: NumericArray | null;
23
23
  /** Color of visual highlight of "selected" item */
24
- highlightColor?: NumberArray;
24
+ highlightColor?: NumericArray;
25
25
  /** Color range 0-1 or 0-255 */
26
26
  useFloatColors?: boolean;
27
27
  };
@@ -44,9 +44,9 @@ export type PickingUniforms = {
44
44
  /** Do we have a highlighted item? */
45
45
  isHighlightActive?: boolean;
46
46
  /** Set to a picking color to visually highlight that item */
47
- highlightedObjectColor?: NumberArray;
47
+ highlightedObjectColor?: NumericArray;
48
48
  /** Color of visual highlight of "selected" item */
49
- highlightColor?: NumberArray;
49
+ highlightColor?: NumericArray;
50
50
  };
51
51
 
52
52
  const vs = /* glsl */ `\
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
- export const lightingUniforms = /* glsl */ `\
5
+ export const lightingUniformsGLSL = /* glsl */ `\
6
6
  precision highp int;
7
7
 
8
8
  // #if (defined(SHADER_TYPE_FRAGMENT) && defined(LIGHTING_FRAGMENT)) || (defined(SHADER_TYPE_VERTEX) && defined(LIGHTING_VERTEX))
@@ -0,0 +1,57 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ export const lightingUniformsWGSL = /* wgsl */ `\
6
+ // #if (defined(SHADER_TYPE_FRAGMENT) && defined(LIGHTING_FRAGMENT)) || (defined(SHADER_TYPE_VERTEX) && defined(LIGHTING_VERTEX))
7
+ struct AmbientLight {
8
+ color: vec3<f32>,
9
+ };
10
+
11
+ struct PointLight {
12
+ color: vec3<f32>,
13
+ position: vec3<f32>,
14
+ attenuation: vec3<f32>, // 2nd order x:Constant-y:Linear-z:Exponential
15
+ };
16
+
17
+ struct DirectionalLight {
18
+ color: vec3<f32>,
19
+ direction: vec3<f32>,
20
+ };
21
+
22
+ struct lightingUniforms {
23
+ enabled: i32,
24
+ poightCount: i32,
25
+ directionalLightCount: i32,
26
+
27
+ ambientColor: vec3<f32>,
28
+
29
+ // TODO - support multiple lights by uncommenting arrays below
30
+ lightType: i32,
31
+ lightColor: vec3<f32>,
32
+ lightDirection: vec3<f32>,
33
+ lightPosition: vec3<f32>,
34
+ lightAttenuation: vec3<f32>,
35
+
36
+ // AmbientLight ambientLight;
37
+ // PointLight pointLight[MAX_LIGHTS];
38
+ // DirectionalLight directionalLight[MAX_LIGHTS];
39
+ };
40
+
41
+ // Binding 0:1 is reserved for lighting (Note: could go into separate bind group as it is stable across draw calls)
42
+ @binding(1) @group(0) var<uniform> lighting : lightingUniforms;
43
+
44
+ fn lighting_getPointLight(index: i32) -> PointLight {
45
+ return PointLight(lighting.lightColor, lighting.lightPosition, lighting.lightAttenuation);
46
+ }
47
+
48
+ fn lighting_getDirectionalLight(index: i32) -> DirectionalLight {
49
+ return DirectionalLight(lighting.lightColor, lighting.lightDirection);
50
+ }
51
+
52
+ fn getPointLightAttenuation(pointLight: PointLight, distance: f32) -> f32 {
53
+ return pointLight.attenuation.x
54
+ + pointLight.attenuation.y * distance
55
+ + pointLight.attenuation.z * distance * distance;
56
+ }
57
+ `;
@@ -4,7 +4,8 @@
4
4
 
5
5
  import type {NumberArray} from '@math.gl/types';
6
6
  import {ShaderModule} from '../../../lib/shader-module/shader-module';
7
- import {lightingUniforms} from './lighting-uniforms-glsl';
7
+ import {lightingUniformsGLSL} from './lighting-uniforms-glsl';
8
+ import {lightingUniformsWGSL} from './lighting-uniforms-wgsl';
8
9
 
9
10
  /** Max number of supported lights (in addition to ambient light */
10
11
  const MAX_LIGHTS = 5;
@@ -100,8 +101,9 @@ export const lighting = {
100
101
  lightDirection: [1, 1, 1],
101
102
  lightAttenuation: [1, 1, 1]
102
103
  },
103
- vs: lightingUniforms,
104
- fs: lightingUniforms,
104
+ source: lightingUniformsWGSL,
105
+ vs: lightingUniformsGLSL,
106
+ fs: lightingUniformsGLSL,
105
107
 
106
108
  getUniforms
107
109
  } as const satisfies ShaderModule<LightingProps, LightingUniforms>;
@@ -2,31 +2,40 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
- import type {NumberArray} from '@math.gl/types';
5
+ import type {Vector3Like, TypedArray} from '@math.gl/core';
6
6
  import {ShaderModule} from '../../../lib/shader-module/shader-module';
7
7
 
8
8
  export type DirlightProps = {
9
- lightDirection?: NumberArray | [number, number, number];
9
+ lightDirection?: Vector3Like | TypedArray;
10
10
  };
11
11
 
12
12
  export type DirlightUniforms = DirlightProps;
13
13
 
14
14
  // TODO
15
- export const VS_WGSL = /* WGSL */ `\
16
- void dirlight_setNormal(normal: vec3<f32>) {
17
- dirlight_vNormal = normalize(normal);
18
- }
19
- `;
15
+ export const SOURCE_WGSL = /* WGSL */ `\
16
+ struct dirlightUniforms {
17
+ lightDirection: vec3<f32>,
18
+ };
20
19
 
21
- // TODO
22
- export const FS_WGSL = /* WGSL */ `\
23
- uniform dirlightUniforms {
24
- vec3 lightDirection;
25
- } dirlight;
20
+ alias DirlightNormal = vec3<f32>;
21
+
22
+ struct DirlightInputs {
23
+ normal: DirlightNormal,
24
+ };
25
+
26
+ @binding(1) @group(0) var<uniform> dirlight : dirlightUniforms;
27
+
28
+ // For vertex
29
+ fn dirlight_setNormal(normal: vec3<f32>) -> DirlightNormal {
30
+ return normalize(normal);
31
+ }
26
32
 
27
33
  // Returns color attenuated by angle from light source
28
- fn dirlight_filterColor(color: vec4<f32>, dirlightInputs): vec4<f32> {
29
- const d: float = abs(dot(dirlight_vNormal, normalize(dirlight.lightDirection)));
34
+ fn dirlight_filterColor(color: vec4<f32>, inputs: DirlightInputs) -> vec4<f32> {
35
+ // TODO - fix default light direction
36
+ // let lightDirection = dirlight.lightDirection;
37
+ let lightDirection = vec3<f32>(1, 1, 1);
38
+ let d: f32 = abs(dot(inputs.normal, normalize(lightDirection)));
30
39
  return vec4<f32>(color.rgb * d, color.a);
31
40
  }
32
41
  `;
@@ -62,6 +71,7 @@ export const dirlight = {
62
71
 
63
72
  name: 'dirlight',
64
73
  dependencies: [],
74
+ source: SOURCE_WGSL,
65
75
  vs: VS_GLSL,
66
76
  fs: FS_GLSL,
67
77
 
@@ -4,6 +4,7 @@
4
4
 
5
5
  import {ShaderModule} from '../../../lib/shader-module/shader-module';
6
6
  import {lighting} from '../lights/lighting';
7
+ import {PHONG_WGSL} from './phong-shaders-wgsl';
7
8
  import {PHONG_VS, PHONG_FS} from './phong-shaders-glsl';
8
9
 
9
10
  export type PhongMaterialProps = PhongMaterialUniforms;
@@ -24,6 +25,7 @@ export const phongMaterial = {
24
25
  name: 'phong-lighting',
25
26
  dependencies: [lighting],
26
27
  // Note these are switched between phong and gouraud
28
+ source: PHONG_WGSL,
27
29
  vs: PHONG_VS,
28
30
  fs: PHONG_FS,
29
31
  defines: {
@@ -0,0 +1,132 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ export const PHONG_WGSL = /* wgsl */ `\
6
+ struct phongMaterialUniforms {
7
+ ambient: f32,
8
+ diffuse: f32,
9
+ shininess: f32,
10
+ specularColor: vec3<f32>,
11
+ };
12
+
13
+ @binding(2) @group(0) var<uniform> material : phongMaterialUniforms;
14
+
15
+ fn lighting_getLightColor(surfaceColor: vec3<f32>, light_direction: vec3<f32>, view_direction: vec3<f32>, normal_worldspace: vec3<f32>, color: vec3<f32>) -> vec3<f32> {
16
+ let halfway_direction: vec3<f32> = normalize(light_direction + view_direction);
17
+ var lambertian: f32 = dot(light_direction, normal_worldspace);
18
+ var specular: f32 = 0.0;
19
+ if (lambertian > 0.0) {
20
+ let specular_angle = max(dot(normal_worldspace, halfway_direction), 0.0);
21
+ specular = pow(specular_angle, material.shininess);
22
+ }
23
+ lambertian = max(lambertian, 0.0);
24
+ return (lambertian * material.diffuse * surfaceColor + specular * material.specularColor) * color;
25
+ }
26
+
27
+ fn lighting_getLightColor2(surfaceColor: vec3<f32>, cameraPosition: vec3<f32>, position_worldspace: vec3<f32>, normal_worldspace: vec3<f32>) -> vec3<f32> {
28
+ var lightColor: vec3<f32> = surfaceColor;
29
+
30
+ if (lighting.enabled == 0) {
31
+ return lightColor;
32
+ }
33
+
34
+ let view_direction: vec3<f32> = normalize(cameraPosition - position_worldspace);
35
+ lightColor = material.ambient * surfaceColor * lighting.ambientColor;
36
+
37
+ if (lighting.lightType == 0) {
38
+ let pointLight: PointLight = lighting_getPointLight(0);
39
+ let light_position_worldspace: vec3<f32> = pointLight.position;
40
+ let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
41
+ lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
42
+ } else if (lighting.lightType == 1) {
43
+ var directionalLight: DirectionalLight = lighting_getDirectionalLight(0);
44
+ lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
45
+ }
46
+
47
+ return lightColor;
48
+ /*
49
+ for (int i = 0; i < MAX_LIGHTS; i++) {
50
+ if (i >= lighting.pointLightCount) {
51
+ break;
52
+ }
53
+ PointLight pointLight = lighting.pointLight[i];
54
+ vec3 light_position_worldspace = pointLight.position;
55
+ vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
56
+ lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
57
+ }
58
+
59
+ for (int i = 0; i < MAX_LIGHTS; i++) {
60
+ if (i >= lighting.directionalLightCount) {
61
+ break;
62
+ }
63
+ DirectionalLight directionalLight = lighting.directionalLight[i];
64
+ lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
65
+ }
66
+ */
67
+ }
68
+
69
+ fn lighting_getSpecularLightColor(cameraPosition: vec3<f32>, position_worldspace: vec3<f32>, normal_worldspace: vec3<f32>) -> vec3<f32>{
70
+ var lightColor = vec3<f32>(0, 0, 0);
71
+ let surfaceColor = vec3<f32>(0, 0, 0);
72
+
73
+ if (lighting.enabled == 0) {
74
+ let view_direction = normalize(cameraPosition - position_worldspace);
75
+
76
+ switch (lighting.lightType) {
77
+ case 0, default: {
78
+ let pointLight: PointLight = lighting_getPointLight(0);
79
+ let light_position_worldspace: vec3<f32> = pointLight.position;
80
+ let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
81
+ lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
82
+ }
83
+ case 1: {
84
+ let directionalLight: DirectionalLight = lighting_getDirectionalLight(0);
85
+ lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
86
+ }
87
+ }
88
+ }
89
+ return lightColor;
90
+ }
91
+ `;
92
+
93
+ // TODO - handle multiple lights
94
+ /**
95
+ for (int i = 0; i < MAX_LIGHTS; i++) {
96
+ if (i >= lighting.pointLightCount) {
97
+ break;
98
+ }
99
+ PointLight pointLight = lighting_getPointLight(i);
100
+ vec3 light_position_worldspace = pointLight.position;
101
+ vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
102
+ lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
103
+ }
104
+
105
+ for (int i = 0; i < MAX_LIGHTS; i++) {
106
+ if (i >= lighting.directionalLightCount) {
107
+ break;
108
+ }
109
+ PointLight pointLight = lighting_getDirectionalLight(i);
110
+ lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
111
+ }
112
+ }
113
+ /**
114
+ for (int i = 0; i < MAX_LIGHTS; i++) {
115
+ if (i >= lighting.pointLightCount) {
116
+ break;
117
+ }
118
+ PointLight pointLight = lighting_getPointLight(i);
119
+ vec3 light_position_worldspace = pointLight.position;
120
+ vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
121
+ lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
122
+ }
123
+
124
+ for (int i = 0; i < MAX_LIGHTS; i++) {
125
+ if (i >= lighting.directionalLightCount) {
126
+ break;
127
+ }
128
+ PointLight pointLight = lighting_getDirectionalLight(i);
129
+ lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
130
+ }
131
+ }
132
+ */
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
- import type {NumberArray} from '@math.gl/types';
5
+ import type {NumericArray} from '@math.gl/types';
6
6
 
7
7
  /**
8
8
  * Calculate WebGL 64 bit float
@@ -11,7 +11,7 @@ import type {NumberArray} from '@math.gl/types';
11
11
  * @param startIndex - the index in the output array to fill from. Default 0.
12
12
  * @returns - the fp64 representation of the input number
13
13
  */
14
- export function fp64ify(a: number, out: NumberArray = [], startIndex: number = 0): NumberArray {
14
+ export function fp64ify(a: number, out: NumericArray = [], startIndex: number = 0): NumericArray {
15
15
  const hiPart = Math.fround(a);
16
16
  const loPart = a - hiPart;
17
17
  out[startIndex] = hiPart;
@@ -33,7 +33,7 @@ export function fp64LowPart(a: number): number {
33
33
  * @param matrix the input matrix
34
34
  * @returns the fp64 representation of the input matrix
35
35
  */
36
- export function fp64ifyMatrix4(matrix: NumberArray): Float32Array {
36
+ export function fp64ifyMatrix4(matrix: NumericArray): Float32Array {
37
37
  // Transpose the projection matrix to column major for GLSL.
38
38
  const matrixFP64 = new Float32Array(32);
39
39
  for (let i = 0; i < 4; ++i) {
@@ -3,13 +3,13 @@
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
5
  import {ShaderModule} from '../../../lib/shader-module/shader-module';
6
- import type {NumberArray} from '../../../types';
6
+ import type {NumericArray} from '../../../types';
7
7
  import {project} from '../../project/project';
8
8
 
9
9
  /* eslint-disable camelcase */
10
10
 
11
11
  export type DirlightOptions = {
12
- lightDirection?: NumberArray;
12
+ lightDirection?: NumericArray;
13
13
  };
14
14
 
15
15
  const DEFAULT_MODULE_OPTIONS: Required<DirlightOptions> = {