pbrLighting.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "vec3 lambertianDiffuse(vec3 diffuseColor)\n\
  3. {\n\
  4. return diffuseColor / czm_pi;\n\
  5. }\n\
  6. \n\
  7. vec3 fresnelSchlick2(vec3 f0, vec3 f90, float VdotH)\n\
  8. {\n\
  9. return f0 + (f90 - f0) * pow(clamp(1.0 - VdotH, 0.0, 1.0), 5.0);\n\
  10. }\n\
  11. \n\
  12. float smithVisibilityG1(float NdotV, float roughness)\n\
  13. {\n\
  14. // this is the k value for direct lighting.\n\
  15. // for image based lighting it will be roughness^2 / 2\n\
  16. float k = (roughness + 1.0) * (roughness + 1.0) / 8.0;\n\
  17. return NdotV / (NdotV * (1.0 - k) + k);\n\
  18. }\n\
  19. \n\
  20. float smithVisibilityGGX(float roughness, float NdotL, float NdotV)\n\
  21. {\n\
  22. return (\n\
  23. smithVisibilityG1(NdotL, roughness) *\n\
  24. smithVisibilityG1(NdotV, roughness)\n\
  25. );\n\
  26. }\n\
  27. \n\
  28. float GGX(float roughness, float NdotH)\n\
  29. {\n\
  30. float roughnessSquared = roughness * roughness;\n\
  31. float f = (NdotH * roughnessSquared - NdotH) * NdotH + 1.0;\n\
  32. return roughnessSquared / (czm_pi * f * f);\n\
  33. }\n\
  34. \n\
  35. /**\n\
  36. * Compute the diffuse and specular contributions using physically based\n\
  37. * rendering. This function only handles direct lighting.\n\
  38. * <p>\n\
  39. * This function only handles the lighting calculations. Metallic/roughness\n\
  40. * and specular/glossy must be handled separately. See {@czm_pbrMetallicRoughnessMaterial}, {@czm_pbrSpecularGlossinessMaterial} and {@czm_defaultPbrMaterial}\n\
  41. * </p>\n\
  42. *\n\
  43. * @name czm_pbrlighting\n\
  44. * @glslFunction\n\
  45. *\n\
  46. * @param {vec3} positionEC The position of the fragment in eye coordinates\n\
  47. * @param {vec3} normalEC The surface normal in eye coordinates\n\
  48. * @param {vec3} lightDirectionEC Unit vector pointing to the light source in eye coordinates.\n\
  49. * @param {vec3} lightColorHdr radiance of the light source. This is a HDR value.\n\
  50. * @param {czm_pbrParameters} The computed PBR parameters.\n\
  51. * @return {vec3} The computed HDR color\n\
  52. *\n\
  53. * @example\n\
  54. * czm_pbrParameters pbrParameters = czm_pbrMetallicRoughnessMaterial(\n\
  55. * baseColor,\n\
  56. * metallic,\n\
  57. * roughness\n\
  58. * );\n\
  59. * vec3 color = czm_pbrlighting(\n\
  60. * positionEC,\n\
  61. * normalEC,\n\
  62. * lightDirectionEC,\n\
  63. * lightColorHdr,\n\
  64. * pbrParameters);\n\
  65. */\n\
  66. vec3 czm_pbrLighting(\n\
  67. vec3 positionEC,\n\
  68. vec3 normalEC,\n\
  69. vec3 lightDirectionEC,\n\
  70. vec3 lightColorHdr,\n\
  71. czm_pbrParameters pbrParameters\n\
  72. )\n\
  73. {\n\
  74. vec3 v = -normalize(positionEC);\n\
  75. vec3 l = normalize(lightDirectionEC);\n\
  76. vec3 h = normalize(v + l);\n\
  77. vec3 n = normalEC;\n\
  78. float NdotL = clamp(dot(n, l), 0.001, 1.0);\n\
  79. float NdotV = abs(dot(n, v)) + 0.001;\n\
  80. float NdotH = clamp(dot(n, h), 0.0, 1.0);\n\
  81. float LdotH = clamp(dot(l, h), 0.0, 1.0);\n\
  82. float VdotH = clamp(dot(v, h), 0.0, 1.0);\n\
  83. \n\
  84. vec3 f0 = pbrParameters.f0;\n\
  85. float reflectance = max(max(f0.r, f0.g), f0.b);\n\
  86. vec3 f90 = vec3(clamp(reflectance * 25.0, 0.0, 1.0));\n\
  87. vec3 F = fresnelSchlick2(f0, f90, VdotH);\n\
  88. \n\
  89. float alpha = pbrParameters.roughness;\n\
  90. float G = smithVisibilityGGX(alpha, NdotL, NdotV);\n\
  91. float D = GGX(alpha, NdotH);\n\
  92. vec3 specularContribution = F * G * D / (4.0 * NdotL * NdotV);\n\
  93. \n\
  94. vec3 diffuseColor = pbrParameters.diffuseColor;\n\
  95. // F here represents the specular contribution\n\
  96. vec3 diffuseContribution = (1.0 - F) * lambertianDiffuse(diffuseColor);\n\
  97. \n\
  98. // Lo = (diffuse + specular) * Li * NdotL\n\
  99. return (diffuseContribution + specularContribution) * NdotL * lightColorHdr;\n\
  100. }\n\
  101. ";