koolkat

composite.fsh

Jan 3rd, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.90 KB | None | 0 0
  1. #version 120
  2.  
  3. uniform sampler2D gcolor;
  4. uniform sampler2D gdepth;
  5. uniform sampler2D gnormal;
  6. uniform sampler2D gaux1;   // specular map
  7. uniform sampler2D shadow;
  8.  
  9. varying vec4 texcoord;
  10.  
  11. varying vec3 lightVector;
  12. varying vec3 specMultiplier;
  13.  
  14. uniform mat4 gbufferProjectionInverse;
  15. uniform mat4 gbufferModelViewInverse;
  16. uniform mat4 shadowProjection;
  17. uniform mat4 shadowModelView;
  18.  
  19. varying vec3 heldLightSpecMultiplier;
  20. varying float heldLightMagnitude;
  21.  
  22. void main() {
  23.     gl_FragData[0] = texture2D(gcolor, texcoord.st);
  24.     gl_FragData[1] = texture2D(gdepth, texcoord.st);
  25.     gl_FragData[2] = texture2D(gnormal, texcoord.st);
  26.     gl_FragData[4] = texture2D(gaux1, texcoord.st);
  27.        
  28.     vec4 fragposition = gbufferProjectionInverse * vec4(texcoord.s * 2.0 - 1.0, texcoord.t * 2.0 - 1.0, 2.0 * texture2D(gdepth, texcoord.st).x - 1.0, 1.0);
  29.     fragposition /= fragposition.w;
  30.  
  31.     float distance = sqrt(fragposition.x * fragposition.x + fragposition.y * fragposition.y + fragposition.z * fragposition.z);
  32.  
  33.     float shading = 1.0;
  34.  
  35.     if (distance < 25.0 && distance > 0.1) {
  36.         // shadows
  37.         vec4 worldposition = gbufferModelViewInverse * fragposition;
  38.  
  39.         float xzDistanceSquared = worldposition.x * worldposition.x + worldposition.z * worldposition.z;
  40.         float yDistanceSquared  = worldposition.y * worldposition.y;
  41.        
  42.         if (yDistanceSquared < 225.0) {
  43.             worldposition = shadowModelView * worldposition;
  44.             float comparedepth = -worldposition.z;
  45.             worldposition = shadowProjection * worldposition;
  46.             worldposition /= worldposition.w;
  47.            
  48.             worldposition.st = worldposition.st * 0.5 + 0.5;
  49.                
  50.             if (comparedepth > 0.0 && worldposition.s < 1.0 && worldposition.s > 0.0 && worldposition.t < 1.0 && worldposition.t > 0.0){
  51.                 float shadowMult = min(1.0 - xzDistanceSquared / 625.0, 1.0) * min(1.0 - yDistanceSquared / 225.0, 1.0);
  52.                 float shadowSample = texture2D(shadow, worldposition.st).z;
  53.                 float shadowDepth = 2.0 * 0.05 * 256.0 / (256.0 + 0.05 - (2.0 * shadowSample - 1.0) * (256.0 - 0.05));
  54.                 shading = 1.0 - shadowMult * (clamp(comparedepth - shadowDepth - 0.2, 0.0, 3.0) / 3.0 * 0.4 - 0.2);
  55.             }
  56.         }
  57.     }
  58.     vec3 npos = normalize(fragposition.xyz);
  59.  
  60.     vec3 bump = reflect(npos, texture2D(gnormal, texcoord.st).xyz * 2.0 - 1.0);
  61.  
  62.     vec3 specularColor = texture2D(gaux1, texcoord.st).rgb;
  63.    
  64.     float s;
  65.     if(shading > 1.1) {
  66.         s = max(dot(bump, lightVector), 0.0);
  67.         gl_FragData[3] = vec4(min(texture2D(gcolor, texcoord.st).rgb + specularColor * s * s * s * specMultiplier, 1.0), 1.0);
  68.     }
  69.     else {
  70.         gl_FragData[3] = texture2D(gcolor, texcoord.st);
  71.     }
  72.     if (heldLightMagnitude > 0.0) {
  73.         if (distance < heldLightMagnitude && distance > 0.1) {
  74.             float intensity = 1.0 - min(distance / heldLightMagnitude, 1.0);
  75.             s = max(dot(bump, -npos), 0.0);
  76.             gl_FragData[3].rgb = min(gl_FragData[3].rgb + intensity * specularColor * s * s * s * heldLightSpecMultiplier, 1.0);
  77.         }
  78.     }
  79.    
  80.     gl_FragData[3].rgb *= shading;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment