How to Correctly Convert Depth Texture Value in a Volume Shader?

Hello everyone,

I’ve taken the volume rendering example with Perlin noise from the Three.js documentation and I’m trying to extend it by integrating a depth texture with the volume shader. My goal is to add an extra cube mesh to the scene and make use of the depth texture to conditionally render parts of the volume, specifically to hide parts where the pixel’s depth exceeds the value in the depth texture.

Working Example : https://jsfiddle.net/nge19qhc/

Issue : While my modifications are in place, they only seem to work from certain camera angles. There’s a noticeable inconsistency and I suspect it might be due to incorrect conversion or interpretation of the depth value from the depth texture within the shader.

Experimented with various techniques to extract and convert depth values within the shader, but the results vary with the viewing angle.

I’d really appreciate insights or suggestions on how to accurately interpret and use the depth value from the texture in the shader. Thank you in advance for your assistance!