Skip to content

Commit

Permalink
Add maskFunction setting
Browse files Browse the repository at this point in the history
Addresses #529
  • Loading branch information
vanruesc committed Sep 19, 2023
1 parent ec9a912 commit a686017
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
26 changes: 25 additions & 1 deletion src/effects/DepthOfFieldEffect.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ export class DepthOfFieldEffect extends Effect {

this.maskPass = new ShaderPass(new MaskMaterial(this.renderTargetCoC.texture));
const maskMaterial = this.maskPass.fullscreenMaterial;
maskMaterial.maskFunction = MaskFunction.MULTIPLY_RGB;
maskMaterial.colorChannel = ColorChannel.GREEN;
this.maskFunction = MaskFunction.MULTIPLY_RGB;

/**
* A bokeh blur pass for the foreground colors.
Expand Down Expand Up @@ -266,6 +266,30 @@ export class DepthOfFieldEffect extends Effect {

}

/**
* The mask function. Default is `MULTIPLY_RGB`.
*
* @type {MaskFunction}
*/

get maskFunction() {

return this.maskPass.fullscreenMaterial.maskFunction;

}

set maskFunction(value) {

if(this.maskFunction !== value) {

this.defines.set("MASK_FUNCTION", value.toFixed(0));
this.maskPass.fullscreenMaterial.maskFunction = value;
this.setChanged();

}

}

/**
* The circle of confusion material.
*
Expand Down
16 changes: 11 additions & 5 deletions src/effects/glsl/depth-of-field.frag
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ void mainImage(const in vec4 inputColor, const in vec2 uv, const in float depth,
vec4 colorNear = texture2D(nearColorBuffer, uv);
vec4 colorFar = texture2D(farColorBuffer, uv);

vec2 cocNearFar = vec2(
texture2D(nearCoCBuffer, uv).r,
texture2D(farCoCBuffer, uv).g
);
#if MASK_FUNCTION == 1

cocNearFar = min(cocNearFar * scale, 1.0);
// Use the CoC from the far color buffer's alpha channel.
vec2 cocNearFar = vec2(texture2D(nearCoCBuffer, uv).r, colorFar.a);
cocNearFar.x = min(cocNearFar.x * scale, 1.0);

#else

vec2 cocNearFar = vec2(texture2D(nearCoCBuffer, uv).r, texture2D(farCoCBuffer, uv).g);
cocNearFar = min(cocNearFar * scale, 1.0);

#endif

// The far color buffer has been premultiplied with the CoC buffer.
vec4 result = inputColor * (1.0 - cocNearFar.y) + colorFar;
Expand Down

0 comments on commit a686017

Please sign in to comment.