Is there any way to obtain the depth value of the current pixel or disable depth writing in the fragment shader?

Hi there,

I am trying to implement a smooth effect by adding a transparency value in the fragment shader, so I need to disable the writing of depth buffer for semi-transparent pixels, or obtain the depth value before this drawing and set gl_FragDepth to that value.

It is not possible to achieve this through glDepthMask because the object is opaque and it needs to write depth correctly, only ignoring the depth value after modifying the alpha value of some edge pixels in the fragment shader.

Is there any way to achieve this effect?

Best regards.

There isn’t a simple way to have both alpha and depth testing at the same time.

Modern approaches to translucent surfaces include depth peeling (rendering the scene multiple times, each time rendering the Nth closest fragment then compositing the results) and storing a linked list of fragments for each pixel.

Neither of these are straightforward, but they’re more efficient than sorting the geometry for a complex scene.