Skip to content
Snippets Groups Projects
Commit 719e3f14 authored by David Parks's avatar David Parks
Browse files

SH-1374 Integrate bokeh depth of field effect from Tofu, but preserve edge detection.

parent af6133df
No related branches found
No related tags found
No related merge requests found
...@@ -36,86 +36,97 @@ float getDepth(vec2 pos_screen) ...@@ -36,86 +36,97 @@ float getDepth(vec2 pos_screen)
return p.z/p.w; return p.z/p.w;
} }
void dofSample(inout vec4 diff, inout float w, float fd, float x, float y) float calc_cof(float depth)
{
float sc = abs(depth-focal_distance)/-depth*blur_constant;
sc /= magnification;
// tan_pixel_angle = pixel_length/-depth;
float pixel_length = tan_pixel_angle*-focal_distance;
sc = sc/pixel_length;
sc *= 1.414;
return sc;
}
void dofSampleNear(inout vec4 diff, inout float w, float cur_sc, vec2 tc)
{ {
vec2 tc = vary_fragcoord.xy+vec2(x,y);
float d = getDepth(tc); float d = getDepth(tc);
float sc = calc_cof(d);
float wg = 1.0; float wg = 1.0;
//if (d < fd)
//{ vec4 s = texture2DRect(diffuseRect, tc);
// diff += texture2DRect(diffuseRect, tc); // de-weight dull areas to make highlights 'pop'
// w = 1.0; wg *= s.r+s.g+s.b;
//}
if (d > fd) diff += wg*s;
{
wg = max(d/fd, 0.1);
}
diff += texture2DRect(diffuseRect, tc+vec2(0.5,0.5))*wg*0.25;
diff += texture2DRect(diffuseRect, tc+vec2(-0.5,0.5))*wg*0.25;
diff += texture2DRect(diffuseRect, tc+vec2(0.5,-0.5))*wg*0.25;
diff += texture2DRect(diffuseRect, tc+vec2(-0.5,-0.5))*wg*0.25;
w += wg; w += wg;
} }
void dofSampleNear(inout vec4 diff, inout float w, float x, float y) void dofSample(inout vec4 diff, inout float w, float min_sc, float cur_depth, vec2 tc)
{ {
vec2 tc = vary_fragcoord.xy+vec2(x,y); float d = getDepth(tc);
float sc = calc_cof(d);
if (sc > min_sc //sampled pixel is more "out of focus" than current sample radius
|| d < cur_depth) //sampled pixel is further away than current pixel
{
float wg = 1.0;
diff += texture2DRect(diffuseRect, tc); vec4 s = texture2DRect(diffuseRect, tc);
w += 1.0; // de-weight dull areas to make highlights 'pop'
wg *= s.r+s.g+s.b;
diff += wg*s;
w += wg;
}
} }
void main() void main()
{ {
vec3 norm = texture2DRect(normalMap, vary_fragcoord.xy).xyz; vec3 norm = texture2DRect(normalMap, vary_fragcoord.xy).xyz;
norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
vec2 tc = vary_fragcoord.xy; vec2 tc = vary_fragcoord.xy;
float sc = 0.75; float depth = getDepth(tc);
float depth;
depth = getDepth(tc);
vec4 diff = texture2DRect(diffuseRect, vary_fragcoord.xy); vec4 diff = texture2DRect(diffuseRect, vary_fragcoord.xy);
{ //pixel is behind far focal plane {
float w = 1.0; float w = 1.0;
sc = (abs(depth-focal_distance)/-depth)*blur_constant; float sc = calc_cof(depth);
sc = min(abs(sc), 10.0);
sc /= magnification;
// tan_pixel_angle = pixel_length/-depth;
float pixel_length = tan_pixel_angle*-focal_distance;
sc = sc/pixel_length;
//diff.r = sc;
sc = min(abs(sc), 8.0);
//sc = 4.0;
float fd = depth*0.5f; float fd = depth*0.5f;
while (sc > 0.5) float PI = 3.14159265358979323846264;
// sample quite uniformly spaced points within a circle, for a circular 'bokeh'
//if (depth < focal_distance)
{ {
dofSample(diff,w, fd, sc,sc); while (sc > 0.5)
dofSample(diff,w, fd, -sc,sc); {
dofSample(diff,w, fd, sc,-sc); int its = int(max(1.0,(sc*3.7)));
dofSample(diff,w, fd, -sc,-sc); for (int i=0; i<its; ++i)
{
sc -= 0.5; float ang = sc+i*2*PI/its; // sc is added for rotary perturbance
float sc2 = sc*1.414; float samp_x = sc*sin(ang);
dofSample(diff,w, fd, 0,sc2); float samp_y = sc*cos(ang);
dofSample(diff,w, fd, 0,-sc2); // you could test sample coords against an interesting non-circular aperture shape here, if desired.
dofSample(diff,w, fd, -sc2,0); dofSample(diff, w, sc, depth, vary_fragcoord.xy + vec2(samp_x,samp_y));
dofSample(diff,w, fd, sc2,0); }
sc -= 0.5; sc -= 1.0;
}
} }
diff /= w; diff /= w;
...@@ -123,5 +134,4 @@ void main() ...@@ -123,5 +134,4 @@ void main()
vec4 bloom = texture2D(bloomMap, vary_fragcoord.xy/screen_res); vec4 bloom = texture2D(bloomMap, vary_fragcoord.xy/screen_res);
gl_FragColor = diff + bloom; gl_FragColor = diff + bloom;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment