[2968] | 1 | #include "../shaderenv.h"
|
---|
[2967] | 2 |
|
---|
[2974] | 3 | struct fragment
|
---|
[2968] | 4 | {
|
---|
[3136] | 5 | float2 texCoords: TEXCOORD0;
|
---|
[2968] | 6 | };
|
---|
[2967] | 7 |
|
---|
[3026] | 8 | // the barrier for detecting a discontinuity (x = normal, y = depth)
|
---|
[3136] | 9 | //const uniform float4 barrier = float4(5e-5f, 5e-5f, 0, 0);
|
---|
| 10 | const uniform float4 barrier = float4(0.6f, 9e-2f, 0, 0);
|
---|
[3026] | 11 | // the weights for normal / depth discontinuity (x = normal, y = depth)
|
---|
[3136] | 12 | const uniform float4 weights = float4(0.5f, 0.5f, 1.0f, 1.0f);
|
---|
| 13 | //uniform float4 weights = float4(1.0f, 1.0f, 1.0f, 1.0f);
|
---|
| 14 | // the maximal size of the filter kernel
|
---|
| 15 | const uniform float kernel = 0.5f;
|
---|
[2968] | 16 |
|
---|
| 17 |
|
---|
| 18 |
|
---|
[2974] | 19 | float4 main(fragment IN,
|
---|
[2968] | 20 | uniform sampler2D colors,
|
---|
[3136] | 21 | uniform sampler2D normals,
|
---|
| 22 | uniform float2 offsets[8]): COLOR
|
---|
[2968] | 23 | {
|
---|
[3136] | 24 | // center normal
|
---|
[3137] | 25 | const float3 centerNormal = tex2Dlod(normals, float4(IN.texCoords, 0, 0)).xyz;
|
---|
[3136] | 26 | // center color
|
---|
| 27 | const float4 centerColor = tex2Dlod(colors, float4(IN.texCoords, 0, 0));
|
---|
| 28 | // center depth
|
---|
| 29 | const float4 centerDepth = float4(centerColor.w);
|
---|
| 30 |
|
---|
| 31 | const float2 lt = IN.texCoords + offsets[0];
|
---|
| 32 | const float2 rb = IN.texCoords + offsets[1];
|
---|
| 33 | const float2 rt = IN.texCoords + offsets[2];
|
---|
| 34 | const float2 lb = IN.texCoords + offsets[3];
|
---|
[2968] | 35 |
|
---|
[3136] | 36 | // compute normal discontinuities
|
---|
[2968] | 37 | float4 nd;
|
---|
| 38 |
|
---|
[3137] | 39 | nd.x = dot(centerNormal, tex2Dlod(normals, float4(lt, 0, 0)).xyz);
|
---|
| 40 | nd.y = dot(centerNormal, tex2Dlod(normals, float4(rb, 0, 0)).xyz);
|
---|
| 41 | nd.z = dot(centerNormal, tex2Dlod(normals, float4(rt, 0, 0)).xyz);
|
---|
| 42 | nd.w = dot(centerNormal, tex2Dlod(normals, float4(lb, 0, 0)).xyz);
|
---|
[2968] | 43 |
|
---|
[3136] | 44 | nd -= float4(barrier.x);
|
---|
| 45 | nd = step(float4(0.0f), nd);
|
---|
[2968] | 46 |
|
---|
[3136] | 47 | // weight the influence of normal discontinuities
|
---|
| 48 | //const float ne = saturate(dot(nd, weights.x));
|
---|
| 49 | const float ne = nd.x * nd.y *nd.z * nd.w * weights.x;
|
---|
[2968] | 50 |
|
---|
[3146] | 51 |
|
---|
[3136] | 52 | ////////////
|
---|
| 53 | // depth filter => compute gradient difference
|
---|
[2968] | 54 | // (c - sample) + (c - opposite sample)
|
---|
| 55 |
|
---|
[3136] | 56 | float depthVals[8];
|
---|
[2968] | 57 |
|
---|
[3136] | 58 | for (int i = 0; i < 8; ++ i)
|
---|
| 59 | {
|
---|
| 60 | depthVals[i] = (float)tex2Dlod(colors, float4(IN.texCoords + offsets[i], 0, 0)).w;
|
---|
| 61 | }
|
---|
[2968] | 62 |
|
---|
[3136] | 63 | float4 dd; // the gradients
|
---|
[2968] | 64 |
|
---|
[3136] | 65 | for (int i = 0; i < 4; ++ i)
|
---|
| 66 | {
|
---|
[3146] | 67 | dd[i] = depthVals[i * 2] + depthVals[i * 2 + 1];
|
---|
[3136] | 68 | }
|
---|
[2968] | 69 |
|
---|
[3136] | 70 | //dd = abs(2.0f * (1.0f - abs(1.0f - centerDepth / dd))) - barrier.y;
|
---|
| 71 | dd = abs(2.0f * centerDepth - dd) - barrier.y;
|
---|
| 72 | dd = step(dd, float4(0.0f));
|
---|
[2968] | 73 |
|
---|
[3137] | 74 | //const float de = saturate(dot(dd, weights.y));
|
---|
| 75 | const float de = weights.y * dd.x + dd.y + dd.z + dd.w;
|
---|
[2968] | 76 |
|
---|
| 77 |
|
---|
[3136] | 78 | ///////////////////////////
|
---|
[2968] | 79 |
|
---|
[3136] | 80 | // weight: 0 = no antialiasing, 1 = full antialiasing
|
---|
| 81 | const float w = (1.0f - de * ne) * kernel.x;
|
---|
[2968] | 82 |
|
---|
[3136] | 83 | // smoothed color: the offset is big where discontinuities are
|
---|
[2968] | 84 | // (a - c) * w + c = a * w + c * (1 - w)
|
---|
[3136] | 85 | const float2 width = IN.texCoords * (1.0f - w);
|
---|
[2968] | 86 |
|
---|
[3136] | 87 | float4 s0 = tex2Dlod(colors, float4(width + lt * w, 0, 0));
|
---|
| 88 | float4 s1 = tex2Dlod(colors, float4(width + rb * w, 0, 0));
|
---|
| 89 | float4 s2 = tex2Dlod(colors, float4(width + rt * w, 0, 0));
|
---|
| 90 | float4 s3 = tex2Dlod(colors, float4(width + lb * w, 0, 0));
|
---|
[2968] | 91 |
|
---|
[3146] | 92 |
|
---|
[3136] | 93 | //float4 col = (s0 + s1 + s2 + s3 + centerColor) * 0.2f;
|
---|
[3137] | 94 | float4 col = (s0 + s1 + s2 + s3) * 0.25f;
|
---|
| 95 | //float4 col = float4(ne.x, ne.x, ne.x, 0);
|
---|
[3136] | 96 | //float4 col = float4(de.x, de.x, de.x, 0);
|
---|
| 97 | //float4 col = float4(w, w, w, 0);
|
---|
[3146] | 98 | //float4 col = float4(centerNormal * 0.5f + float3(0.5f), 0);
|
---|
| 99 | //float4 col = float4(centerNormal, 0);
|
---|
[3147] | 100 |
|
---|
[3136] | 101 | // push through the current depth
|
---|
| 102 | col.w = centerDepth.x;
|
---|
[2968] | 103 |
|
---|
| 104 | return col;
|
---|
[2865] | 105 | } |
---|