Issue with transparent texture on 3D primitive, XNA 4.0

為{幸葍}努か 提交于 2020-01-21 02:28:00

问题


I need to draw a large set of cubes, all with (possibly) unique textures on each side. Some of the textures also have parts of transparency. The cubes that are behind ones with transparent textures should show through the transparent texture. However, it seems that the order in which I draw the cubes decides if the transparency works or not, which is something 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. It produces this result:

As you can see, the textures behind the leaf cube are not visible on the other side. When i reverse index 3 and 0 on in the array, I get this:

It is clear that the order of drawing is affecting the cubes. I suspect it may have to do with the blend mode, but I have no idea where to start with that.


回答1:


You are relying on depth buffering to achieve occlusion. This technique only works for opaque objects.

To achieve correct occlusion for a scene containing transparent objects:

  1. Set DepthBufferEnable and DepthBufferWriteEnable to true

  2. Draw all opaque geometry

  3. Leave DepthBufferEnable set to true, but change DepthBufferWriteEnable to false

  4. Sort alpha blended objects by distance from the camera, then draw them in order from back to front

Extract from Depth sorting alpha blended objects by Shawn Hargreaves




回答2:


Drawing transparent objects properly is harder than regular ones. The reason is when face is rendered by default it marks all pixels as drawn at certain depth and as result pixels that are behind will not be drawn at all. I'd recommend getting a book on 3d rendering and look through for more details.

The easiest approach you already found - draw transparent objects AFTER non-transparent ones. Works for transpreant and semi-transparent objects. Note that transparent objects need to be sorted to be drawn correctly (unlike non-transparent ones).

In your particular case (non-semitransparent) you can change texture renreding to NOT render anything for transparent parts.




回答3:


You may be able to use this if you don't have semi-transparent pixels on the objects. It'll either render completely solid or won't write to the Z-Buffer. As in Riemers Alpha Testing.




回答4:


XNA (and DirectX and all major 3D libraries) take in consideration something called culling. Although from your code I cannot tell for sure, from the images I think this is your problem. The polygons that you don't see have the vertices in the wrong order. If this is the problem, you have two solutions:

  • either turn culling off (device.RenderState.CullMode = CullMode.None; if I remember correctly)
  • apply your texture twice, with the points of the polygon both in clockwise order and counter clockwise


来源:https://stackoverflow.com/questions/4683030/issue-with-transparent-texture-on-3d-primitive-xna-4-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!