koolkat

composite.fsh

Jan 24th, 2012
3,217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 120
  2.  
  3. //FXAA shader by kool_Kat
  4.  
  5. uniform sampler2D gcolor;
  6. uniform sampler2D gdepth;
  7.  
  8. uniform float viewHeight;
  9. uniform float viewWidth;
  10.  
  11. varying vec4 texcoord;
  12.  
  13.  
  14. vec4 fxaa() {
  15.     float pw = 1.0 / viewWidth;
  16.     float ph = 1.0 / viewHeight;
  17.    
  18.     vec3 rgbNW = texture2D(gcolor, texcoord.xy + vec2(-pw,-ph)).xyz;
  19.     vec3 rgbNE = texture2D(gcolor, texcoord.xy + vec2(pw,-ph)).xyz;
  20.     vec3 rgbSW = texture2D(gcolor, texcoord.xy + vec2(-pw,ph)).xyz;
  21.     vec3 rgbSE = texture2D(gcolor, texcoord.xy + vec2(pw,ph)).xyz;
  22.     vec3 rgbM =  texture2D(gcolor, texcoord.xy).xyz;
  23.    
  24.     vec3 luma = vec3(0.299, 0.587, 0.114);
  25.     float lumaNW = dot(rgbNW, luma);
  26.     float lumaNE = dot(rgbNE, luma);
  27.     float lumaSW = dot(rgbSW, luma);
  28.     float lumaSE = dot(rgbSE, luma);
  29.     float lumaM = dot(rgbM, luma);
  30.    
  31.     float lumaMin = min(lumaM, min(min(lumaNW,lumaNE),min(lumaSW,lumaSE)));
  32.     float lumaMax = max(lumaM, max(max(lumaNW,lumaNE),max(lumaSW,lumaSE)));
  33.    
  34.     vec2 dir;
  35.     dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
  36.     dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
  37.    
  38.     float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * 1.0/8.0), 1.0/128.0);
  39.    
  40.     float rcpDirMin = 1.0 /(min(abs(dir.x), abs(dir.y)) + dirReduce);
  41.    
  42.     dir = min(vec2(4.0,4.0),max(vec2(-4.0,-4.0),dir * rcpDirMin)) * vec2(pw,ph);
  43.    
  44.     vec3 rgbA = (1.0/2.0) * (texture2D(gcolor, texcoord.xy + dir * (1.0/3,0 - 0.5)).xyz +
  45.                             texture2D(gcolor, texcoord.xy + dir * (2.0/3.0 - 0.5)).xyz);
  46.                            
  47.     vec3 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * (texture2D(gcolor, texcoord.xy + dir * (0.0/3.0 - 0.5)).xyz +
  48.                                                 texture2D(gcolor, texcoord.xy + dir * (3.0/3.0 - 0.5)).xyz);
  49.    
  50.     float lumaB = dot(rgbB, luma);
  51.    
  52.     if((lumaB < lumaMin) || (lumaB > lumaMax)) {
  53.         return vec4(rgbA , 1.0);
  54.     }
  55.     return vec4(rgbB, 1.0);
  56. }
  57.  
  58. void main() {
  59.     gl_FragData[0] = texture2D(gcolor, texcoord.st);
  60.     gl_FragData[1] = texture2D(gdepth, texcoord.st);
  61.     gl_FragData[3] = fxaa();
  62. }
Advertisement
Add Comment
Please, Sign In to add comment