Draw Model Using Alpha Mask

A number of models in my game use alpha masks to draw transparent textures, such as leaves. Unfortunately, in MonoGame, when rendering a model using BasicEffects, the alpha component of textures seems to be ignored, and the textures are instead rendered with alpha as black. To demonstrate this, I constructed a model with leaf textures using a transparent png image. On the left, you can see the model rendering as intended in 3ds max, and on the right, you can see the same model being rendered in MonoGame using BasicEffects.


I know there are a few threads floating around which deal with applying alpha masks in MonoGame, but I’m still having some trouble understanding how to deal with this situation. In the simplest terms, how should I render models with alpha-masked textures in Monogame?

See a solution here: https://digitalerr0r.wordpress.com/2009/04/30/xna-shader-programming-tutorial-13-alpha-mapping/ :wink:
To go further with alphamasking:
http://www.kevinboulanger.net/

1 Like

As I understand it, you’re only using the leaf texture (which has an alpha channel), no other textures for transparency, right?

In that case this is simply a BlendState issue. It can be resolved by setting GraphicsDevice.BlendState to BlendState.AlphaBlend. The default is BlendState.Opaque, which just ignores alpha. You should render the leaves back to front to get the right result. If your leaves overlap and you can’t sort them (i.e. part of one leaf is in front of another, but another part is behind it), you’ll need some more complex techniques.

For binary alpha (1 or 0) you can use the built-in AlphaTestEffect instead. The benefit is that you don’t have to sort the leaves.

Here’s a cool blog post about handling transparency/translucency: https://medium.com/@bgolus/anti-aliased-alpha-test-the-esoteric-alpha-to-coverage-8b177335ae4f
It describes a technique called ‘alpha to coverage’ in depth. It solves the sorting issue that comes with blending. I don’t think setting MSAA mask is currently supported in MG though.

1 Like

Ah, thank you for both of your responses! I read the linked posts and did some further research, and found that this thread solved my needs well enough 3D Model w/ Transparent Textures using a custom shader to clip transparent pixels when rendering the model did the trick. Unfortunately, using a custom Effect also means that I no longer have access to the lighting and Alpha provided by the BasicEffect class, so I will most likely merge those features into my Effect when I have more time to mess with it. I should mention, there are two minor bugs in the C# code linked (not the shader):

  1. After line 72, temp was never applied back to the mesh part. There should be another line stating:
    meshPart.Effect = temp;

  2. Line 58 incorrectly refers to Parameters[“WorldViewProj”], which does not exist. It should say:
    effect.Parameters[“WorldViewProjection”].SetValue(worldViewProj);

It’s here :wink: not in code but under it:
“Edit: Nevermind, I figured it out and I feel stupid. Inside my RemapModel I wasn’t setting meshPart.Effect to the temporary one that I made. Made that change and it started to work properly…”

@Alkher I went back and revisited the alpha map tutorial that you linked to, but I can’t seem to get it to compile against my version of MonoGame. Using the source on their github - https://github.com/petriw/XNA-Shader-Programming/blob/master/Tutorial13_AlphaMap/Tutorial13_AlphaMap/Content/Shader.fx I receive the error, “(86,3) : Unexpected token ‘S’ found. Expected CloseBracket”. The line in question is “Sampler[0] = (ColorMapSampler);” which seems innocuous enough. Would you be able to point me in the right direction in addressing this? I’m still very new to the use of shaders I’m afraid.

The shader is an “xna” one, you have to fix its code. You can find what’s missing if you create an effect with the pipelinetool, and comparing the lines missing and how methods are called. :wink:

Thanks for the tip, I had been under the impression that XNA and MonoGame shaders were compatible with each other. I found this repository of XNA shaders converted to MonoGame: https://github.com/InfiniteProductions/MonoGameShaders and specifically found the port of Riemer’s Effect4 shader to be just what I needed: https://github.com/InfiniteProductions/MonoGameShaders/blob/master/RiemersHLSLTutorial/Content/Effect4.fx

In my game each scene contains a single background mesh that seamlessly tiles into itself while a fixed camera pans across it. The background mesh tile is rendered 3-6 times depending on its distance from the camera, so as to always cover the entire screen. To get everything rendering correctly, I broke each background mesh into two fbx files: one containing the solid objects, and one containing the objects with texture alpha. I first render the solid mesh tiles from front to back with BlendState=Opaque, then I render the alpha mesh tiles from back to front with BlendState=AlphaBlend. The end result is exactly what I was aiming for:


It seems all of my questions regarding alpha and shader implementation have been resolved now. Thanks!

1 Like