brdf.fs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*******************************************************************************************
  2. *
  3. * BRDF LUT Generation - Bidirectional reflectance distribution function fragment shader
  4. *
  5. * REF: https://github.com/HectorMF/BRDFGenerator
  6. *
  7. * Copyright (c) 2017 Victor Fisac
  8. *
  9. **********************************************************************************************/
  10. #version 330
  11. // Input vertex attributes (from vertex shader)
  12. in vec2 fragTexCoord;
  13. // Constant values
  14. const float PI = 3.14159265359;
  15. const uint MAX_SAMPLES = 1024u;
  16. // Output fragment color
  17. out vec4 finalColor;
  18. vec2 Hammersley(uint i, uint N);
  19. float RadicalInverseVdC(uint bits);
  20. float GeometrySchlickGGX(float NdotV, float roughness);
  21. float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness);
  22. vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness);
  23. vec2 IntegrateBRDF(float NdotV, float roughness);
  24. float RadicalInverseVdC(uint bits)
  25. {
  26. bits = (bits << 16u) | (bits >> 16u);
  27. bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
  28. bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
  29. bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
  30. bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
  31. return float(bits) * 2.3283064365386963e-10; // / 0x100000000
  32. }
  33. // Compute Hammersley coordinates
  34. vec2 Hammersley(uint i, uint N)
  35. {
  36. return vec2(float(i)/float(N), RadicalInverseVdC(i));
  37. }
  38. // Integrate number of importance samples for (roughness and NoV)
  39. vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness)
  40. {
  41. float a = roughness*roughness;
  42. float phi = 2.0 * PI * Xi.x;
  43. float cosTheta = sqrt((1.0 - Xi.y)/(1.0 + (a*a - 1.0)*Xi.y));
  44. float sinTheta = sqrt(1.0 - cosTheta*cosTheta);
  45. // Transform from spherical coordinates to cartesian coordinates (halfway vector)
  46. vec3 H = vec3(cos(phi)*sinTheta, sin(phi)*sinTheta, cosTheta);
  47. // Transform from tangent space H vector to world space sample vector
  48. vec3 up = ((abs(N.z) < 0.999) ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0));
  49. vec3 tangent = normalize(cross(up, N));
  50. vec3 bitangent = cross(N, tangent);
  51. vec3 sampleVec = tangent*H.x + bitangent*H.y + N*H.z;
  52. return normalize(sampleVec);
  53. }
  54. float GeometrySchlickGGX(float NdotV, float roughness)
  55. {
  56. // For IBL k is calculated different
  57. float k = (roughness*roughness)/2.0;
  58. float nom = NdotV;
  59. float denom = NdotV*(1.0 - k) + k;
  60. return nom/denom;
  61. }
  62. // Compute the geometry term for the BRDF given roughness squared, NoV, NoL
  63. float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
  64. {
  65. float NdotV = max(dot(N, V), 0.0);
  66. float NdotL = max(dot(N, L), 0.0);
  67. float ggx2 = GeometrySchlickGGX(NdotV, roughness);
  68. float ggx1 = GeometrySchlickGGX(NdotL, roughness);
  69. return ggx1*ggx2;
  70. }
  71. vec2 IntegrateBRDF(float NdotV, float roughness)
  72. {
  73. float A = 0.0;
  74. float B = 0.0;
  75. vec3 V = vec3(sqrt(1.0 - NdotV*NdotV), 0.0, NdotV);
  76. vec3 N = vec3(0.0, 0.0, 1.0);
  77. for (uint i = 0u; i < MAX_SAMPLES; i++)
  78. {
  79. // Generate a sample vector that's biased towards the preferred alignment direction (importance sampling)
  80. vec2 Xi = Hammersley(i, MAX_SAMPLES); // Compute a Hammersely coordinate
  81. vec3 H = ImportanceSampleGGX(Xi, N, roughness); // Integrate number of importance samples for (roughness and NoV)
  82. vec3 L = normalize(2.0*dot(V, H)*H - V); // Compute reflection vector L
  83. float NdotL = max(L.z, 0.0); // Compute normal dot light
  84. float NdotH = max(H.z, 0.0); // Compute normal dot half
  85. float VdotH = max(dot(V, H), 0.0); // Compute view dot half
  86. if (NdotL > 0.0)
  87. {
  88. float G = GeometrySmith(N, V, L, roughness); // Compute the geometry term for the BRDF given roughness squared, NoV, NoL
  89. float GVis = (G*VdotH)/(NdotH*NdotV); // Compute the visibility term given G, VoH, NoH, NoV, NoL
  90. float Fc = pow(1.0 - VdotH, 5.0); // Compute the fresnel term given VoH
  91. A += (1.0 - Fc)*GVis; // Sum the result given fresnel, geometry, visibility
  92. B += Fc*GVis;
  93. }
  94. }
  95. // Calculate brdf average sample
  96. A /= float(MAX_SAMPLES);
  97. B /= float(MAX_SAMPLES);
  98. return vec2(A, B);
  99. }
  100. void main()
  101. {
  102. // Calculate brdf based on texture coordinates
  103. vec2 brdf = IntegrateBRDF(fragTexCoord.x, fragTexCoord.y);
  104. // Calculate final fragment color
  105. finalColor = vec4(brdf.r, brdf.g, 0.0, 1.0);
  106. }