Skip to content

Commit cce7e31

Browse files
authored
Merge pull request #14150 from mrdoob/revert-14093-dev7
Revert "DOF2: Fix broken example"
2 parents 9b09e0f + afb4b94 commit cce7e31

File tree

2 files changed

+93
-24
lines changed

2 files changed

+93
-24
lines changed

examples/js/shaders/BokehShader2.js

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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
"} "

examples/webgl_postprocessing_dof2.html

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,13 @@
226226
jsDepthCalculation: true,
227227
shaderFocus: false,
228228

229-
fstop: 10,
229+
fstop: 2.2,
230230
maxblur: 1.0,
231231

232+
showFocus: false,
232233
focalDepth: 2.8,
233234
manualdof: false,
235+
vignetting: false,
234236
depthblur: false,
235237

236238
threshold: 0.5,
@@ -272,10 +274,13 @@
272274
gui.add( effectController, 'shaderFocus' ).onChange( matChanger );
273275
gui.add( effectController, 'focalDepth', 0.0, 200.0 ).listen().onChange( matChanger );
274276

275-
gui.add( effectController, 'fstop', 1, 50, 0.1 ).onChange( matChanger );
277+
gui.add( effectController, 'fstop', 0.1, 22, 0.001 ).onChange( matChanger );
276278
gui.add( effectController, 'maxblur', 0.0, 5.0, 0.025 ).onChange( matChanger );
277279

280+
gui.add( effectController, 'showFocus' ).onChange( matChanger );
278281
gui.add( effectController, 'manualdof' ).onChange( matChanger );
282+
gui.add( effectController, 'vignetting' ).onChange( matChanger );
283+
279284
gui.add( effectController, 'depthblur' ).onChange( matChanger );
280285

281286
gui.add( effectController, 'threshold', 0, 1, 0.001 ).onChange( matChanger );
@@ -284,8 +289,11 @@
284289
gui.add( effectController, 'fringe', 0, 5, 0.001 ).onChange( matChanger );
285290

286291
gui.add( effectController, 'focalLength', 16, 80, 0.001 ).onChange( matChanger );
292+
287293
gui.add( effectController, 'noise' ).onChange( matChanger );
294+
288295
gui.add( effectController, 'dithering', 0, 0.001, 0.0001 ).onChange( matChanger );
296+
289297
gui.add( effectController, 'pentagon' ).onChange( matChanger );
290298

291299
gui.add( shaderSettings, 'rings', 1, 8).step(1).onChange( shaderUpdate );
@@ -370,8 +378,7 @@
370378
defines: {
371379
RINGS: shaderSettings.rings,
372380
SAMPLES: shaderSettings.samples,
373-
DEPTH_PACKING: 1, // RGBADepth
374-
PERSPECTIVE_CAMERA: 1
381+
DEPTH_PACKING: 1 // RGBADepth
375382
}
376383

377384
} );
@@ -399,6 +406,28 @@
399406

400407
}
401408

409+
function linearize( depth ) {
410+
411+
var zfar = camera.far;
412+
var znear = camera.near;
413+
return - zfar * znear / ( depth * ( zfar - znear ) - zfar );
414+
415+
}
416+
417+
418+
function smoothstep( near, far, depth ) {
419+
420+
var x = saturate( ( depth - near ) / ( far - near ) );
421+
return x * x * ( 3 - 2 * x );
422+
423+
}
424+
425+
function saturate( x ) {
426+
427+
return Math.max( 0, Math.min( 1, x ) );
428+
429+
}
430+
402431
function render() {
403432

404433
var time = Date.now() * 0.00015;
@@ -417,13 +446,22 @@
417446

418447
var intersects = raycaster.intersectObjects( scene.children, true );
419448

420-
var targetDistance = ( intersects.length > 0 ) ? intersects[ 0 ].distance : 1000;
421449

422-
distance += ( targetDistance - distance ) * 0.05;
450+
if ( intersects.length > 0 ) {
451+
452+
var targetDistance = intersects[ 0 ].distance;
423453

424-
postprocessing.bokeh_uniforms[ 'focalDepth' ].value = distance;
454+
distance += (targetDistance - distance) * 0.03;
425455

426-
effectController[ 'focalDepth' ] = distance;
456+
var sdistance = smoothstep(camera.near, camera.far, distance);
457+
458+
var ldistance = linearize(1 - sdistance);
459+
460+
postprocessing.bokeh_uniforms[ 'focalDepth' ].value = ldistance;
461+
462+
effectController['focalDepth'] = ldistance;
463+
464+
}
427465

428466
}
429467

0 commit comments

Comments
 (0)