-
Notifications
You must be signed in to change notification settings - Fork 3
/
final09_DOF.fsh
173 lines (152 loc) · 5.51 KB
/
final09_DOF.fsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
#version 130
uniform sampler2D sampler0;
uniform sampler2D sampler1;
uniform sampler2D sampler2;
uniform sampler2D cleanDepth;
uniform float aspectRatio;
uniform float near;
uniform float far;
vec4 TexCoord0 = gl_TexCoord[0];
float INFINITY = 1000.0;
#ifdef DOF_ENABLED
float
getDepth (vec2 coord)
{
float depth = texture2D (cleanDepth, coord).x;
float depth2 = texture2D (sampler2, coord).x;
if (depth2 < 1.0)
{
depth = depth2;
}
if (depth == 1.0)
{
return INFINITY;
}
depth = (2.0 * near) / (far + near - depth * (far - near));
return depth;
}
float samples = float (0);
vec2 space;
float
getCursorDepth (vec2 coord)
{
return (2.0 * near) / (far + near -
texture2D (cleanDepth, coord).x * (far - near));
}
vec4
getSampleWithBoundsCheck (vec2 offset)
{
vec2 coord = TexCoord0.st + offset;
if (coord.s <= 1.0 && coord.s >= 0.0 && coord.t <= 1.0 && coord.t >= 0.0)
{
samples += 1.0;
return texture2D (sampler0, coord);
}
else
{
return vec4 (0.0);
}
}
vec4
getBlurredColor ()
{
vec4 blurredColor = vec4 (0.0);
float depth = getDepth (TexCoord0.xy);
vec2 aspectCorrection = vec2 (1.0, aspectRatio) * 0.005;
vec2 ac0_4 = 0.4 * aspectCorrection; // 0.4
vec2 ac0_29 = 0.29 * aspectCorrection; // 0.29
vec2 ac0_15 = 0.15 * aspectCorrection; // 0.15
vec2 ac0_37 = 0.37 * aspectCorrection; // 0.37
vec2 lowSpace = TexCoord0.st;
vec2 highSpace = 1.0 - lowSpace;
space =
vec2 (min (lowSpace.s, highSpace.s), min (lowSpace.t, highSpace.t));
if (space.s >= ac0_4.s && space.t >= ac0_4.t)
{
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (0.0, ac0_4.t));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (ac0_4.s, 0.0));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (0.0, -ac0_4.t));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (-ac0_4.s, 0.0));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (ac0_29.s, -ac0_29.t));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (ac0_29.s, ac0_29.t));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (-ac0_29.s, ac0_29.t));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (-ac0_29.s, -ac0_29.t));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (ac0_15.s, ac0_37.t));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (-ac0_37.s, ac0_15.t));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (ac0_37.s, -ac0_15.t));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (-ac0_15.s, -ac0_37.t));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (-ac0_15.s, ac0_37.t));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (ac0_37.s, ac0_15.t));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (-ac0_37.s, -ac0_15.t));
blurredColor +=
texture2D (sampler0, TexCoord0.st + vec2 (ac0_15.s, -ac0_37.t));
blurredColor /= 16.0;
}
else
{
blurredColor += getSampleWithBoundsCheck (vec2 (0.0, ac0_4.t));
blurredColor += getSampleWithBoundsCheck (vec2 (ac0_4.s, 0.0));
blurredColor += getSampleWithBoundsCheck (vec2 (0.0, -ac0_4.t));
blurredColor += getSampleWithBoundsCheck (vec2 (-ac0_4.s, 0.0));
blurredColor += getSampleWithBoundsCheck (vec2 (ac0_29.s, -ac0_29.t));
blurredColor += getSampleWithBoundsCheck (vec2 (ac0_29.s, ac0_29.t));
blurredColor += getSampleWithBoundsCheck (vec2 (-ac0_29.s, ac0_29.t));
blurredColor +=
getSampleWithBoundsCheck (vec2 (-ac0_29.s, -ac0_29.t));
blurredColor += getSampleWithBoundsCheck (vec2 (ac0_15.s, ac0_37.t));
blurredColor += getSampleWithBoundsCheck (vec2 (-ac0_37.s, ac0_15.t));
blurredColor += getSampleWithBoundsCheck (vec2 (ac0_37.s, -ac0_15.t));
blurredColor +=
getSampleWithBoundsCheck (vec2 (-ac0_15.s, -ac0_37.t));
blurredColor += getSampleWithBoundsCheck (vec2 (-ac0_15.s, ac0_37.t));
blurredColor += getSampleWithBoundsCheck (vec2 (ac0_37.s, ac0_15.t));
blurredColor +=
getSampleWithBoundsCheck (vec2 (-ac0_37.s, -ac0_15.t));
blurredColor += getSampleWithBoundsCheck (vec2 (ac0_15.s, -ac0_37.t));
blurredColor /= samples;
}
return blurredColor;
}
#endif
void
main ()
{
vec4 baseColor = texture2D (sampler0, TexCoord0.st);
#ifdef DOF_ENABLED
float depth = getDepth (TexCoord0.st);
float cursorDepth = getCursorDepth (vec2 (0.5, 0.5));
if (depth < cursorDepth)
baseColor =
mix (baseColor, getBlurredColor (),
clamp (2.0 *
((clamp (cursorDepth, 0.0, HYPERFOCAL) -
depth) / (clamp (cursorDepth, 0.0, HYPERFOCAL))),
0.0, 1.0));
else
baseColor =
mix (baseColor, getBlurredColor (),
1.0 -
clamp ((((cursorDepth * HYPERFOCAL) /
(HYPERFOCAL - cursorDepth)) - (depth -
cursorDepth)) /
((cursorDepth * HYPERFOCAL) /
(HYPERFOCAL - cursorDepth)), 0.0, 1.0));
#endif
gl_FragColor = baseColor;
}