We have a scene rendered to a WebGLRenderTarget
with a depth texture which is being passed to a ShaderMaterial
in a different scene as tDepth
. Following is the fragment shader for that ShaderMaterial
. It does a depth check and then renders onto the canvas. (I can just disable autoclear of depth and have it working, but I want a custom depth function, hence the code).
varying vec2 vUv;
uniform sampler2D tDepth;
uniform float width;
uniform float height;
void main() {
vec2 clipCoord = vec2(gl_FragCoord.x / width , gl_FragCoord.y / height)
float oldDepth = texture2D(tDepth, clipCoord).r; // Gets wrong value.
float currentDepth = gl_FragCoord.z;
if (oldDepth > currentDepth) {
gl_FragColor = vec4(1,0,0,0);
} else {
discard;
}
}
How do I get the correct value of oldDepth
?
tDepth
texture? what format is it in?tDepth
is therenderTarget.depthTexture
passed as a uniform to the shader.