Skip to content

Commit

Permalink
Added DoF setting to BokehPass.
Browse files Browse the repository at this point in the history
This lets the user define an area in front of and behind the focus point that remains sharp.
  • Loading branch information
vanruesc committed Oct 3, 2017
1 parent e9f6015 commit 99519cb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/materials/BokehMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ export class BokehMaterial extends ShaderMaterial {
*
* @param {PerspectiveCamera} [camera] - A camera.
* @param {Object} [options] - The options.
* @param {Number} [options.focus=1.0] - Focus distance.
* @param {Number} [options.aperture=0.025] - Camera aperture scale. Bigger values for shallower depth of field.
* @param {Number} [options.focus=1.0] - The focus distance, corresponds directly with the scene depth.
* @param {Number} [options.dof=0.02] - Depth of field. An area in front of and behind the focus point that still appears sharp.
* @param {Number} [options.aperture=0.025] - Camera aperture scale. Bigger values for stronger blur and shallower depth of field.
* @param {Number} [options.maxBlur=1.0] - Maximum blur strength.
*/

constructor(camera = null, options = {}) {

if(options.focus === undefined) { options.focus = 1.0; }
if(options.dof === undefined) { options.dof = 0.02; }
if(options.aperture === undefined) { options.aperture = 0.025; }
if(options.maxBlur === undefined) { options.maxBlur = 1.0; }

Expand All @@ -42,6 +44,7 @@ export class BokehMaterial extends ShaderMaterial {
tDepth: new Uniform(null),

focus: new Uniform(options.focus),
dof: new Uniform(options.dof),
aperture: new Uniform(options.aperture),
maxBlur: new Uniform(options.maxBlur)

Expand Down
10 changes: 9 additions & 1 deletion src/materials/glsl/bokeh/shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ uniform sampler2D tDiffuse;
uniform sampler2D tDepth;

uniform float focus;
uniform float dof;
uniform float aspect;
uniform float aperture;
uniform float maxBlur;
Expand Down Expand Up @@ -40,7 +41,14 @@ void main() {

#endif

float factor = depth - focus;
float focusNear = clamp(focus - dof, 0.0, 1.0);
float focusFar = clamp(focus + dof, 0.0, 1.0);

// Calculate a DoF mask.
float low = step(depth, focusNear);
float high = step(focusFar, depth);

float factor = (depth - focusNear) * low + (depth - focusFar) * high;

vec2 dofBlur = vec2(clamp(factor * aperture, -maxBlur, maxBlur));

Expand Down
1 change: 1 addition & 0 deletions src/passes/BokehPass.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class BokehPass extends Pass {
* @param {PerspectiveCamera} camera - The main camera. Used to obtain the aspect ratio and the near and far plane settings.
* @param {Object} [options] - Additional parameters.
* @param {Number} [options.focus=1.0] - Focus distance.
* @param {Number} [options.dof=0.02] - Depth of field. An area in front of and behind the focus point that still appears sharp.
* @param {Number} [options.aperture=0.025] - Camera aperture scale. Bigger values for shallower depth of field.
* @param {Number} [options.maxBlur=1.0] - Maximum blur strength.
*/
Expand Down

0 comments on commit 99519cb

Please sign in to comment.