0

I'm having trouble loading a textured model with alpha blending from blender to xna.

On blender I know that the texture has the correct alpha values, but whenever I draw the model into my game the extra space that was supposed to be transparent is instead filled black

This is what my draw method looks like.

public void draw()
    {

        Matrix posTranslated = Matrix.CreateTranslation(position); 
        foreach (ModelMesh mesh in model.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.EnableDefaultLighting();


                effect.View = view;
                effect.Projection = proj;
                effect.World = modelTransforms[mesh.ParentBone.Index] * posTranslated;
            }

            mesh.Draw();

        }
    }

am I missing any effects or changes to the graphics device? I've been searching for days and have still had little luck with this issue. Please help T.T

Charles Haro
  • 1,866
  • 3
  • 22
  • 36

1 Answers1

2

Have you set these properties?

graphicsDevice.RenderState.AlphaBlendEnable = true;
graphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
graphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;

If you're using XNA 4.0 you need to set the blend state instead of the old RenderState.

BlendState blendState = new BlendState()
{
    AlphaSourceBlend = Blend.SourceAlpha,
    AlphaDestinationBlend = Blend.InverseSourceAlpha, 
    ColorDestinationBlend = Blend.InverseSourceAlpha, // Required for Reach profile
};
GraphicsDevice.BlendState = blendState;
Andy
  • 6,366
  • 1
  • 32
  • 37
  • would they go into the nested for loop? also I"m using xna 4.0 I just looked and I didn't see a RenderState in graphicsDevice, are you talking about BlendState maybe? – Charles Haro Jun 14 '12 at 08:06
  • You need to set them before you call the draw method, they're a bank of switches that can either be left set or changed for each object that you render, if you're never changing them then it's safe to do it just once after the graphics device is created. – Andy Jun 14 '12 at 08:09
  • did you see the edit to my commit? I don't see a RenderState field in graphicsDevice. Are you using xna 4.0? – Charles Haro Jun 14 '12 at 08:36
  • Thank you for all your help, but now I'm getting a NotSupportedException XNA Framework Reach profile does not support separate alpha blend. BlendState.ColorDestinationBlend and AlphaDestinationBlend must be set to the same value. I tried to add ColorSourceBlend = Blend.SourceAlpha, in the blendstate declaration but it didn't change anything – Charles Haro Jun 14 '12 at 08:55
  • Add the line ColorDestinationBlend = Blend.InverseSourceAlpha, after the AlphaDestinationBlend property – Andy Jun 14 '12 at 09:02
  • Oh sweet ^.^ it works now :D but it looks a little off, It kinda looks like the mesh isn't disapearing in the proper place giving this kinda camo looking effect. I'm sorry to keep bugging you but do you think you could point me in any direct to resolve this issue >. – Charles Haro Jun 14 '12 at 09:13
  • Can you post an image of what you're seeing? – Andy Jun 14 '12 at 09:20
  • https://www.dropbox.com/s/xanjjt45vr21gk0/errors.png the emulator shows what i'm talking about, notice how it doesn't really blend in with back ground? – Charles Haro Jun 14 '12 at 09:28
  • So you shouldn't see the darker off white colour around the pictures? I'll bet there is colour information in your texture, make sure the alpha for the pixels that are off white is fully set to 0 – Andy Jun 14 '12 at 09:31
  • hmm that would be in the blender settings huh? – Charles Haro Jun 14 '12 at 10:10
  • Yeah, somewhere you'll be able to sample a pixel and see it's colour attributes, if not just drop it in to a paint package that can handle alpha values like paint.net. – Andy Jun 14 '12 at 10:14
  • Hey so i just realized I want to alpha testing not alpha blending, if I do alpha testing then it shouldn't render those pixels instead of making them transparent. So if you don't mind: How do I do alpha testing instead of alpha blending? – Charles Haro Jun 15 '12 at 02:20
  • Could you try adding another question, this conversation has become quite long and if others want to learn to use alpha testing they'll be able to find it. – Andy Jun 15 '12 at 07:02
  • also the alpha in belnder is set to 1 hmm maybe i need to turn off premultiplying – Charles Haro Jun 15 '12 at 07:08
  • What is the alternative of `device.RenderState.AlphaFunction = CompareFunction.GreaterEqual;` and `device.RenderState.ReferenceAlpha = 200` in XNA 4.0? – Shashwat May 31 '14 at 11:06