@luma.gl/shadertools 9.2.6 → 9.3.0-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/dist/dist.dev.js +5388 -6438
  2. package/dist/dist.min.js +2264 -322
  3. package/dist/index.cjs +3601 -566
  4. package/dist/index.cjs.map +4 -4
  5. package/dist/index.d.ts +12 -2
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +5 -2
  8. package/dist/index.js.map +1 -1
  9. package/dist/lib/glsl-utils/shader-utils.js +4 -4
  10. package/dist/lib/glsl-utils/shader-utils.js.map +1 -1
  11. package/dist/lib/preprocessor/preprocessor.d.ts.map +1 -1
  12. package/dist/lib/preprocessor/preprocessor.js +35 -8
  13. package/dist/lib/preprocessor/preprocessor.js.map +1 -1
  14. package/dist/lib/shader-assembler.d.ts +10 -0
  15. package/dist/lib/shader-assembler.d.ts.map +1 -1
  16. package/dist/lib/shader-assembler.js +20 -3
  17. package/dist/lib/shader-assembler.js.map +1 -1
  18. package/dist/lib/shader-assembly/assemble-shaders.d.ts +23 -2
  19. package/dist/lib/shader-assembly/assemble-shaders.d.ts.map +1 -1
  20. package/dist/lib/shader-assembly/assemble-shaders.js +265 -12
  21. package/dist/lib/shader-assembly/assemble-shaders.js.map +1 -1
  22. package/dist/lib/shader-assembly/wgsl-binding-debug.d.ts +37 -0
  23. package/dist/lib/shader-assembly/wgsl-binding-debug.d.ts.map +1 -0
  24. package/dist/lib/shader-assembly/wgsl-binding-debug.js +144 -0
  25. package/dist/lib/shader-assembly/wgsl-binding-debug.js.map +1 -0
  26. package/dist/lib/shader-assembly/wgsl-binding-scan.d.ts +19 -0
  27. package/dist/lib/shader-assembly/wgsl-binding-scan.d.ts.map +1 -0
  28. package/dist/lib/shader-assembly/wgsl-binding-scan.js +151 -0
  29. package/dist/lib/shader-assembly/wgsl-binding-scan.js.map +1 -0
  30. package/dist/lib/shader-generator/glsl/generate-glsl.js +7 -4
  31. package/dist/lib/shader-generator/glsl/generate-glsl.js.map +1 -1
  32. package/dist/lib/shader-generator/wgsl/generate-wgsl.d.ts.map +1 -1
  33. package/dist/lib/shader-generator/wgsl/generate-wgsl.js +3 -0
  34. package/dist/lib/shader-generator/wgsl/generate-wgsl.js.map +1 -1
  35. package/dist/lib/shader-module/shader-module-uniform-layout.d.ts +91 -0
  36. package/dist/lib/shader-module/shader-module-uniform-layout.d.ts.map +1 -0
  37. package/dist/lib/shader-module/shader-module-uniform-layout.js +253 -0
  38. package/dist/lib/shader-module/shader-module-uniform-layout.js.map +1 -0
  39. package/dist/lib/shader-module/shader-module.d.ts +12 -6
  40. package/dist/lib/shader-module/shader-module.d.ts.map +1 -1
  41. package/dist/lib/shader-module/shader-module.js.map +1 -1
  42. package/dist/lib/utils/assert.d.ts.map +1 -1
  43. package/dist/lib/utils/assert.js +3 -1
  44. package/dist/lib/utils/assert.js.map +1 -1
  45. package/dist/lib/utils/uniform-types.d.ts +11 -7
  46. package/dist/lib/utils/uniform-types.d.ts.map +1 -1
  47. package/dist/modules/engine/picking/picking.d.ts +5 -2
  48. package/dist/modules/engine/picking/picking.d.ts.map +1 -1
  49. package/dist/modules/engine/picking/picking.js +5 -2
  50. package/dist/modules/engine/picking/picking.js.map +1 -1
  51. package/dist/modules/engine/project/project.d.ts +1 -1
  52. package/dist/modules/engine/project/project.js +1 -1
  53. package/dist/modules/engine/skin/skin.d.ts +30 -0
  54. package/dist/modules/engine/skin/skin.d.ts.map +1 -0
  55. package/dist/modules/engine/skin/skin.js +86 -0
  56. package/dist/modules/engine/skin/skin.js.map +1 -0
  57. package/dist/modules/lighting/gouraud-material/gouraud-material.d.ts +1 -0
  58. package/dist/modules/lighting/gouraud-material/gouraud-material.d.ts.map +1 -1
  59. package/dist/modules/lighting/gouraud-material/gouraud-material.js +3 -0
  60. package/dist/modules/lighting/gouraud-material/gouraud-material.js.map +1 -1
  61. package/dist/modules/lighting/ibl/ibl.d.ts +26 -0
  62. package/dist/modules/lighting/ibl/ibl.d.ts.map +1 -0
  63. package/dist/modules/lighting/ibl/ibl.js +33 -0
  64. package/dist/modules/lighting/ibl/ibl.js.map +1 -0
  65. package/dist/modules/lighting/lambert-material/lambert-material.d.ts +10 -0
  66. package/dist/modules/lighting/lambert-material/lambert-material.d.ts.map +1 -0
  67. package/dist/modules/lighting/lambert-material/lambert-material.js +33 -0
  68. package/dist/modules/lighting/lambert-material/lambert-material.js.map +1 -0
  69. package/dist/modules/lighting/lambert-material/lambert-shaders-glsl.d.ts +3 -0
  70. package/dist/modules/lighting/lambert-material/lambert-shaders-glsl.d.ts.map +1 -0
  71. package/dist/modules/lighting/lambert-material/lambert-shaders-glsl.js +60 -0
  72. package/dist/modules/lighting/lambert-material/lambert-shaders-glsl.js.map +1 -0
  73. package/dist/modules/lighting/lambert-material/lambert-shaders-wgsl.d.ts +2 -0
  74. package/dist/modules/lighting/lambert-material/lambert-shaders-wgsl.d.ts.map +1 -0
  75. package/dist/modules/lighting/lambert-material/lambert-shaders-wgsl.js +73 -0
  76. package/dist/modules/lighting/lambert-material/lambert-shaders-wgsl.js.map +1 -0
  77. package/dist/modules/lighting/lights/lighting-glsl.d.ts +1 -1
  78. package/dist/modules/lighting/lights/lighting-glsl.d.ts.map +1 -1
  79. package/dist/modules/lighting/lights/lighting-glsl.js +44 -38
  80. package/dist/modules/lighting/lights/lighting-glsl.js.map +1 -1
  81. package/dist/modules/lighting/lights/lighting-wgsl.d.ts +1 -1
  82. package/dist/modules/lighting/lights/lighting-wgsl.d.ts.map +1 -1
  83. package/dist/modules/lighting/lights/lighting-wgsl.js +46 -18
  84. package/dist/modules/lighting/lights/lighting-wgsl.js.map +1 -1
  85. package/dist/modules/lighting/lights/lighting.d.ts +104 -62
  86. package/dist/modules/lighting/lights/lighting.d.ts.map +1 -1
  87. package/dist/modules/lighting/lights/lighting.js +107 -68
  88. package/dist/modules/lighting/lights/lighting.js.map +1 -1
  89. package/dist/modules/lighting/no-material/dirlight.d.ts +8 -3
  90. package/dist/modules/lighting/no-material/dirlight.d.ts.map +1 -1
  91. package/dist/modules/lighting/no-material/dirlight.js +5 -3
  92. package/dist/modules/lighting/no-material/dirlight.js.map +1 -1
  93. package/dist/modules/lighting/pbr-material/pbr-material-glsl.d.ts +2 -2
  94. package/dist/modules/lighting/pbr-material/pbr-material-glsl.d.ts.map +1 -1
  95. package/dist/modules/lighting/pbr-material/pbr-material-glsl.js +694 -38
  96. package/dist/modules/lighting/pbr-material/pbr-material-glsl.js.map +1 -1
  97. package/dist/modules/lighting/pbr-material/pbr-material-wgsl.d.ts +2 -2
  98. package/dist/modules/lighting/pbr-material/pbr-material-wgsl.d.ts.map +1 -1
  99. package/dist/modules/lighting/pbr-material/pbr-material-wgsl.js +956 -109
  100. package/dist/modules/lighting/pbr-material/pbr-material-wgsl.js.map +1 -1
  101. package/dist/modules/lighting/pbr-material/pbr-material.d.ts +242 -43
  102. package/dist/modules/lighting/pbr-material/pbr-material.d.ts.map +1 -1
  103. package/dist/modules/lighting/pbr-material/pbr-material.js +178 -2
  104. package/dist/modules/lighting/pbr-material/pbr-material.js.map +1 -1
  105. package/dist/modules/lighting/pbr-material/pbr-projection.d.ts.map +1 -1
  106. package/dist/modules/lighting/pbr-material/pbr-projection.js +14 -2
  107. package/dist/modules/lighting/pbr-material/pbr-projection.js.map +1 -1
  108. package/dist/modules/lighting/pbr-material/pbr-scene.d.ts +40 -0
  109. package/dist/modules/lighting/pbr-material/pbr-scene.d.ts.map +1 -0
  110. package/dist/modules/lighting/pbr-material/pbr-scene.js +67 -0
  111. package/dist/modules/lighting/pbr-material/pbr-scene.js.map +1 -0
  112. package/dist/modules/lighting/phong-material/phong-material.d.ts +1 -0
  113. package/dist/modules/lighting/phong-material/phong-material.d.ts.map +1 -1
  114. package/dist/modules/lighting/phong-material/phong-material.js +4 -0
  115. package/dist/modules/lighting/phong-material/phong-material.js.map +1 -1
  116. package/dist/modules/lighting/phong-material/phong-shaders-glsl.d.ts +2 -2
  117. package/dist/modules/lighting/phong-material/phong-shaders-glsl.d.ts.map +1 -1
  118. package/dist/modules/lighting/phong-material/phong-shaders-glsl.js +17 -6
  119. package/dist/modules/lighting/phong-material/phong-shaders-glsl.js.map +1 -1
  120. package/dist/modules/lighting/phong-material/phong-shaders-wgsl.d.ts +1 -40
  121. package/dist/modules/lighting/phong-material/phong-shaders-wgsl.d.ts.map +1 -1
  122. package/dist/modules/lighting/phong-material/phong-shaders-wgsl.js +71 -76
  123. package/dist/modules/lighting/phong-material/phong-shaders-wgsl.js.map +1 -1
  124. package/dist/modules/math/fp64/fp64-arithmetic-glsl.d.ts +1 -1
  125. package/dist/modules/math/fp64/fp64-arithmetic-glsl.d.ts.map +1 -1
  126. package/dist/modules/math/fp64/fp64-arithmetic-glsl.js +42 -11
  127. package/dist/modules/math/fp64/fp64-arithmetic-glsl.js.map +1 -1
  128. package/dist/modules/math/fp64/fp64-arithmetic-wgsl.d.ts +2 -0
  129. package/dist/modules/math/fp64/fp64-arithmetic-wgsl.d.ts.map +1 -0
  130. package/dist/modules/math/fp64/fp64-arithmetic-wgsl.js +212 -0
  131. package/dist/modules/math/fp64/fp64-arithmetic-wgsl.js.map +1 -0
  132. package/dist/modules/math/fp64/fp64.d.ts +1 -0
  133. package/dist/modules/math/fp64/fp64.d.ts.map +1 -1
  134. package/dist/modules/math/fp64/fp64.js +8 -2
  135. package/dist/modules/math/fp64/fp64.js.map +1 -1
  136. package/dist/modules/math/random/random.d.ts +1 -1
  137. package/dist/modules/math/random/random.d.ts.map +1 -1
  138. package/dist/modules/math/random/random.js +2 -3
  139. package/dist/modules/math/random/random.js.map +1 -1
  140. package/package.json +4 -5
  141. package/src/index.ts +37 -6
  142. package/src/lib/glsl-utils/shader-utils.ts +4 -4
  143. package/src/lib/preprocessor/preprocessor.ts +44 -8
  144. package/src/lib/shader-assembler.ts +25 -3
  145. package/src/lib/shader-assembly/assemble-shaders.ts +506 -13
  146. package/src/lib/shader-assembly/wgsl-binding-debug.ts +227 -0
  147. package/src/lib/shader-assembly/wgsl-binding-scan.ts +228 -0
  148. package/src/lib/shader-generator/glsl/generate-glsl.ts +11 -5
  149. package/src/lib/shader-generator/wgsl/generate-wgsl.ts +6 -0
  150. package/src/lib/shader-module/shader-module-uniform-layout.ts +420 -0
  151. package/src/lib/shader-module/shader-module.ts +17 -7
  152. package/src/lib/utils/assert.ts +3 -1
  153. package/src/lib/utils/uniform-types.ts +24 -9
  154. package/src/modules/engine/picking/picking.ts +5 -2
  155. package/src/modules/engine/project/project.ts +1 -1
  156. package/src/modules/engine/skin/skin.ts +114 -0
  157. package/src/modules/lighting/gouraud-material/gouraud-material.ts +4 -0
  158. package/src/modules/lighting/ibl/ibl.ts +44 -0
  159. package/src/modules/lighting/lambert-material/lambert-material.ts +42 -0
  160. package/src/modules/lighting/lambert-material/lambert-shaders-glsl.ts +61 -0
  161. package/src/modules/lighting/lambert-material/lambert-shaders-wgsl.ts +73 -0
  162. package/src/modules/lighting/lights/lighting-glsl.ts +44 -38
  163. package/src/modules/lighting/lights/lighting-wgsl.ts +46 -18
  164. package/src/modules/lighting/lights/lighting.ts +198 -99
  165. package/src/modules/lighting/no-material/dirlight.ts +5 -3
  166. package/src/modules/lighting/pbr-material/pbr-material-glsl.ts +694 -38
  167. package/src/modules/lighting/pbr-material/pbr-material-wgsl.ts +956 -109
  168. package/src/modules/lighting/pbr-material/pbr-material.ts +294 -7
  169. package/src/modules/lighting/pbr-material/pbr-projection.ts +15 -2
  170. package/src/modules/lighting/pbr-material/pbr-scene.ts +91 -0
  171. package/src/modules/lighting/phong-material/phong-material.ts +5 -0
  172. package/src/modules/lighting/phong-material/phong-shaders-glsl.ts +17 -6
  173. package/src/modules/lighting/phong-material/phong-shaders-wgsl.ts +71 -77
  174. package/src/modules/math/fp64/fp64-arithmetic-glsl.ts +42 -11
  175. package/src/modules/math/fp64/fp64-arithmetic-wgsl.ts +212 -0
  176. package/src/modules/math/fp64/fp64.ts +9 -3
  177. package/src/modules/math/random/random.ts +2 -3
  178. package/dist/lib/wgsl/get-shader-layout-wgsl.d.ts +0 -8
  179. package/dist/lib/wgsl/get-shader-layout-wgsl.d.ts.map +0 -1
  180. package/dist/lib/wgsl/get-shader-layout-wgsl.js +0 -95
  181. package/dist/lib/wgsl/get-shader-layout-wgsl.js.map +0 -1
  182. package/src/lib/wgsl/get-shader-layout-wgsl.ts +0 -105
@@ -1,3 +1,3 @@
1
- export declare const vs = "out vec3 pbr_vPosition;\nout vec2 pbr_vUV;\n\n#ifdef HAS_NORMALS\n# ifdef HAS_TANGENTS\nout mat3 pbr_vTBN;\n# else\nout vec3 pbr_vNormal;\n# endif\n#endif\n\nvoid pbr_setPositionNormalTangentUV(vec4 position, vec4 normal, vec4 tangent, vec2 uv)\n{\n vec4 pos = pbrProjection.modelMatrix * position;\n pbr_vPosition = vec3(pos.xyz) / pos.w;\n\n#ifdef HAS_NORMALS\n#ifdef HAS_TANGENTS\n vec3 normalW = normalize(vec3(pbrProjection.normalMatrix * vec4(normal.xyz, 0.0)));\n vec3 tangentW = normalize(vec3(pbrProjection.modelMatrix * vec4(tangent.xyz, 0.0)));\n vec3 bitangentW = cross(normalW, tangentW) * tangent.w;\n pbr_vTBN = mat3(tangentW, bitangentW, normalW);\n#else // HAS_TANGENTS != 1\n pbr_vNormal = normalize(vec3(pbrProjection.modelMatrix * vec4(normal.xyz, 0.0)));\n#endif\n#endif\n\n#ifdef HAS_UV\n pbr_vUV = uv;\n#else\n pbr_vUV = vec2(0.,0.);\n#endif\n}\n";
2
- export declare const fs = "precision highp float;\n\nuniform pbrMaterialUniforms {\n // Material is unlit\n bool unlit;\n\n // Base color map\n bool baseColorMapEnabled;\n vec4 baseColorFactor;\n\n bool normalMapEnabled; \n float normalScale; // #ifdef HAS_NORMALMAP\n\n bool emissiveMapEnabled;\n vec3 emissiveFactor; // #ifdef HAS_EMISSIVEMAP\n\n vec2 metallicRoughnessValues;\n bool metallicRoughnessMapEnabled;\n\n bool occlusionMapEnabled;\n float occlusionStrength; // #ifdef HAS_OCCLUSIONMAP\n \n bool alphaCutoffEnabled;\n float alphaCutoff; // #ifdef ALPHA_CUTOFF\n \n // IBL\n bool IBLenabled;\n vec2 scaleIBLAmbient; // #ifdef USE_IBL\n \n // debugging flags used for shader output of intermediate PBR variables\n // #ifdef PBR_DEBUG\n vec4 scaleDiffBaseMR;\n vec4 scaleFGDSpec;\n // #endif\n} pbrMaterial;\n\n// Samplers\n#ifdef HAS_BASECOLORMAP\nuniform sampler2D pbr_baseColorSampler;\n#endif\n#ifdef HAS_NORMALMAP\nuniform sampler2D pbr_normalSampler;\n#endif\n#ifdef HAS_EMISSIVEMAP\nuniform sampler2D pbr_emissiveSampler;\n#endif\n#ifdef HAS_METALROUGHNESSMAP\nuniform sampler2D pbr_metallicRoughnessSampler;\n#endif\n#ifdef HAS_OCCLUSIONMAP\nuniform sampler2D pbr_occlusionSampler;\n#endif\n#ifdef USE_IBL\nuniform samplerCube pbr_diffuseEnvSampler;\nuniform samplerCube pbr_specularEnvSampler;\nuniform sampler2D pbr_brdfLUT;\n#endif\n\n// Inputs from vertex shader\n\nin vec3 pbr_vPosition;\nin vec2 pbr_vUV;\n\n#ifdef HAS_NORMALS\n#ifdef HAS_TANGENTS\nin mat3 pbr_vTBN;\n#else\nin vec3 pbr_vNormal;\n#endif\n#endif\n\n// Encapsulate the various inputs used by the various functions in the shading equation\n// We store values in this struct to simplify the integration of alternative implementations\n// of the shading terms, outlined in the Readme.MD Appendix.\nstruct PBRInfo {\n float NdotL; // cos angle between normal and light direction\n float NdotV; // cos angle between normal and view direction\n float NdotH; // cos angle between normal and half vector\n float LdotH; // cos angle between light direction and half vector\n float VdotH; // cos angle between view direction and half vector\n float perceptualRoughness; // roughness value, as authored by the model creator (input to shader)\n float metalness; // metallic value at the surface\n vec3 reflectance0; // full reflectance color (normal incidence angle)\n vec3 reflectance90; // reflectance color at grazing angle\n float alphaRoughness; // roughness mapped to a more linear change in the roughness (proposed by [2])\n vec3 diffuseColor; // color contribution from diffuse lighting\n vec3 specularColor; // color contribution from specular lighting\n vec3 n; // normal at surface point\n vec3 v; // vector from surface point to camera\n};\n\nconst float M_PI = 3.141592653589793;\nconst float c_MinRoughness = 0.04;\n\nvec4 SRGBtoLINEAR(vec4 srgbIn)\n{\n#ifdef MANUAL_SRGB\n#ifdef SRGB_FAST_APPROXIMATION\n vec3 linOut = pow(srgbIn.xyz,vec3(2.2));\n#else // SRGB_FAST_APPROXIMATION\n vec3 bLess = step(vec3(0.04045),srgbIn.xyz);\n vec3 linOut = mix( srgbIn.xyz/vec3(12.92), pow((srgbIn.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), bLess );\n#endif //SRGB_FAST_APPROXIMATION\n return vec4(linOut,srgbIn.w);;\n#else //MANUAL_SRGB\n return srgbIn;\n#endif //MANUAL_SRGB\n}\n\n// Find the normal for this fragment, pulling either from a predefined normal map\n// or from the interpolated mesh normal and tangent attributes.\nvec3 getNormal()\n{\n // Retrieve the tangent space matrix\n#ifndef HAS_TANGENTS\n vec3 pos_dx = dFdx(pbr_vPosition);\n vec3 pos_dy = dFdy(pbr_vPosition);\n vec3 tex_dx = dFdx(vec3(pbr_vUV, 0.0));\n vec3 tex_dy = dFdy(vec3(pbr_vUV, 0.0));\n vec3 t = (tex_dy.t * pos_dx - tex_dx.t * pos_dy) / (tex_dx.s * tex_dy.t - tex_dy.s * tex_dx.t);\n\n#ifdef HAS_NORMALS\n vec3 ng = normalize(pbr_vNormal);\n#else\n vec3 ng = cross(pos_dx, pos_dy);\n#endif\n\n t = normalize(t - ng * dot(ng, t));\n vec3 b = normalize(cross(ng, t));\n mat3 tbn = mat3(t, b, ng);\n#else // HAS_TANGENTS\n mat3 tbn = pbr_vTBN;\n#endif\n\n#ifdef HAS_NORMALMAP\n vec3 n = texture(pbr_normalSampler, pbr_vUV).rgb;\n n = normalize(tbn * ((2.0 * n - 1.0) * vec3(pbrMaterial.normalScale, pbrMaterial.normalScale, 1.0)));\n#else\n // The tbn matrix is linearly interpolated, so we need to re-normalize\n vec3 n = normalize(tbn[2].xyz);\n#endif\n\n return n;\n}\n\n// Calculation of the lighting contribution from an optional Image Based Light source.\n// Precomputed Environment Maps are required uniform inputs and are computed as outlined in [1].\n// See our README.md on Environment Maps [3] for additional discussion.\n#ifdef USE_IBL\nvec3 getIBLContribution(PBRInfo pbrInfo, vec3 n, vec3 reflection)\n{\n float mipCount = 9.0; // resolution of 512x512\n float lod = (pbrInfo.perceptualRoughness * mipCount);\n // retrieve a scale and bias to F0. See [1], Figure 3\n vec3 brdf = SRGBtoLINEAR(texture(pbr_brdfLUT,\n vec2(pbrInfo.NdotV, 1.0 - pbrInfo.perceptualRoughness))).rgb;\n vec3 diffuseLight = SRGBtoLINEAR(texture(pbr_diffuseEnvSampler, n)).rgb;\n\n#ifdef USE_TEX_LOD\n vec3 specularLight = SRGBtoLINEAR(texture(pbr_specularEnvSampler, reflection, lod)).rgb;\n#else\n vec3 specularLight = SRGBtoLINEAR(texture(pbr_specularEnvSampler, reflection)).rgb;\n#endif\n\n vec3 diffuse = diffuseLight * pbrInfo.diffuseColor;\n vec3 specular = specularLight * (pbrInfo.specularColor * brdf.x + brdf.y);\n\n // For presentation, this allows us to disable IBL terms\n diffuse *= pbrMaterial.scaleIBLAmbient.x;\n specular *= pbrMaterial.scaleIBLAmbient.y;\n\n return diffuse + specular;\n}\n#endif\n\n// Basic Lambertian diffuse\n// Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog\n// See also [1], Equation 1\nvec3 diffuse(PBRInfo pbrInfo)\n{\n return pbrInfo.diffuseColor / M_PI;\n}\n\n// The following equation models the Fresnel reflectance term of the spec equation (aka F())\n// Implementation of fresnel from [4], Equation 15\nvec3 specularReflection(PBRInfo pbrInfo)\n{\n return pbrInfo.reflectance0 +\n (pbrInfo.reflectance90 - pbrInfo.reflectance0) *\n pow(clamp(1.0 - pbrInfo.VdotH, 0.0, 1.0), 5.0);\n}\n\n// This calculates the specular geometric attenuation (aka G()),\n// where rougher material will reflect less light back to the viewer.\n// This implementation is based on [1] Equation 4, and we adopt their modifications to\n// alphaRoughness as input as originally proposed in [2].\nfloat geometricOcclusion(PBRInfo pbrInfo)\n{\n float NdotL = pbrInfo.NdotL;\n float NdotV = pbrInfo.NdotV;\n float r = pbrInfo.alphaRoughness;\n\n float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));\n float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV)));\n return attenuationL * attenuationV;\n}\n\n// The following equation(s) model the distribution of microfacet normals across\n// the area being drawn (aka D())\n// Implementation from \"Average Irregularity Representation of a Roughened Surface\n// for Ray Reflection\" by T. S. Trowbridge, and K. P. Reitz\n// Follows the distribution function recommended in the SIGGRAPH 2013 course notes\n// from EPIC Games [1], Equation 3.\nfloat microfacetDistribution(PBRInfo pbrInfo)\n{\n float roughnessSq = pbrInfo.alphaRoughness * pbrInfo.alphaRoughness;\n float f = (pbrInfo.NdotH * roughnessSq - pbrInfo.NdotH) * pbrInfo.NdotH + 1.0;\n return roughnessSq / (M_PI * f * f);\n}\n\nvoid PBRInfo_setAmbientLight(inout PBRInfo pbrInfo) {\n pbrInfo.NdotL = 1.0;\n pbrInfo.NdotH = 0.0;\n pbrInfo.LdotH = 0.0;\n pbrInfo.VdotH = 1.0;\n}\n\nvoid PBRInfo_setDirectionalLight(inout PBRInfo pbrInfo, vec3 lightDirection) {\n vec3 n = pbrInfo.n;\n vec3 v = pbrInfo.v;\n vec3 l = normalize(lightDirection); // Vector from surface point to light\n vec3 h = normalize(l+v); // Half vector between both l and v\n\n pbrInfo.NdotL = clamp(dot(n, l), 0.001, 1.0);\n pbrInfo.NdotH = clamp(dot(n, h), 0.0, 1.0);\n pbrInfo.LdotH = clamp(dot(l, h), 0.0, 1.0);\n pbrInfo.VdotH = clamp(dot(v, h), 0.0, 1.0);\n}\n\nvoid PBRInfo_setPointLight(inout PBRInfo pbrInfo, PointLight pointLight) {\n vec3 light_direction = normalize(pointLight.position - pbr_vPosition);\n PBRInfo_setDirectionalLight(pbrInfo, light_direction);\n}\n\nvec3 calculateFinalColor(PBRInfo pbrInfo, vec3 lightColor) {\n // Calculate the shading terms for the microfacet specular shading model\n vec3 F = specularReflection(pbrInfo);\n float G = geometricOcclusion(pbrInfo);\n float D = microfacetDistribution(pbrInfo);\n\n // Calculation of analytical lighting contribution\n vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInfo);\n vec3 specContrib = F * G * D / (4.0 * pbrInfo.NdotL * pbrInfo.NdotV);\n // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)\n return pbrInfo.NdotL * lightColor * (diffuseContrib + specContrib);\n}\n\nvec4 pbr_filterColor(vec4 colorUnused)\n{\n // The albedo may be defined from a base texture or a flat color\n#ifdef HAS_BASECOLORMAP\n vec4 baseColor = SRGBtoLINEAR(texture(pbr_baseColorSampler, pbr_vUV)) * pbrMaterial.baseColorFactor;\n#else\n vec4 baseColor = pbrMaterial.baseColorFactor;\n#endif\n\n#ifdef ALPHA_CUTOFF\n if (baseColor.a < pbrMaterial.alphaCutoff) {\n discard;\n }\n#endif\n\n vec3 color = vec3(0, 0, 0);\n\n if(pbrMaterial.unlit){\n color.rgb = baseColor.rgb;\n }\n else{\n // Metallic and Roughness material properties are packed together\n // In glTF, these factors can be specified by fixed scalar values\n // or from a metallic-roughness map\n float perceptualRoughness = pbrMaterial.metallicRoughnessValues.y;\n float metallic = pbrMaterial.metallicRoughnessValues.x;\n#ifdef HAS_METALROUGHNESSMAP\n // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.\n // This layout intentionally reserves the 'r' channel for (optional) occlusion map data\n vec4 mrSample = texture(pbr_metallicRoughnessSampler, pbr_vUV);\n perceptualRoughness = mrSample.g * perceptualRoughness;\n metallic = mrSample.b * metallic;\n#endif\n perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0);\n metallic = clamp(metallic, 0.0, 1.0);\n // Roughness is authored as perceptual roughness; as is convention,\n // convert to material roughness by squaring the perceptual roughness [2].\n float alphaRoughness = perceptualRoughness * perceptualRoughness;\n\n vec3 f0 = vec3(0.04);\n vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - f0);\n diffuseColor *= 1.0 - metallic;\n vec3 specularColor = mix(f0, baseColor.rgb, metallic);\n\n // Compute reflectance.\n float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);\n\n // For typical incident reflectance range (between 4% to 100%) set the grazing\n // reflectance to 100% for typical fresnel effect.\n // For very low reflectance range on highly diffuse objects (below 4%),\n // incrementally reduce grazing reflecance to 0%.\n float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);\n vec3 specularEnvironmentR0 = specularColor.rgb;\n vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;\n\n vec3 n = getNormal(); // normal at surface point\n vec3 v = normalize(pbrProjection.camera - pbr_vPosition); // Vector from surface point to camera\n\n float NdotV = clamp(abs(dot(n, v)), 0.001, 1.0);\n vec3 reflection = -normalize(reflect(v, n));\n\n PBRInfo pbrInfo = PBRInfo(\n 0.0, // NdotL\n NdotV,\n 0.0, // NdotH\n 0.0, // LdotH\n 0.0, // VdotH\n perceptualRoughness,\n metallic,\n specularEnvironmentR0,\n specularEnvironmentR90,\n alphaRoughness,\n diffuseColor,\n specularColor,\n n,\n v\n );\n\n\n#ifdef USE_LIGHTS\n // Apply ambient light\n PBRInfo_setAmbientLight(pbrInfo);\n color += calculateFinalColor(pbrInfo, lighting.ambientColor);\n\n // Apply directional light\n for(int i = 0; i < lighting.directionalLightCount; i++) {\n if (i < lighting.directionalLightCount) {\n PBRInfo_setDirectionalLight(pbrInfo, lighting_getDirectionalLight(i).direction);\n color += calculateFinalColor(pbrInfo, lighting_getDirectionalLight(i).color);\n }\n }\n\n // Apply point light\n for(int i = 0; i < lighting.pointLightCount; i++) {\n if (i < lighting.pointLightCount) {\n PBRInfo_setPointLight(pbrInfo, lighting_getPointLight(i));\n float attenuation = getPointLightAttenuation(lighting_getPointLight(i), distance(lighting_getPointLight(i).position, pbr_vPosition));\n color += calculateFinalColor(pbrInfo, lighting_getPointLight(i).color / attenuation);\n }\n }\n#endif\n\n // Calculate lighting contribution from image based lighting source (IBL)\n#ifdef USE_IBL\n if (pbrMaterial.IBLenabled) {\n color += getIBLContribution(pbrInfo, n, reflection);\n }\n#endif\n\n // Apply optional PBR terms for additional (optional) shading\n#ifdef HAS_OCCLUSIONMAP\n if (pbrMaterial.occlusionMapEnabled) {\n float ao = texture(pbr_occlusionSampler, pbr_vUV).r;\n color = mix(color, color * ao, pbrMaterial.occlusionStrength);\n }\n#endif\n\n#ifdef HAS_EMISSIVEMAP\n if (pbrMaterial.emissiveMapEnabled) {\n vec3 emissive = SRGBtoLINEAR(texture(pbr_emissiveSampler, pbr_vUV)).rgb * pbrMaterial.emissiveFactor;\n color += emissive;\n }\n#endif\n\n // This section uses mix to override final color for reference app visualization\n // of various parameters in the lighting equation.\n#ifdef PBR_DEBUG\n // TODO: Figure out how to debug multiple lights\n\n // color = mix(color, F, pbr_scaleFGDSpec.x);\n // color = mix(color, vec3(G), pbr_scaleFGDSpec.y);\n // color = mix(color, vec3(D), pbr_scaleFGDSpec.z);\n // color = mix(color, specContrib, pbr_scaleFGDSpec.w);\n\n // color = mix(color, diffuseContrib, pbr_scaleDiffBaseMR.x);\n color = mix(color, baseColor.rgb, pbrMaterial.scaleDiffBaseMR.y);\n color = mix(color, vec3(metallic), pbrMaterial.scaleDiffBaseMR.z);\n color = mix(color, vec3(perceptualRoughness), pbrMaterial.scaleDiffBaseMR.w);\n#endif\n\n }\n\n return vec4(pow(color,vec3(1.0/2.2)), baseColor.a);\n}\n";
1
+ export declare const vs = "out vec3 pbr_vPosition;\nout vec2 pbr_vUV0;\nout vec2 pbr_vUV1;\n\n#ifdef HAS_NORMALS\n# ifdef HAS_TANGENTS\nout mat3 pbr_vTBN;\n# else\nout vec3 pbr_vNormal;\n# endif\n#endif\n\nvoid pbr_setPositionNormalTangentUV(\n vec4 position,\n vec4 normal,\n vec4 tangent,\n vec2 uv0,\n vec2 uv1\n)\n{\n vec4 pos = pbrProjection.modelMatrix * position;\n pbr_vPosition = vec3(pos.xyz) / pos.w;\n\n#ifdef HAS_NORMALS\n#ifdef HAS_TANGENTS\n vec3 normalW = normalize(vec3(pbrProjection.normalMatrix * vec4(normal.xyz, 0.0)));\n vec3 tangentW = normalize(vec3(pbrProjection.modelMatrix * vec4(tangent.xyz, 0.0)));\n vec3 bitangentW = cross(normalW, tangentW) * tangent.w;\n pbr_vTBN = mat3(tangentW, bitangentW, normalW);\n#else // HAS_TANGENTS != 1\n pbr_vNormal = normalize(vec3(pbrProjection.modelMatrix * vec4(normal.xyz, 0.0)));\n#endif\n#endif\n\n#ifdef HAS_UV\n pbr_vUV0 = uv0;\n#else\n pbr_vUV0 = vec2(0.,0.);\n#endif\n\n pbr_vUV1 = uv1;\n}\n";
2
+ export declare const fs = "precision highp float;\n\nlayout(std140) uniform pbrMaterialUniforms {\n // Material is unlit\n bool unlit;\n\n // Base color map\n bool baseColorMapEnabled;\n vec4 baseColorFactor;\n\n bool normalMapEnabled; \n float normalScale; // #ifdef HAS_NORMALMAP\n\n bool emissiveMapEnabled;\n vec3 emissiveFactor; // #ifdef HAS_EMISSIVEMAP\n\n vec2 metallicRoughnessValues;\n bool metallicRoughnessMapEnabled;\n\n bool occlusionMapEnabled;\n float occlusionStrength; // #ifdef HAS_OCCLUSIONMAP\n \n bool alphaCutoffEnabled;\n float alphaCutoff; // #ifdef ALPHA_CUTOFF\n\n vec3 specularColorFactor;\n float specularIntensityFactor;\n bool specularColorMapEnabled;\n bool specularIntensityMapEnabled;\n\n float ior;\n\n float transmissionFactor;\n bool transmissionMapEnabled;\n\n float thicknessFactor;\n float attenuationDistance;\n vec3 attenuationColor;\n\n float clearcoatFactor;\n float clearcoatRoughnessFactor;\n bool clearcoatMapEnabled;\n bool clearcoatRoughnessMapEnabled;\n\n vec3 sheenColorFactor;\n float sheenRoughnessFactor;\n bool sheenColorMapEnabled;\n bool sheenRoughnessMapEnabled;\n\n float iridescenceFactor;\n float iridescenceIor;\n vec2 iridescenceThicknessRange;\n bool iridescenceMapEnabled;\n\n float anisotropyStrength;\n float anisotropyRotation;\n vec2 anisotropyDirection;\n bool anisotropyMapEnabled;\n\n float emissiveStrength;\n \n // IBL\n bool IBLenabled;\n vec2 scaleIBLAmbient; // #ifdef USE_IBL\n \n // debugging flags used for shader output of intermediate PBR variables\n // #ifdef PBR_DEBUG\n vec4 scaleDiffBaseMR;\n vec4 scaleFGDSpec;\n // #endif\n\n int baseColorUVSet;\n mat3 baseColorUVTransform;\n int metallicRoughnessUVSet;\n mat3 metallicRoughnessUVTransform;\n int normalUVSet;\n mat3 normalUVTransform;\n int occlusionUVSet;\n mat3 occlusionUVTransform;\n int emissiveUVSet;\n mat3 emissiveUVTransform;\n int specularColorUVSet;\n mat3 specularColorUVTransform;\n int specularIntensityUVSet;\n mat3 specularIntensityUVTransform;\n int transmissionUVSet;\n mat3 transmissionUVTransform;\n int thicknessUVSet;\n mat3 thicknessUVTransform;\n int clearcoatUVSet;\n mat3 clearcoatUVTransform;\n int clearcoatRoughnessUVSet;\n mat3 clearcoatRoughnessUVTransform;\n int clearcoatNormalUVSet;\n mat3 clearcoatNormalUVTransform;\n int sheenColorUVSet;\n mat3 sheenColorUVTransform;\n int sheenRoughnessUVSet;\n mat3 sheenRoughnessUVTransform;\n int iridescenceUVSet;\n mat3 iridescenceUVTransform;\n int iridescenceThicknessUVSet;\n mat3 iridescenceThicknessUVTransform;\n int anisotropyUVSet;\n mat3 anisotropyUVTransform;\n} pbrMaterial;\n\n// Samplers\n#ifdef HAS_BASECOLORMAP\nuniform sampler2D pbr_baseColorSampler;\n#endif\n#ifdef HAS_NORMALMAP\nuniform sampler2D pbr_normalSampler;\n#endif\n#ifdef HAS_EMISSIVEMAP\nuniform sampler2D pbr_emissiveSampler;\n#endif\n#ifdef HAS_METALROUGHNESSMAP\nuniform sampler2D pbr_metallicRoughnessSampler;\n#endif\n#ifdef HAS_OCCLUSIONMAP\nuniform sampler2D pbr_occlusionSampler;\n#endif\n#ifdef HAS_SPECULARCOLORMAP\nuniform sampler2D pbr_specularColorSampler;\n#endif\n#ifdef HAS_SPECULARINTENSITYMAP\nuniform sampler2D pbr_specularIntensitySampler;\n#endif\n#ifdef HAS_TRANSMISSIONMAP\nuniform sampler2D pbr_transmissionSampler;\n#endif\n#ifdef HAS_THICKNESSMAP\nuniform sampler2D pbr_thicknessSampler;\n#endif\n#ifdef HAS_CLEARCOATMAP\nuniform sampler2D pbr_clearcoatSampler;\n#endif\n#ifdef HAS_CLEARCOATROUGHNESSMAP\nuniform sampler2D pbr_clearcoatRoughnessSampler;\n#endif\n#ifdef HAS_CLEARCOATNORMALMAP\nuniform sampler2D pbr_clearcoatNormalSampler;\n#endif\n#ifdef HAS_SHEENCOLORMAP\nuniform sampler2D pbr_sheenColorSampler;\n#endif\n#ifdef HAS_SHEENROUGHNESSMAP\nuniform sampler2D pbr_sheenRoughnessSampler;\n#endif\n#ifdef HAS_IRIDESCENCEMAP\nuniform sampler2D pbr_iridescenceSampler;\n#endif\n#ifdef HAS_IRIDESCENCETHICKNESSMAP\nuniform sampler2D pbr_iridescenceThicknessSampler;\n#endif\n#ifdef HAS_ANISOTROPYMAP\nuniform sampler2D pbr_anisotropySampler;\n#endif\n// Inputs from vertex shader\n\nin vec3 pbr_vPosition;\nin vec2 pbr_vUV0;\nin vec2 pbr_vUV1;\n\n#ifdef HAS_NORMALS\n#ifdef HAS_TANGENTS\nin mat3 pbr_vTBN;\n#else\nin vec3 pbr_vNormal;\n#endif\n#endif\n\n// Encapsulate the various inputs used by the various functions in the shading equation\n// We store values in this struct to simplify the integration of alternative implementations\n// of the shading terms, outlined in the Readme.MD Appendix.\nstruct PBRInfo {\n float NdotL; // cos angle between normal and light direction\n float NdotV; // cos angle between normal and view direction\n float NdotH; // cos angle between normal and half vector\n float LdotH; // cos angle between light direction and half vector\n float VdotH; // cos angle between view direction and half vector\n float perceptualRoughness; // roughness value, as authored by the model creator (input to shader)\n float metalness; // metallic value at the surface\n vec3 reflectance0; // full reflectance color (normal incidence angle)\n vec3 reflectance90; // reflectance color at grazing angle\n float alphaRoughness; // roughness mapped to a more linear change in the roughness (proposed by [2])\n vec3 diffuseColor; // color contribution from diffuse lighting\n vec3 specularColor; // color contribution from specular lighting\n vec3 n; // normal at surface point\n vec3 v; // vector from surface point to camera\n};\n\nconst float M_PI = 3.141592653589793;\nconst float c_MinRoughness = 0.04;\n\nvec3 calculateFinalColor(PBRInfo pbrInfo, vec3 lightColor);\n\nvec4 SRGBtoLINEAR(vec4 srgbIn)\n{\n#ifdef MANUAL_SRGB\n#ifdef SRGB_FAST_APPROXIMATION\n vec3 linOut = pow(srgbIn.xyz,vec3(2.2));\n#else // SRGB_FAST_APPROXIMATION\n vec3 bLess = step(vec3(0.04045),srgbIn.xyz);\n vec3 linOut = mix( srgbIn.xyz/vec3(12.92), pow((srgbIn.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), bLess );\n#endif //SRGB_FAST_APPROXIMATION\n return vec4(linOut,srgbIn.w);;\n#else //MANUAL_SRGB\n return srgbIn;\n#endif //MANUAL_SRGB\n}\n\nvec2 getMaterialUV(int uvSet, mat3 uvTransform)\n{\n vec2 baseUV = uvSet == 1 ? pbr_vUV1 : pbr_vUV0;\n return (uvTransform * vec3(baseUV, 1.0)).xy;\n}\n\n// Build the tangent basis from interpolated attributes or screen-space derivatives.\nmat3 getTBN(vec2 uv)\n{\n#ifndef HAS_TANGENTS\n vec3 pos_dx = dFdx(pbr_vPosition);\n vec3 pos_dy = dFdy(pbr_vPosition);\n vec3 tex_dx = dFdx(vec3(uv, 0.0));\n vec3 tex_dy = dFdy(vec3(uv, 0.0));\n vec3 t = (tex_dy.t * pos_dx - tex_dx.t * pos_dy) / (tex_dx.s * tex_dy.t - tex_dy.s * tex_dx.t);\n\n#ifdef HAS_NORMALS\n vec3 ng = normalize(pbr_vNormal);\n#else\n vec3 ng = cross(pos_dx, pos_dy);\n#endif\n\n t = normalize(t - ng * dot(ng, t));\n vec3 b = normalize(cross(ng, t));\n mat3 tbn = mat3(t, b, ng);\n#else // HAS_TANGENTS\n mat3 tbn = pbr_vTBN;\n#endif\n\n return tbn;\n}\n\n// Find the normal for this fragment, pulling either from a predefined normal map\n// or from the interpolated mesh normal and tangent attributes.\nvec3 getMappedNormal(sampler2D normalSampler, mat3 tbn, float normalScale, vec2 uv)\n{\n vec3 n = texture(normalSampler, uv).rgb;\n return normalize(tbn * ((2.0 * n - 1.0) * vec3(normalScale, normalScale, 1.0)));\n}\n\nvec3 getNormal(mat3 tbn, vec2 uv)\n{\n#ifdef HAS_NORMALMAP\n vec3 n = getMappedNormal(pbr_normalSampler, tbn, pbrMaterial.normalScale, uv);\n#else\n // The tbn matrix is linearly interpolated, so we need to re-normalize\n vec3 n = normalize(tbn[2].xyz);\n#endif\n\n return n;\n}\n\nvec3 getClearcoatNormal(mat3 tbn, vec3 baseNormal, vec2 uv)\n{\n#ifdef HAS_CLEARCOATNORMALMAP\n return getMappedNormal(pbr_clearcoatNormalSampler, tbn, 1.0, uv);\n#else\n return baseNormal;\n#endif\n}\n\n// Calculation of the lighting contribution from an optional Image Based Light source.\n// Precomputed Environment Maps are required uniform inputs and are computed as outlined in [1].\n// See our README.md on Environment Maps [3] for additional discussion.\n#ifdef USE_IBL\nvec3 getIBLContribution(PBRInfo pbrInfo, vec3 n, vec3 reflection)\n{\n float mipCount = 9.0; // resolution of 512x512\n float lod = (pbrInfo.perceptualRoughness * mipCount);\n // retrieve a scale and bias to F0. See [1], Figure 3\n vec3 brdf = SRGBtoLINEAR(texture(pbr_brdfLUT,\n vec2(pbrInfo.NdotV, 1.0 - pbrInfo.perceptualRoughness))).rgb;\n vec3 diffuseLight = SRGBtoLINEAR(texture(pbr_diffuseEnvSampler, n)).rgb;\n\n#ifdef USE_TEX_LOD\n vec3 specularLight = SRGBtoLINEAR(texture(pbr_specularEnvSampler, reflection, lod)).rgb;\n#else\n vec3 specularLight = SRGBtoLINEAR(texture(pbr_specularEnvSampler, reflection)).rgb;\n#endif\n\n vec3 diffuse = diffuseLight * pbrInfo.diffuseColor;\n vec3 specular = specularLight * (pbrInfo.specularColor * brdf.x + brdf.y);\n\n // For presentation, this allows us to disable IBL terms\n diffuse *= pbrMaterial.scaleIBLAmbient.x;\n specular *= pbrMaterial.scaleIBLAmbient.y;\n\n return diffuse + specular;\n}\n#endif\n\n// Basic Lambertian diffuse\n// Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog\n// See also [1], Equation 1\nvec3 diffuse(PBRInfo pbrInfo)\n{\n return pbrInfo.diffuseColor / M_PI;\n}\n\n// The following equation models the Fresnel reflectance term of the spec equation (aka F())\n// Implementation of fresnel from [4], Equation 15\nvec3 specularReflection(PBRInfo pbrInfo)\n{\n return pbrInfo.reflectance0 +\n (pbrInfo.reflectance90 - pbrInfo.reflectance0) *\n pow(clamp(1.0 - pbrInfo.VdotH, 0.0, 1.0), 5.0);\n}\n\n// This calculates the specular geometric attenuation (aka G()),\n// where rougher material will reflect less light back to the viewer.\n// This implementation is based on [1] Equation 4, and we adopt their modifications to\n// alphaRoughness as input as originally proposed in [2].\nfloat geometricOcclusion(PBRInfo pbrInfo)\n{\n float NdotL = pbrInfo.NdotL;\n float NdotV = pbrInfo.NdotV;\n float r = pbrInfo.alphaRoughness;\n\n float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));\n float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV)));\n return attenuationL * attenuationV;\n}\n\n// The following equation(s) model the distribution of microfacet normals across\n// the area being drawn (aka D())\n// Implementation from \"Average Irregularity Representation of a Roughened Surface\n// for Ray Reflection\" by T. S. Trowbridge, and K. P. Reitz\n// Follows the distribution function recommended in the SIGGRAPH 2013 course notes\n// from EPIC Games [1], Equation 3.\nfloat microfacetDistribution(PBRInfo pbrInfo)\n{\n float roughnessSq = pbrInfo.alphaRoughness * pbrInfo.alphaRoughness;\n float f = (pbrInfo.NdotH * roughnessSq - pbrInfo.NdotH) * pbrInfo.NdotH + 1.0;\n return roughnessSq / (M_PI * f * f);\n}\n\nfloat maxComponent(vec3 value)\n{\n return max(max(value.r, value.g), value.b);\n}\n\nfloat getDielectricF0(float ior)\n{\n float clampedIor = max(ior, 1.0);\n float ratio = (clampedIor - 1.0) / (clampedIor + 1.0);\n return ratio * ratio;\n}\n\nvec2 normalizeDirection(vec2 direction)\n{\n float directionLength = length(direction);\n return directionLength > 0.0001 ? direction / directionLength : vec2(1.0, 0.0);\n}\n\nvec2 rotateDirection(vec2 direction, float rotation)\n{\n float s = sin(rotation);\n float c = cos(rotation);\n return vec2(direction.x * c - direction.y * s, direction.x * s + direction.y * c);\n}\n\nvec3 getIridescenceTint(float iridescence, float thickness, float NdotV)\n{\n if (iridescence <= 0.0) {\n return vec3(1.0);\n }\n\n float phase = 0.015 * thickness * pbrMaterial.iridescenceIor + (1.0 - NdotV) * 6.0;\n vec3 thinFilmTint =\n 0.5 + 0.5 * cos(vec3(phase, phase + 2.0943951, phase + 4.1887902));\n return mix(vec3(1.0), thinFilmTint, iridescence);\n}\n\nvec3 getVolumeAttenuation(float thickness)\n{\n if (thickness <= 0.0) {\n return vec3(1.0);\n }\n\n vec3 attenuationCoefficient =\n -log(max(pbrMaterial.attenuationColor, vec3(0.0001))) /\n max(pbrMaterial.attenuationDistance, 0.0001);\n return exp(-attenuationCoefficient * thickness);\n}\n\nPBRInfo createClearcoatPBRInfo(PBRInfo basePBRInfo, vec3 clearcoatNormal, float clearcoatRoughness)\n{\n float perceptualRoughness = clamp(clearcoatRoughness, c_MinRoughness, 1.0);\n float alphaRoughness = perceptualRoughness * perceptualRoughness;\n float NdotV = clamp(abs(dot(clearcoatNormal, basePBRInfo.v)), 0.001, 1.0);\n\n return PBRInfo(\n basePBRInfo.NdotL,\n NdotV,\n basePBRInfo.NdotH,\n basePBRInfo.LdotH,\n basePBRInfo.VdotH,\n perceptualRoughness,\n 0.0,\n vec3(0.04),\n vec3(1.0),\n alphaRoughness,\n vec3(0.0),\n vec3(0.04),\n clearcoatNormal,\n basePBRInfo.v\n );\n}\n\nvec3 calculateClearcoatContribution(\n PBRInfo pbrInfo,\n vec3 lightColor,\n vec3 clearcoatNormal,\n float clearcoatFactor,\n float clearcoatRoughness\n) {\n if (clearcoatFactor <= 0.0) {\n return vec3(0.0);\n }\n\n PBRInfo clearcoatPBRInfo = createClearcoatPBRInfo(pbrInfo, clearcoatNormal, clearcoatRoughness);\n return calculateFinalColor(clearcoatPBRInfo, lightColor) * clearcoatFactor;\n}\n\n#ifdef USE_IBL\nvec3 calculateClearcoatIBLContribution(\n PBRInfo pbrInfo,\n vec3 clearcoatNormal,\n vec3 reflection,\n float clearcoatFactor,\n float clearcoatRoughness\n) {\n if (clearcoatFactor <= 0.0) {\n return vec3(0.0);\n }\n\n PBRInfo clearcoatPBRInfo = createClearcoatPBRInfo(pbrInfo, clearcoatNormal, clearcoatRoughness);\n return getIBLContribution(clearcoatPBRInfo, clearcoatNormal, reflection) * clearcoatFactor;\n}\n#endif\n\nvec3 calculateSheenContribution(\n PBRInfo pbrInfo,\n vec3 lightColor,\n vec3 sheenColor,\n float sheenRoughness\n) {\n if (maxComponent(sheenColor) <= 0.0) {\n return vec3(0.0);\n }\n\n float sheenFresnel = pow(clamp(1.0 - pbrInfo.VdotH, 0.0, 1.0), 5.0);\n float sheenVisibility = mix(1.0, pbrInfo.NdotL * pbrInfo.NdotV, sheenRoughness);\n return pbrInfo.NdotL *\n lightColor *\n sheenColor *\n (0.25 + 0.75 * sheenFresnel) *\n sheenVisibility *\n (1.0 - pbrInfo.metalness);\n}\n\nfloat calculateAnisotropyBoost(\n PBRInfo pbrInfo,\n vec3 anisotropyTangent,\n float anisotropyStrength\n) {\n if (anisotropyStrength <= 0.0) {\n return 1.0;\n }\n\n vec3 anisotropyBitangent = normalize(cross(pbrInfo.n, anisotropyTangent));\n float bitangentViewAlignment = abs(dot(pbrInfo.v, anisotropyBitangent));\n return mix(1.0, 0.65 + 0.7 * bitangentViewAlignment, anisotropyStrength);\n}\n\nvec3 calculateMaterialLightColor(\n PBRInfo pbrInfo,\n vec3 lightColor,\n vec3 clearcoatNormal,\n float clearcoatFactor,\n float clearcoatRoughness,\n vec3 sheenColor,\n float sheenRoughness,\n vec3 anisotropyTangent,\n float anisotropyStrength\n) {\n float anisotropyBoost = calculateAnisotropyBoost(pbrInfo, anisotropyTangent, anisotropyStrength);\n vec3 color = calculateFinalColor(pbrInfo, lightColor) * anisotropyBoost;\n color += calculateClearcoatContribution(\n pbrInfo,\n lightColor,\n clearcoatNormal,\n clearcoatFactor,\n clearcoatRoughness\n );\n color += calculateSheenContribution(pbrInfo, lightColor, sheenColor, sheenRoughness);\n return color;\n}\n\nvoid PBRInfo_setAmbientLight(inout PBRInfo pbrInfo) {\n pbrInfo.NdotL = 1.0;\n pbrInfo.NdotH = 0.0;\n pbrInfo.LdotH = 0.0;\n pbrInfo.VdotH = 1.0;\n}\n\nvoid PBRInfo_setDirectionalLight(inout PBRInfo pbrInfo, vec3 lightDirection) {\n vec3 n = pbrInfo.n;\n vec3 v = pbrInfo.v;\n vec3 l = normalize(lightDirection); // Vector from surface point to light\n vec3 h = normalize(l+v); // Half vector between both l and v\n\n pbrInfo.NdotL = clamp(dot(n, l), 0.001, 1.0);\n pbrInfo.NdotH = clamp(dot(n, h), 0.0, 1.0);\n pbrInfo.LdotH = clamp(dot(l, h), 0.0, 1.0);\n pbrInfo.VdotH = clamp(dot(v, h), 0.0, 1.0);\n}\n\nvoid PBRInfo_setPointLight(inout PBRInfo pbrInfo, PointLight pointLight) {\n vec3 light_direction = normalize(pointLight.position - pbr_vPosition);\n PBRInfo_setDirectionalLight(pbrInfo, light_direction);\n}\n\nvoid PBRInfo_setSpotLight(inout PBRInfo pbrInfo, SpotLight spotLight) {\n vec3 light_direction = normalize(spotLight.position - pbr_vPosition);\n PBRInfo_setDirectionalLight(pbrInfo, light_direction);\n}\n\nvec3 calculateFinalColor(PBRInfo pbrInfo, vec3 lightColor) {\n // Calculate the shading terms for the microfacet specular shading model\n vec3 F = specularReflection(pbrInfo);\n float G = geometricOcclusion(pbrInfo);\n float D = microfacetDistribution(pbrInfo);\n\n // Calculation of analytical lighting contribution\n vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInfo);\n vec3 specContrib = F * G * D / (4.0 * pbrInfo.NdotL * pbrInfo.NdotV);\n // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)\n return pbrInfo.NdotL * lightColor * (diffuseContrib + specContrib);\n}\n\nvec4 pbr_filterColor(vec4 colorUnused)\n{\n vec2 baseColorUV = getMaterialUV(pbrMaterial.baseColorUVSet, pbrMaterial.baseColorUVTransform);\n vec2 metallicRoughnessUV = getMaterialUV(\n pbrMaterial.metallicRoughnessUVSet,\n pbrMaterial.metallicRoughnessUVTransform\n );\n vec2 normalUV = getMaterialUV(pbrMaterial.normalUVSet, pbrMaterial.normalUVTransform);\n vec2 occlusionUV = getMaterialUV(pbrMaterial.occlusionUVSet, pbrMaterial.occlusionUVTransform);\n vec2 emissiveUV = getMaterialUV(pbrMaterial.emissiveUVSet, pbrMaterial.emissiveUVTransform);\n vec2 specularColorUV = getMaterialUV(\n pbrMaterial.specularColorUVSet,\n pbrMaterial.specularColorUVTransform\n );\n vec2 specularIntensityUV = getMaterialUV(\n pbrMaterial.specularIntensityUVSet,\n pbrMaterial.specularIntensityUVTransform\n );\n vec2 transmissionUV = getMaterialUV(\n pbrMaterial.transmissionUVSet,\n pbrMaterial.transmissionUVTransform\n );\n vec2 thicknessUV = getMaterialUV(pbrMaterial.thicknessUVSet, pbrMaterial.thicknessUVTransform);\n vec2 clearcoatUV = getMaterialUV(pbrMaterial.clearcoatUVSet, pbrMaterial.clearcoatUVTransform);\n vec2 clearcoatRoughnessUV = getMaterialUV(\n pbrMaterial.clearcoatRoughnessUVSet,\n pbrMaterial.clearcoatRoughnessUVTransform\n );\n vec2 clearcoatNormalUV = getMaterialUV(\n pbrMaterial.clearcoatNormalUVSet,\n pbrMaterial.clearcoatNormalUVTransform\n );\n vec2 sheenColorUV = getMaterialUV(\n pbrMaterial.sheenColorUVSet,\n pbrMaterial.sheenColorUVTransform\n );\n vec2 sheenRoughnessUV = getMaterialUV(\n pbrMaterial.sheenRoughnessUVSet,\n pbrMaterial.sheenRoughnessUVTransform\n );\n vec2 iridescenceUV = getMaterialUV(\n pbrMaterial.iridescenceUVSet,\n pbrMaterial.iridescenceUVTransform\n );\n vec2 iridescenceThicknessUV = getMaterialUV(\n pbrMaterial.iridescenceThicknessUVSet,\n pbrMaterial.iridescenceThicknessUVTransform\n );\n vec2 anisotropyUV = getMaterialUV(\n pbrMaterial.anisotropyUVSet,\n pbrMaterial.anisotropyUVTransform\n );\n\n // The albedo may be defined from a base texture or a flat color\n#ifdef HAS_BASECOLORMAP\n vec4 baseColor =\n SRGBtoLINEAR(texture(pbr_baseColorSampler, baseColorUV)) * pbrMaterial.baseColorFactor;\n#else\n vec4 baseColor = pbrMaterial.baseColorFactor;\n#endif\n\n#ifdef ALPHA_CUTOFF\n if (baseColor.a < pbrMaterial.alphaCutoff) {\n discard;\n }\n#endif\n\n vec3 color = vec3(0, 0, 0);\n\n float transmission = 0.0;\n\n if(pbrMaterial.unlit){\n color.rgb = baseColor.rgb;\n }\n else{\n // Metallic and Roughness material properties are packed together\n // In glTF, these factors can be specified by fixed scalar values\n // or from a metallic-roughness map\n float perceptualRoughness = pbrMaterial.metallicRoughnessValues.y;\n float metallic = pbrMaterial.metallicRoughnessValues.x;\n#ifdef HAS_METALROUGHNESSMAP\n // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.\n // This layout intentionally reserves the 'r' channel for (optional) occlusion map data\n vec4 mrSample = texture(pbr_metallicRoughnessSampler, metallicRoughnessUV);\n perceptualRoughness = mrSample.g * perceptualRoughness;\n metallic = mrSample.b * metallic;\n#endif\n perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0);\n metallic = clamp(metallic, 0.0, 1.0);\n mat3 tbn = getTBN(normalUV);\n vec3 n = getNormal(tbn, normalUV); // normal at surface point\n vec3 v = normalize(pbrProjection.camera - pbr_vPosition); // Vector from surface point to camera\n float NdotV = clamp(abs(dot(n, v)), 0.001, 1.0);\n#ifdef USE_MATERIAL_EXTENSIONS\n bool useExtendedPBR =\n pbrMaterial.specularColorMapEnabled ||\n pbrMaterial.specularIntensityMapEnabled ||\n abs(pbrMaterial.specularIntensityFactor - 1.0) > 0.0001 ||\n maxComponent(abs(pbrMaterial.specularColorFactor - vec3(1.0))) > 0.0001 ||\n abs(pbrMaterial.ior - 1.5) > 0.0001 ||\n pbrMaterial.transmissionMapEnabled ||\n pbrMaterial.transmissionFactor > 0.0001 ||\n pbrMaterial.clearcoatMapEnabled ||\n pbrMaterial.clearcoatRoughnessMapEnabled ||\n pbrMaterial.clearcoatFactor > 0.0001 ||\n pbrMaterial.clearcoatRoughnessFactor > 0.0001 ||\n pbrMaterial.sheenColorMapEnabled ||\n pbrMaterial.sheenRoughnessMapEnabled ||\n maxComponent(pbrMaterial.sheenColorFactor) > 0.0001 ||\n pbrMaterial.sheenRoughnessFactor > 0.0001 ||\n pbrMaterial.iridescenceMapEnabled ||\n pbrMaterial.iridescenceFactor > 0.0001 ||\n abs(pbrMaterial.iridescenceIor - 1.3) > 0.0001 ||\n abs(pbrMaterial.iridescenceThicknessRange.x - 100.0) > 0.0001 ||\n abs(pbrMaterial.iridescenceThicknessRange.y - 400.0) > 0.0001 ||\n pbrMaterial.anisotropyMapEnabled ||\n pbrMaterial.anisotropyStrength > 0.0001 ||\n abs(pbrMaterial.anisotropyRotation) > 0.0001 ||\n length(pbrMaterial.anisotropyDirection - vec2(1.0, 0.0)) > 0.0001;\n#else\n bool useExtendedPBR = false;\n#endif\n\n if (!useExtendedPBR) {\n // Keep the baseline metallic-roughness implementation byte-for-byte equivalent in behavior.\n float alphaRoughness = perceptualRoughness * perceptualRoughness;\n\n vec3 f0 = vec3(0.04);\n vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - f0);\n diffuseColor *= 1.0 - metallic;\n vec3 specularColor = mix(f0, baseColor.rgb, metallic);\n\n float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);\n float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);\n vec3 specularEnvironmentR0 = specularColor.rgb;\n vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;\n vec3 reflection = -normalize(reflect(v, n));\n\n PBRInfo pbrInfo = PBRInfo(\n 0.0, // NdotL\n NdotV,\n 0.0, // NdotH\n 0.0, // LdotH\n 0.0, // VdotH\n perceptualRoughness,\n metallic,\n specularEnvironmentR0,\n specularEnvironmentR90,\n alphaRoughness,\n diffuseColor,\n specularColor,\n n,\n v\n );\n\n#ifdef USE_LIGHTS\n PBRInfo_setAmbientLight(pbrInfo);\n color += calculateFinalColor(pbrInfo, lighting.ambientColor);\n\n for(int i = 0; i < lighting.directionalLightCount; i++) {\n if (i < lighting.directionalLightCount) {\n PBRInfo_setDirectionalLight(pbrInfo, lighting_getDirectionalLight(i).direction);\n color += calculateFinalColor(pbrInfo, lighting_getDirectionalLight(i).color);\n }\n }\n\n for(int i = 0; i < lighting.pointLightCount; i++) {\n if (i < lighting.pointLightCount) {\n PBRInfo_setPointLight(pbrInfo, lighting_getPointLight(i));\n float attenuation = getPointLightAttenuation(lighting_getPointLight(i), distance(lighting_getPointLight(i).position, pbr_vPosition));\n color += calculateFinalColor(pbrInfo, lighting_getPointLight(i).color / attenuation);\n }\n }\n\n for(int i = 0; i < lighting.spotLightCount; i++) {\n if (i < lighting.spotLightCount) {\n PBRInfo_setSpotLight(pbrInfo, lighting_getSpotLight(i));\n float attenuation = getSpotLightAttenuation(lighting_getSpotLight(i), pbr_vPosition);\n color += calculateFinalColor(pbrInfo, lighting_getSpotLight(i).color / attenuation);\n }\n }\n#endif\n\n#ifdef USE_IBL\n if (pbrMaterial.IBLenabled) {\n color += getIBLContribution(pbrInfo, n, reflection);\n }\n#endif\n\n#ifdef HAS_OCCLUSIONMAP\n if (pbrMaterial.occlusionMapEnabled) {\n float ao = texture(pbr_occlusionSampler, occlusionUV).r;\n color = mix(color, color * ao, pbrMaterial.occlusionStrength);\n }\n#endif\n\n vec3 emissive = pbrMaterial.emissiveFactor;\n#ifdef HAS_EMISSIVEMAP\n if (pbrMaterial.emissiveMapEnabled) {\n emissive *= SRGBtoLINEAR(texture(pbr_emissiveSampler, emissiveUV)).rgb;\n }\n#endif\n color += emissive * pbrMaterial.emissiveStrength;\n\n#ifdef PBR_DEBUG\n color = mix(color, baseColor.rgb, pbrMaterial.scaleDiffBaseMR.y);\n color = mix(color, vec3(metallic), pbrMaterial.scaleDiffBaseMR.z);\n color = mix(color, vec3(perceptualRoughness), pbrMaterial.scaleDiffBaseMR.w);\n#endif\n\n return vec4(pow(color, vec3(1.0 / 2.2)), baseColor.a);\n }\n\n float specularIntensity = pbrMaterial.specularIntensityFactor;\n#ifdef HAS_SPECULARINTENSITYMAP\n if (pbrMaterial.specularIntensityMapEnabled) {\n specularIntensity *= texture(pbr_specularIntensitySampler, specularIntensityUV).a;\n }\n#endif\n\n vec3 specularFactor = pbrMaterial.specularColorFactor;\n#ifdef HAS_SPECULARCOLORMAP\n if (pbrMaterial.specularColorMapEnabled) {\n specularFactor *= SRGBtoLINEAR(texture(pbr_specularColorSampler, specularColorUV)).rgb;\n }\n#endif\n\n transmission = pbrMaterial.transmissionFactor;\n#ifdef HAS_TRANSMISSIONMAP\n if (pbrMaterial.transmissionMapEnabled) {\n transmission *= texture(pbr_transmissionSampler, transmissionUV).r;\n }\n#endif\n transmission = clamp(transmission * (1.0 - metallic), 0.0, 1.0);\n float thickness = max(pbrMaterial.thicknessFactor, 0.0);\n#ifdef HAS_THICKNESSMAP\n thickness *= texture(pbr_thicknessSampler, thicknessUV).g;\n#endif\n\n float clearcoatFactor = pbrMaterial.clearcoatFactor;\n float clearcoatRoughness = pbrMaterial.clearcoatRoughnessFactor;\n#ifdef HAS_CLEARCOATMAP\n if (pbrMaterial.clearcoatMapEnabled) {\n clearcoatFactor *= texture(pbr_clearcoatSampler, clearcoatUV).r;\n }\n#endif\n#ifdef HAS_CLEARCOATROUGHNESSMAP\n if (pbrMaterial.clearcoatRoughnessMapEnabled) {\n clearcoatRoughness *= texture(pbr_clearcoatRoughnessSampler, clearcoatRoughnessUV).g;\n }\n#endif\n clearcoatFactor = clamp(clearcoatFactor, 0.0, 1.0);\n clearcoatRoughness = clamp(clearcoatRoughness, c_MinRoughness, 1.0);\n vec3 clearcoatNormal = getClearcoatNormal(getTBN(clearcoatNormalUV), n, clearcoatNormalUV);\n\n vec3 sheenColor = pbrMaterial.sheenColorFactor;\n float sheenRoughness = pbrMaterial.sheenRoughnessFactor;\n#ifdef HAS_SHEENCOLORMAP\n if (pbrMaterial.sheenColorMapEnabled) {\n sheenColor *= SRGBtoLINEAR(texture(pbr_sheenColorSampler, sheenColorUV)).rgb;\n }\n#endif\n#ifdef HAS_SHEENROUGHNESSMAP\n if (pbrMaterial.sheenRoughnessMapEnabled) {\n sheenRoughness *= texture(pbr_sheenRoughnessSampler, sheenRoughnessUV).a;\n }\n#endif\n sheenRoughness = clamp(sheenRoughness, c_MinRoughness, 1.0);\n\n float iridescence = pbrMaterial.iridescenceFactor;\n#ifdef HAS_IRIDESCENCEMAP\n if (pbrMaterial.iridescenceMapEnabled) {\n iridescence *= texture(pbr_iridescenceSampler, iridescenceUV).r;\n }\n#endif\n iridescence = clamp(iridescence, 0.0, 1.0);\n float iridescenceThickness = mix(\n pbrMaterial.iridescenceThicknessRange.x,\n pbrMaterial.iridescenceThicknessRange.y,\n 0.5\n );\n#ifdef HAS_IRIDESCENCETHICKNESSMAP\n iridescenceThickness = mix(\n pbrMaterial.iridescenceThicknessRange.x,\n pbrMaterial.iridescenceThicknessRange.y,\n texture(pbr_iridescenceThicknessSampler, iridescenceThicknessUV).g\n );\n#endif\n\n float anisotropyStrength = clamp(pbrMaterial.anisotropyStrength, 0.0, 1.0);\n vec2 anisotropyDirection = normalizeDirection(pbrMaterial.anisotropyDirection);\n#ifdef HAS_ANISOTROPYMAP\n if (pbrMaterial.anisotropyMapEnabled) {\n vec3 anisotropySample = texture(pbr_anisotropySampler, anisotropyUV).rgb;\n anisotropyStrength *= anisotropySample.b;\n vec2 mappedDirection = anisotropySample.rg * 2.0 - 1.0;\n if (length(mappedDirection) > 0.0001) {\n anisotropyDirection = normalize(mappedDirection);\n }\n }\n#endif\n anisotropyDirection = rotateDirection(anisotropyDirection, pbrMaterial.anisotropyRotation);\n vec3 anisotropyTangent = normalize(tbn[0] * anisotropyDirection.x + tbn[1] * anisotropyDirection.y);\n if (length(anisotropyTangent) < 0.0001) {\n anisotropyTangent = normalize(tbn[0]);\n }\n float anisotropyViewAlignment = abs(dot(v, anisotropyTangent));\n perceptualRoughness = mix(\n perceptualRoughness,\n clamp(perceptualRoughness * (1.0 - 0.6 * anisotropyViewAlignment), c_MinRoughness, 1.0),\n anisotropyStrength\n );\n\n // Roughness is authored as perceptual roughness; as is convention,\n // convert to material roughness by squaring the perceptual roughness [2].\n float alphaRoughness = perceptualRoughness * perceptualRoughness;\n\n float dielectricF0 = getDielectricF0(pbrMaterial.ior);\n vec3 dielectricSpecularF0 = min(\n vec3(dielectricF0) * specularFactor * specularIntensity,\n vec3(1.0)\n );\n vec3 iridescenceTint = getIridescenceTint(iridescence, iridescenceThickness, NdotV);\n dielectricSpecularF0 = mix(\n dielectricSpecularF0,\n dielectricSpecularF0 * iridescenceTint,\n iridescence\n );\n vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - dielectricSpecularF0);\n diffuseColor *= (1.0 - metallic) * (1.0 - transmission);\n vec3 specularColor = mix(dielectricSpecularF0, baseColor.rgb, metallic);\n\n float baseLayerEnergy = 1.0 - clearcoatFactor * 0.25;\n diffuseColor *= baseLayerEnergy;\n specularColor *= baseLayerEnergy;\n\n // Compute reflectance.\n float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);\n\n // For typical incident reflectance range (between 4% to 100%) set the grazing\n // reflectance to 100% for typical fresnel effect.\n // For very low reflectance range on highly diffuse objects (below 4%),\n // incrementally reduce grazing reflecance to 0%.\n float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);\n vec3 specularEnvironmentR0 = specularColor.rgb;\n vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;\n vec3 reflection = -normalize(reflect(v, n));\n\n PBRInfo pbrInfo = PBRInfo(\n 0.0, // NdotL\n NdotV,\n 0.0, // NdotH\n 0.0, // LdotH\n 0.0, // VdotH\n perceptualRoughness,\n metallic,\n specularEnvironmentR0,\n specularEnvironmentR90,\n alphaRoughness,\n diffuseColor,\n specularColor,\n n,\n v\n );\n\n\n#ifdef USE_LIGHTS\n // Apply ambient light\n PBRInfo_setAmbientLight(pbrInfo);\n color += calculateMaterialLightColor(\n pbrInfo,\n lighting.ambientColor,\n clearcoatNormal,\n clearcoatFactor,\n clearcoatRoughness,\n sheenColor,\n sheenRoughness,\n anisotropyTangent,\n anisotropyStrength\n );\n\n // Apply directional light\n for(int i = 0; i < lighting.directionalLightCount; i++) {\n if (i < lighting.directionalLightCount) {\n PBRInfo_setDirectionalLight(pbrInfo, lighting_getDirectionalLight(i).direction);\n color += calculateMaterialLightColor(\n pbrInfo,\n lighting_getDirectionalLight(i).color,\n clearcoatNormal,\n clearcoatFactor,\n clearcoatRoughness,\n sheenColor,\n sheenRoughness,\n anisotropyTangent,\n anisotropyStrength\n );\n }\n }\n\n // Apply point light\n for(int i = 0; i < lighting.pointLightCount; i++) {\n if (i < lighting.pointLightCount) {\n PBRInfo_setPointLight(pbrInfo, lighting_getPointLight(i));\n float attenuation = getPointLightAttenuation(lighting_getPointLight(i), distance(lighting_getPointLight(i).position, pbr_vPosition));\n color += calculateMaterialLightColor(\n pbrInfo,\n lighting_getPointLight(i).color / attenuation,\n clearcoatNormal,\n clearcoatFactor,\n clearcoatRoughness,\n sheenColor,\n sheenRoughness,\n anisotropyTangent,\n anisotropyStrength\n );\n }\n }\n\n for(int i = 0; i < lighting.spotLightCount; i++) {\n if (i < lighting.spotLightCount) {\n PBRInfo_setSpotLight(pbrInfo, lighting_getSpotLight(i));\n float attenuation = getSpotLightAttenuation(lighting_getSpotLight(i), pbr_vPosition);\n color += calculateMaterialLightColor(\n pbrInfo,\n lighting_getSpotLight(i).color / attenuation,\n clearcoatNormal,\n clearcoatFactor,\n clearcoatRoughness,\n sheenColor,\n sheenRoughness,\n anisotropyTangent,\n anisotropyStrength\n );\n }\n }\n#endif\n\n // Calculate lighting contribution from image based lighting source (IBL)\n#ifdef USE_IBL\n if (pbrMaterial.IBLenabled) {\n color += getIBLContribution(pbrInfo, n, reflection) *\n calculateAnisotropyBoost(pbrInfo, anisotropyTangent, anisotropyStrength);\n color += calculateClearcoatIBLContribution(\n pbrInfo,\n clearcoatNormal,\n -normalize(reflect(v, clearcoatNormal)),\n clearcoatFactor,\n clearcoatRoughness\n );\n color += sheenColor * pbrMaterial.scaleIBLAmbient.x * (1.0 - sheenRoughness) * 0.25;\n }\n#endif\n\n // Apply optional PBR terms for additional (optional) shading\n#ifdef HAS_OCCLUSIONMAP\n if (pbrMaterial.occlusionMapEnabled) {\n float ao = texture(pbr_occlusionSampler, occlusionUV).r;\n color = mix(color, color * ao, pbrMaterial.occlusionStrength);\n }\n#endif\n\n vec3 emissive = pbrMaterial.emissiveFactor;\n#ifdef HAS_EMISSIVEMAP\n if (pbrMaterial.emissiveMapEnabled) {\n emissive *= SRGBtoLINEAR(texture(pbr_emissiveSampler, emissiveUV)).rgb;\n }\n#endif\n color += emissive * pbrMaterial.emissiveStrength;\n\n if (transmission > 0.0) {\n color = mix(color, color * getVolumeAttenuation(thickness), transmission);\n }\n\n // This section uses mix to override final color for reference app visualization\n // of various parameters in the lighting equation.\n#ifdef PBR_DEBUG\n // TODO: Figure out how to debug multiple lights\n\n // color = mix(color, F, pbr_scaleFGDSpec.x);\n // color = mix(color, vec3(G), pbr_scaleFGDSpec.y);\n // color = mix(color, vec3(D), pbr_scaleFGDSpec.z);\n // color = mix(color, specContrib, pbr_scaleFGDSpec.w);\n\n // color = mix(color, diffuseContrib, pbr_scaleDiffBaseMR.x);\n color = mix(color, baseColor.rgb, pbrMaterial.scaleDiffBaseMR.y);\n color = mix(color, vec3(metallic), pbrMaterial.scaleDiffBaseMR.z);\n color = mix(color, vec3(perceptualRoughness), pbrMaterial.scaleDiffBaseMR.w);\n#endif\n\n }\n\n float alpha = clamp(baseColor.a * (1.0 - transmission), 0.0, 1.0);\n return vec4(pow(color,vec3(1.0/2.2)), alpha);\n}\n";
3
3
  //# sourceMappingURL=pbr-material-glsl.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"pbr-material-glsl.d.ts","sourceRoot":"","sources":["../../../../src/modules/lighting/pbr-material/pbr-material-glsl.ts"],"names":[],"mappings":"AAWA,eAAO,MAAM,EAAE,m3BAkCd,CAAC;AAEF,eAAO,MAAM,EAAE,wwcAkZd,CAAC"}
1
+ {"version":3,"file":"pbr-material-glsl.d.ts","sourceRoot":"","sources":["../../../../src/modules/lighting/pbr-material/pbr-material-glsl.ts"],"names":[],"mappings":"AAWA,eAAO,MAAM,EAAE,67BA2Cd,CAAC;AAEF,eAAO,MAAM,EAAE,2nmCAyhCd,CAAC"}