Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

124 rader
4.2 KiB

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