[3232] | 1 | #include "../shaderenv.h"
|
---|
| 2 |
|
---|
| 3 | struct fragment
|
---|
| 4 | {
|
---|
| 5 | float2 texCoords: TEXCOORD0;
|
---|
| 6 | };
|
---|
| 7 |
|
---|
| 8 |
|
---|
[3235] | 9 | float2 ComputeBlurFactors(float4 color, float sceneRange, float zFocus)
|
---|
[3232] | 10 | {
|
---|
| 11 | float2 result;
|
---|
| 12 |
|
---|
[3235] | 13 | const float m = 1.0f;
|
---|
| 14 | const float focalLen = 1.0f;
|
---|
| 15 |
|
---|
| 16 | const float scale = 5e4f;
|
---|
| 17 |
|
---|
[3232] | 18 | // Compute blur factor based on the CoC size scaled and normalized to 0..1 range
|
---|
[3235] | 19 | //float blur = abs(dLens * focalLen * (zFocus - color.w)) / (1e-6f + abs(zFocus * (color.w - focalLen)));
|
---|
| 20 |
|
---|
| 21 | const float dist = abs(zFocus - color.w);
|
---|
| 22 | const float blur = scale * m * focalLen * dist / (sceneRange * (1e-6f + abs(color.w + dist * step(0, zFocus - color.w))));
|
---|
[3232] | 23 |
|
---|
[3235] | 24 | //float blur = saturate(blur * scale / maxCoC);
|
---|
[3232] | 25 |
|
---|
| 26 | // Depth/blurriness value scaled to 0..1 range
|
---|
[3235] | 27 | result = float2(color.w / sceneRange, blur);
|
---|
[3232] | 28 |
|
---|
| 29 | return result;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | float4 Blur(float4 color,
|
---|
| 34 | float2 texCoords,
|
---|
| 35 | float2 blurFactors,
|
---|
| 36 | sampler2D colorsTex,
|
---|
| 37 | float2 filterOffs[NUM_DOF_TABS]
|
---|
| 38 | )
|
---|
| 39 | {
|
---|
[3235] | 40 | float4 avgCol = float4(.0f);
|
---|
[3232] | 41 | float total_w = .0f;
|
---|
| 42 |
|
---|
| 43 | for (int i = 0; i < NUM_DOF_TABS; ++ i)
|
---|
| 44 | {
|
---|
[3235] | 45 | float2 sampleTexCoord = texCoords + filterOffs[i] * blurFactors.y;
|
---|
[3232] | 46 | float4 sampleCol = tex2Dlod(colorsTex, float4(sampleTexCoord, 0, 0));
|
---|
| 47 |
|
---|
| 48 | // combine the weights
|
---|
| 49 | float w = 1;
|
---|
| 50 |
|
---|
[3235] | 51 | avgCol += sampleCol * w;
|
---|
[3232] | 52 | total_w += w;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[3235] | 55 | avgCol /= max(total_w, 1e-6f);
|
---|
[3232] | 56 |
|
---|
[3235] | 57 | return avgCol;
|
---|
[3232] | 58 | }
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | float4 DepthOfField(fragment IN,
|
---|
| 62 | uniform sampler2D colorsTex,
|
---|
| 63 | uniform float2 filterOffs[NUM_DOF_TABS],
|
---|
[3235] | 64 | uniform float sceneRange,
|
---|
| 65 | uniform float zFocus
|
---|
[3232] | 66 | ): COLOR
|
---|
| 67 | {
|
---|
| 68 | // center color
|
---|
| 69 | const float4 color = tex2Dlod(colorsTex, float4(IN.texCoords, 0, 0));
|
---|
| 70 |
|
---|
[3235] | 71 | zFocus = min(zFocus, sceneRange * 0.5f);
|
---|
| 72 | float2 blurFactors = ComputeBlurFactors(color, sceneRange, zFocus);
|
---|
| 73 |
|
---|
| 74 | //float4 outCol = float4(color.x, color.y, blurFactors.y, color.w);
|
---|
[3232] | 75 | float4 outCol = Blur(color, IN.texCoords, blurFactors, colorsTex, filterOffs);
|
---|
| 76 |
|
---|
| 77 | return outCol;
|
---|
| 78 | } |
---|