Skip to content

Commit 294cc26

Browse files
committed
Add workaround for pmndrs postprocessing depth texture issue
* Copy depth texture to intermediate buffer to prevent binding depth texture to both input and output buffers in compositor shader * See pmndrs/postprocessing#225, pmndrs/postprocessing#416 for additional context
1 parent 9b295d1 commit 294cc26

1 file changed

Lines changed: 40 additions & 1 deletion

File tree

‎src/compositorPass.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Pass, Resizable } from 'postprocessing';
1+
import { CopyPass, Pass, Resizable } from 'postprocessing';
22
import * as THREE from 'three';
33
import type { PerspectiveCamera } from 'three';
44

@@ -67,6 +67,9 @@ export class GodraysCompositorMaterial extends THREE.ShaderMaterial implements R
6767

6868
export class GodraysCompositorPass extends Pass {
6969
sceneCamera: PerspectiveCamera;
70+
private depthCopyRenderTexture: THREE.WebGLRenderTarget | null = null;
71+
private depthTextureCopyPass: CopyPass | null = null;
72+
7073
constructor(props: GodraysCompositorMaterialProps) {
7174
super('GodraysCompositorPass');
7275
this.fullscreenMaterial = new GodraysCompositorMaterial(props);
@@ -92,8 +95,44 @@ export class GodraysCompositorPass extends Pass {
9295
): void {
9396
(this.fullscreenMaterial as GodraysCompositorMaterial).uniforms.sceneDiffuse.value =
9497
inputBuffer.texture;
98+
99+
// There is a limitation in the pmndrs postprocessing library that causes rendering issues when
100+
// the depth texture provided to the effect is the same as the one bound to the output buffer.
101+
//
102+
// To work around this, we copy the depth texture to a new render target and use that instead
103+
// if it's found to be the same.
104+
const sceneDepth = (this.fullscreenMaterial as GodraysCompositorMaterial).uniforms.sceneDepth
105+
.value;
106+
if (sceneDepth && outputBuffer && sceneDepth === outputBuffer.depthTexture) {
107+
if (!this.depthCopyRenderTexture) {
108+
this.depthCopyRenderTexture = new THREE.WebGLRenderTarget(
109+
outputBuffer.depthTexture.image.width,
110+
outputBuffer.depthTexture.image.height,
111+
{
112+
minFilter: outputBuffer.depthTexture.minFilter,
113+
magFilter: outputBuffer.depthTexture.magFilter,
114+
format: outputBuffer.depthTexture.format,
115+
generateMipmaps: outputBuffer.depthTexture.generateMipmaps,
116+
}
117+
);
118+
}
119+
if (!this.depthTextureCopyPass) {
120+
this.depthTextureCopyPass = new CopyPass();
121+
}
122+
123+
this.depthTextureCopyPass.render(
124+
renderer,
125+
(this.fullscreenMaterial as GodraysCompositorMaterial).uniforms.sceneDepth.value,
126+
this.depthCopyRenderTexture
127+
);
128+
(this.fullscreenMaterial as GodraysCompositorMaterial).uniforms.sceneDepth.value =
129+
this.depthCopyRenderTexture.texture;
130+
}
131+
95132
renderer.setRenderTarget(outputBuffer);
96133
renderer.render(this.scene, this.camera);
134+
135+
(this.fullscreenMaterial as GodraysCompositorMaterial).uniforms.sceneDepth.value = sceneDepth;
97136
}
98137

99138
override setDepthTexture(

0 commit comments

Comments
 (0)