Sign in to follow this  
Zerocyde

Simple question about transparent textures and what can be seen though them.

Recommended Posts

Zerocyde    116
This is probably a very simple and well-known issue, but it's something that's turning out rather tough to find info on by googling.

Basically, when I use a transparent texture in my direct 3d program, the only other objects I can see when I look though the transparently textured object, are objects that were rendered BEFORE the transparently textured object was. Seeing as changing the entire rendering order every time the transparently textured object moves seems ridiculous, I assume there is some well known solution for this.

Three screen shots for extra examples. I have a 7x7x7 hollow cube made up of smaller, equal sized cubes. Which ever cube I look at has it's texture changed to a transparent texture (working on picking atm). First screenshot shows me inside the hollow cube of cubes looking out at the skybox. The second screenshot shows me outside the hollow cube of cubes looking in. As you can see, the cubes rendered before the cube I'm looking though are visible, the rest aren't. The third screen shot shows a big planet in the background that is not visible though a cube.

Thanks ahead of time for your help.

Share this post


Link to post
Share on other sites
Adam_42    3629
Generally this is solved by adjusting the render order. The simplest implementaion is:

1. At load time (or in your asset processing pipeline) flag any textures that have alpha in them as transparent.
2. Create a complete list of objects to render.
3. Sort that list. You want to render.
- Opaque objects first, in order from closest to furthest away. That makes the z-buffer work more efficiently.
- Transparent objects second, furthest away ones first.
4. Render the objects.

Note that for textures where the alpha channel is either 100% opaque or 100% transparent you may be able to get away with just turning on the alpha test and treating them as opaque. It doesn't always look that good on the smaller mip map levels though when there's lots of detail in the alpha channel. Edited by Adam_42

Share this post


Link to post
Share on other sites
Tom KQT    1704
[quote name='Zerocyde' timestamp='1335647047' post='4935678']
Seeing as changing the entire rendering order every time the transparently textured object moves seems ridiculous, I assume there is some well known solution for this.
[/quote]

As Adam_42 already said, you don't have to change the entire rendering order.

But you really have to sort transparent objects back-to-front. Because of how alpha blending works, other objects will be visible through (semi-)transparent objects ONLY if rendered first. And I should also mention that this doesn't apply to whole objects, but to individual pixels.

When the device is rendering a transparent pixel to the backbuffer, it combines it with the pixel that already is in backbuffer on this position.

Imagine object A which is opaque and is positioned at the back and object B which is half-transparent and in the front of object A.

Let's say you render object B first, its pixels are blended with the actual content of the back buffer, which is black (suppose you cleared backbuffer to black and there are no other objects but A and B). So you'll get a darkened object B (blended 50:50 with black color). Then you render object A, but because of depth test (z buffer), its pixels won't be rendered at all.

Now let's say you render object A first, it will be drawn normally. And when you render object B later, it will pass depth test (it is in the front) and it will blend 50:50 with the existing pixels in the backbuffer, which is an image of object A. You get the expected result, you'll see A through B.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this