6
DoF with bokeh GLSL shader v2.4
7
original DoF with Bokeh GLSL shader 2.4 by Martins Upitis (martinsh) (devlog-martinsh.blogspot.com)
8
edited by Fireclaw the Fox
10
----------------------
11
This work is licensed under a Creative Commons Attribution 3.0 Unported License.
12
So you are free to share, modify and adapt it for your needs, and even use it for commercial use.
13
I would also love to hear about a project you are using it.
17
----------------------
20
Edited by Fireclaw the Fox to work with Panda3D
23
- physically accurate DoF simulation calculated from "focalDepth" ,"focalLength", "f-stop" and "CoC" parameters.
24
- option for artist controlled DoF simulation calculated only from "focalDepth" and individual controls for near and far blur
25
- added "circe of confusion" (CoC) parameter in mm to accurately simulate DoF with different camera sensor or film sizes
30
- new and physically little more accurate DoF
31
- two extra input variables - focal length and aperture iris diameter
32
- added a debug visualization of focus point and focal range
35
- added an option for pentagonal bokeh shape
39
- variable sample count to increase quality/performance
40
- option to blur depth buffer to reduce hard edges
41
- option to dither the samples with noise or pattern
42
- bokeh chromatic aberration/fringing
43
- bokeh bias to bring out bokeh edges
44
- image thresholding to bring out highlights when image is out of focus
48
uniform sampler2D renderedTexture;
49
uniform sampler2D depthTexture;
50
uniform float renderedTextureWidth;
51
uniform float renderedTextureHeight;
55
float width = renderedTextureWidth; //texture width
56
float height = renderedTextureHeight; //texture height
58
vec2 texel = vec2(1.0/width,1.0/height);
62
//uniform variables from external script
64
uniform float focalDepth; //focal distance value in meters, but you may use autofocus option below
65
uniform float focalLength; //focal length in mm
66
uniform float fstop; //f-stop value
67
uniform bool showFocus; //show debug focus point and focal range (red = focal point, green = focal range)
70
make sure that these two values are the same for your camera, otherwise distances will be wrong.
73
float znear = 1; //camera clipping start
74
float zfar = 1000; //camera clipping end
76
//------------------------------------------
79
int samples = 3; //samples on the first ring
80
int rings = 3; //ring count
82
bool manualdof = false; //manual dof calculation
83
float ndofstart = 1.0; //near dof blur start
84
float ndofdist = 2.0; //near dof blur falloff distance
85
float fdofstart = 1.0; //far dof blur start
86
float fdofdist = 3.0; //far dof blur falloff distance
88
float CoC = 0.03;//circle of confusion size in mm (35mm film = 0.03mm)
90
bool vignetting = true; //use optical lens vignetting?
91
float vignout = 1.3; //vignetting outer border
92
float vignin = 0.0; //vignetting inner border
93
float vignfade = 22.0; //f-stops till vignete fades
95
bool autofocus = true; //use autofocus in shader? disable if you use external focalDepth value
96
vec2 focus = vec2(0.5,0.5); // autofocus point on screen (0.0,0.0 - left lower corner, 1.0,1.0 - upper right)
97
float maxblur = 1.0; //clamp value of max blur (0.0 = no blur,1.0 default)
99
float threshold = 0.5; //highlight threshold;
100
float gain = 2.0; //highlight gain;
102
float bias = 0.5; //bokeh edge bias
103
float fringe = 0.7; //bokeh chromatic aberration/fringing
105
bool noise = true; //use noise instead of pattern for sample dithering
106
float namount = 0.0001; //dither amount
108
bool depthblur = false; //blur the depth buffer?
109
float dbsize = 1.25; //depthblursize
112
next part is experimental
113
not looking good with small sample and ring count
114
looks okay starting from samples = 4, rings = 4
117
bool pentagon = false; //use pentagon as bokeh shape?
118
float feather = 0.4; //pentagon shape feather
120
out vec4 frameBufferColor;
121
//------------------------------------------
124
float penta(vec2 coords) //pentagonal shape
126
float scale = float(rings) - 1.3;
127
vec4 HS0 = vec4( 1.0, 0.0, 0.0, 1.0);
128
vec4 HS1 = vec4( 0.309016994, 0.951056516, 0.0, 1.0);
129
vec4 HS2 = vec4(-0.809016994, 0.587785252, 0.0, 1.0);
130
vec4 HS3 = vec4(-0.809016994,-0.587785252, 0.0, 1.0);
131
vec4 HS4 = vec4( 0.309016994,-0.951056516, 0.0, 1.0);
132
vec4 HS5 = vec4( 0.0 ,0.0 , 1.0, 1.0);
134
vec4 one = vec4( 1.0 );
136
vec4 P = vec4((coords),vec2(scale, scale));
138
vec4 dist = vec4(0.0);
139
float inorout = -4.0;
141
dist.x = dot( P, HS0 );
142
dist.y = dot( P, HS1 );
143
dist.z = dot( P, HS2 );
144
dist.w = dot( P, HS3 );
146
dist = smoothstep( -feather, feather, dist );
148
inorout += dot( dist, one );
150
dist.x = dot( P, HS4 );
151
dist.y = HS5.w - abs( P.z );
153
dist = smoothstep( -feather, feather, dist );
156
return clamp( inorout, 0.0, 1.0 );
159
float bdepth(vec2 coords) //blurring depth
165
vec2 wh = vec2(texel.x, texel.y) * dbsize;
167
offset[0] = vec2(-wh.x,-wh.y);
168
offset[1] = vec2( 0.0, -wh.y);
169
offset[2] = vec2( wh.x -wh.y);
171
offset[3] = vec2(-wh.x, 0.0);
172
offset[4] = vec2( 0.0, 0.0);
173
offset[5] = vec2( wh.x, 0.0);
175
offset[6] = vec2(-wh.x, wh.y);
176
offset[7] = vec2( 0.0, wh.y);
177
offset[8] = vec2( wh.x, wh.y);
179
kernel[0] = 1.0/16.0; kernel[1] = 2.0/16.0; kernel[2] = 1.0/16.0;
180
kernel[3] = 2.0/16.0; kernel[4] = 4.0/16.0; kernel[5] = 2.0/16.0;
181
kernel[6] = 1.0/16.0; kernel[7] = 2.0/16.0; kernel[8] = 1.0/16.0;
184
for( int i=0; i<9; i++ )
186
float tmp = texture(depthTexture, coords + offset[i]).r;
187
d += tmp * kernel[i];
194
vec3 color(vec2 coords,float blur) //processing the sample
196
vec3 col = vec3(0.0);
198
col.r = texture(renderedTexture,coords + vec2(0.0,1.0)*texel*fringe*blur).r;
199
col.g = texture(renderedTexture,coords + vec2(-0.866,-0.5)*texel*fringe*blur).g;
200
col.b = texture(renderedTexture,coords + vec2(0.866,-0.5)*texel*fringe*blur).b;
202
vec3 lumcoeff = vec3(0.299,0.587,0.114);
203
float lum = dot(col.rgb, lumcoeff);
204
float thresh = max((lum-threshold)*gain, 0.0);
205
return col+mix(vec3(0.0),col,thresh*blur);
208
vec2 rand(vec2 coord) //generating noise/pattern texture for dithering
210
float noiseX = ((fract(1.0-coord.s*(width/2.0))*0.25)+(fract(coord.t*(height/2.0))*0.75))*2.0-1.0;
211
float noiseY = ((fract(1.0-coord.s*(width/2.0))*0.75)+(fract(coord.t*(height/2.0))*0.25))*2.0-1.0;
215
noiseX = clamp(fract(sin(dot(coord ,vec2(12.9898,78.233))) * 43758.5453),0.0,1.0)*2.0-1.0;
216
noiseY = clamp(fract(sin(dot(coord ,vec2(12.9898,78.233)*2.0)) * 43758.5453),0.0,1.0)*2.0-1.0;
218
return vec2(noiseX,noiseY);
221
vec3 debugFocus(vec3 col, float blur, float depth)
223
float edge = 0.002*depth; //distance based edge smoothing
224
float m = clamp(smoothstep(0.0,edge,blur),0.0,1.0);
225
float e = clamp(smoothstep(1.0-edge,1.0,blur),0.0,1.0);
227
col = mix(col,vec3(1.0,0.5,0.0),(1.0-m)*0.6);
228
col = mix(col,vec3(0.0,0.5,1.0),((1.0-e)-(1.0-m))*0.2);
233
float linearize(float depth)
235
return -zfar * znear / (depth * (zfar - znear) - zfar);
240
float dist = distance(texcoord.xy, vec2(0.5,0.5));
241
dist = smoothstep(vignout+(fstop/vignfade), vignin+(fstop/vignfade), dist);
242
return clamp(dist,0.0,1.0);
247
//scene depth calculation
249
float depth = linearize(texture(depthTexture,texcoord.xy).x);
253
depth = linearize(bdepth(texcoord.xy));
256
//focal plane calculation
258
float fDepth = focalDepth;
262
fDepth = linearize(texture(depthTexture,focus).x);
265
//dof blur factor calculation
271
float a = depth-fDepth; //focal plane
272
float b = (a-fdofstart)/fdofdist; //far DoF
273
float c = (-a-ndofstart)/ndofdist; //near Dof
279
float f = focalLength; //focal length in mm
280
float d = fDepth*1000.0; //focal plane in mm
281
float o = depth*1000.0; //depth in mm
283
float a = (o*f)/(o-f);
284
float b = (d*f)/(d-f);
285
float c = (d-f)/(d*fstop*CoC);
290
blur = clamp(blur,0.0,1.0);
292
// calculation of pattern for ditering
294
vec2 noise = rand(texcoord.xy)*namount*blur;
296
// getting blur x and y step factor
298
float w = (1.0/width)*blur*maxblur+noise.x;
299
float h = (1.0/height)*blur*maxblur+noise.y;
301
// calculation of final color
303
vec3 col = vec3(0.0);
305
if(blur < 0.05) //some optimization thingy
307
col = texture(renderedTexture, texcoord.xy).rgb;
312
col = texture(renderedTexture, texcoord.xy).rgb;
316
for (int i = 1; i <= rings; i += 1)
318
ringsamples = i * samples;
320
for (int j = 0 ; j < ringsamples ; j += 1)
322
float step = PI*2.0 / float(ringsamples);
323
float pw = (cos(float(j)*step)*float(i));
324
float ph = (sin(float(j)*step)*float(i));
328
p = penta(vec2(pw,ph));
330
col += color(texcoord.xy + vec2(pw*w,ph*h),blur)*mix(1.0,(float(i))/(float(rings)),bias)*p;
331
s += 1.0*mix(1.0,(float(i))/(float(rings)),bias)*p;
334
col /= s; //divide by sample count
339
col = debugFocus(col, blur, depth);
347
frameBufferColor.rgb = col;
348
frameBufferColor.a = 1.0;