[3255] | 1 | struct VS_OUT
|
---|
| 2 | {
|
---|
| 3 | float4 hPosition : POSITION;
|
---|
| 4 | float2 texCoord : TEXCOORD0;
|
---|
| 5 | };
|
---|
| 6 |
|
---|
| 7 | VS_OUT Default_VS(float4 position : POSITION,
|
---|
| 8 | float4 texCoord : TEXCOORD0)
|
---|
| 9 | {
|
---|
| 10 | VS_OUT OUT;
|
---|
| 11 | OUT.hPosition = float4(sign(position.xy),0,1);
|
---|
| 12 | OUT.texCoord = (float2(OUT.hPosition.x, -OUT.hPosition.y) + 1.0) / 2.0;
|
---|
| 13 | return OUT;
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | #define NUM_DOF_TAPS 12
|
---|
| 17 |
|
---|
| 18 | float4 DoF_PS(VS_OUT IN,
|
---|
| 19 | uniform float width,
|
---|
| 20 | uniform float height,
|
---|
| 21 | //uniform float focalDist,
|
---|
| 22 | uniform float focalRange,
|
---|
| 23 | uniform float maxCoC,
|
---|
| 24 | uniform sampler2D ImageTexture: register(s0),
|
---|
| 25 | uniform sampler2D DepthTexture: register(s1)
|
---|
| 26 | ) : COLOR
|
---|
| 27 | {
|
---|
| 28 | float2 filterTaps[NUM_DOF_TAPS];
|
---|
| 29 |
|
---|
| 30 | float dx = 1.0f / width;
|
---|
| 31 | float dy = 1.0f / height;
|
---|
| 32 |
|
---|
| 33 | filterTaps[0] = float2(-0.326212f * dx, -0.405805f * dy);
|
---|
| 34 | filterTaps[1] = float2(-0.840144f * dx, -0.07358f * dy);
|
---|
| 35 | filterTaps[2] = float2(-0.695914f * dx, 0.457137f * dy);
|
---|
| 36 | filterTaps[3] = float2(-0.203345f * dx, 0.620716f * dy);
|
---|
| 37 | filterTaps[4] = float2(0.96234f * dx, -0.194983f * dy);
|
---|
| 38 | filterTaps[5] = float2(0.473434f * dx, -0.480026f * dy);
|
---|
| 39 | filterTaps[6] = float2(0.519456f * dx, 0.767022f * dy);
|
---|
| 40 | filterTaps[7] = float2(0.185461f * dx, -0.893124f * dy);
|
---|
| 41 | filterTaps[8] = float2(0.507431f * dx, 0.064425f * dy);
|
---|
| 42 | filterTaps[9] = float2(0.89642f * dx, 0.412458f * dy);
|
---|
| 43 | filterTaps[10] = float2(-0.32194f * dx, -0.932615f * dy);
|
---|
| 44 | filterTaps[11] = float2(-0.791559f * dx, -0.597705f * dy);
|
---|
| 45 |
|
---|
| 46 | IN.texCoord += float2(0.5/width, 0.5/height);
|
---|
| 47 |
|
---|
| 48 | float focalDist = tex2D(DepthTexture, float2(0.5,0.5)).x;
|
---|
| 49 | float4 color = tex2D(ImageTexture, IN.texCoord);
|
---|
| 50 | float centerDepth = color.a;//tex2D(DepthTexture, IN.texCoord).x;
|
---|
| 51 | //return abs(centerDepth - focalDist);
|
---|
| 52 | float blur = saturate(abs(1/centerDepth - 1/focalDist) * focalRange);
|
---|
| 53 |
|
---|
| 54 | float sizeCoC = blur * maxCoC;
|
---|
| 55 |
|
---|
| 56 | float totalContribution = 1.0f;
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | // Run through all taps
|
---|
| 60 | for (int i = 0; i < NUM_DOF_TAPS; i++)
|
---|
| 61 | {
|
---|
| 62 | // Compute tap coordinates
|
---|
| 63 | float2 tapCoord = IN.texCoord + filterTaps[i] * sizeCoC;
|
---|
| 64 |
|
---|
| 65 | // Fetch tap sample
|
---|
| 66 | float4 tapColor = tex2D(ImageTexture, tapCoord);
|
---|
| 67 | float tapDepth = tapColor.a;//tex2D(DepthTexture, tapCoord).x;
|
---|
| 68 |
|
---|
| 69 | float blur = saturate(abs(tapDepth - focalDist) * focalRange);
|
---|
| 70 |
|
---|
| 71 | // Compute tap contribution
|
---|
| 72 | float tapContribution = (tapDepth > centerDepth) ? 1.0f : blur;
|
---|
| 73 |
|
---|
| 74 | // Accumulate color and contribution
|
---|
| 75 | color += tapColor * tapContribution;
|
---|
| 76 | totalContribution += tapContribution;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | // Normalize to get proper luminance
|
---|
| 80 | float4 finalColor = color / totalContribution;
|
---|
| 81 |
|
---|
| 82 | //return tex2D(ImageTexture, IN.texCoord).r;
|
---|
| 83 | return finalColor;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | float4 sampleCenter_PS(VS_OUT IN,uniform sampler2D ImageTexture: register(s0), uniform float blending):COLOR
|
---|
| 87 | {
|
---|
| 88 | float dx = 1.0 / 640.0;
|
---|
| 89 | float dy = 1.0 / 480.0;
|
---|
| 90 | float sample = 0;
|
---|
| 91 | sample += tex2D(ImageTexture, float2(0.5, 0.5)).a;
|
---|
| 92 | sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(dx, 0)).a;
|
---|
| 93 | sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(dx, dy)).a;
|
---|
| 94 | sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(0, dy)).a;
|
---|
| 95 | sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(-dx, 0)).a;
|
---|
| 96 | sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(-dx, -dy)).a;
|
---|
| 97 | sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(0, -dy)).a;
|
---|
| 98 | sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(-dx, dy)).a;
|
---|
| 99 | sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(dx, -dy)).a;
|
---|
| 100 | sample /= 9.0;
|
---|
| 101 |
|
---|
| 102 | return float4(sample, sample, sample, blending);
|
---|
| 103 | } |
---|