DOF.glsl
4.9 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
@export ecgl.dof.coc
uniform sampler2D depth;
uniform float zNear: 0.1;
uniform float zFar: 2000;
uniform float focalDistance: 3;
// Object in range are perfectly in focus
uniform float focalRange: 1;
// 30mm
uniform float focalLength: 30;
// f/2.8
uniform float fstop: 2.8;
varying vec2 v_Texcoord;
@import clay.util.encode_float
void main()
{
float z = texture2D(depth, v_Texcoord).r * 2.0 - 1.0;
float dist = 2.0 * zNear * zFar / (zFar + zNear - z * (zFar - zNear));
float aperture = focalLength / fstop;
float coc;
float uppper = focalDistance + focalRange;
float lower = focalDistance - focalRange;
if (dist <= uppper && dist >= lower) {
// Object in range are perfectly in focus
coc = 0.5;
}
else {
// Adjust focalDistance
float focalAdjusted = dist > uppper ? uppper : lower;
// GPU Gems Depth of Field: A Survey of Techniques
coc = abs(aperture * (focalLength * (dist - focalAdjusted)) / (dist * (focalAdjusted - focalLength)));
// Clamp on the near focus plane and far focus plane
// PENDING
// Float value can only be [0.0 - 1.0)
coc = clamp(coc, 0.0, 2.0) / 2.00001;
// Near field
if (dist < lower) {
coc = -coc;
}
coc = coc * 0.5 + 0.5;
}
// R: coc, < 0.5 is near field, > 0.5 is far field
gl_FragColor = encodeFloat(coc);
}
@end
@export ecgl.dof.composite
#define DEBUG 0
uniform sampler2D original;
uniform sampler2D blurred;
uniform sampler2D nearfield;
uniform sampler2D coc;
uniform sampler2D nearcoc;
varying vec2 v_Texcoord;
@import clay.util.rgbm
@import clay.util.float
void main()
{
vec4 blurredColor = texture2D(blurred, v_Texcoord);
vec4 originalColor = texture2D(original, v_Texcoord);
float fCoc = decodeFloat(texture2D(coc, v_Texcoord));
// FIXME blur after premultiply will have white edge
// blurredColor.rgb /= max(fCoc, 0.1);
fCoc = abs(fCoc * 2.0 - 1.0);
float weight = smoothstep(0.0, 1.0, fCoc);
// float weight = fCoc;
#ifdef NEARFIELD_ENABLED
vec4 nearfieldColor = texture2D(nearfield, v_Texcoord);
float fNearCoc = decodeFloat(texture2D(nearcoc, v_Texcoord));
fNearCoc = abs(fNearCoc * 2.0 - 1.0);
// FIXME
gl_FragColor = encodeHDR(
mix(
nearfieldColor, mix(originalColor, blurredColor, weight),
// near field blur is too unobvious if use linear blending
pow(1.0 - fNearCoc, 4.0)
)
);
#else
gl_FragColor = encodeHDR(mix(originalColor, blurredColor, weight));
#endif
// #if DEBUG == 1
// // Show coc
// gl_FragColor = vec4(vec3(fCoc), 1.0);
// #elif DEBUG == 2
// // Show near coc
// gl_FragColor = vec4(vec3(fNearCoc), 1.0);
// #elif DEBUG == 3
// gl_FragColor = encodeHDR(blurredColor);
// #elif DEBUG == 4
// // gl_FragColor = vec4(vec3(nearfieldTexel.a), 1.0);
// gl_FragColor = encodeHDR(nearfieldColor);
// #endif
}
@end
@export ecgl.dof.diskBlur
#define POISSON_KERNEL_SIZE 16;
uniform sampler2D texture;
uniform sampler2D coc;
varying vec2 v_Texcoord;
uniform float blurRadius : 10.0;
uniform vec2 textureSize : [512.0, 512.0];
uniform vec2 poissonKernel[POISSON_KERNEL_SIZE];
uniform float percent;
float nrand(const in vec2 n) {
return fract(sin(dot(n.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
@import clay.util.rgbm
@import clay.util.float
void main()
{
vec2 offset = blurRadius / textureSize;
float rnd = 6.28318 * nrand(v_Texcoord + 0.07 * percent );
float cosa = cos(rnd);
float sina = sin(rnd);
vec4 basis = vec4(cosa, -sina, sina, cosa);
#if !defined(BLUR_NEARFIELD) && !defined(BLUR_COC)
offset *= abs(decodeFloat(texture2D(coc, v_Texcoord)) * 2.0 - 1.0);
#endif
#ifdef BLUR_COC
float cocSum = 0.0;
#else
vec4 color = vec4(0.0);
#endif
float weightSum = 0.0;
for (int i = 0; i < POISSON_KERNEL_SIZE; i++) {
vec2 ofs = poissonKernel[i];
ofs = vec2(dot(ofs, basis.xy), dot(ofs, basis.zw));
vec2 uv = v_Texcoord + ofs * offset;
vec4 texel = texture2D(texture, uv);
float w = 1.0;
#ifdef BLUR_COC
float fCoc = decodeFloat(texel) * 2.0 - 1.0;
// Blur coc in nearfield
cocSum += clamp(fCoc, -1.0, 0.0) * w;
#else
texel = texel;
#if !defined(BLUR_NEARFIELD)
float fCoc = decodeFloat(texture2D(coc, uv)) * 2.0 - 1.0;
// TODO DOF premult to avoid bleeding, can be tweaked (currently x^3)
// tradeoff between bleeding dof and out of focus object that shrinks too much
w *= abs(fCoc);
#endif
texel.rgb *= texel.a;
color += texel * w;
#endif
weightSum += w;
}
#ifdef BLUR_COC
gl_FragColor = encodeFloat(clamp(cocSum / weightSum, -1.0, 0.0) * 0.5 + 0.5);
#else
color /= weightSum;
color.rgb /= (color.a + 0.0001);
// TODO Windows will not be totally transparent if color.rgb is not 0 and color.a is 0.
gl_FragColor = color;
#endif
}
@end