1
$\begingroup$

I would like to make certain objects in my scene transparent, but only the parts visible to the camera when opaque.

Currently, when I make objects transparent, I can see other transparent objects behind them, which I don't want. I only want to be able to see opaque objects behind transparent objects.

This is what happens now:

enter image description here

This is what I want:

enter image description here

$\endgroup$
  • $\begingroup$ Could you clarify what you mean? It may help to include your motivation for wanting this effect, and to use simpler example images using only 2 or 3 objects. $\endgroup$ – trichoplax Sep 10 '16 at 5:33
3
$\begingroup$

The simplest solution is to do three passes:

  • Render opaque meshes to a buffer (front-to-back, depth read/write on)
  • Render translucent meshes to another buffer (front-to-back, depth read/write on)
    This makes sure that only the closest translucent mesh is rendered.
  • Alpha blend translucent mesh on top of opaque buffer using opaque depth buffer. (depth read on)
    This will take care of opaque meshes blocking translucent meshes and preserving translucent opacity levels. Depth test for values less or equal for small early z test (potential) optimisation.

EDIT: As mentioned below you could use the same depth buffer for the translucent pass and do the alpha blend then, the key is rendering the translucent pass front-to-back and depth write/testing.

$\endgroup$
  • 2
    $\begingroup$ This also works if you keep the same depth buffer for the two render passes without clearing it between them. $\endgroup$ – Julien Guertault Sep 12 '16 at 16:52
1
$\begingroup$

Disclaimer: I haven't actually tested this but it seems feasible.

I agree with trichoplax that what you want could possibly be expressed more clearly but, assuming I've understood correctly, would the following do the job?

First I assume you can supply the opaque and translucent geometry separately, and that the opaque is sent first. I'm also going to assume there may be translucent geometry with exactly the same pixel Z values.(If this isn't the case, the stencil requirements could probably be dropped.)

To handle the "single translucent layer", clear the stencil buffer and send the translucent polygons to update the Z-buffer and set the stencil buffer on each pixel, but don't touch the frame buffer.

Submit the translucent data a second time (to actually update the framebuffer), but change the Z test to 'equal' and only allow drawing on the pixels that are currently set in the the stencil buffer. Also clear the stencil for any pixels that pass the tests.

This, I think, should result in only the closest translucency layer being applied.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.