@luma.gl/shadertools 9.1.0-alpha.1 → 9.1.0-alpha.12

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 (244) hide show
  1. package/dist/dist.dev.js +1005 -487
  2. package/dist/dist.min.js +2489 -1625
  3. package/dist/index.cjs +2864 -1817
  4. package/dist/index.cjs.map +4 -4
  5. package/dist/index.d.ts +29 -28
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +8 -12
  8. package/dist/lib/glsl-utils/shader-utils.d.ts.map +1 -1
  9. package/dist/lib/glsl-utils/shader-utils.js +2 -3
  10. package/dist/lib/shader-assembler.d.ts +3 -4
  11. package/dist/lib/shader-assembler.d.ts.map +1 -1
  12. package/dist/lib/shader-assembler.js +3 -2
  13. package/dist/lib/shader-assembly/assemble-shaders.d.ts +4 -3
  14. package/dist/lib/shader-assembly/assemble-shaders.d.ts.map +1 -1
  15. package/dist/lib/shader-assembly/assemble-shaders.js +46 -18
  16. package/dist/lib/shader-assembly/platform-defines.d.ts.map +1 -1
  17. package/dist/lib/shader-assembly/platform-defines.js +14 -6
  18. package/dist/lib/shader-module/shader-module-dependencies.d.ts +53 -0
  19. package/dist/lib/shader-module/shader-module-dependencies.d.ts.map +1 -0
  20. package/dist/lib/{shader-assembly/resolve-modules.js → shader-module/shader-module-dependencies.js} +38 -14
  21. package/dist/lib/shader-module/shader-module.d.ts +30 -12
  22. package/dist/lib/shader-module/shader-module.d.ts.map +1 -1
  23. package/dist/lib/shader-module/shader-module.js +76 -6
  24. package/dist/lib/shader-module/shader-pass.d.ts +3 -2
  25. package/dist/lib/shader-module/shader-pass.d.ts.map +1 -1
  26. package/dist/module-injectors.d.ts +2 -2
  27. package/dist/module-injectors.d.ts.map +1 -1
  28. package/dist/module-injectors.js +13 -10
  29. package/dist/modules/engine/geometry/geometry.d.ts.map +1 -1
  30. package/dist/modules/engine/geometry/geometry.js +12 -8
  31. package/dist/modules/engine/picking/picking.d.ts +9 -9
  32. package/dist/modules/engine/picking/picking.d.ts.map +1 -1
  33. package/dist/modules/engine/picking/picking.js +103 -66
  34. package/dist/modules/engine/project/project.d.ts +2 -2
  35. package/dist/modules/engine/project/project.d.ts.map +1 -1
  36. package/dist/modules/engine/project/project.js +36 -22
  37. package/dist/modules/engine/transform/transform.d.ts.map +1 -1
  38. package/dist/modules/engine/transform/transform.js +28 -16
  39. package/dist/modules/lighting/gouraud-material/gouraud-material.d.ts +10 -7
  40. package/dist/modules/lighting/gouraud-material/gouraud-material.d.ts.map +1 -1
  41. package/dist/modules/lighting/gouraud-material/gouraud-material.js +2 -0
  42. package/dist/modules/lighting/gouraud-material/gouraud-shaders-glsl.d.ts +2 -2
  43. package/dist/modules/lighting/gouraud-material/gouraud-shaders-glsl.d.ts.map +1 -1
  44. package/dist/modules/lighting/gouraud-material/gouraud-shaders-glsl.js +79 -53
  45. package/dist/modules/lighting/lights/lighting-uniforms-glsl.d.ts +1 -1
  46. package/dist/modules/lighting/lights/lighting-uniforms-glsl.d.ts.map +1 -1
  47. package/dist/modules/lighting/lights/lighting-uniforms-glsl.js +38 -23
  48. package/dist/modules/lighting/lights/lighting-uniforms-wgsl.d.ts +2 -0
  49. package/dist/modules/lighting/lights/lighting-uniforms-wgsl.d.ts.map +1 -0
  50. package/dist/modules/lighting/lights/lighting-uniforms-wgsl.js +56 -0
  51. package/dist/modules/lighting/lights/lighting.d.ts +5 -4
  52. package/dist/modules/lighting/lights/lighting.d.ts.map +1 -1
  53. package/dist/modules/lighting/lights/lighting.js +7 -5
  54. package/dist/modules/lighting/no-material/dirlight.d.ts +8 -8
  55. package/dist/modules/lighting/no-material/dirlight.d.ts.map +1 -1
  56. package/dist/modules/lighting/no-material/dirlight.js +34 -20
  57. package/dist/modules/lighting/pbr-material/pbr-fragment-glsl.d.ts +1 -1
  58. package/dist/modules/lighting/pbr-material/pbr-fragment-glsl.d.ts.map +1 -1
  59. package/dist/modules/lighting/pbr-material/pbr-fragment-glsl.js +304 -181
  60. package/dist/modules/lighting/pbr-material/pbr-material.d.ts +72 -69
  61. package/dist/modules/lighting/pbr-material/pbr-material.d.ts.map +1 -1
  62. package/dist/modules/lighting/pbr-material/pbr-material.js +4 -2
  63. package/dist/modules/lighting/pbr-material/pbr-uniforms-glsl.d.ts +1 -1
  64. package/dist/modules/lighting/pbr-material/pbr-uniforms-glsl.d.ts.map +1 -1
  65. package/dist/modules/lighting/pbr-material/pbr-uniforms-glsl.js +38 -20
  66. package/dist/modules/lighting/pbr-material/pbr-vertex-glsl.d.ts +1 -1
  67. package/dist/modules/lighting/pbr-material/pbr-vertex-glsl.d.ts.map +1 -1
  68. package/dist/modules/lighting/pbr-material/pbr-vertex-glsl.js +21 -16
  69. package/dist/modules/lighting/phong-material/phong-material.d.ts +14 -12
  70. package/dist/modules/lighting/phong-material/phong-material.d.ts.map +1 -1
  71. package/dist/modules/lighting/phong-material/phong-material.js +5 -3
  72. package/dist/modules/lighting/phong-material/phong-shaders-glsl.d.ts +2 -2
  73. package/dist/modules/lighting/phong-material/phong-shaders-glsl.d.ts.map +1 -1
  74. package/dist/modules/lighting/phong-material/phong-shaders-glsl.js +82 -54
  75. package/dist/modules/lighting/phong-material/phong-shaders-wgsl.d.ts +41 -0
  76. package/dist/modules/lighting/phong-material/phong-shaders-wgsl.d.ts.map +1 -0
  77. package/dist/modules/lighting/phong-material/phong-shaders-wgsl.js +130 -0
  78. package/dist/modules/math/fp32/fp32.d.ts.map +1 -1
  79. package/dist/modules/math/fp32/fp32.js +127 -100
  80. package/dist/modules/math/fp64/fp64-utils.d.ts +3 -3
  81. package/dist/modules/math/fp64/fp64-utils.d.ts.map +1 -1
  82. package/dist/modules/math/random/random.d.ts +1 -1
  83. package/dist/modules/math/random/random.d.ts.map +1 -1
  84. package/dist/modules/math/random/random.js +3 -3
  85. package/dist/modules/module-injectors.d.ts +2 -2
  86. package/dist/modules/module-injectors.d.ts.map +1 -1
  87. package/dist/modules/module-injectors.js +13 -10
  88. package/dist/modules/postprocessing/image-adjust-filters/brightnesscontrast.d.ts +4 -1
  89. package/dist/modules/postprocessing/image-adjust-filters/brightnesscontrast.d.ts.map +1 -1
  90. package/dist/modules/postprocessing/image-adjust-filters/brightnesscontrast.js +16 -12
  91. package/dist/modules/postprocessing/image-adjust-filters/denoise.d.ts +4 -1
  92. package/dist/modules/postprocessing/image-adjust-filters/denoise.d.ts.map +1 -1
  93. package/dist/modules/postprocessing/image-adjust-filters/denoise.js +21 -17
  94. package/dist/modules/postprocessing/image-adjust-filters/huesaturation.d.ts +5 -2
  95. package/dist/modules/postprocessing/image-adjust-filters/huesaturation.d.ts.map +1 -1
  96. package/dist/modules/postprocessing/image-adjust-filters/huesaturation.js +29 -22
  97. package/dist/modules/postprocessing/image-adjust-filters/noise.d.ts +4 -1
  98. package/dist/modules/postprocessing/image-adjust-filters/noise.d.ts.map +1 -1
  99. package/dist/modules/postprocessing/image-adjust-filters/noise.js +14 -10
  100. package/dist/modules/postprocessing/image-adjust-filters/sepia.d.ts +4 -1
  101. package/dist/modules/postprocessing/image-adjust-filters/sepia.d.ts.map +1 -1
  102. package/dist/modules/postprocessing/image-adjust-filters/sepia.js +17 -12
  103. package/dist/modules/postprocessing/image-adjust-filters/vibrance.d.ts +4 -1
  104. package/dist/modules/postprocessing/image-adjust-filters/vibrance.d.ts.map +1 -1
  105. package/dist/modules/postprocessing/image-adjust-filters/vibrance.js +12 -9
  106. package/dist/modules/postprocessing/image-adjust-filters/vignette.d.ts +4 -1
  107. package/dist/modules/postprocessing/image-adjust-filters/vignette.d.ts.map +1 -1
  108. package/dist/modules/postprocessing/image-adjust-filters/vignette.js +13 -10
  109. package/dist/modules/postprocessing/image-blur-filters/tiltshift.d.ts +5 -2
  110. package/dist/modules/postprocessing/image-blur-filters/tiltshift.d.ts.map +1 -1
  111. package/dist/modules/postprocessing/image-blur-filters/tiltshift.js +40 -26
  112. package/dist/modules/postprocessing/image-blur-filters/triangleblur.d.ts +5 -2
  113. package/dist/modules/postprocessing/image-blur-filters/triangleblur.d.ts.map +1 -1
  114. package/dist/modules/postprocessing/image-blur-filters/triangleblur.js +32 -19
  115. package/dist/modules/postprocessing/image-blur-filters/zoomblur.d.ts +4 -1
  116. package/dist/modules/postprocessing/image-blur-filters/zoomblur.d.ts.map +1 -1
  117. package/dist/modules/postprocessing/image-blur-filters/zoomblur.js +2 -0
  118. package/dist/modules/postprocessing/image-fun-filters/colorhalftone.d.ts +4 -1
  119. package/dist/modules/postprocessing/image-fun-filters/colorhalftone.d.ts.map +1 -1
  120. package/dist/modules/postprocessing/image-fun-filters/colorhalftone.js +31 -27
  121. package/dist/modules/postprocessing/image-fun-filters/dotscreen.d.ts +4 -1
  122. package/dist/modules/postprocessing/image-fun-filters/dotscreen.d.ts.map +1 -1
  123. package/dist/modules/postprocessing/image-fun-filters/dotscreen.js +19 -15
  124. package/dist/modules/postprocessing/image-fun-filters/edgework.d.ts +8 -5
  125. package/dist/modules/postprocessing/image-fun-filters/edgework.d.ts.map +1 -1
  126. package/dist/modules/postprocessing/image-fun-filters/edgework.js +53 -42
  127. package/dist/modules/postprocessing/image-fun-filters/hexagonalpixelate.d.ts +4 -1
  128. package/dist/modules/postprocessing/image-fun-filters/hexagonalpixelate.d.ts.map +1 -1
  129. package/dist/modules/postprocessing/image-fun-filters/hexagonalpixelate.js +41 -33
  130. package/dist/modules/postprocessing/image-fun-filters/ink.d.ts +4 -1
  131. package/dist/modules/postprocessing/image-fun-filters/ink.d.ts.map +1 -1
  132. package/dist/modules/postprocessing/image-fun-filters/ink.js +27 -25
  133. package/dist/modules/postprocessing/image-fun-filters/magnify.d.ts +2 -1
  134. package/dist/modules/postprocessing/image-fun-filters/magnify.d.ts.map +1 -1
  135. package/dist/modules/postprocessing/image-fun-filters/magnify.js +17 -16
  136. package/dist/modules/postprocessing/image-warp-filters/bulgepinch.d.ts +5 -2
  137. package/dist/modules/postprocessing/image-warp-filters/bulgepinch.d.ts.map +1 -1
  138. package/dist/modules/postprocessing/image-warp-filters/bulgepinch.js +24 -20
  139. package/dist/modules/postprocessing/image-warp-filters/swirl.d.ts +5 -2
  140. package/dist/modules/postprocessing/image-warp-filters/swirl.d.ts.map +1 -1
  141. package/dist/modules/postprocessing/image-warp-filters/swirl.js +26 -22
  142. package/dist/modules/postprocessing/image-warp-filters/warp.d.ts +2 -1
  143. package/dist/modules/postprocessing/image-warp-filters/warp.d.ts.map +1 -1
  144. package/dist/modules/postprocessing/image-warp-filters/warp.js +9 -8
  145. package/dist/modules-webgl1/geometry/geometry.d.ts.map +1 -1
  146. package/dist/modules-webgl1/geometry/geometry.js +12 -8
  147. package/dist/modules-webgl1/lighting/dirlight/dirlight.d.ts +2 -2
  148. package/dist/modules-webgl1/lighting/dirlight/dirlight.d.ts.map +1 -1
  149. package/dist/modules-webgl1/lighting/dirlight/dirlight.js +8 -5
  150. package/dist/modules-webgl1/lighting/lights/lights-glsl.d.ts +1 -1
  151. package/dist/modules-webgl1/lighting/lights/lights-glsl.d.ts.map +1 -1
  152. package/dist/modules-webgl1/lighting/lights/lights-glsl.js +19 -11
  153. package/dist/modules-webgl1/lighting/pbr/pbr-fragment-glsl.d.ts +1 -1
  154. package/dist/modules-webgl1/lighting/pbr/pbr-fragment-glsl.d.ts.map +1 -1
  155. package/dist/modules-webgl1/lighting/pbr/pbr-fragment-glsl.js +268 -157
  156. package/dist/modules-webgl1/lighting/pbr/pbr-vertex-glsl.d.ts +1 -1
  157. package/dist/modules-webgl1/lighting/pbr/pbr-vertex-glsl.d.ts.map +1 -1
  158. package/dist/modules-webgl1/lighting/pbr/pbr-vertex-glsl.js +16 -12
  159. package/dist/modules-webgl1/lighting/phong-lighting/phong-lighting-glsl.d.ts +1 -1
  160. package/dist/modules-webgl1/lighting/phong-lighting/phong-lighting-glsl.d.ts.map +1 -1
  161. package/dist/modules-webgl1/lighting/phong-lighting/phong-lighting-glsl.js +64 -55
  162. package/dist/modules-webgl1/math/fp64/fp64-arithmetic-glsl.d.ts +1 -1
  163. package/dist/modules-webgl1/math/fp64/fp64-arithmetic-glsl.d.ts.map +1 -1
  164. package/dist/modules-webgl1/math/fp64/fp64-arithmetic-glsl.js +113 -81
  165. package/dist/modules-webgl1/math/fp64/fp64-functions-glsl.d.ts +1 -1
  166. package/dist/modules-webgl1/math/fp64/fp64-functions-glsl.d.ts.map +1 -1
  167. package/dist/modules-webgl1/math/fp64/fp64-functions-glsl.js +588 -433
  168. package/dist/modules-webgl1/project/project.d.ts +2 -2
  169. package/dist/modules-webgl1/project/project.d.ts.map +1 -1
  170. package/dist/modules-webgl1/project/project.js +5 -4
  171. package/package.json +5 -5
  172. package/src/index.ts +78 -32
  173. package/src/lib/glsl-utils/shader-utils.ts +1 -3
  174. package/src/lib/shader-assembler.ts +7 -8
  175. package/src/lib/shader-assembly/assemble-shaders.ts +55 -22
  176. package/src/lib/shader-assembly/platform-defines.ts +5 -6
  177. package/src/lib/shader-module/shader-module-dependencies.ts +111 -0
  178. package/src/lib/shader-module/shader-module.ts +134 -19
  179. package/src/lib/shader-module/shader-pass.ts +3 -2
  180. package/src/module-injectors.ts +2 -4
  181. package/src/modules/engine/geometry/geometry.ts +2 -3
  182. package/src/modules/engine/picking/picking.ts +12 -10
  183. package/src/modules/engine/project/project.ts +2 -3
  184. package/src/modules/engine/transform/transform.ts +1 -3
  185. package/src/modules/lighting/gouraud-material/gouraud-material.ts +4 -0
  186. package/src/modules/lighting/gouraud-material/gouraud-shaders-glsl.ts +2 -4
  187. package/src/modules/lighting/lights/lighting-uniforms-glsl.ts +1 -3
  188. package/src/modules/lighting/lights/lighting-uniforms-wgsl.ts +57 -0
  189. package/src/modules/lighting/lights/lighting.ts +9 -5
  190. package/src/modules/lighting/no-material/dirlight.ts +30 -19
  191. package/src/modules/lighting/pbr-material/pbr-fragment-glsl.ts +1 -2
  192. package/src/modules/lighting/pbr-material/pbr-material.ts +44 -38
  193. package/src/modules/lighting/pbr-material/pbr-uniforms-glsl.ts +1 -3
  194. package/src/modules/lighting/pbr-material/pbr-vertex-glsl.ts +1 -3
  195. package/src/modules/lighting/phong-material/phong-material.ts +6 -3
  196. package/src/modules/lighting/phong-material/phong-shaders-glsl.ts +2 -4
  197. package/src/modules/lighting/phong-material/phong-shaders-wgsl.ts +132 -0
  198. package/src/modules/math/fp32/fp32.ts +1 -2
  199. package/src/modules/math/fp64/fp64-utils.ts +3 -3
  200. package/src/modules/math/random/random.ts +1 -2
  201. package/src/modules/module-injectors.ts +2 -4
  202. package/src/modules/postprocessing/image-adjust-filters/brightnesscontrast.ts +6 -2
  203. package/src/modules/postprocessing/image-adjust-filters/denoise.ts +7 -3
  204. package/src/modules/postprocessing/image-adjust-filters/huesaturation.ts +9 -4
  205. package/src/modules/postprocessing/image-adjust-filters/noise.ts +6 -2
  206. package/src/modules/postprocessing/image-adjust-filters/sepia.ts +6 -2
  207. package/src/modules/postprocessing/image-adjust-filters/vibrance.ts +5 -2
  208. package/src/modules/postprocessing/image-adjust-filters/vignette.ts +11 -4
  209. package/src/modules/postprocessing/image-blur-filters/tiltshift.ts +6 -2
  210. package/src/modules/postprocessing/image-blur-filters/triangleblur.ts +6 -2
  211. package/src/modules/postprocessing/image-blur-filters/zoomblur.ts +5 -0
  212. package/src/modules/postprocessing/image-fun-filters/colorhalftone.ts +6 -2
  213. package/src/modules/postprocessing/image-fun-filters/dotscreen.ts +6 -2
  214. package/src/modules/postprocessing/image-fun-filters/edgework.ts +10 -4
  215. package/src/modules/postprocessing/image-fun-filters/hexagonalpixelate.ts +6 -2
  216. package/src/modules/postprocessing/image-fun-filters/ink.ts +8 -3
  217. package/src/modules/postprocessing/image-fun-filters/magnify.ts +3 -2
  218. package/src/modules/postprocessing/image-warp-filters/bulgepinch.ts +6 -2
  219. package/src/modules/postprocessing/image-warp-filters/swirl.ts +8 -2
  220. package/src/modules/postprocessing/image-warp-filters/warp.ts +3 -2
  221. package/src/modules-webgl1/geometry/geometry.ts +2 -3
  222. package/src/modules-webgl1/lighting/dirlight/dirlight.ts +3 -4
  223. package/src/modules-webgl1/lighting/lights/lights-glsl.ts +1 -3
  224. package/src/modules-webgl1/lighting/pbr/pbr-fragment-glsl.ts +1 -2
  225. package/src/modules-webgl1/lighting/pbr/pbr-vertex-glsl.ts +1 -3
  226. package/src/modules-webgl1/lighting/phong-lighting/phong-lighting-glsl.ts +1 -3
  227. package/src/modules-webgl1/math/fp64/fp64-arithmetic-glsl.ts +1 -3
  228. package/src/modules-webgl1/math/fp64/fp64-functions-glsl.ts +1 -3
  229. package/src/modules-webgl1/project/project.ts +1 -2
  230. package/dist/lib/glsl-utils/highlight.d.ts +0 -6
  231. package/dist/lib/glsl-utils/highlight.d.ts.map +0 -1
  232. package/dist/lib/glsl-utils/highlight.js +0 -9
  233. package/dist/lib/shader-assembly/resolve-modules.d.ts +0 -40
  234. package/dist/lib/shader-assembly/resolve-modules.d.ts.map +0 -1
  235. package/dist/lib/shader-module/normalize-shader-module.d.ts +0 -3
  236. package/dist/lib/shader-module/normalize-shader-module.d.ts.map +0 -1
  237. package/dist/lib/shader-module/normalize-shader-module.js +0 -14
  238. package/dist/lib/shader-module/shader-module-instance.d.ts +0 -28
  239. package/dist/lib/shader-module/shader-module-instance.d.ts.map +0 -1
  240. package/dist/lib/shader-module/shader-module-instance.js +0 -126
  241. package/src/lib/glsl-utils/highlight.ts +0 -11
  242. package/src/lib/shader-assembly/resolve-modules.ts +0 -81
  243. package/src/lib/shader-module/normalize-shader-module.ts +0 -17
  244. package/src/lib/shader-module/shader-module-instance.ts +0 -167
@@ -6,31 +6,49 @@
6
6
  // This fragment shader defines a reference implementation for Physically Based Shading of
7
7
  // a microfacet surface material defined by a glTF model.
8
8
  // TODO - better do the checks outside of shader
9
- import { glsl } from "../../../lib/glsl-utils/highlight.js";
10
- export const fs = `\
9
+ export const fs = /* glsl */ `\
11
10
  precision highp float;
11
+
12
12
  uniform Projection {
13
- uniform vec3 u_Camera;
13
+ // Projection
14
+ uniform vec3 u_Camera;
14
15
  };
16
+
15
17
  uniform pbrMaterial {
16
- bool unlit;
17
- bool baseColorMapEnabled;
18
- vec4 baseColorFactor;
19
- bool normalMapEnabled;
20
- float normalScale;
21
- bool emissiveMapEnabled;
22
- vec3 emissiveFactor;
23
- vec2 metallicRoughnessValues;
24
- bool metallicRoughnessMapEnabled;
25
- bool occlusionMapEnabled;
26
- float occlusionStrength;
27
- bool alphaCutoffEnabled;
28
- float alphaCutoff;
29
- bool IBLenabled;
30
- vec2 scaleIBLAmbient;
31
- vec4 scaleDiffBaseMR;
32
- vec4 scaleFGDSpec;
18
+ // Material is unlit
19
+ bool unlit;
20
+
21
+ // Base color map
22
+ bool baseColorMapEnabled;
23
+ vec4 baseColorFactor;
24
+
25
+ bool normalMapEnabled;
26
+ float normalScale; // #ifdef HAS_NORMALMAP
27
+
28
+ bool emissiveMapEnabled;
29
+ vec3 emissiveFactor; // #ifdef HAS_EMISSIVEMAP
30
+
31
+ vec2 metallicRoughnessValues;
32
+ bool metallicRoughnessMapEnabled;
33
+
34
+ bool occlusionMapEnabled;
35
+ float occlusionStrength; // #ifdef HAS_OCCLUSIONMAP
36
+
37
+ bool alphaCutoffEnabled;
38
+ float alphaCutoff; // #ifdef ALPHA_CUTOFF
39
+
40
+ // IBL
41
+ bool IBLenabled;
42
+ vec2 scaleIBLAmbient; // #ifdef USE_IBL
43
+
44
+ // debugging flags used for shader output of intermediate PBR variables
45
+ // #ifdef PBR_DEBUG
46
+ vec4 scaleDiffBaseMR;
47
+ vec4 scaleFGDSpec;
48
+ // #endif
33
49
  } u_pbrMaterial;
50
+
51
+ // Samplers
34
52
  #ifdef HAS_BASECOLORMAP
35
53
  uniform sampler2D u_BaseColorSampler;
36
54
  #endif
@@ -51,8 +69,12 @@ uniform samplerCube u_DiffuseEnvSampler;
51
69
  uniform samplerCube u_SpecularEnvSampler;
52
70
  uniform sampler2D u_brdfLUT;
53
71
  #endif
72
+
73
+ // Inputs from vertex shader
74
+
54
75
  varying vec3 pbr_vPosition;
55
76
  varying vec2 pbr_vUV;
77
+
56
78
  #ifdef HAS_NORMALS
57
79
  #ifdef HAS_TANGENTS
58
80
  varying mat3 pbr_vTBN;
@@ -60,233 +82,334 @@ varying mat3 pbr_vTBN;
60
82
  varying vec3 pbr_vNormal;
61
83
  #endif
62
84
  #endif
85
+
86
+ // Encapsulate the various inputs used by the various functions in the shading equation
87
+ // We store values in this struct to simplify the integration of alternative implementations
88
+ // of the shading terms, outlined in the Readme.MD Appendix.
63
89
  struct PBRInfo {
64
- float NdotL;
65
- float NdotV;
66
- float NdotH;
67
- float LdotH;
68
- float VdotH;
69
- float perceptualRoughness;
70
- float metalness;
71
- vec3 reflectance0;
72
- vec3 reflectance90;
73
- float alphaRoughness;
74
- vec3 diffuseColor;
75
- vec3 specularColor;
76
- vec3 n;
77
- vec3 v;
90
+ float NdotL; // cos angle between normal and light direction
91
+ float NdotV; // cos angle between normal and view direction
92
+ float NdotH; // cos angle between normal and half vector
93
+ float LdotH; // cos angle between light direction and half vector
94
+ float VdotH; // cos angle between view direction and half vector
95
+ float perceptualRoughness; // roughness value, as authored by the model creator (input to shader)
96
+ float metalness; // metallic value at the surface
97
+ vec3 reflectance0; // full reflectance color (normal incidence angle)
98
+ vec3 reflectance90; // reflectance color at grazing angle
99
+ float alphaRoughness; // roughness mapped to a more linear change in the roughness (proposed by [2])
100
+ vec3 diffuseColor; // color contribution from diffuse lighting
101
+ vec3 specularColor; // color contribution from specular lighting
102
+ vec3 n; // normal at surface point
103
+ vec3 v; // vector from surface point to camera
78
104
  };
105
+
79
106
  const float M_PI = 3.141592653589793;
80
107
  const float c_MinRoughness = 0.04;
108
+
81
109
  vec4 SRGBtoLINEAR(vec4 srgbIn)
82
110
  {
83
111
  #ifdef MANUAL_SRGB
84
112
  #ifdef SRGB_FAST_APPROXIMATION
85
- vec3 linOut = pow(srgbIn.xyz,vec3(2.2));
86
- #else
87
- vec3 bLess = step(vec3(0.04045),srgbIn.xyz);
88
- vec3 linOut = mix( srgbIn.xyz/vec3(12.92), pow((srgbIn.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), bLess );
89
- #endif
90
- return vec4(linOut,srgbIn.w);;
91
- #else
92
- return srgbIn;
93
- #endif
113
+ vec3 linOut = pow(srgbIn.xyz,vec3(2.2));
114
+ #else // SRGB_FAST_APPROXIMATION
115
+ vec3 bLess = step(vec3(0.04045),srgbIn.xyz);
116
+ vec3 linOut = mix( srgbIn.xyz/vec3(12.92), pow((srgbIn.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), bLess );
117
+ #endif //SRGB_FAST_APPROXIMATION
118
+ return vec4(linOut,srgbIn.w);;
119
+ #else //MANUAL_SRGB
120
+ return srgbIn;
121
+ #endif //MANUAL_SRGB
94
122
  }
123
+
124
+ // Find the normal for this fragment, pulling either from a predefined normal map
125
+ // or from the interpolated mesh normal and tangent attributes.
95
126
  vec3 getNormal()
96
127
  {
128
+ // Retrieve the tangent space matrix
97
129
  #ifndef HAS_TANGENTS
98
- vec3 pos_dx = dFdx(pbr_vPosition);
99
- vec3 pos_dy = dFdy(pbr_vPosition);
100
- vec3 tex_dx = dFdx(vec3(pbr_vUV, 0.0));
101
- vec3 tex_dy = dFdy(vec3(pbr_vUV, 0.0));
102
- vec3 t = (tex_dy.t * pos_dx - tex_dx.t * pos_dy) / (tex_dx.s * tex_dy.t - tex_dy.s * tex_dx.t);
130
+ vec3 pos_dx = dFdx(pbr_vPosition);
131
+ vec3 pos_dy = dFdy(pbr_vPosition);
132
+ vec3 tex_dx = dFdx(vec3(pbr_vUV, 0.0));
133
+ vec3 tex_dy = dFdy(vec3(pbr_vUV, 0.0));
134
+ vec3 t = (tex_dy.t * pos_dx - tex_dx.t * pos_dy) / (tex_dx.s * tex_dy.t - tex_dy.s * tex_dx.t);
135
+
103
136
  #ifdef HAS_NORMALS
104
- vec3 ng = normalize(pbr_vNormal);
137
+ vec3 ng = normalize(pbr_vNormal);
105
138
  #else
106
- vec3 ng = cross(pos_dx, pos_dy);
139
+ vec3 ng = cross(pos_dx, pos_dy);
107
140
  #endif
108
- t = normalize(t - ng * dot(ng, t));
109
- vec3 b = normalize(cross(ng, t));
110
- mat3 tbn = mat3(t, b, ng);
111
- #else
112
- mat3 tbn = pbr_vTBN;
141
+
142
+ t = normalize(t - ng * dot(ng, t));
143
+ vec3 b = normalize(cross(ng, t));
144
+ mat3 tbn = mat3(t, b, ng);
145
+ #else // HAS_TANGENTS
146
+ mat3 tbn = pbr_vTBN;
113
147
  #endif
148
+
114
149
  #ifdef HAS_NORMALMAP
115
- vec3 n = texture2D(u_NormalSampler, pbr_vUV).rgb;
116
- n = normalize(tbn * ((2.0 * n - 1.0) * vec3(u_pbrMaterial.normalScale, u_pbrMaterial.normalScale, 1.0)));
150
+ vec3 n = texture2D(u_NormalSampler, pbr_vUV).rgb;
151
+ n = normalize(tbn * ((2.0 * n - 1.0) * vec3(u_pbrMaterial.normalScale, u_pbrMaterial.normalScale, 1.0)));
117
152
  #else
118
- vec3 n = normalize(tbn[2].xyz);
153
+ // The tbn matrix is linearly interpolated, so we need to re-normalize
154
+ vec3 n = normalize(tbn[2].xyz);
119
155
  #endif
120
- return n;
156
+
157
+ return n;
121
158
  }
159
+
160
+ // Calculation of the lighting contribution from an optional Image Based Light source.
161
+ // Precomputed Environment Maps are required uniform inputs and are computed as outlined in [1].
162
+ // See our README.md on Environment Maps [3] for additional discussion.
122
163
  #ifdef USE_IBL
123
164
  vec3 getIBLContribution(PBRInfo pbrInfo, vec3 n, vec3 reflection)
124
165
  {
125
- float mipCount = 9.0;
126
- float lod = (pbrInfo.perceptualRoughness * mipCount);
127
- vec3 brdf = SRGBtoLINEAR(texture2D(u_brdfLUT,
128
- vec2(pbrInfo.NdotV, 1.0 - pbrInfo.perceptualRoughness))).rgb;
129
- vec3 diffuseLight = SRGBtoLINEAR(textureCube(u_DiffuseEnvSampler, n)).rgb;
166
+ float mipCount = 9.0; // resolution of 512x512
167
+ float lod = (pbrInfo.perceptualRoughness * mipCount);
168
+ // retrieve a scale and bias to F0. See [1], Figure 3
169
+ vec3 brdf = SRGBtoLINEAR(texture2D(u_brdfLUT,
170
+ vec2(pbrInfo.NdotV, 1.0 - pbrInfo.perceptualRoughness))).rgb;
171
+ vec3 diffuseLight = SRGBtoLINEAR(textureCube(u_DiffuseEnvSampler, n)).rgb;
172
+
130
173
  #ifdef USE_TEX_LOD
131
- vec3 specularLight = SRGBtoLINEAR(textureCubeLod(u_SpecularEnvSampler, reflection, lod)).rgb;
174
+ vec3 specularLight = SRGBtoLINEAR(textureCubeLod(u_SpecularEnvSampler, reflection, lod)).rgb;
132
175
  #else
133
- vec3 specularLight = SRGBtoLINEAR(textureCube(u_SpecularEnvSampler, reflection)).rgb;
176
+ vec3 specularLight = SRGBtoLINEAR(textureCube(u_SpecularEnvSampler, reflection)).rgb;
134
177
  #endif
135
- vec3 diffuse = diffuseLight * pbrInfo.diffuseColor;
136
- vec3 specular = specularLight * (pbrInfo.specularColor * brdf.x + brdf.y);
137
- diffuse *= u_pbrMaterial.scaleIBLAmbient.x;
138
- specular *= u_pbrMaterial.scaleIBLAmbient.y;
139
- return diffuse + specular;
178
+
179
+ vec3 diffuse = diffuseLight * pbrInfo.diffuseColor;
180
+ vec3 specular = specularLight * (pbrInfo.specularColor * brdf.x + brdf.y);
181
+
182
+ // For presentation, this allows us to disable IBL terms
183
+ diffuse *= u_pbrMaterial.scaleIBLAmbient.x;
184
+ specular *= u_pbrMaterial.scaleIBLAmbient.y;
185
+
186
+ return diffuse + specular;
140
187
  }
141
188
  #endif
189
+
190
+ // Basic Lambertian diffuse
191
+ // Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog
192
+ // See also [1], Equation 1
142
193
  vec3 diffuse(PBRInfo pbrInfo)
143
194
  {
144
- return pbrInfo.diffuseColor / M_PI;
195
+ return pbrInfo.diffuseColor / M_PI;
145
196
  }
197
+
198
+ // The following equation models the Fresnel reflectance term of the spec equation (aka F())
199
+ // Implementation of fresnel from [4], Equation 15
146
200
  vec3 specularReflection(PBRInfo pbrInfo)
147
201
  {
148
- return pbrInfo.reflectance0 +
149
- (pbrInfo.reflectance90 - pbrInfo.reflectance0) *
150
- pow(clamp(1.0 - pbrInfo.VdotH, 0.0, 1.0), 5.0);
202
+ return pbrInfo.reflectance0 +
203
+ (pbrInfo.reflectance90 - pbrInfo.reflectance0) *
204
+ pow(clamp(1.0 - pbrInfo.VdotH, 0.0, 1.0), 5.0);
151
205
  }
206
+
207
+ // This calculates the specular geometric attenuation (aka G()),
208
+ // where rougher material will reflect less light back to the viewer.
209
+ // This implementation is based on [1] Equation 4, and we adopt their modifications to
210
+ // alphaRoughness as input as originally proposed in [2].
152
211
  float geometricOcclusion(PBRInfo pbrInfo)
153
212
  {
154
- float NdotL = pbrInfo.NdotL;
155
- float NdotV = pbrInfo.NdotV;
156
- float r = pbrInfo.alphaRoughness;
157
- float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));
158
- float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV)));
159
- return attenuationL * attenuationV;
213
+ float NdotL = pbrInfo.NdotL;
214
+ float NdotV = pbrInfo.NdotV;
215
+ float r = pbrInfo.alphaRoughness;
216
+
217
+ float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));
218
+ float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV)));
219
+ return attenuationL * attenuationV;
160
220
  }
221
+
222
+ // The following equation(s) model the distribution of microfacet normals across
223
+ // the area being drawn (aka D())
224
+ // Implementation from "Average Irregularity Representation of a Roughened Surface
225
+ // for Ray Reflection" by T. S. Trowbridge, and K. P. Reitz
226
+ // Follows the distribution function recommended in the SIGGRAPH 2013 course notes
227
+ // from EPIC Games [1], Equation 3.
161
228
  float microfacetDistribution(PBRInfo pbrInfo)
162
229
  {
163
- float roughnessSq = pbrInfo.alphaRoughness * pbrInfo.alphaRoughness;
164
- float f = (pbrInfo.NdotH * roughnessSq - pbrInfo.NdotH) * pbrInfo.NdotH + 1.0;
165
- return roughnessSq / (M_PI * f * f);
230
+ float roughnessSq = pbrInfo.alphaRoughness * pbrInfo.alphaRoughness;
231
+ float f = (pbrInfo.NdotH * roughnessSq - pbrInfo.NdotH) * pbrInfo.NdotH + 1.0;
232
+ return roughnessSq / (M_PI * f * f);
166
233
  }
234
+
167
235
  void PBRInfo_setAmbientLight(inout PBRInfo pbrInfo) {
168
- pbrInfo.NdotL = 1.0;
169
- pbrInfo.NdotH = 0.0;
170
- pbrInfo.LdotH = 0.0;
171
- pbrInfo.VdotH = 1.0;
236
+ pbrInfo.NdotL = 1.0;
237
+ pbrInfo.NdotH = 0.0;
238
+ pbrInfo.LdotH = 0.0;
239
+ pbrInfo.VdotH = 1.0;
172
240
  }
241
+
173
242
  void PBRInfo_setDirectionalLight(inout PBRInfo pbrInfo, vec3 lightDirection) {
174
- vec3 n = pbrInfo.n;
175
- vec3 v = pbrInfo.v;
176
- vec3 l = normalize(lightDirection);
177
- vec3 h = normalize(l+v);
178
- pbrInfo.NdotL = clamp(dot(n, l), 0.001, 1.0);
179
- pbrInfo.NdotH = clamp(dot(n, h), 0.0, 1.0);
180
- pbrInfo.LdotH = clamp(dot(l, h), 0.0, 1.0);
181
- pbrInfo.VdotH = clamp(dot(v, h), 0.0, 1.0);
243
+ vec3 n = pbrInfo.n;
244
+ vec3 v = pbrInfo.v;
245
+ vec3 l = normalize(lightDirection); // Vector from surface point to light
246
+ vec3 h = normalize(l+v); // Half vector between both l and v
247
+
248
+ pbrInfo.NdotL = clamp(dot(n, l), 0.001, 1.0);
249
+ pbrInfo.NdotH = clamp(dot(n, h), 0.0, 1.0);
250
+ pbrInfo.LdotH = clamp(dot(l, h), 0.0, 1.0);
251
+ pbrInfo.VdotH = clamp(dot(v, h), 0.0, 1.0);
182
252
  }
253
+
183
254
  void PBRInfo_setPointLight(inout PBRInfo pbrInfo, PointLight pointLight) {
184
- vec3 light_direction = normalize(pointLight.position - pbr_vPosition);
185
- PBRInfo_setDirectionalLight(pbrInfo, light_direction);
255
+ vec3 light_direction = normalize(pointLight.position - pbr_vPosition);
256
+ PBRInfo_setDirectionalLight(pbrInfo, light_direction);
186
257
  }
258
+
187
259
  vec3 calculateFinalColor(PBRInfo pbrInfo, vec3 lightColor) {
188
- vec3 F = specularReflection(pbrInfo);
189
- float G = geometricOcclusion(pbrInfo);
190
- float D = microfacetDistribution(pbrInfo);
191
- vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInfo);
192
- vec3 specContrib = F * G * D / (4.0 * pbrInfo.NdotL * pbrInfo.NdotV);
193
- return pbrInfo.NdotL * lightColor * (diffuseContrib + specContrib);
260
+ // Calculate the shading terms for the microfacet specular shading model
261
+ vec3 F = specularReflection(pbrInfo);
262
+ float G = geometricOcclusion(pbrInfo);
263
+ float D = microfacetDistribution(pbrInfo);
264
+
265
+ // Calculation of analytical lighting contribution
266
+ vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInfo);
267
+ vec3 specContrib = F * G * D / (4.0 * pbrInfo.NdotL * pbrInfo.NdotV);
268
+ // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)
269
+ return pbrInfo.NdotL * lightColor * (diffuseContrib + specContrib);
194
270
  }
271
+
195
272
  vec4 pbr_filterColor(vec4 colorUnused)
196
273
  {
274
+ // The albedo may be defined from a base texture or a flat color
197
275
  #ifdef HAS_BASECOLORMAP
198
- vec4 baseColor = SRGBtoLINEAR(texture2D(u_BaseColorSampler, pbr_vUV)) * u_pbrMaterial.baseColorFactor;
276
+ vec4 baseColor = SRGBtoLINEAR(texture2D(u_BaseColorSampler, pbr_vUV)) * u_pbrMaterial.baseColorFactor;
199
277
  #else
200
- vec4 baseColor = u_pbrMaterial.baseColorFactor;
278
+ vec4 baseColor = u_pbrMaterial.baseColorFactor;
201
279
  #endif
280
+
202
281
  #ifdef ALPHA_CUTOFF
203
- if (baseColor.a < u_pbrMaterial.alphaCutoff) {
204
- discard;
205
- }
282
+ if (baseColor.a < u_pbrMaterial.alphaCutoff) {
283
+ discard;
284
+ }
206
285
  #endif
207
- vec3 color = vec3(0, 0, 0);
208
- if(u_pbrMaterial.unlit){
209
- color.rgb = baseColor.rgb;
210
- }
211
- else{
212
- float perceptualRoughness = u_pbrMaterial.metallicRoughnessValues.y;
213
- float metallic = u_pbrMaterial.metallicRoughnessValues.x;
286
+
287
+ vec3 color = vec3(0, 0, 0);
288
+
289
+ if(u_pbrMaterial.unlit){
290
+ color.rgb = baseColor.rgb;
291
+ }
292
+ else{
293
+ // Metallic and Roughness material properties are packed together
294
+ // In glTF, these factors can be specified by fixed scalar values
295
+ // or from a metallic-roughness map
296
+ float perceptualRoughness = u_pbrMaterial.metallicRoughnessValues.y;
297
+ float metallic = u_pbrMaterial.metallicRoughnessValues.x;
214
298
  #ifdef HAS_METALROUGHNESSMAP
215
- vec4 mrSample = texture2D(u_MetallicRoughnessSampler, pbr_vUV);
216
- perceptualRoughness = mrSample.g * perceptualRoughness;
217
- metallic = mrSample.b * metallic;
299
+ // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.
300
+ // This layout intentionally reserves the 'r' channel for (optional) occlusion map data
301
+ vec4 mrSample = texture2D(u_MetallicRoughnessSampler, pbr_vUV);
302
+ perceptualRoughness = mrSample.g * perceptualRoughness;
303
+ metallic = mrSample.b * metallic;
218
304
  #endif
219
- perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0);
220
- metallic = clamp(metallic, 0.0, 1.0);
221
- float alphaRoughness = perceptualRoughness * perceptualRoughness;
222
- vec3 f0 = vec3(0.04);
223
- vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - f0);
224
- diffuseColor *= 1.0 - metallic;
225
- vec3 specularColor = mix(f0, baseColor.rgb, metallic);
226
- float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);
227
- float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);
228
- vec3 specularEnvironmentR0 = specularColor.rgb;
229
- vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;
230
- vec3 n = getNormal();
231
- vec3 v = normalize(u_Camera - pbr_vPosition);
232
- float NdotV = clamp(abs(dot(n, v)), 0.001, 1.0);
233
- vec3 reflection = -normalize(reflect(v, n));
234
- PBRInfo pbrInfo = PBRInfo(
235
- 0.0,
236
- NdotV,
237
- 0.0,
238
- 0.0,
239
- 0.0,
240
- perceptualRoughness,
241
- metallic,
242
- specularEnvironmentR0,
243
- specularEnvironmentR90,
244
- alphaRoughness,
245
- diffuseColor,
246
- specularColor,
247
- n,
248
- v
249
- );
305
+ perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0);
306
+ metallic = clamp(metallic, 0.0, 1.0);
307
+ // Roughness is authored as perceptual roughness; as is convention,
308
+ // convert to material roughness by squaring the perceptual roughness [2].
309
+ float alphaRoughness = perceptualRoughness * perceptualRoughness;
310
+
311
+ vec3 f0 = vec3(0.04);
312
+ vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - f0);
313
+ diffuseColor *= 1.0 - metallic;
314
+ vec3 specularColor = mix(f0, baseColor.rgb, metallic);
315
+
316
+ // Compute reflectance.
317
+ float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);
318
+
319
+ // For typical incident reflectance range (between 4% to 100%) set the grazing
320
+ // reflectance to 100% for typical fresnel effect.
321
+ // For very low reflectance range on highly diffuse objects (below 4%),
322
+ // incrementally reduce grazing reflecance to 0%.
323
+ float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);
324
+ vec3 specularEnvironmentR0 = specularColor.rgb;
325
+ vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;
326
+
327
+ vec3 n = getNormal(); // normal at surface point
328
+ vec3 v = normalize(u_Camera - pbr_vPosition); // Vector from surface point to camera
329
+
330
+ float NdotV = clamp(abs(dot(n, v)), 0.001, 1.0);
331
+ vec3 reflection = -normalize(reflect(v, n));
332
+
333
+ PBRInfo pbrInfo = PBRInfo(
334
+ 0.0, // NdotL
335
+ NdotV,
336
+ 0.0, // NdotH
337
+ 0.0, // LdotH
338
+ 0.0, // VdotH
339
+ perceptualRoughness,
340
+ metallic,
341
+ specularEnvironmentR0,
342
+ specularEnvironmentR90,
343
+ alphaRoughness,
344
+ diffuseColor,
345
+ specularColor,
346
+ n,
347
+ v
348
+ );
349
+
250
350
  #ifdef USE_LIGHTS
251
- PBRInfo_setAmbientLight(pbrInfo);
252
- color += calculateFinalColor(pbrInfo, lighting_uAmbientLight.color);
253
- for(int i = 0; i < lighting_uDirectionalLightCount; i++) {
254
- if (i < lighting_uDirectionalLightCount) {
255
- PBRInfo_setDirectionalLight(pbrInfo, lighting_uDirectionalLight[i].direction);
256
- color += calculateFinalColor(pbrInfo, lighting_uDirectionalLight[i].color);
257
- }
258
- }
259
- for(int i = 0; i < lighting_uPointLightCount; i++) {
260
- if (i < lighting_uPointLightCount) {
261
- PBRInfo_setPointLight(pbrInfo, lighting_uPointLight[i]);
262
- float attenuation = getPointLightAttenuation(lighting_uPointLight[i], distance(lighting_uPointLight[i].position, pbr_vPosition));
263
- color += calculateFinalColor(pbrInfo, lighting_uPointLight[i].color / attenuation);
264
- }
265
- }
351
+ // Apply ambient light
352
+ PBRInfo_setAmbientLight(pbrInfo);
353
+ color += calculateFinalColor(pbrInfo, lighting_uAmbientLight.color);
354
+
355
+ // Apply directional light
356
+ for(int i = 0; i < lighting_uDirectionalLightCount; i++) {
357
+ if (i < lighting_uDirectionalLightCount) {
358
+ PBRInfo_setDirectionalLight(pbrInfo, lighting_uDirectionalLight[i].direction);
359
+ color += calculateFinalColor(pbrInfo, lighting_uDirectionalLight[i].color);
360
+ }
361
+ }
362
+
363
+ // Apply point light
364
+ for(int i = 0; i < lighting_uPointLightCount; i++) {
365
+ if (i < lighting_uPointLightCount) {
366
+ PBRInfo_setPointLight(pbrInfo, lighting_uPointLight[i]);
367
+ float attenuation = getPointLightAttenuation(lighting_uPointLight[i], distance(lighting_uPointLight[i].position, pbr_vPosition));
368
+ color += calculateFinalColor(pbrInfo, lighting_uPointLight[i].color / attenuation);
369
+ }
370
+ }
266
371
  #endif
372
+
373
+ // Calculate lighting contribution from image based lighting source (IBL)
267
374
  #ifdef USE_IBL
268
- if (u_pbrMateral.IBLEnabled) {
269
- color += getIBLContribution(pbrInfo, n, reflection);
270
- }
375
+ if (u_pbrMateral.IBLEnabled) {
376
+ color += getIBLContribution(pbrInfo, n, reflection);
377
+ }
271
378
  #endif
379
+
380
+ // Apply optional PBR terms for additional (optional) shading
272
381
  #ifdef HAS_OCCLUSIONMAP
273
- if (u_pbrMaterial.occlusionMapEnabled) {
274
- float ao = texture2D(u_OcclusionSampler, pbr_vUV).r;
275
- color = mix(color, color * ao, u_pbrMaterial.occlusionStrength);
276
- }
382
+ if (u_pbrMaterial.occlusionMapEnabled) {
383
+ float ao = texture2D(u_OcclusionSampler, pbr_vUV).r;
384
+ color = mix(color, color * ao, u_pbrMaterial.occlusionStrength);
385
+ }
277
386
  #endif
387
+
278
388
  #ifdef HAS_EMISSIVEMAP
279
- if (u_pbrMaterial.emmissiveMapEnabled) {
280
- vec3 emissive = SRGBtoLINEAR(texture2D(u_EmissiveSampler, pbr_vUV)).rgb * u_pbrMaterial.emissiveFactor;
281
- color += emissive;
282
- }
389
+ if (u_pbrMaterial.emmissiveMapEnabled) {
390
+ vec3 emissive = SRGBtoLINEAR(texture2D(u_EmissiveSampler, pbr_vUV)).rgb * u_pbrMaterial.emissiveFactor;
391
+ color += emissive;
392
+ }
283
393
  #endif
394
+
395
+ // This section uses mix to override final color for reference app visualization
396
+ // of various parameters in the lighting equation.
284
397
  #ifdef PBR_DEBUG
285
- color = mix(color, baseColor.rgb, u_pbrMaterial.scaleDiffBaseMR.y);
286
- color = mix(color, vec3(metallic), u_pbrMaterial.scaleDiffBaseMR.z);
287
- color = mix(color, vec3(perceptualRoughness), u_pbrMaterial.scaleDiffBaseMR.w);
398
+ // TODO: Figure out how to debug multiple lights
399
+
400
+ // color = mix(color, F, u_ScaleFGDSpec.x);
401
+ // color = mix(color, vec3(G), u_ScaleFGDSpec.y);
402
+ // color = mix(color, vec3(D), u_ScaleFGDSpec.z);
403
+ // color = mix(color, specContrib, u_ScaleFGDSpec.w);
404
+
405
+ // color = mix(color, diffuseContrib, u_ScaleDiffBaseMR.x);
406
+ color = mix(color, baseColor.rgb, u_pbrMaterial.scaleDiffBaseMR.y);
407
+ color = mix(color, vec3(metallic), u_pbrMaterial.scaleDiffBaseMR.z);
408
+ color = mix(color, vec3(perceptualRoughness), u_pbrMaterial.scaleDiffBaseMR.w);
288
409
  #endif
289
- }
290
- return vec4(pow(color,vec3(1.0/2.2)), baseColor.a);
410
+
411
+ }
412
+
413
+ return vec4(pow(color,vec3(1.0/2.2)), baseColor.a);
291
414
  }
292
415
  `;