Problem with transparent texture on a 3D primitive, XNA 4.0

I need to draw a large set of cubes, all with (possibly) unique textures on each side. Some of the textures also have transparency parts. Cubes that are behind transparent textures should show through the transparent texture. However, it seems that the order in which I draw the cubes decides whether transparency works or not, which I want to avoid. Look here:

cubeEffect.CurrentTechnique = cubeEffect.Techniques["Textured"]; Block[] cubes = new Block[4]; cubes[0] = new Block(BlockType.leaves, new Vector3(0, 0, 3)); cubes[1] = new Block(BlockType.dirt, new Vector3(0, 1, 3)); cubes[2] = new Block(BlockType.log, new Vector3(0, 0, 4)); cubes[3] = new Block(BlockType.gold, new Vector3(0, 1, 4)); foreach(Block b in cubes) { b.shape.RenderShape(GraphicsDevice, cubeEffect); } 

This is the code in the Draw method. This gives the following result: the first image

As you can see, the textures behind the sheet cube are not visible from the other side. When I reverse the index 3 and 0 in the array, I get this: correct image

It is clear that the drawing order affects the cubes. I suspect this may be due to the blend mode, but I have no idea where to start.

+4
source share
4 answers

You rely on depth buffering to achieve occlusion. This method works only for opaque objects.

To achieve the correct occlusion for a scene containing transparent objects:

  • Set DepthBufferEnable and DepthBufferWriteEnable to true

  • Draw all the opaque geometry

  • Leave DepthBufferEnable to true, but change DepthBufferWriteEnable to false

  • Sort alpha mixed objects by distance from the camera, then draw them in order back

Excerpt from Sort Depth of Alpha Mixed Objects by Shawn Hargreaves

+7
source

Proper drawing of transparent objects is more complicated than usual. The reason is that when a face is displayed by default, it marks all the pixels drawn at a certain depth, and the pixels of the results will not be drawn at all. I would recommend getting a 3d rendering book and looking at more details.

The easiest approach you've already found is to draw transparent objects AFTER opaque ones. Works for transparent and translucent objects. Note that transparent objects must be sorted correctly (as opposed to opaque ones).

In your specific case (not translucent), you can change the rendering of the texture so as NOT to display anything for the transparent parts.

+1
source

You may be able to use this if you do not have translucent pixels on the objects. It will either become completely solid or will not be written to the Z-buffer. Like in the Riemers Alpha Testing .

0
source

XNA (and DirectX and all major 3D libraries) take into account something called culling . Although from your code I can’t say exactly from the images, I think this is your problem. Polygons that you don’t see have vertices in the wrong order. If this is a problem, you have two solutions:

  • either shutdown ( device.RenderState.CullMode = CullMode.None; if I remember correctly)
  • apply the texture twice, with polygon points both clockwise and counterclockwise
-2
source

All Articles