Advertisement
purplejragon

Untitled

Feb 4th, 2021
1,744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 330 core
  2. out vec4 FragColor;
  3. in vec2 TexCoords;
  4. in vec3 WorldPos;
  5. in vec3 Normal;
  6.  
  7. uniform sampler2D albedoMap;
  8. uniform sampler2D metallicMap;
  9. uniform sampler2D roughnessMap;
  10. uniform sampler2D normalMap;
  11. uniform sampler2D aoMap;
  12.  
  13. uniform vec3 lightPositions[1];
  14. uniform vec3 lightColors[1];
  15.  
  16. uniform vec3 camPos;
  17.  
  18. const float PI = 3.14159265359;
  19.  
  20. vec3 getNormalFromMap()
  21. {
  22.     vec3 tangentNormal = texture(normalMap, TexCoords).xyz * 2.0 - 1.0;
  23.  
  24.     vec3 Q1  = dFdx(WorldPos);
  25.     vec3 Q2  = dFdy(WorldPos);
  26.     vec2 st1 = dFdx(TexCoords);
  27.     vec2 st2 = dFdy(TexCoords);
  28.  
  29.     vec3 N   = normalize(Normal);
  30.     vec3 T  = normalize(Q1*st2.t - Q2*st1.t);
  31.     vec3 B  = -normalize(cross(N, T));
  32.     mat3 TBN = mat3(T, B, N);
  33.  
  34.     return normalize(TBN * tangentNormal);
  35. }
  36. float DistributionGGX(vec3 N, vec3 H, float roughness)
  37. {
  38.     float a = roughness * roughness;
  39.     float a2 = a * a;
  40.     float NdotH = max(dot(N, H), 0.0);
  41.     float NdotH2 = NdotH*NdotH;
  42.  
  43.     float nom = a2;
  44.     float denom = (NdotH2 * (a2 - 1.0) + 1.0);
  45.     denom = PI * denom * denom;
  46.  
  47.     return nom / max(denom, 0.0000001);
  48. }
  49. float GeometrySchlickGGX(float NdotV, float roughness)
  50. {
  51.     float r = (roughness + 1.0);
  52.     float k = (r*r) / 8.0;
  53.  
  54.     float nom = NdotV;
  55.     float denom = NdotV * (1.0 - k) + k;
  56.  
  57.     return nom / denom;
  58. }
  59. float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
  60. {
  61.     float NdotV = max(dot(N, V), 0.0);
  62.     float NdotL = max(dot(N, L), 0.0);
  63.     float ggx2 = GeometrySchlickGGX(NdotV, roughness);
  64.     float ggx1 = GeometrySchlickGGX(NdotL, roughness);
  65.  
  66.     return ggx1 * ggx2;
  67. }
  68. vec3 fresnelSchlick(float cosTheta, vec3 F0)
  69. {
  70.     return F0 + (1.0 - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0);
  71. }
  72. void main()
  73. {
  74.     vec3 albedo = pow(texture(albedoMap, TexCoords).rgb, vec3(2.2));
  75.     float metallic = texture(metallicMap, TexCoords).r;
  76.     float roughness = texture(roughnessMap, TexCoords).r;
  77.     float ao = texture(aoMap, TexCoords).r;
  78.     vec3 N = getNormalFromMap();
  79.  
  80.     vec3 V = normalize(camPos - WorldPos);
  81.  
  82.     vec3 F0 = vec3(0.04);
  83.     F0 = mix(F0, albedo, metallic);
  84.  
  85.     vec3 Lo = vec3(0.0);
  86.     for(int i = 0; i < 1; ++i)
  87.     {
  88.         vec3 L = normalize(lightPositions[i] - WorldPos);
  89.         vec3 H = normalize(V + L);
  90.         float distance = length(lightPositions[i] - WorldPos);
  91.         float attenuation = 1.0 / (distance * distance);
  92.         vec3 radiance = lightColors[i] * attenuation;
  93.  
  94.         float NDF = DistributionGGX(N, H, roughness);  
  95.         float G = GeometrySmith(N, V, L, roughness);      
  96.         vec3 F = fresnelSchlick(clamp(dot(H, V), 0.0, 1.0), F0);
  97.            
  98.         vec3 nominator = NDF * G * F;
  99.         float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
  100.         vec3 specular = nominator / max(denominator, 0.001); // prevent divide by zero for NdotV=0.0 or NdotL=0.0
  101.        
  102.         vec3 kS = F;
  103.         vec3 kD = vec3(1.0) - kS;
  104.         kD *= 1.0 - metallic;    
  105.  
  106.         float NdotL = max(dot(N, L), 0.0);        
  107.  
  108.         Lo += (kD * albedo / PI + specular) * radiance * NdotL;
  109.     }  
  110.    
  111.     vec3 ambient = vec3(0.03) * albedo * ao;
  112.  
  113.     vec3 color = ambient + Lo;
  114.  
  115.     color = color / (color + vec3(1.0));
  116.  
  117.     // Do this:
  118.     //FragColor = vec4(color, 1.0);
  119.     FragColor = vec4(vec3(texture(albedoMap, TexCoords)), 1.0);
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement