No Description

pbrtexture.frag 4.9KB

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