[2968] | 1 | #include "../shaderenv.h"
|
---|
[2967] | 2 |
|
---|
[2974] | 3 | struct fragment
|
---|
[2968] | 4 | {
|
---|
| 5 | float2 c: TEXCOORD0; // center
|
---|
| 6 | float2 lt: TEXCOORD1; // left top
|
---|
| 7 | float2 rb: TEXCOORD2; // right bottom
|
---|
| 8 | float2 rt: TEXCOORD3; // right top
|
---|
| 9 | float2 lb: TEXCOORD4; // left bottom
|
---|
| 10 | float4 lr: TEXCOORD5; // left / right
|
---|
| 11 | float4 tb: TEXCOORD6; // top / bottom
|
---|
| 12 | };
|
---|
[2967] | 13 |
|
---|
[3026] | 14 | // the barrier for detecting a discontinuity (x = normal, y = depth)
|
---|
| 15 | uniform float4 e_barrier = float4(5e-5, 5e-5, 0, 0);
|
---|
| 16 | // the weights for normal / depth discontinuity (x = normal, y = depth)
|
---|
| 17 | uniform float4 e_weights = float4(0.5f, 0.5f, 1.0f, 1.0f);
|
---|
| 18 | //uniform float4 e_weights = float4(1.0f, 1.0f, 1.0f, 1.0f);
|
---|
[2968] | 19 | uniform float4 e_kernel = float4(0.5f, 1.0f, 1.0f, 1.0f);
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 |
|
---|
[2974] | 23 | float4 main(fragment IN,
|
---|
[2968] | 24 | uniform sampler2D colors,
|
---|
[2973] | 25 | uniform sampler2D normals
|
---|
[2968] | 26 | ): COLOR
|
---|
| 27 | {
|
---|
| 28 | //return tex2D(colors, IN.c.xy);
|
---|
| 29 |
|
---|
| 30 | // normal discontinuity filter
|
---|
| 31 | float3 nc = (float3)tex2D(normals, IN.c.xy);
|
---|
| 32 |
|
---|
| 33 | float4 nd;
|
---|
| 34 | nd.x = dot(nc, float3(tex2D(normals, IN.lt.xy)));
|
---|
| 35 | nd.y = dot(nc, float3(tex2D(normals, IN.rb.xy)));
|
---|
| 36 | nd.z = dot(nc, float3(tex2D(normals, IN.rt.xy)));
|
---|
| 37 | nd.w = dot(nc, float3(tex2D(normals, IN.lb.xy)));
|
---|
| 38 |
|
---|
| 39 | nd -= e_barrier.x;
|
---|
| 40 | nd = step((float4)0.0f, nd);
|
---|
| 41 |
|
---|
| 42 | float ne = saturate(dot(nd, e_weights.x));
|
---|
| 43 |
|
---|
| 44 | // construct opposite coordinates
|
---|
| 45 | float4 lrr = IN.lr.wzyx;
|
---|
| 46 | float4 tbr = IN.tb.wzyx;
|
---|
| 47 |
|
---|
| 48 | // depth filter: compute gradient difference:
|
---|
| 49 | // (c - sample) + (c - opposite sample)
|
---|
| 50 |
|
---|
| 51 | float4 dc = float4(tex2D(colors, IN.c).w);
|
---|
| 52 |
|
---|
| 53 | float pos_lt = (float)tex2D(colors, IN.lt.xy).w;
|
---|
| 54 | float pos_rb = (float)tex2D(colors, IN.rb.xy).w;
|
---|
| 55 |
|
---|
| 56 | float pos_lb = (float)tex2D(colors, IN.lb.xy).w;
|
---|
| 57 | float pos_rt = (float)tex2D(colors, IN.rt.xy).w;
|
---|
| 58 |
|
---|
| 59 | float pos_l = (float)tex2D(colors, IN.lr.xy).w;
|
---|
| 60 | float pos_r = (float)tex2D(colors, lrr.xy).w;
|
---|
| 61 |
|
---|
| 62 | float pos_t = (float)tex2D(colors, IN.tb.xy).w;
|
---|
| 63 | float pos_b = (float)tex2D(colors, tbr.xy).w;
|
---|
| 64 |
|
---|
| 65 | float4 dd;
|
---|
| 66 |
|
---|
| 67 | dd.x = pos_lt + pos_rb;
|
---|
| 68 | dd.y = pos_lb + pos_rt;
|
---|
| 69 | dd.z = pos_l + pos_r;
|
---|
| 70 | dd.w = pos_t + pos_b;
|
---|
| 71 |
|
---|
| 72 | dd = abs(2.0f * dc - dd) - e_barrier.y;
|
---|
| 73 | dd = step(dd, (float4)0.0f);
|
---|
| 74 |
|
---|
| 75 | float de = saturate(dot(dd, e_weights.y));
|
---|
| 76 |
|
---|
| 77 | // weight: 0 = no aa, 1 = full antialiasing
|
---|
| 78 | float w = (1.0f - de * ne) * e_kernel.x;
|
---|
| 79 |
|
---|
| 80 | // smoothed color
|
---|
| 81 | // (a - c) * w + c = a * w + c * (1 - w)
|
---|
| 82 | float2 offset = IN.c.xy * (1.0f - w);
|
---|
| 83 |
|
---|
| 84 | float4 s0 = tex2Dlod(colors, float4(offset + IN.lt.xy * w, 0, 0));
|
---|
| 85 | float4 s1 = tex2Dlod(colors, float4(offset + IN.rb.xy * w, 0, 0));
|
---|
| 86 | float4 s2 = tex2Dlod(colors, float4(offset + IN.rt.xy * w, 0, 0));
|
---|
| 87 | float4 s3 = tex2Dlod(colors, float4(offset + IN.lb.xy * w, 0, 0));
|
---|
| 88 |
|
---|
[2976] | 89 | float4 centerColor = tex2Dlod(colors, float4(IN.c.xy, 0, 0));
|
---|
[2968] | 90 |
|
---|
[2976] | 91 | float4 col = (s0 + s1 + s2 + s3 + centerColor) * 0.2f;
|
---|
[2968] | 92 | //return (s0 + s1 + s2 + s3) * 0.25f;
|
---|
| 93 |
|
---|
[2976] | 94 | col.w = centerColor.w;
|
---|
| 95 |
|
---|
[2968] | 96 | return col;
|
---|
[2865] | 97 | } |
---|