SHARE
TWEET

Untitled

a guest Jul 9th, 2017 133 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 120
  2.  
  3. //uniform sampler2D albedo_map;
  4. //uniform sampler2D normal_map;
  5. //uniform sampler2D metallic_map;
  6. //uniform sampler2D roughness_map;
  7. //uniform sampler2D ao_map;
  8.  
  9. uniform vec3 albedo;
  10. uniform vec3 normal;
  11. uniform float metallic;
  12. uniform float roughness;
  13. uniform float ao;
  14.  
  15. varying vec3 v_normal;
  16. varying vec3 v_pos;
  17.  
  18. #define PI 3.14159265359
  19.  
  20. float GGXDistribution(float NdotH, float roughness)
  21. {
  22.     float a = roughness*roughness;  // Disney's reparameterization for roughness
  23.     float a2 = a*a;
  24.     float denom = NdotH*NdotH * (a2 - 1.0) + 1.0;
  25.     denom = PI * denom * denom;
  26.     return a2 / denom;
  27. }
  28.  
  29. float SchlickGGXGeometry(float NdotV, float roughness)
  30. {
  31.     float r = roughness + 1.0;
  32.     float k = (r*r) * 0.125;
  33.     return NdotV / (NdotV * (1.0 - k) + k);
  34. }
  35.  
  36. float SmithGeometry(float NdotV, float NdotL, float roughness)
  37. {
  38.     return SchlickGGXGeometry(NdotV, roughness)*SchlickGGXGeometry(NdotL, roughness);
  39. }
  40.  
  41. vec3 SchlickFresnel(float cosTheta, vec3 F0)
  42. {
  43.     return F0 + (1.0 - F0) * pow(2.0, (-5.55473*cosTheta-6.98316)*cosTheta);
  44. }
  45.  
  46. void main()
  47. {
  48.     vec3 lightColor = vec3(23.47, 21.31, 20.79);
  49.     vec3 radiance = lightColor; // No attenuation
  50.  
  51.     vec2 texCoords = gl_TexCoord[0].st;
  52.  
  53.     //vec3 albedo = pow(texture2D(albedo_map, texCoords).rgb, vec3(2.2));
  54.     //float metallic = texture2D(metallic_map, texCoords).r;
  55.     //float roughness = texture2D(roughness_map, texCoords).r;
  56.     //float ao = texture2D(ao_map, texCoords).r;
  57.     vec3 N = normalize(v_normal);
  58.     // Assuming directional light source
  59.     vec3 V = -normalize(v_pos);
  60.     vec3 L = normalize(gl_LightSource[0].position.xyz);
  61.     vec3 H = normalize(V + L);
  62.  
  63.     float NdotL = max(dot(N, L), 0.0);
  64.     float NdotV = max(dot(N, V), 0.0);
  65.     float NdotH = max(dot(N, H), 0.0);
  66.  
  67.     float D = GGXDistribution(NdotH, roughness);
  68.     float G = SmithGeometry(NdotV, NdotL, roughness);
  69.     // Most dielectric surfaces look visually correct with a constant F0 of 0.04.
  70.     // For metallic surfaces F0 is given by the albedo.
  71.     vec3 F0 = vec3(0.04);
  72.     F0 = mix(F0, albedo, metallic);
  73.     // Use halfway vector instead of normal to avoid strong fresnel highlights
  74.     vec3 F = SchlickFresnel(max(dot(H, V), 0.0), F0);
  75.  
  76.     vec3 specular = (D * G * F) / (4 * NdotV * NdotL + 0.001);
  77.  
  78.     // Metallic surfaces don't have diffuse reflections
  79.     vec3 kD = (vec3(1.0) - F) * (1.0 - metallic);
  80.  
  81.     vec3 Lo = vec3(0.0);
  82.     Lo += (kD * albedo / PI + specular) * radiance * NdotL;
  83.     vec3 ambient = vec3(0.03) * albedo * ao;
  84.     vec3 color = ambient + Lo;
  85.  
  86.     // Reinhard tone mapping
  87.     color = color / (color + vec3(1.0));
  88.     // Gamma correction
  89.     color = pow(color, vec3(1.0/2.2));
  90.  
  91.     gl_FragColor = vec4(color, 1.0);
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top