No Description

pbribl.frag 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #version 450
  2. layout (location = 0) in vec3 inWorldPos;
  3. layout (location = 1) in vec3 inNormal;
  4. layout (location = 2) in vec2 inUV;
  5. layout (binding = 0) uniform UBO {
  6. mat4 projection;
  7. mat4 model;
  8. mat4 view;
  9. vec3 camPos;
  10. } ubo;
  11. layout (binding = 1) uniform UBOParams {
  12. vec4 lights[4];
  13. float exposure;
  14. float gamma;
  15. } uboParams;
  16. layout(push_constant) uniform PushConsts {
  17. layout(offset = 12) float roughness;
  18. layout(offset = 16) float metallic;
  19. layout(offset = 20) float specular;
  20. layout(offset = 24) float r;
  21. layout(offset = 28) float g;
  22. layout(offset = 32) float b;
  23. } material;
  24. layout (binding = 2) uniform samplerCube samplerIrradiance;
  25. layout (binding = 3) uniform sampler2D samplerBRDFLUT;
  26. layout (binding = 4) uniform samplerCube prefilteredMap;
  27. layout (location = 0) out vec4 outColor;
  28. #define PI 3.1415926535897932384626433832795
  29. #define ALBEDO vec3(material.r, material.g, material.b)
  30. // From http://filmicgames.com/archives/75
  31. vec3 Uncharted2Tonemap(vec3 x)
  32. {
  33. float A = 0.15;
  34. float B = 0.50;
  35. float C = 0.10;
  36. float D = 0.20;
  37. float E = 0.02;
  38. float F = 0.30;
  39. return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;
  40. }
  41. // Normal Distribution function --------------------------------------
  42. float D_GGX(float dotNH, float roughness)
  43. {
  44. float alpha = roughness * roughness;
  45. float alpha2 = alpha * alpha;
  46. float denom = dotNH * dotNH * (alpha2 - 1.0) + 1.0;
  47. return (alpha2)/(PI * denom*denom);
  48. }
  49. // Geometric Shadowing function --------------------------------------
  50. float G_SchlicksmithGGX(float dotNL, float dotNV, float roughness)
  51. {
  52. float r = (roughness + 1.0);
  53. float k = (r*r) / 8.0;
  54. float GL = dotNL / (dotNL * (1.0 - k) + k);
  55. float GV = dotNV / (dotNV * (1.0 - k) + k);
  56. return GL * GV;
  57. }
  58. // Fresnel function ----------------------------------------------------
  59. vec3 F_Schlick(float cosTheta, vec3 F0)
  60. {
  61. return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
  62. }
  63. vec3 F_SchlickR(float cosTheta, vec3 F0, float roughness)
  64. {
  65. return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(1.0 - cosTheta, 5.0);
  66. }
  67. vec3 prefilteredReflection(vec3 R, float roughness)
  68. {
  69. const float MAX_REFLECTION_LOD = 9.0; // todo: param/const
  70. float lod = roughness * MAX_REFLECTION_LOD;
  71. float lodf = floor(lod);
  72. float lodc = ceil(lod);
  73. vec3 a = textureLod(prefilteredMap, R, lodf).rgb;
  74. vec3 b = textureLod(prefilteredMap, R, lodc).rgb;
  75. return mix(a, b, lod - lodf);
  76. }
  77. vec3 specularContribution(vec3 L, vec3 V, vec3 N, vec3 F0, float metallic, float roughness)
  78. {
  79. // Precalculate vectors and dot products
  80. vec3 H = normalize (V + L);
  81. float dotNH = clamp(dot(N, H), 0.0, 1.0);
  82. float dotNV = clamp(dot(N, V), 0.0, 1.0);
  83. float dotNL = clamp(dot(N, L), 0.0, 1.0);
  84. // Light color fixed
  85. vec3 lightColor = vec3(1.0);
  86. vec3 color = vec3(0.0);
  87. if (dotNL > 0.0) {
  88. // D = Normal distribution (Distribution of the microfacets)
  89. float D = D_GGX(dotNH, roughness);
  90. // G = Geometric shadowing term (Microfacets shadowing)
  91. float G = G_SchlicksmithGGX(dotNL, dotNV, roughness);
  92. // F = Fresnel factor (Reflectance depending on angle of incidence)
  93. vec3 F = F_Schlick(dotNV, F0);
  94. vec3 spec = D * F * G / (4.0 * dotNL * dotNV + 0.001);
  95. vec3 kD = (vec3(1.0) - F) * (1.0 - metallic);
  96. color += (kD * ALBEDO / PI + spec) * dotNL;
  97. }
  98. return color;
  99. }
  100. void main()
  101. {
  102. vec3 N = normalize(inNormal);
  103. vec3 V = normalize(ubo.camPos - inWorldPos);
  104. vec3 R = reflect(-V, N);
  105. float metallic = material.metallic;
  106. float roughness = material.roughness;
  107. vec3 F0 = vec3(0.04);
  108. F0 = mix(F0, ALBEDO, metallic);
  109. vec3 Lo = vec3(0.0);
  110. for(int i = 0; i < uboParams.lights[i].length(); i++) {
  111. vec3 L = normalize(uboParams.lights[i].xyz - inWorldPos);
  112. Lo += specularContribution(L, V, N, F0, metallic, roughness);
  113. }
  114. vec2 brdf = texture(samplerBRDFLUT, vec2(max(dot(N, V), 0.0), roughness)).rg;
  115. vec3 reflection = prefilteredReflection(R, roughness).rgb;
  116. vec3 irradiance = texture(samplerIrradiance, N).rgb;
  117. // Diffuse based on irradiance
  118. vec3 diffuse = irradiance * ALBEDO;
  119. vec3 F = F_SchlickR(max(dot(N, V), 0.0), F0, roughness);
  120. // Specular reflectance
  121. vec3 specular = reflection * (F * brdf.x + brdf.y);
  122. // Ambient part
  123. vec3 kD = 1.0 - F;
  124. kD *= 1.0 - metallic;
  125. vec3 ambient = (kD * diffuse + specular);
  126. vec3 color = ambient + Lo;
  127. // Tone mapping
  128. color = Uncharted2Tonemap(color * uboParams.exposure);
  129. color = color * (1.0f / Uncharted2Tonemap(vec3(11.2f)));
  130. // Gamma correction
  131. color = pow(color, vec3(1.0f / uboParams.gamma));
  132. outColor = vec4(color, 1.0);
  133. }