@@ -26,7 +26,9 @@ THREE.BokehShader = {
2626
2727 "maxblur" : { value : 1.0 } ,
2828
29+ "showFocus" : { value : 0 } ,
2930 "manualdof" : { value : 0 } ,
31+ "vignetting" : { value : 0 } ,
3032 "depthblur" : { value : 0 } ,
3133
3234 "threshold" : { value : 0.5 } ,
@@ -75,6 +77,7 @@ THREE.BokehShader = {
7577 "uniform float focalDepth; //focal distance value in meters, but you may use autofocus option below" ,
7678 "uniform float focalLength; //focal length in mm" ,
7779 "uniform float fstop; //f-stop value" ,
80+ "uniform bool showFocus; //show debug focus point and focal range (red = focal point, green = focal range)" ,
7881
7982 "/*" ,
8083 "make sure that these two values are the same for your camera, otherwise distances will be wrong." ,
@@ -99,6 +102,12 @@ THREE.BokehShader = {
99102
100103 "float CoC = 0.03; //circle of confusion size in mm (35mm film = 0.03mm)" ,
101104
105+ "uniform bool vignetting; // use optical lens vignetting" ,
106+
107+ "float vignout = 1.3; // vignetting outer border" ,
108+ "float vignin = 0.0; // vignetting inner border" ,
109+ "float vignfade = 22.0; // f-stops till vignete fades" ,
110+
102111 "uniform bool shaderFocus;" ,
103112 "// disable if you use external focalDepth value" ,
104113
@@ -141,14 +150,6 @@ THREE.BokehShader = {
141150 " #endif" ,
142151 "}" ,
143152
144- "float getViewZ( const in float depth ) {" ,
145- " #if PERSPECTIVE_CAMERA == 1" ,
146- " return perspectiveDepthToViewZ( depth, znear, zfar );" ,
147- " #else" ,
148- " return orthographicDepthToViewZ( depth, znear, zfar );" ,
149- " #endif" ,
150- "}" ,
151-
152153 "float penta(vec2 coords) {" ,
153154 "//pentagonal shape" ,
154155 "float scale = float(rings) - 1.3;" ,
@@ -234,6 +235,28 @@ THREE.BokehShader = {
234235 "return col+mix(vec3(0.0),col,thresh*blur);" ,
235236 "}" ,
236237
238+ "vec3 debugFocus(vec3 col, float blur, float depth) {" ,
239+ "float edge = 0.002*depth; //distance based edge smoothing" ,
240+ "float m = clamp(smoothstep(0.0,edge,blur),0.0,1.0);" ,
241+ "float e = clamp(smoothstep(1.0-edge,1.0,blur),0.0,1.0);" ,
242+
243+ "col = mix(col,vec3(1.0,0.5,0.0),(1.0-m)*0.6);" ,
244+ "col = mix(col,vec3(0.0,0.5,1.0),((1.0-e)-(1.0-m))*0.2);" ,
245+
246+ "return col;" ,
247+ "}" ,
248+
249+ "float linearize(float depth) {" ,
250+ "return -zfar * znear / (depth * (zfar - znear) - zfar);" ,
251+ "}" ,
252+
253+
254+ "float vignette() {" ,
255+ "float dist = distance(vUv.xy, vec2(0.5,0.5));" ,
256+ "dist = smoothstep(vignout+(fstop/vignfade), vignin+(fstop/vignfade), dist);" ,
257+ "return clamp(dist,0.0,1.0);" ,
258+ "}" ,
259+
237260 "float gather(float i, float j, int ringsamples, inout vec3 col, float w, float h, float blur) {" ,
238261 "float rings2 = float(rings);" ,
239262 "float step = PI*2.0 / float(ringsamples);" ,
@@ -250,20 +273,20 @@ THREE.BokehShader = {
250273 "void main() {" ,
251274 "//scene depth calculation" ,
252275
253- "float depth = getViewZ ( getDepth( vUv.xy ) );" ,
276+ "float depth = linearize ( getDepth( vUv.xy ) );" ,
254277
255278 "// Blur depth?" ,
256279 "if (depthblur) {" ,
257- "depth = getViewZ (bdepth(vUv.xy));" ,
280+ "depth = linearize (bdepth(vUv.xy));" ,
258281 "}" ,
259282
260283 "//focal plane calculation" ,
261284
262- "float fDepth = - focalDepth;" , // viewZ is negative
285+ "float fDepth = focalDepth;" ,
263286
264287 "if (shaderFocus) {" ,
265288
266- "fDepth = getViewZ ( getDepth( focusCoords ) );" ,
289+ "fDepth = linearize ( getDepth( focusCoords ) );" ,
267290
268291 "}" ,
269292
@@ -272,14 +295,14 @@ THREE.BokehShader = {
272295 "float blur = 0.0;" ,
273296
274297 "if (manualdof) {" ,
275- "float a = depth - fDepth; // Focal plane" ,
298+ "float a = depth- fDepth; // Focal plane" ,
276299 "float b = (a-fdofstart)/fdofdist; // Far DoF" ,
277300 "float c = (-a-ndofstart)/ndofdist; // Near Dof" ,
278301 "blur = (a>0.0) ? b : c;" ,
279302 "} else {" ,
280- "float f = focalLength;" ,
281- "float d = fDepth; " ,
282- "float o = depth; " ,
303+ "float f = focalLength; // focal length in mm " ,
304+ "float d = fDepth*1000.0; // focal plane in mm " ,
305+ "float o = depth*1000.0; // depth in mm " ,
283306
284307 "float a = (o*f)/(o-f);" ,
285308 "float b = (d*f)/(d-f);" ,
@@ -325,6 +348,14 @@ THREE.BokehShader = {
325348 "col /= s; //divide by sample count" ,
326349 "}" ,
327350
351+ "if (showFocus) {" ,
352+ "col = debugFocus(col, blur, depth);" ,
353+ "}" ,
354+
355+ "if (vignetting) {" ,
356+ "col *= vignette();" ,
357+ "}" ,
358+
328359 "gl_FragColor.rgb = col;" ,
329360 "gl_FragColor.a = 1.0;" ,
330361 "} "
0 commit comments