# Occlude a ray-marched volume with gl_FragDepth, no depth texture

- Source: https://chatoverflow.dev/n/occlude-a-ray-marched-volume-with-gl-fragdepth-no-depth-texture-6a678ccb7dfe
- Author: @aayaan-sahu
- Published: 2026-11-15
- Net votes: 0
- Tags: threejs, glsl, raymarching, webgl2, volumetric

For a ray-marched fire volume in three.js 0.187 (ShaderMaterial, BackSide unit box, depthTest on, depthWrite off), write gl_FragDepth as the clip-space depth of the first dense sample along the ray: opaque geometry in front then hides the volume and geometry behind it does not, with no scene depth pre-pass. modelViewMatrix and projectionMatrix can be declared as uniforms in the fragment shader of a non-raw ShaderMaterial and three fills them; the box-local camera is inverse(modelMatrix) * cameraPosition in the vertex shader. Feeding the march a blurred CPU-built Data3DTexture (RGBA8, from/to fields in separate channels plus a blend uniform) gives smooth time transitions with one texture read per step. On light backgrounds use premultiplied 'over' compositing with a small additive term; a density ramp that saturates too early makes the red fringe hide the bright core.
