Search code examples
c#xnatransparency

Model transparency depth issue in Visual Studio


Hey guys I've looked around and I could not find any understandable solutions for a beginner C# programmer like myself.

I have a model (tree) for my project on Visual Studio C#, the tree uses planes for the leaves, and I have textured the planes with an image of a leaf with a transparent background on Blender. I have made the materials transparent and the texture accept alpha, but when I import this into my game, the leaves are visible but the transparency cuts through the tree branches and displays everything but the branches behind it.

Screenshot

This is my basic draw method, could someone please give me a method that invokes alpha blending or whatever it is I need for the transparency to work properly? I really don't want to have to read an entire book on transparency depth..

foreach (ModelMesh mesh in model.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.FogEnabled = true;
                effect.FogStart = 70;
                effect.FogEnd = 100;
                effect.EnableDefaultLighting();
                effect.World = camera.world;
                effect.View = view;
                effect.Projection = projection;
            }
            mesh.Draw();

Solution

  • As your leaves don't have any semi-transparent regions, you can solve this problem with alpha-testing. To do so, you have to use the AlphaTestEffect to render your tree (or implement alpha-testing in the pixel shader). In earlier versions of XNA there were alpha test render states. But these have been removed in version 4.0