SHARE
TWEET

Minecraft's Shaders: combined FXAA and Color Correction

a guest Sep 21st, 2013 153 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #extension GL_EXT_gpu_shader4 : enable
  2.  
  3. uniform sampler2D DiffuseSampler;
  4. uniform vec2 OutSize;
  5.  
  6. uniform float VxOffset;
  7. uniform float SpanMax;
  8. uniform float ReduceMul;
  9.  
  10. varying vec2 texCoord;
  11. varying vec4 posPos;
  12.  
  13. #define FxaaTex(t, p) texture2D(t, p)
  14.  
  15. #if __VERSION__ >= 130
  16.     #define OffsetVec(a, b) ivec2(a, b)
  17.     #define FxaaTexOff(t, p, o, r) textureOffset(t, p, o)
  18. #elif defined(GL_EXT_gpu_shader4)
  19.     #define OffsetVec(a, b) ivec2(a, b)
  20.     #define FxaaTexOff(t, p, o, r) texture2DLodOffset(t, p, 0.0, o)
  21. #else
  22.     #define OffsetVec(a, b) vec2(a, b)
  23.     #define FxaaTexOff(t, p, o, r) texture2D(t, p + o * r)
  24. #endif
  25.  
  26. vec3 colorCorrect (vec3 color)  {
  27.         vec3 Gray = vec3(0.3, 0.59, 0.11);
  28.         float Saturation = 1.8;
  29.         float Luma = dot(color, Gray);
  30.     vec3 Chroma = color - Luma;
  31.     color = (Chroma * Saturation) + Luma;
  32.     return color;
  33. }
  34.  
  35. vec3 FxaaPixelShader(
  36.   vec4 posPos,   // Output of FxaaVertexShader interpolated across screen.
  37.   sampler2D tex, // Input texture.
  38.   vec2 rcpFrame) // Constant {1.0/frameWidth, 1.0/frameHeight}.
  39. {
  40.  
  41.     #define FXAA_REDUCE_MIN   (1.0/128.0)
  42.     //#define FXAA_REDUCE_MUL   (1.0/8.0)
  43.     //#define FXAA_SPAN_MAX     8.0
  44.  
  45.     vec3 rgbNW = colorCorrect(FxaaTex(tex, posPos.zw).xyz);
  46.     vec3 rgbNE = colorCorrect(FxaaTexOff(tex, posPos.zw, OffsetVec(1,0), rcpFrame.xy).xyz);
  47.     vec3 rgbSW = colorCorrect(FxaaTexOff(tex, posPos.zw, OffsetVec(0,1), rcpFrame.xy).xyz);
  48.     vec3 rgbSE = colorCorrect(FxaaTexOff(tex, posPos.zw, OffsetVec(1,1), rcpFrame.xy).xyz);
  49.  
  50.     vec3 rgbM  = colorCorrect(FxaaTex(tex, posPos.xy).xyz);
  51.  
  52.     vec3 luma = vec3(0.299, 0.587, 0.114);
  53.     float lumaNW = dot(rgbNW, luma);
  54.     float lumaNE = dot(rgbNE, luma);
  55.     float lumaSW = dot(rgbSW, luma);
  56.     float lumaSE = dot(rgbSE, luma);
  57.     float lumaM  = dot(rgbM,  luma);
  58.  
  59.     float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
  60.     float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
  61.  
  62.     vec2 dir;
  63.     dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
  64.     dir.y =  ((lumaNW + lumaSW) - (lumaNE + lumaSE));
  65.  
  66.     float dirReduce = max(
  67.         (lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * ReduceMul),
  68.         FXAA_REDUCE_MIN);
  69.     float rcpDirMin = 1.0/(min(abs(dir.x), abs(dir.y)) + dirReduce);
  70.     dir = min(vec2( SpanMax,  SpanMax),
  71.           max(vec2(-SpanMax, -SpanMax),
  72.           dir * rcpDirMin)) * rcpFrame.xy;
  73.  
  74.     vec3 rgbA = (1.0/2.0) * (
  75.         FxaaTex(tex, posPos.xy + dir * vec2(1.0/3.0 - 0.5)).xyz +
  76.         FxaaTex(tex, posPos.xy + dir * vec2(2.0/3.0 - 0.5)).xyz);
  77.     vec3 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * (
  78.         FxaaTex(tex, posPos.xy + dir * vec2(0.0/3.0 - 0.5)).xyz +
  79.         FxaaTex(tex, posPos.xy + dir * vec2(3.0/3.0 - 0.5)).xyz);
  80.                
  81.         rgbA = colorCorrect(rgbA);
  82.         rgbB = colorCorrect(rgbB);
  83.  
  84.     float lumaB = dot(rgbB, luma);
  85.  
  86.     if ((lumaB < lumaMin) || (lumaB > lumaMax)) {
  87.         return rgbA;
  88.     } else {
  89.         return rgbB;
  90.     }
  91. }
  92.  
  93. void main() {
  94.     gl_FragColor = vec4(FxaaPixelShader(posPos, DiffuseSampler, 1.0 / OutSize), 1.0);
  95. }
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