Not all meshes are applying transparency correctly

Hello everyone!

I have a model of a ship, where the flags and ropes have textures with transparency.
My model has 5 meshes (body of the ship, set of flags, another set of flags, cannons and ropes).
Transparency works fine on everything except on one of the set of flag.

The green arrows indicate the set of flags and the ropes, where transparency is being applied correctly.
The red ones point to where the problem is occurring.

Debugging, I realized that the BasicEffect of the Mesh where the problem occurs is with Alpha as 1 instead of 0 as the other two with transparency.
When I tried to “force” 0 for this Alpha, the texture disappeared and the Mesh site was filled with the background, hiding everything behind.

The textures used in the sets of flags are the same, implemented (in blender) in exactly the same way.
The output file is a fbx, and the textures are tga.

Follows a view of where the transparencies are and how it should appear.

And the code i’m using for draw:

    public override void Draw(GameTime gameTime)
    {
        GraphicsDevice.RasterizerState = RasterizerState.CullNone;
        GraphicsDevice.BlendState = BlendState.AlphaBlend;
        GraphicsDevice.DepthStencilState = DepthStencilState.Default;
        
        foreach (ModelMesh mesh in Model.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.AmbientLightColor = new Vector3(1f, 0, 0);
                effect.View = ScreenManager.GameViewMatrix;
                effect.World = WorldMatrix;
                effect.Projection = ProjectionMatrix;
            }
            mesh.Draw();
        }
        base.Draw(gameTime);
    }

Has anyone ever experienced this or could point me to a way to search and solve the problem?

Have you tried with depthread and not the default value ?
If alpha is used, it should be set “per mesh”, not onto the whole model => faster perfs and less head scratch

With alpha test you need to ensure the meshes are sorted far to near to avoid problems

1 Like

the order of rendering matters, because all your meshes draw to the depth buffer, this is a general 3d “issue”, not specific to monogame.

So if your flag is drawn first, all the covered pixels get it’s depth. Meshes that are drawn behind these pixels are ignored afterwards.

The default way of doing things then is to draw alpha objects without drawing to the depth buffer, since they should not cover things in the background. But you need to sort your transparent objects from far to close if you want to work correctly with them (unless using alpha test).

It’s really a large topic, i suggest goggling AlphaBlend and AlphaTest to get the basic concepts.

1 Like

Order really matters! I did not mind that.
I’m trying out different orders and changing the model schemas until I find one that looks good!
If my scheme does not give satisfactory results, I will delve into AlphaBlend and AlphaTest as suggested.

Thanks all for the tip.

:wink: Shawn Hargreaves Blog Index

How Do I Make My Game Look Good?
The most common approach:

Set DepthBufferEnable and DepthBufferWriteEnable to true
Draw all opaque geometry
Leave DepthBufferEnable set to true, but change DepthBufferWriteEnable to false
Sort alpha blended objects by distance from the camera, then draw them in order from back to front
This relies on a combination of all three sorting techniques:

Opaque objects are sorted by the depth buffer
Alpha versus opaque objects are also handled by the depth buffer (so you will never see an alpha blended object through a closer opaque one)
Painter’s algorithm sorts alpha blended objects relative to each other (which causes sorting errors if two alpha blended objects intersect)
Relies on backface culling to sort the individual triangles within a single alpha blended object (which causes sorting errors if alpha blended objects are not convex)
The results are not perfect, but this is efficient, reasonably easy to implement, and good enough for most games.

There are various things you can do to improve the sorting accuracy:

Avoid alpha blending! The more things you can make opaque, the easier and more accurate your sorting will be. Do you really need alpha blending everywhere you are using it? If your level design calls for layer upon layer of glass windows, consider changing the design make it easier to implement. If you are using alpha blending for cut-out shapes such as trees, consider using alpha test instead, which is a binary accept/reject decision where the accepted pixels remain opaque and can be sorted by the depth buffer.

Relax, don’t worry. Maybe the sorting errors aren’t actually so bad? Perhaps you can tweak your graphics (making the alpha channel softer and more translucent) to make the mistakes less obvious. This is the approach used by our Particle 3D sample, which makes no attempt to sort individual particles within each cloud of smoke, but chose a particle texture that makes this look ok. If you change the smoke texture to something more solid, the sorting errors will be noticeable.

If you have alpha blended models that are not convex, maybe you could change them to make them more convex? Even if they cannot be perfectly convex, the closer they become, the fewer sorting errors will result. Consider splitting complex models into multiple pieces that can be sorted independently. A human body is nowhere near convex, but if you separate the torso, head, arms, etc, each individual piece is approximately convex.

If you have texture masks that are basically on/off cut-outs, but which include a few alpha blended pixels for antialiasing around their

1 Like

Very Nice!!!
Thanks!