# How can I draw some of my XNA models semi-transparent? (C#, XNA 4.0)

I've recently made some basic Blender models without transparency for a new game I've been working on, but I'm having some problems rendering them semi-transparently. Namely, I haven't been able to figure out how to render them semi-transparent at all.

I've looked around quite a bit for code samples, and for some reason I just can't find anything that fits. I figure transparency probably has to do with BlendState.AlphaBlend somehow, but I'm not sure how exactly.

My models are simple VertexPositionColor models that consist of a single mesh. I've tried turning transparency on in the material in blender, but it doesn't seem to do anything when I render it again in C#. (Except make it appear transparent in blender) Also - I'm not looking for anything fancy like a crystal-like effect that requires making my model more than one mesh, just something simple and effective that will make my models at least able to be seen through.

So basically, my questions are:

• How can I render an opaque VertexPositionColor model at a given percent transparent?
• Is there a non-shader solution to my problem?
• Is there a code sample anywhere for doing model transparency in XNA 4.0?

Thanks!

A non-shader solution isn't possible, since its the shader that is responsible for rendering the model. A non custom shader solution is, and the link above uses BasicEffect, which is XNA's defaul phong shader.

Copying the code out if the thread ever goes away.

public void DrawTransparent(float AlphaValue)
{
foreach (ModelMesh modelMesh in _Model.Meshes)
{
foreach (BasicEffect BasicEffect in modelMesh.Effects)
{
BasicEffect.World = _Instance;
BasicEffect.View = Helper.View;
BasicEffect.Projection = Helper.Projection;
BasicEffect.LightingEnabled = true;

BasicEffect.AmbientLightColor = new Vector3(.3f, .3f, .3f);
BasicEffect.DirectionalLight0.DiffuseColor = Color.White.ToVector3();
BasicEffect.DirectionalLight0.Direction = Helper.SunDirection;
BasicEffect.DirectionalLight0.SpecularColor = Vector3.Zero;

Helper.GraphicsDeviceManager.GraphicsDevice.BlendState = BlendState.AlphaBlend;
BasicEffect.Alpha = AlphaValue;
}
modelMesh.Draw();
}
}


You'll probably have to tweak this code sample so it works with your code, but it should be enough to point you on track. The AlphaValue parameter is a floating point value between 0f and 1f, with 0f being fully transparent, 1f being fully opaque, and 0.5f being half transparent.

Some of the material settings in my model files don't seem to be exported across into XNA correctly - limitations of the basic Model/Effect in XNA I would assume. I've wound up with my own custom Model class and import pipeline so I can get the all the data I want from my raw model files into my custom Model Object.

Anyway, hopefully this is enough to put you on the right track.

• Very helpful, thanks! I actually saw that thread but there was no explanation so I had no clue it was actually XNA's default shader. I've added the code and it works (close) to perfectly. Thanks again. Apr 9, 2015 at 23:36