Skip to content

Commit 479deb1

Browse files
committed
Improve DepthOfFieldEffect
Fixes pmndrs#529
1 parent e9b19d4 commit 479deb1

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/effects/DepthOfFieldEffect.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class DepthOfFieldEffect extends Effect {
5959
["nearColorBuffer", new Uniform(null)],
6060
["farColorBuffer", new Uniform(null)],
6161
["nearCoCBuffer", new Uniform(null)],
62+
["farCoCBuffer", new Uniform(null)],
6263
["scale", new Uniform(1.0)]
6364
])
6465
});
@@ -126,6 +127,7 @@ export class DepthOfFieldEffect extends Effect {
126127

127128
this.renderTargetCoC = this.renderTarget.clone();
128129
this.renderTargetCoC.texture.name = "DoF.CoC";
130+
this.uniforms.get("farCoCBuffer").value = this.renderTargetCoC.texture;
129131

130132
/**
131133
* A render target that stores a blurred copy of the circle of confusion.
@@ -180,7 +182,7 @@ export class DepthOfFieldEffect extends Effect {
180182

181183
this.maskPass = new ShaderPass(new MaskMaterial(this.renderTargetCoC.texture));
182184
const maskMaterial = this.maskPass.fullscreenMaterial;
183-
maskMaterial.maskFunction = MaskFunction.MULTIPLY_RGB_SET_ALPHA;
185+
maskMaterial.maskFunction = MaskFunction.MULTIPLY_RGB;
184186
maskMaterial.colorChannel = ColorChannel.GREEN;
185187

186188
/**
@@ -508,12 +510,12 @@ export class DepthOfFieldEffect extends Effect {
508510
this.maskPass.setSize(width, height);
509511

510512
// These buffers require full resolution to prevent color bleeding.
513+
this.renderTargetFar.setSize(width, height);
511514
this.renderTargetCoC.setSize(width, height);
512515
this.renderTargetMasked.setSize(width, height);
513516

514517
this.renderTarget.setSize(w, h);
515518
this.renderTargetNear.setSize(w, h);
516-
this.renderTargetFar.setSize(w, h);
517519
this.renderTargetCoCBlurred.setSize(w, h);
518520

519521
// Optimization: 1 / (TexelSize * ResolutionScale) = FullResolution

src/effects/glsl/depth-of-field.frag

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,24 @@
1111
#endif
1212

1313
uniform lowp sampler2D nearCoCBuffer;
14+
uniform lowp sampler2D farCoCBuffer;
1415
uniform float scale;
1516

1617
void mainImage(const in vec4 inputColor, const in vec2 uv, const in float depth, out vec4 outputColor) {
1718

1819
vec4 colorNear = texture2D(nearColorBuffer, uv);
1920
vec4 colorFar = texture2D(farColorBuffer, uv);
2021

21-
float cocNear = texture2D(nearCoCBuffer, uv).r;
22-
cocNear = min(cocNear * scale, 1.0);
22+
vec2 cocNearFar = vec2(
23+
texture2D(nearCoCBuffer, uv).r,
24+
texture2D(farCoCBuffer, uv).g
25+
);
26+
27+
cocNearFar = min(cocNearFar * scale, 1.0);
2328

2429
// The far color buffer has been premultiplied with the CoC buffer.
25-
vec4 result = inputColor * (1.0 - colorFar.a) + colorFar;
26-
result = mix(result, colorNear, cocNear);
30+
vec4 result = inputColor * (1.0 - cocNearFar.y) + colorFar;
31+
result = mix(result, colorNear, cocNearFar.x);
2732

2833
outputColor = result;
2934

0 commit comments

Comments
 (0)