BrdfLutGeneratorFS.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "varying vec2 v_textureCoordinates;\n\
  3. const float M_PI = 3.141592653589793;\n\
  4. \n\
  5. float vdcRadicalInverse(int i)\n\
  6. {\n\
  7. float r;\n\
  8. float base = 2.0;\n\
  9. float value = 0.0;\n\
  10. float invBase = 1.0 / base;\n\
  11. float invBi = invBase;\n\
  12. for (int x = 0; x < 100; x++)\n\
  13. {\n\
  14. if (i <= 0)\n\
  15. {\n\
  16. break;\n\
  17. }\n\
  18. r = mod(float(i), base);\n\
  19. value += r * invBi;\n\
  20. invBi *= invBase;\n\
  21. i = int(float(i) * invBase);\n\
  22. }\n\
  23. return value;\n\
  24. }\n\
  25. \n\
  26. vec2 hammersley2D(int i, int N)\n\
  27. {\n\
  28. return vec2(float(i) / float(N), vdcRadicalInverse(i));\n\
  29. }\n\
  30. \n\
  31. vec3 importanceSampleGGX(vec2 xi, float roughness, vec3 N)\n\
  32. {\n\
  33. float a = roughness * roughness;\n\
  34. float phi = 2.0 * M_PI * xi.x;\n\
  35. float cosTheta = sqrt((1.0 - xi.y) / (1.0 + (a * a - 1.0) * xi.y));\n\
  36. float sinTheta = sqrt(1.0 - cosTheta * cosTheta);\n\
  37. vec3 H = vec3(sinTheta * cos(phi), sinTheta * sin(phi), cosTheta);\n\
  38. vec3 upVector = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);\n\
  39. vec3 tangentX = normalize(cross(upVector, N));\n\
  40. vec3 tangentY = cross(N, tangentX);\n\
  41. return tangentX * H.x + tangentY * H.y + N * H.z;\n\
  42. }\n\
  43. \n\
  44. float G1_Smith(float NdotV, float k)\n\
  45. {\n\
  46. return NdotV / (NdotV * (1.0 - k) + k);\n\
  47. }\n\
  48. \n\
  49. float G_Smith(float roughness, float NdotV, float NdotL)\n\
  50. {\n\
  51. float k = roughness * roughness / 2.0;\n\
  52. return G1_Smith(NdotV, k) * G1_Smith(NdotL, k);\n\
  53. }\n\
  54. \n\
  55. vec2 integrateBrdf(float roughness, float NdotV)\n\
  56. {\n\
  57. vec3 V = vec3(sqrt(1.0 - NdotV * NdotV), 0.0, NdotV);\n\
  58. float A = 0.0;\n\
  59. float B = 0.0;\n\
  60. const int NumSamples = 1024;\n\
  61. for (int i = 0; i < NumSamples; i++)\n\
  62. {\n\
  63. vec2 xi = hammersley2D(i, NumSamples);\n\
  64. vec3 H = importanceSampleGGX(xi, roughness, vec3(0.0, 0.0, 1.0));\n\
  65. vec3 L = 2.0 * dot(V, H) * H - V;\n\
  66. float NdotL = clamp(L.z, 0.0, 1.0);\n\
  67. float NdotH = clamp(H.z, 0.0, 1.0);\n\
  68. float VdotH = clamp(dot(V, H), 0.0, 1.0);\n\
  69. if (NdotL > 0.0)\n\
  70. {\n\
  71. float G = G_Smith(roughness, NdotV, NdotL);\n\
  72. float G_Vis = G * VdotH / (NdotH * NdotV);\n\
  73. float Fc = pow(1.0 - VdotH, 5.0);\n\
  74. A += (1.0 - Fc) * G_Vis;\n\
  75. B += Fc * G_Vis;\n\
  76. }\n\
  77. }\n\
  78. return vec2(A, B) / float(NumSamples);\n\
  79. }\n\
  80. \n\
  81. void main()\n\
  82. {\n\
  83. gl_FragColor = vec4(integrateBrdf(v_textureCoordinates.y, v_textureCoordinates.x), 0.0, 1.0);\n\
  84. }\n\
  85. ";