[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);
|
---|
[3364] | 22 | const float blur = scale * m * focalLen *
|
---|
| 23 | dist / (sceneRange * (1e-6f + abs(color.w + dist * step(0, zFocus - color.w))));
|
---|
[3232] | 24 |
|
---|
[3235] | 25 | //float blur = saturate(blur * scale / maxCoC);
|
---|
[3232] | 26 |
|
---|
| 27 | // Depth/blurriness value scaled to 0..1 range
|
---|
[3235] | 28 | result = float2(color.w / sceneRange, blur);
|
---|
[3232] | 29 |
|
---|
| 30 | return result;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | float4 Blur(float4 color,
|
---|
| 35 | float2 texCoords,
|
---|
| 36 | float2 blurFactors,
|
---|
| 37 | sampler2D colorsTex,
|
---|
| 38 | float2 filterOffs[NUM_DOF_TABS]
|
---|
| 39 | )
|
---|
| 40 | {
|
---|
[3235] | 41 | float4 avgCol = float4(.0f);
|
---|
[3232] | 42 | float total_w = .0f;
|
---|
| 43 |
|
---|
| 44 | for (int i = 0; i < NUM_DOF_TABS; ++ i)
|
---|
| 45 | {
|
---|
[3235] | 46 | float2 sampleTexCoord = texCoords + filterOffs[i] * blurFactors.y;
|
---|
[3232] | 47 | float4 sampleCol = tex2Dlod(colorsTex, float4(sampleTexCoord, 0, 0));
|
---|
| 48 |
|
---|
| 49 | // combine the weights
|
---|
| 50 | float w = 1;
|
---|
| 51 |
|
---|
[3235] | 52 | avgCol += sampleCol * w;
|
---|
[3232] | 53 | total_w += w;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[3235] | 56 | avgCol /= max(total_w, 1e-6f);
|
---|
[3232] | 57 |
|
---|
[3235] | 58 | return avgCol;
|
---|
[3232] | 59 | }
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | float4 DepthOfField(fragment IN,
|
---|
| 63 | uniform sampler2D colorsTex,
|
---|
| 64 | uniform float2 filterOffs[NUM_DOF_TABS],
|
---|
[3235] | 65 | uniform float sceneRange,
|
---|
| 66 | uniform float zFocus
|
---|
[3232] | 67 | ): COLOR
|
---|
| 68 | {
|
---|
| 69 | // center color
|
---|
| 70 | const float4 color = tex2Dlod(colorsTex, float4(IN.texCoords, 0, 0));
|
---|
| 71 |
|
---|
[3235] | 72 | zFocus = min(zFocus, sceneRange * 0.5f);
|
---|
| 73 | float2 blurFactors = ComputeBlurFactors(color, sceneRange, zFocus);
|
---|
| 74 |
|
---|
| 75 | //float4 outCol = float4(color.x, color.y, blurFactors.y, color.w);
|
---|
[3232] | 76 | float4 outCol = Blur(color, IN.texCoords, blurFactors, colorsTex, filterOffs);
|
---|
| 77 |
|
---|
| 78 | return outCol;
|
---|
| 79 | } |
---|