@luma.gl/shadertools 9.1.0-alpha.16 → 9.1.0-alpha.18

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 (118) hide show
  1. package/dist/dist.dev.js +277 -344
  2. package/dist/dist.min.js +215 -314
  3. package/dist/index.cjs +276 -346
  4. package/dist/index.cjs.map +4 -4
  5. package/dist/index.d.ts +4 -2
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +0 -1
  8. package/dist/lib/shader-module/shader-module.d.ts +24 -14
  9. package/dist/lib/shader-module/shader-module.d.ts.map +1 -1
  10. package/dist/lib/shader-module/shader-module.js +11 -15
  11. package/dist/lib/shader-module/shader-pass.d.ts +4 -2
  12. package/dist/lib/shader-module/shader-pass.d.ts.map +1 -1
  13. package/dist/lib/utils/uniform-types.d.ts +11 -0
  14. package/dist/lib/utils/uniform-types.d.ts.map +1 -0
  15. package/dist/lib/utils/uniform-types.js +1 -0
  16. package/dist/modules/engine/picking/picking.d.ts +7 -7
  17. package/dist/modules/engine/picking/picking.d.ts.map +1 -1
  18. package/dist/modules/engine/picking/picking.js +2 -2
  19. package/dist/modules/engine/project/project.d.ts +9 -8
  20. package/dist/modules/engine/project/project.d.ts.map +1 -1
  21. package/dist/modules/lighting/gouraud-material/gouraud-material.d.ts +3 -57
  22. package/dist/modules/lighting/gouraud-material/gouraud-material.d.ts.map +1 -1
  23. package/dist/modules/lighting/gouraud-material/gouraud-material.js +9 -6
  24. package/dist/modules/lighting/lights/lighting-uniforms-glsl.d.ts +1 -1
  25. package/dist/modules/lighting/lights/lighting-uniforms-glsl.d.ts.map +1 -1
  26. package/dist/modules/lighting/lights/lighting-uniforms-glsl.js +35 -12
  27. package/dist/modules/lighting/lights/lighting.d.ts +55 -29
  28. package/dist/modules/lighting/lights/lighting.d.ts.map +1 -1
  29. package/dist/modules/lighting/lights/lighting.js +46 -35
  30. package/dist/modules/lighting/no-material/dirlight.d.ts +3 -3
  31. package/dist/modules/lighting/no-material/dirlight.d.ts.map +1 -1
  32. package/dist/modules/lighting/no-material/dirlight.js +1 -1
  33. package/dist/modules/lighting/pbr-material/pbr-fragment-glsl.d.ts +1 -1
  34. package/dist/modules/lighting/pbr-material/pbr-fragment-glsl.d.ts.map +1 -1
  35. package/dist/modules/lighting/pbr-material/pbr-fragment-glsl.js +56 -60
  36. package/dist/modules/lighting/pbr-material/pbr-material.d.ts +56 -98
  37. package/dist/modules/lighting/pbr-material/pbr-material.d.ts.map +1 -1
  38. package/dist/modules/lighting/pbr-material/pbr-material.js +14 -22
  39. package/dist/modules/lighting/pbr-material/pbr-projection.d.ts +10 -0
  40. package/dist/modules/lighting/pbr-material/pbr-projection.d.ts.map +1 -0
  41. package/dist/modules/lighting/pbr-material/pbr-projection.js +24 -0
  42. package/dist/modules/lighting/pbr-material/pbr-vertex-glsl.d.ts +1 -1
  43. package/dist/modules/lighting/pbr-material/pbr-vertex-glsl.d.ts.map +1 -1
  44. package/dist/modules/lighting/pbr-material/pbr-vertex-glsl.js +8 -16
  45. package/dist/modules/lighting/phong-material/phong-material.d.ts +5 -59
  46. package/dist/modules/lighting/phong-material/phong-material.d.ts.map +1 -1
  47. package/dist/modules/lighting/phong-material/phong-material.js +6 -4
  48. package/dist/modules/lighting/phong-material/phong-shaders-glsl.d.ts +1 -40
  49. package/dist/modules/lighting/phong-material/phong-shaders-glsl.d.ts.map +1 -1
  50. package/dist/modules/lighting/phong-material/phong-shaders-glsl.js +8 -87
  51. package/dist/modules-webgl1/project/project.d.ts +6 -4
  52. package/dist/modules-webgl1/project/project.d.ts.map +1 -1
  53. package/dist/passes/postprocessing/fxaa/fxaa.d.ts +1 -1
  54. package/dist/passes/postprocessing/fxaa/fxaa.d.ts.map +1 -1
  55. package/dist/passes/postprocessing/fxaa/fxaa.js +1 -1
  56. package/dist/passes/postprocessing/image-adjust-filters/brightnesscontrast.d.ts +0 -1
  57. package/dist/passes/postprocessing/image-adjust-filters/brightnesscontrast.d.ts.map +1 -1
  58. package/dist/passes/postprocessing/image-adjust-filters/brightnesscontrast.js +0 -1
  59. package/dist/passes/postprocessing/image-adjust-filters/huesaturation.d.ts +0 -1
  60. package/dist/passes/postprocessing/image-adjust-filters/huesaturation.d.ts.map +1 -1
  61. package/dist/passes/postprocessing/image-adjust-filters/huesaturation.js +0 -1
  62. package/dist/passes/postprocessing/image-adjust-filters/vibrance.d.ts +3 -0
  63. package/dist/passes/postprocessing/image-adjust-filters/vibrance.d.ts.map +1 -1
  64. package/dist/passes/postprocessing/image-adjust-filters/vibrance.js +3 -0
  65. package/dist/passes/postprocessing/image-blur-filters/tiltshift.d.ts +2 -2
  66. package/dist/passes/postprocessing/image-blur-filters/tiltshift.d.ts.map +1 -1
  67. package/dist/passes/postprocessing/image-blur-filters/triangleblur.d.ts +1 -1
  68. package/dist/passes/postprocessing/image-blur-filters/triangleblur.d.ts.map +1 -1
  69. package/dist/passes/postprocessing/image-blur-filters/zoomblur.d.ts +1 -1
  70. package/dist/passes/postprocessing/image-blur-filters/zoomblur.d.ts.map +1 -1
  71. package/dist/passes/postprocessing/image-fun-filters/colorhalftone.d.ts +1 -1
  72. package/dist/passes/postprocessing/image-fun-filters/colorhalftone.d.ts.map +1 -1
  73. package/dist/passes/postprocessing/image-fun-filters/dotscreen.d.ts +1 -1
  74. package/dist/passes/postprocessing/image-fun-filters/dotscreen.d.ts.map +1 -1
  75. package/dist/passes/postprocessing/image-fun-filters/edgework.d.ts +15 -11
  76. package/dist/passes/postprocessing/image-fun-filters/edgework.d.ts.map +1 -1
  77. package/dist/passes/postprocessing/image-fun-filters/edgework.js +24 -12
  78. package/dist/passes/postprocessing/image-fun-filters/hexagonalpixelate.d.ts +1 -1
  79. package/dist/passes/postprocessing/image-fun-filters/hexagonalpixelate.d.ts.map +1 -1
  80. package/dist/passes/postprocessing/image-fun-filters/magnify.d.ts +2 -2
  81. package/dist/passes/postprocessing/image-fun-filters/magnify.d.ts.map +1 -1
  82. package/dist/passes/postprocessing/image-warp-filters/bulgepinch.d.ts +1 -1
  83. package/dist/passes/postprocessing/image-warp-filters/bulgepinch.d.ts.map +1 -1
  84. package/package.json +3 -3
  85. package/src/index.ts +9 -3
  86. package/src/lib/shader-module/shader-module.ts +31 -20
  87. package/src/lib/shader-module/shader-pass.ts +7 -4
  88. package/src/lib/utils/uniform-types.ts +66 -0
  89. package/src/modules/engine/picking/picking.ts +9 -9
  90. package/src/modules/engine/project/project.ts +10 -9
  91. package/src/modules/lighting/gouraud-material/gouraud-material.ts +14 -12
  92. package/src/modules/lighting/lights/lighting-uniforms-glsl.ts +35 -12
  93. package/src/modules/lighting/lights/lighting.ts +83 -54
  94. package/src/modules/lighting/no-material/dirlight.ts +3 -3
  95. package/src/modules/lighting/pbr-material/pbr-fragment-glsl.ts +56 -60
  96. package/src/modules/lighting/pbr-material/pbr-material.ts +40 -74
  97. package/src/modules/lighting/pbr-material/pbr-projection.ts +39 -0
  98. package/src/modules/lighting/pbr-material/pbr-vertex-glsl.ts +8 -16
  99. package/src/modules/lighting/phong-material/phong-material.ts +12 -12
  100. package/src/modules/lighting/phong-material/phong-shaders-glsl.ts +8 -88
  101. package/src/modules-webgl1/project/project.ts +8 -7
  102. package/src/passes/postprocessing/fxaa/fxaa.ts +2 -2
  103. package/src/passes/postprocessing/image-adjust-filters/brightnesscontrast.ts +0 -1
  104. package/src/passes/postprocessing/image-adjust-filters/huesaturation.ts +0 -1
  105. package/src/passes/postprocessing/image-adjust-filters/vibrance.ts +3 -0
  106. package/src/passes/postprocessing/image-blur-filters/tiltshift.ts +2 -2
  107. package/src/passes/postprocessing/image-blur-filters/triangleblur.ts +1 -1
  108. package/src/passes/postprocessing/image-blur-filters/zoomblur.ts +1 -1
  109. package/src/passes/postprocessing/image-fun-filters/colorhalftone.ts +1 -1
  110. package/src/passes/postprocessing/image-fun-filters/dotscreen.ts +1 -1
  111. package/src/passes/postprocessing/image-fun-filters/edgework.ts +26 -15
  112. package/src/passes/postprocessing/image-fun-filters/hexagonalpixelate.ts +1 -1
  113. package/src/passes/postprocessing/image-fun-filters/magnify.ts +2 -2
  114. package/src/passes/postprocessing/image-warp-filters/bulgepinch.ts +1 -1
  115. package/dist/modules/lighting/gouraud-material/gouraud-shaders-glsl.d.ts +0 -42
  116. package/dist/modules/lighting/gouraud-material/gouraud-shaders-glsl.d.ts.map +0 -1
  117. package/dist/modules/lighting/gouraud-material/gouraud-shaders-glsl.js +0 -134
  118. package/src/modules/lighting/gouraud-material/gouraud-shaders-glsl.ts +0 -137
@@ -2,13 +2,14 @@
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 {log} from '@luma.gl/core';
6
6
  import {ShaderModule} from '../../../lib/shader-module/shader-module';
7
7
  import {lightingUniformsGLSL} from './lighting-uniforms-glsl';
8
8
  import {lightingUniformsWGSL} from './lighting-uniforms-wgsl';
9
+ import type {NumberArray3} from '@math.gl/core';
9
10
 
10
11
  /** Max number of supported lights (in addition to ambient light */
11
- const MAX_LIGHTS = 5;
12
+ const MAX_LIGHTS = 3;
12
13
 
13
14
  /** Whether to divide */
14
15
  const COLOR_FACTOR = 255.0;
@@ -26,23 +27,22 @@ export type Light = AmbientLight | PointLight | DirectionalLight;
26
27
 
27
28
  export type AmbientLight = {
28
29
  type: 'ambient';
29
- color?: Readonly<NumberArray>;
30
+ color?: Readonly<NumberArray3>;
30
31
  intensity?: number;
31
32
  };
32
33
 
33
34
  export type PointLight = {
34
35
  type: 'point';
35
- position: Readonly<NumberArray>;
36
- color?: Readonly<NumberArray>;
36
+ position: Readonly<NumberArray3>;
37
+ color?: Readonly<NumberArray3>;
37
38
  intensity?: number;
38
- attenuation?: number;
39
+ attenuation?: Readonly<NumberArray3>;
39
40
  };
40
41
 
41
42
  export type DirectionalLight = {
42
43
  type: 'directional';
43
- position: Readonly<NumberArray>;
44
- direction: Readonly<NumberArray>;
45
- color?: Readonly<NumberArray>;
44
+ direction: Readonly<NumberArray3>;
45
+ color?: Readonly<NumberArray3>;
46
46
  intensity?: number;
47
47
  };
48
48
 
@@ -59,13 +59,22 @@ export type LightingProps = {
59
59
 
60
60
  export type LightingUniforms = {
61
61
  enabled: number;
62
- ambientLightColor: Readonly<NumberArray>;
63
- numberOfLights: number;
62
+ ambientLightColor: Readonly<NumberArray3>;
63
+ directionalLightCount: number;
64
+ pointLightCount: number;
64
65
  lightType: number; // [];
65
- lightColor: Readonly<NumberArray>; // [];
66
- lightPosition: Readonly<NumberArray>; // [];
67
- lightDirection: Readonly<NumberArray>; // [];
68
- lightAttenuation: Readonly<NumberArray>; // [];
66
+ lightColor0: Readonly<NumberArray3>;
67
+ lightPosition0: Readonly<NumberArray3>;
68
+ lightDirection0: Readonly<NumberArray3>;
69
+ lightAttenuation0: Readonly<NumberArray3>;
70
+ lightColor1: Readonly<NumberArray3>;
71
+ lightPosition1: Readonly<NumberArray3>;
72
+ lightDirection1: Readonly<NumberArray3>;
73
+ lightAttenuation1: Readonly<NumberArray3>;
74
+ lightColor2: Readonly<NumberArray3>;
75
+ lightPosition2: Readonly<NumberArray3>;
76
+ lightDirection2: Readonly<NumberArray3>;
77
+ lightAttenuation2: Readonly<NumberArray3>;
69
78
  };
70
79
 
71
80
  /** UBO ready lighting module */
@@ -81,33 +90,59 @@ export const lighting = {
81
90
 
82
91
  uniformTypes: {
83
92
  enabled: 'i32',
93
+ lightType: 'i32',
94
+
95
+ directionalLightCount: 'i32',
96
+ pointLightCount: 'i32',
97
+
84
98
  ambientLightColor: 'vec3<f32>',
85
- numberOfLights: 'i32', // , array: MAX_LIGHTS,
86
- lightType: 'i32', // , array: MAX_LIGHTS,
87
- lightColor: 'vec3<f32>', // , array: MAX_LIGHTS,
88
- lightPosition: 'vec3<f32>', // , array: MAX_LIGHTS,
99
+
100
+ // TODO define as arrays once we have appropriate uniformTypes
101
+ lightColor0: 'vec3<f32>',
102
+ lightPosition0: 'vec3<f32>',
89
103
  // TODO - could combine direction and attenuation
90
- lightDirection: 'vec3<f32>', // , array: MAX_LIGHTS,
91
- lightAttenuation: 'vec3<f32>' // , array: MAX_LIGHTS},
104
+ lightDirection0: 'vec3<f32>',
105
+ lightAttenuation0: 'vec3<f32>',
106
+
107
+ lightColor1: 'vec3<f32>',
108
+ lightPosition1: 'vec3<f32>',
109
+ lightDirection1: 'vec3<f32>',
110
+ lightAttenuation1: 'vec3<f32>',
111
+ lightColor2: 'vec3<f32>',
112
+ lightPosition2: 'vec3<f32>',
113
+ lightDirection2: 'vec3<f32>',
114
+ lightAttenuation2: 'vec3<f32>'
92
115
  },
93
116
 
94
117
  defaultUniforms: {
95
118
  enabled: 1,
96
- ambientLightColor: [0.1, 0.1, 0.1],
97
- numberOfLights: 0,
98
119
  lightType: LIGHT_TYPE.POINT,
99
- lightColor: [1, 1, 1],
100
- lightPosition: [1, 1, 2],
120
+
121
+ directionalLightCount: 0,
122
+ pointLightCount: 0,
123
+
124
+ ambientLightColor: [0.1, 0.1, 0.1],
125
+ lightColor0: [1, 1, 1],
126
+ lightPosition0: [1, 1, 2],
101
127
  // TODO - could combine direction and attenuation
102
- lightDirection: [1, 1, 1],
103
- lightAttenuation: [1, 1, 1]
128
+ lightDirection0: [1, 1, 1],
129
+ lightAttenuation0: [1, 0, 0],
130
+
131
+ lightColor1: [1, 1, 1],
132
+ lightPosition1: [1, 1, 2],
133
+ lightDirection1: [1, 1, 1],
134
+ lightAttenuation1: [1, 0, 0],
135
+ lightColor2: [1, 1, 1],
136
+ lightPosition2: [1, 1, 2],
137
+ lightDirection2: [1, 1, 1],
138
+ lightAttenuation2: [1, 0, 0]
104
139
  },
105
140
  source: lightingUniformsWGSL,
106
141
  vs: lightingUniformsGLSL,
107
142
  fs: lightingUniformsGLSL,
108
143
 
109
144
  getUniforms
110
- } as const satisfies ShaderModule<LightingProps, LightingUniforms>;
145
+ } as const satisfies ShaderModule<LightingProps, LightingUniforms, {}>;
111
146
 
112
147
  function getUniforms(
113
148
  props?: LightingProps,
@@ -155,43 +190,37 @@ function getLightSourceUniforms({
155
190
  pointLights = [],
156
191
  directionalLights = []
157
192
  }: LightingProps): Partial<LightingUniforms> {
158
- const lightSourceUniforms: Partial<LightingUniforms> = {
159
- // lightType: new Array(MAX_LIGHTS).fill(0),
160
- // lightColor: new Array(MAX_LIGHTS).fill([0, 0, 0]),
161
- // lightPosition: new Array(MAX_LIGHTS).fill([0, 0, 0]),
162
- // lightDirection: new Array(MAX_LIGHTS).fill([0, 0, 0]),
163
- // lightAttenuation: new Array(MAX_LIGHTS).fill([0, 0, 0])
164
- };
193
+ const lightSourceUniforms: Partial<LightingUniforms> = {};
165
194
 
166
195
  lightSourceUniforms.ambientLightColor = convertColor(ambientLight);
167
196
 
168
- let currentLight = 0;
197
+ let currentLight: 0 | 1 | 2 = 0;
169
198
 
170
199
  for (const pointLight of pointLights) {
171
- // lightSourceUniforms.lightType[currentLight] = LIGHT_TYPE.POINT;
172
- // lightSourceUniforms.lightColor[currentLight] = convertColor(pointLight);
173
- // lightSourceUniforms.lightPosition[currentLight] = pointLight.position;
174
- // lightSourceUniforms.lightAttenuation[currentLight] = [pointLight.attenuation || 1, 0, 0];
175
200
  lightSourceUniforms.lightType = LIGHT_TYPE.POINT;
176
- lightSourceUniforms.lightColor = convertColor(pointLight);
177
- lightSourceUniforms.lightPosition = pointLight.position;
178
- lightSourceUniforms.lightAttenuation = [pointLight.attenuation || 1, 0, 0];
201
+
202
+ const i = currentLight as 0 | 1 | 2;
203
+ lightSourceUniforms[`lightColor${i}`] = convertColor(pointLight);
204
+ lightSourceUniforms[`lightPosition${i}`] = pointLight.position;
205
+ lightSourceUniforms[`lightAttenuation${i}`] = pointLight.attenuation || [1, 0, 0];
179
206
  currentLight++;
180
207
  }
181
208
 
182
209
  for (const directionalLight of directionalLights) {
183
- // lightSourceUniforms.lightType[currentLight] = LIGHT_TYPE.DIRECTIONAL;
184
- // lightSourceUniforms.lightColor[currentLight] = convertColor(directionalLight);
185
- // lightSourceUniforms.lightPosition[currentLight] = directionalLight.position;
186
- // lightSourceUniforms.lightDirection[currentLight] = directionalLight.direction;
187
210
  lightSourceUniforms.lightType = LIGHT_TYPE.DIRECTIONAL;
188
- lightSourceUniforms.lightColor = convertColor(directionalLight);
189
- lightSourceUniforms.lightPosition = directionalLight.position;
190
- lightSourceUniforms.lightDirection = directionalLight.direction;
211
+
212
+ const i = currentLight as 0 | 1 | 2;
213
+ lightSourceUniforms[`lightColor${i}`] = convertColor(directionalLight);
214
+ lightSourceUniforms[`lightDirection${i}`] = directionalLight.direction;
191
215
  currentLight++;
192
216
  }
193
217
 
194
- lightSourceUniforms.numberOfLights = currentLight;
218
+ if (currentLight > MAX_LIGHTS) {
219
+ log.warn('MAX_LIGHTS exceeded')();
220
+ }
221
+
222
+ lightSourceUniforms.directionalLightCount = directionalLights.length;
223
+ lightSourceUniforms.pointLightCount = pointLights.length;
195
224
 
196
225
  return lightSourceUniforms;
197
226
  }
@@ -221,8 +250,8 @@ function extractLightTypes(lights: Light[]): LightingProps {
221
250
 
222
251
  /** Take color 0-255 and intensity as input and output 0.0-1.0 range */
223
252
  function convertColor(
224
- colorDef: {color?: Readonly<NumberArray>; intensity?: number} = {}
225
- ): NumberArray {
253
+ colorDef: {color?: Readonly<NumberArray3>; intensity?: number} = {}
254
+ ): NumberArray3 {
226
255
  const {color = [0, 0, 0], intensity = 1.0} = colorDef;
227
- return color.map(component => (component * intensity) / COLOR_FACTOR);
256
+ return color.map(component => (component * intensity) / COLOR_FACTOR) as NumberArray3;
228
257
  }
@@ -2,11 +2,11 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
- import type {Vector3Like, TypedArray} from '@math.gl/core';
5
+ import type {Vector3Like} from '@math.gl/core';
6
6
  import {ShaderModule} from '../../../lib/shader-module/shader-module';
7
7
 
8
8
  export type DirlightProps = {
9
- lightDirection?: Vector3Like | TypedArray;
9
+ lightDirection?: Vector3Like;
10
10
  };
11
11
 
12
12
  export type DirlightUniforms = DirlightProps;
@@ -85,7 +85,7 @@ export const dirlight = {
85
85
  lightDirection: 'vec3<f32>'
86
86
  },
87
87
  defaultUniforms: {
88
- lightDirection: new Float32Array([1, 1, 2])
88
+ lightDirection: [1, 1, 2]
89
89
  },
90
90
  getUniforms
91
91
  } as const satisfies ShaderModule<DirlightProps, DirlightUniforms>;
@@ -13,12 +13,7 @@
13
13
  export const fs = /* glsl */ `\
14
14
  precision highp float;
15
15
 
16
- uniform Projection {
17
- // Projection
18
- uniform vec3 u_Camera;
19
- };
20
-
21
- uniform pbrMaterial {
16
+ uniform pbrMaterialUniforms {
22
17
  // Material is unlit
23
18
  bool unlit;
24
19
 
@@ -50,40 +45,40 @@ uniform pbrMaterial {
50
45
  vec4 scaleDiffBaseMR;
51
46
  vec4 scaleFGDSpec;
52
47
  // #endif
53
- } u_pbrMaterial;
48
+ } pbrMaterial;
54
49
 
55
50
  // Samplers
56
51
  #ifdef HAS_BASECOLORMAP
57
- uniform sampler2D u_BaseColorSampler;
52
+ uniform sampler2D pbr_baseColorSampler;
58
53
  #endif
59
54
  #ifdef HAS_NORMALMAP
60
- uniform sampler2D u_NormalSampler;
55
+ uniform sampler2D pbr_normalSampler;
61
56
  #endif
62
57
  #ifdef HAS_EMISSIVEMAP
63
- uniform sampler2D u_EmissiveSampler;
58
+ uniform sampler2D pbr_emissiveSampler;
64
59
  #endif
65
60
  #ifdef HAS_METALROUGHNESSMAP
66
- uniform sampler2D u_MetallicRoughnessSampler;
61
+ uniform sampler2D pbr_metallicRoughnessSampler;
67
62
  #endif
68
63
  #ifdef HAS_OCCLUSIONMAP
69
- uniform sampler2D u_OcclusionSampler;
64
+ uniform sampler2D pbr_occlusionSampler;
70
65
  #endif
71
66
  #ifdef USE_IBL
72
- uniform samplerCube u_DiffuseEnvSampler;
73
- uniform samplerCube u_SpecularEnvSampler;
74
- uniform sampler2D u_brdfLUT;
67
+ uniform samplerCube pbr_diffuseEnvSampler;
68
+ uniform samplerCube pbr_specularEnvSampler;
69
+ uniform sampler2D pbr_brdfLUT;
75
70
  #endif
76
71
 
77
72
  // Inputs from vertex shader
78
73
 
79
- varying vec3 pbr_vPosition;
80
- varying vec2 pbr_vUV;
74
+ in vec3 pbr_vPosition;
75
+ in vec2 pbr_vUV;
81
76
 
82
77
  #ifdef HAS_NORMALS
83
78
  #ifdef HAS_TANGENTS
84
- varying mat3 pbr_vTBN;
79
+ in mat3 pbr_vTBN;
85
80
  #else
86
- varying vec3 pbr_vNormal;
81
+ in vec3 pbr_vNormal;
87
82
  #endif
88
83
  #endif
89
84
 
@@ -151,8 +146,8 @@ vec3 getNormal()
151
146
  #endif
152
147
 
153
148
  #ifdef HAS_NORMALMAP
154
- vec3 n = texture2D(u_NormalSampler, pbr_vUV).rgb;
155
- n = normalize(tbn * ((2.0 * n - 1.0) * vec3(u_pbrMaterial.normalScale, u_pbrMaterial.normalScale, 1.0)));
149
+ vec3 n = texture(pbr_normalSampler, pbr_vUV).rgb;
150
+ n = normalize(tbn * ((2.0 * n - 1.0) * vec3(pbrMaterial.normalScale, pbrMaterial.normalScale, 1.0)));
156
151
  #else
157
152
  // The tbn matrix is linearly interpolated, so we need to re-normalize
158
153
  vec3 n = normalize(tbn[2].xyz);
@@ -170,22 +165,22 @@ vec3 getIBLContribution(PBRInfo pbrInfo, vec3 n, vec3 reflection)
170
165
  float mipCount = 9.0; // resolution of 512x512
171
166
  float lod = (pbrInfo.perceptualRoughness * mipCount);
172
167
  // retrieve a scale and bias to F0. See [1], Figure 3
173
- vec3 brdf = SRGBtoLINEAR(texture2D(u_brdfLUT,
168
+ vec3 brdf = SRGBtoLINEAR(texture(pbr_brdfLUT,
174
169
  vec2(pbrInfo.NdotV, 1.0 - pbrInfo.perceptualRoughness))).rgb;
175
- vec3 diffuseLight = SRGBtoLINEAR(textureCube(u_DiffuseEnvSampler, n)).rgb;
170
+ vec3 diffuseLight = SRGBtoLINEAR(texture(pbr_diffuseEnvSampler, n)).rgb;
176
171
 
177
172
  #ifdef USE_TEX_LOD
178
- vec3 specularLight = SRGBtoLINEAR(textureCubeLod(u_SpecularEnvSampler, reflection, lod)).rgb;
173
+ vec3 specularLight = SRGBtoLINEAR(texture(pbr_specularEnvSampler, reflection, lod)).rgb;
179
174
  #else
180
- vec3 specularLight = SRGBtoLINEAR(textureCube(u_SpecularEnvSampler, reflection)).rgb;
175
+ vec3 specularLight = SRGBtoLINEAR(texture(pbr_specularEnvSampler, reflection)).rgb;
181
176
  #endif
182
177
 
183
178
  vec3 diffuse = diffuseLight * pbrInfo.diffuseColor;
184
179
  vec3 specular = specularLight * (pbrInfo.specularColor * brdf.x + brdf.y);
185
180
 
186
181
  // For presentation, this allows us to disable IBL terms
187
- diffuse *= u_pbrMaterial.scaleIBLAmbient.x;
188
- specular *= u_pbrMaterial.scaleIBLAmbient.y;
182
+ diffuse *= pbrMaterial.scaleIBLAmbient.x;
183
+ specular *= pbrMaterial.scaleIBLAmbient.y;
189
184
 
190
185
  return diffuse + specular;
191
186
  }
@@ -277,32 +272,32 @@ vec4 pbr_filterColor(vec4 colorUnused)
277
272
  {
278
273
  // The albedo may be defined from a base texture or a flat color
279
274
  #ifdef HAS_BASECOLORMAP
280
- vec4 baseColor = SRGBtoLINEAR(texture2D(u_BaseColorSampler, pbr_vUV)) * u_pbrMaterial.baseColorFactor;
275
+ vec4 baseColor = SRGBtoLINEAR(texture(pbr_baseColorSampler, pbr_vUV)) * pbrMaterial.baseColorFactor;
281
276
  #else
282
- vec4 baseColor = u_pbrMaterial.baseColorFactor;
277
+ vec4 baseColor = pbrMaterial.baseColorFactor;
283
278
  #endif
284
279
 
285
280
  #ifdef ALPHA_CUTOFF
286
- if (baseColor.a < u_pbrMaterial.alphaCutoff) {
281
+ if (baseColor.a < pbrMaterial.alphaCutoff) {
287
282
  discard;
288
283
  }
289
284
  #endif
290
285
 
291
286
  vec3 color = vec3(0, 0, 0);
292
287
 
293
- if(u_pbrMaterial.unlit){
288
+ if(pbrMaterial.unlit){
294
289
  color.rgb = baseColor.rgb;
295
290
  }
296
291
  else{
297
292
  // Metallic and Roughness material properties are packed together
298
293
  // In glTF, these factors can be specified by fixed scalar values
299
294
  // or from a metallic-roughness map
300
- float perceptualRoughness = u_pbrMaterial.metallicRoughnessValues.y;
301
- float metallic = u_pbrMaterial.metallicRoughnessValues.x;
295
+ float perceptualRoughness = pbrMaterial.metallicRoughnessValues.y;
296
+ float metallic = pbrMaterial.metallicRoughnessValues.x;
302
297
  #ifdef HAS_METALROUGHNESSMAP
303
298
  // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.
304
299
  // This layout intentionally reserves the 'r' channel for (optional) occlusion map data
305
- vec4 mrSample = texture2D(u_MetallicRoughnessSampler, pbr_vUV);
300
+ vec4 mrSample = texture(pbr_metallicRoughnessSampler, pbr_vUV);
306
301
  perceptualRoughness = mrSample.g * perceptualRoughness;
307
302
  metallic = mrSample.b * metallic;
308
303
  #endif
@@ -329,7 +324,7 @@ vec4 pbr_filterColor(vec4 colorUnused)
329
324
  vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;
330
325
 
331
326
  vec3 n = getNormal(); // normal at surface point
332
- vec3 v = normalize(u_Camera - pbr_vPosition); // Vector from surface point to camera
327
+ vec3 v = normalize(pbrProjection.camera - pbr_vPosition); // Vector from surface point to camera
333
328
 
334
329
  float NdotV = clamp(abs(dot(n, v)), 0.001, 1.0);
335
330
  vec3 reflection = -normalize(reflect(v, n));
@@ -351,47 +346,48 @@ vec4 pbr_filterColor(vec4 colorUnused)
351
346
  v
352
347
  );
353
348
 
349
+
354
350
  #ifdef USE_LIGHTS
355
351
  // Apply ambient light
356
352
  PBRInfo_setAmbientLight(pbrInfo);
357
- color += calculateFinalColor(pbrInfo, lighting_uAmbientLight.color);
353
+ color += calculateFinalColor(pbrInfo, lighting.ambientColor);
358
354
 
359
355
  // Apply directional light
360
- for(int i = 0; i < lighting_uDirectionalLightCount; i++) {
361
- if (i < lighting_uDirectionalLightCount) {
362
- PBRInfo_setDirectionalLight(pbrInfo, lighting_uDirectionalLight[i].direction);
363
- color += calculateFinalColor(pbrInfo, lighting_uDirectionalLight[i].color);
356
+ for(int i = 0; i < lighting.directionalLightCount; i++) {
357
+ if (i < lighting.directionalLightCount) {
358
+ PBRInfo_setDirectionalLight(pbrInfo, lighting_getDirectionalLight(i).direction);
359
+ color += calculateFinalColor(pbrInfo, lighting_getDirectionalLight(i).color);
364
360
  }
365
361
  }
366
362
 
367
363
  // Apply point light
368
- for(int i = 0; i < lighting_uPointLightCount; i++) {
369
- if (i < lighting_uPointLightCount) {
370
- PBRInfo_setPointLight(pbrInfo, lighting_uPointLight[i]);
371
- float attenuation = getPointLightAttenuation(lighting_uPointLight[i], distance(lighting_uPointLight[i].position, pbr_vPosition));
372
- color += calculateFinalColor(pbrInfo, lighting_uPointLight[i].color / attenuation);
364
+ for(int i = 0; i < lighting.pointLightCount; i++) {
365
+ if (i < lighting.pointLightCount) {
366
+ PBRInfo_setPointLight(pbrInfo, lighting_getPointLight(i));
367
+ float attenuation = getPointLightAttenuation(lighting_getPointLight(i), distance(lighting_getPointLight(i).position, pbr_vPosition));
368
+ color += calculateFinalColor(pbrInfo, lighting_getPointLight(i).color / attenuation);
373
369
  }
374
370
  }
375
371
  #endif
376
372
 
377
373
  // Calculate lighting contribution from image based lighting source (IBL)
378
374
  #ifdef USE_IBL
379
- if (u_pbrMateral.IBLEnabled) {
375
+ if (pbrMaterial.IBLenabled) {
380
376
  color += getIBLContribution(pbrInfo, n, reflection);
381
377
  }
382
378
  #endif
383
379
 
384
- // Apply optional PBR terms for additional (optional) shading
380
+ // Apply optional PBR terms for additional (optional) shading
385
381
  #ifdef HAS_OCCLUSIONMAP
386
- if (u_pbrMaterial.occlusionMapEnabled) {
387
- float ao = texture2D(u_OcclusionSampler, pbr_vUV).r;
388
- color = mix(color, color * ao, u_pbrMaterial.occlusionStrength);
382
+ if (pbrMaterial.occlusionMapEnabled) {
383
+ float ao = texture(pbr_occlusionSampler, pbr_vUV).r;
384
+ color = mix(color, color * ao, pbrMaterial.occlusionStrength);
389
385
  }
390
386
  #endif
391
387
 
392
388
  #ifdef HAS_EMISSIVEMAP
393
- if (u_pbrMaterial.emmissiveMapEnabled) {
394
- vec3 emissive = SRGBtoLINEAR(texture2D(u_EmissiveSampler, pbr_vUV)).rgb * u_pbrMaterial.emissiveFactor;
389
+ if (pbrMaterial.emissiveMapEnabled) {
390
+ vec3 emissive = SRGBtoLINEAR(texture(pbr_emissiveSampler, pbr_vUV)).rgb * pbrMaterial.emissiveFactor;
395
391
  color += emissive;
396
392
  }
397
393
  #endif
@@ -401,15 +397,15 @@ vec4 pbr_filterColor(vec4 colorUnused)
401
397
  #ifdef PBR_DEBUG
402
398
  // TODO: Figure out how to debug multiple lights
403
399
 
404
- // color = mix(color, F, u_ScaleFGDSpec.x);
405
- // color = mix(color, vec3(G), u_ScaleFGDSpec.y);
406
- // color = mix(color, vec3(D), u_ScaleFGDSpec.z);
407
- // color = mix(color, specContrib, u_ScaleFGDSpec.w);
400
+ // color = mix(color, F, pbr_scaleFGDSpec.x);
401
+ // color = mix(color, vec3(G), pbr_scaleFGDSpec.y);
402
+ // color = mix(color, vec3(D), pbr_scaleFGDSpec.z);
403
+ // color = mix(color, specContrib, pbr_scaleFGDSpec.w);
408
404
 
409
- // color = mix(color, diffuseContrib, u_ScaleDiffBaseMR.x);
410
- color = mix(color, baseColor.rgb, u_pbrMaterial.scaleDiffBaseMR.y);
411
- color = mix(color, vec3(metallic), u_pbrMaterial.scaleDiffBaseMR.z);
412
- color = mix(color, vec3(perceptualRoughness), u_pbrMaterial.scaleDiffBaseMR.w);
405
+ // color = mix(color, diffuseContrib, pbr_scaleDiffBaseMR.x);
406
+ color = mix(color, baseColor.rgb, pbrMaterial.scaleDiffBaseMR.y);
407
+ color = mix(color, vec3(metallic), pbrMaterial.scaleDiffBaseMR.z);
408
+ color = mix(color, vec3(perceptualRoughness), pbrMaterial.scaleDiffBaseMR.w);
413
409
  #endif
414
410
 
415
411
  }
@@ -4,61 +4,36 @@
4
4
 
5
5
  /* eslint-disable camelcase */
6
6
 
7
- import type {NumberArray} from '@math.gl/types';
8
7
  import type {Texture} from '@luma.gl/core';
9
- import type {Vector2, Vector3, Vector4} from '@math.gl/core';
8
+ import type {
9
+ Vector2,
10
+ Vector3,
11
+ Vector4,
12
+ NumberArray2,
13
+ NumberArray3,
14
+ NumberArray4
15
+ } from '@math.gl/core';
10
16
 
11
17
  import {ShaderModule} from '../../../lib/shader-module/shader-module';
12
18
  import {lighting} from '../lights/lighting';
13
19
 
14
20
  import {vs} from './pbr-vertex-glsl';
15
21
  import {fs} from './pbr-fragment-glsl';
16
-
17
- export type PBRMaterialProps = PBRMaterialBindings & {
18
- unlit?: boolean;
19
-
20
- // Base color map
21
- baseColorMapEnabled?: boolean;
22
- baseColorFactor?: Readonly<Vector4 | NumberArray>;
23
-
24
- normalMapEnabled?: boolean;
25
- normalScale?: number; // #ifdef HAS_NORMALMAP
26
-
27
- emissiveMapEnabled?: boolean;
28
- emissiveFactor?: Readonly<Vector3 | NumberArray>; // #ifdef HAS_EMISSIVEMAP
29
-
30
- metallicRoughnessValues?: Readonly<Vector2 | NumberArray>;
31
- metallicRoughnessMapEnabled?: boolean;
32
-
33
- occlusionMapEnabled?: boolean;
34
- occlusionStrength?: number; // #ifdef HAS_OCCLUSIONMAP
35
-
36
- alphaCutoffEnabled?: boolean;
37
- alphaCutoff?: number; // #ifdef ALPHA_CUTOFF
38
-
39
- // IBL
40
- IBLenabled?: boolean;
41
- scaleIBLAmbient?: Readonly<Vector2 | NumberArray>; // #ifdef USE_IBL
42
-
43
- // debugging flags used for shader output of intermediate PBR variables
44
- // #ifdef PBR_DEBUG
45
- scaleDiffBaseMR?: Readonly<Vector4 | NumberArray>;
46
- scaleFGDSpec?: Readonly<Vector4 | NumberArray>;
47
- };
22
+ import {pbrProjection} from './pbr-projection';
48
23
 
49
24
  /** Non-uniform block bindings for pbr module */
50
- type PBRMaterialBindings = {
25
+ export type PBRMaterialBindings = {
51
26
  // Samplers
52
- baseColorSampler?: Texture | null; // #ifdef HAS_BASECOLORMAP
53
- normalSampler?: Texture | null; // #ifdef HAS_NORMALMAP
54
- emissiveSampler?: Texture | null; // #ifdef HAS_EMISSIVEMAP
55
- metallicRoughnessSampler?: Texture | null; // #ifdef HAS_METALROUGHNESSMAP
56
- occlusionSampler?: Texture | null; // #ifdef HAS_OCCLUSIONMAP
27
+ pbr_baseColorSampler?: Texture | null; // #ifdef HAS_BASECOLORMAP
28
+ pbr_normalSampler?: Texture | null; // #ifdef HAS_NORMALMAP
29
+ pbr_emissiveSampler?: Texture | null; // #ifdef HAS_EMISSIVEMAP
30
+ pbr_metallicRoughnessSampler?: Texture | null; // #ifdef HAS_METALROUGHNESSMAP
31
+ pbr_occlusionSampler?: Texture | null; // #ifdef HAS_OCCLUSIONMAP
57
32
 
58
33
  // IBL Samplers
59
- diffuseEnvSampler?: Texture | null; // #ifdef USE_IBL (samplerCube)
60
- specularEnvSampler?: Texture | null; // #ifdef USE_IBL (samplerCube)
61
- brdfLUT?: Texture | null; // #ifdef USE_IBL
34
+ pbr_diffuseEnvSampler?: Texture | null; // #ifdef USE_IBL (samplerCube)
35
+ pbr_specularEnvSampler?: Texture | null; // #ifdef USE_IBL (samplerCube)
36
+ pbr_BrdfLUT?: Texture | null; // #ifdef USE_IBL
62
37
  };
63
38
 
64
39
  export type PBRMaterialUniforms = {
@@ -66,15 +41,15 @@ export type PBRMaterialUniforms = {
66
41
 
67
42
  // Base color map
68
43
  baseColorMapEnabled?: boolean;
69
- baseColorFactor?: Readonly<Vector4 | NumberArray>;
44
+ baseColorFactor?: Readonly<Vector4 | NumberArray4>;
70
45
 
71
46
  normalMapEnabled?: boolean;
72
47
  normalScale?: number; // #ifdef HAS_NORMALMAP
73
48
 
74
49
  emissiveMapEnabled?: boolean;
75
- emissiveFactor?: Readonly<Vector3 | NumberArray>; // #ifdef HAS_EMISSIVEMAP
50
+ emissiveFactor?: Readonly<Vector3 | NumberArray3>; // #ifdef HAS_EMISSIVEMAP
76
51
 
77
- metallicRoughnessValues?: Readonly<Vector2 | NumberArray>;
52
+ metallicRoughnessValues?: Readonly<Vector2 | NumberArray2>;
78
53
  metallicRoughnessMapEnabled?: boolean;
79
54
 
80
55
  occlusionMapEnabled?: boolean;
@@ -85,14 +60,16 @@ export type PBRMaterialUniforms = {
85
60
 
86
61
  // IBL
87
62
  IBLenabled?: boolean;
88
- scaleIBLAmbient?: Readonly<Vector2 | NumberArray>; // #ifdef USE_IBL
63
+ scaleIBLAmbient?: Readonly<Vector2 | NumberArray2>; // #ifdef USE_IBL
89
64
 
90
65
  // debugging flags used for shader output of intermediate PBR variables
91
66
  // #ifdef PBR_DEBUG
92
- scaleDiffBaseMR?: Readonly<Vector4 | NumberArray>;
93
- scaleFGDSpec?: Readonly<Vector4 | NumberArray>;
67
+ scaleDiffBaseMR?: Readonly<Vector4 | NumberArray4>;
68
+ scaleFGDSpec?: Readonly<Vector4 | NumberArray4>;
94
69
  };
95
70
 
71
+ export type PBRMaterialProps = PBRMaterialBindings & PBRMaterialUniforms;
72
+
96
73
  /**
97
74
  * An implementation of PBR (Physically-Based Rendering).
98
75
  * Physically Based Shading of a microfacet surface defined by a glTF material.
@@ -101,23 +78,24 @@ export const pbrMaterial = {
101
78
  props: {} as PBRMaterialProps,
102
79
  uniforms: {} as PBRMaterialUniforms,
103
80
 
104
- name: 'pbr',
105
- dependencies: [lighting],
81
+ name: 'pbrMaterial',
82
+ dependencies: [lighting, pbrProjection],
106
83
  vs,
107
84
  fs,
108
85
 
109
86
  defines: {
110
- LIGHTING_FRAGMENT: 1,
111
- HAS_NORMALMAP: 0,
112
- HAS_EMISSIVEMAP: 0,
113
- HAS_OCCLUSIONMAP: 0,
114
- HAS_BASECOLORMAP: 0,
115
- HAS_METALROUGHNESSMAP: 0,
116
- ALPHA_CUTOFF: 0,
117
- USE_IBL: 0,
118
- PBR_DEBUG: 0
87
+ LIGHTING_FRAGMENT: 1
88
+ // TODO defining these as 0 breaks shader
89
+ // HAS_NORMALMAP: 0
90
+ // HAS_EMISSIVEMAP: 0,
91
+ // HAS_OCCLUSIONMAP: 0,
92
+ // HAS_BASECOLORMAP: 0,
93
+ // HAS_METALROUGHNESSMAP: 0,
94
+ // ALPHA_CUTOFF: 0
95
+ // USE_IBL: 0
96
+ // PBR_DEBUG: 0
119
97
  },
120
-
98
+ getUniforms: props => props,
121
99
  uniformTypes: {
122
100
  // Material is unlit
123
101
  unlit: 'i32',
@@ -149,17 +127,5 @@ export const pbrMaterial = {
149
127
  // #ifdef PBR_DEBUG
150
128
  scaleDiffBaseMR: 'vec4<f32>',
151
129
  scaleFGDSpec: 'vec4<f32>'
152
- },
153
-
154
- bindings: {
155
- baseColorSampler: {type: 'texture', location: 8}, // #ifdef HAS_BASECOLORMAP
156
- normalSampler: {type: 'texture', location: 9}, // #ifdef HAS_NORMALMAP
157
- emissiveSampler: {type: 'texture', location: 10}, // #ifdef HAS_EMISSIVEMAP
158
- metallicRoughnessSampler: {type: 'texture', location: 11}, // #ifdef HAS_METALROUGHNESSMAP
159
- occlusionSampler: {type: 'texture', location: 12}, // #ifdef HAS_OCCLUSIONMAP
160
- // IBL Samplers
161
- diffuseEnvSampler: {type: 'texture', location: 13}, // #ifdef USE_IBL (samplerCube)
162
- specularEnvSampler: {type: 'texture', location: 14}, // #ifdef USE_IBL (samplerCube)
163
- brdfLUT: {type: 'texture', location: 15} // #ifdef USE_IBL
164
130
  }
165
- } as const satisfies ShaderModule<PBRMaterialProps, PBRMaterialUniforms>;
131
+ } as const satisfies ShaderModule<PBRMaterialProps, PBRMaterialUniforms, PBRMaterialBindings>;