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?
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.
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):
After line 72, temp was never applied back to the mesh part. There should be another line stating:
meshPart.Effect = temp;
Line 58 incorrectly refers to Parameters[âWorldViewProjâ], which does not exist. It should say:
effect.Parameters[âWorldViewProjectionâ].SetValue(worldViewProj);
Itâs here 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.
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: