SHARE
TWEET

Untitled

a guest Dec 25th, 2014 175 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 400
  2.  
  3. in vec2 coord;
  4.  
  5. uniform vec3 color;
  6. uniform sampler2D depthMap;
  7. uniform sampler2D thicknessMap;
  8. uniform sampler2D normalMap;
  9. uniform vec2 screenSize;
  10. uniform mat4 projection;
  11. uniform mat4 mView;
  12. uniform vec3 lightPos;
  13. uniform float zNear;
  14. uniform float zFar;
  15.  
  16. out vec4 fragColor;
  17.  
  18. const vec3 lightDir = vec3(.5, .5, .5);
  19. const float shininess = 300.0;
  20. const vec3 specularColor = vec3(1.0, 1.0, 1.0);
  21. const float fresPower = 0.1f;
  22. const float fresScale = 0.1;
  23. const float fresBias = 0.1;
  24. const vec3 thicknessRefraction = vec3(0.02, 0.03, 0.06);
  25.  
  26. float linearizeDepth(float depth) {    
  27.         return (2.0 * zNear) / (zFar + zNear - depth * (zFar - zNear));
  28. }
  29.  
  30. vec3 uvToEye(vec2 p, float z) {
  31.         vec2 pos = p * 2.0f - 1.0f;
  32.         vec4 clipPos = vec4(pos, z, 1.0f);
  33.         vec4 viewPos = inverse(projection) * clipPos;
  34.         return viewPos.xyz / viewPos.w;
  35. }
  36.  
  37. void main() {
  38.     float depth = texture(depthMap, coord).x;
  39.     depth = linearizeDepth(depth);
  40.     if (depth >= .99f) {
  41.         fragColor = vec4(1.0f);
  42.         return;
  43.      }
  44.    
  45.     vec3 pos = uvToEye(coord, depth);
  46.    
  47.         vec3 normal = texture(normalMap, coord).xyz;
  48.    
  49.     //Diffuse light
  50.     float diffuse = dot(normal, lightDir) * 0.5f + 0.5f;
  51.    
  52.     //Phong specular
  53.     vec3 viewDir = normalize(-pos);
  54.     vec3 halfVec = normalize(viewDir + lightDir);
  55.     float specular = pow(max(0.0f, dot(normal, halfVec)), shininess);
  56.    
  57.     //Fresnel
  58.     //float fresnel = fresBias + fresScale * pow(1.0f - max(0.0f, dot(normal, viewDir)), fresPower);
  59.    
  60.     //Color from absorption
  61.     float thickness = texture(thicknessMap, coord).x;
  62.     vec3 cBeer = vec3(exp(-.05*thickness), exp(-.05*thickness), exp(-.05*thickness));
  63.     vec3 absorbColor = color * cBeer;
  64.    
  65.     float alpha = min(dot(cBeer, cBeer), 0.8);
  66.    
  67.     //Compositing everything
  68.     vec4 finalColor = vec4(absorbColor + diffuse*specularColor.xyz*specular, alpha);
  69.    
  70.         fragColor = vec4(finalColor);
  71.        
  72.         gl_FragDepth = depth;
  73. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top