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