Home Game Development second – How Do I Do Alpha Transparency Properly In XNA 4.0?

second – How Do I Do Alpha Transparency Properly In XNA 4.0?

0
second – How Do I Do Alpha Transparency Properly In XNA 4.0?

[ad_1]

Okay, I’ve learn a number of articles, tutorials, and questions relating to this. Most level to the identical method which does not remedy my drawback. I would like the power to create semi-transparent sprites (texture2D’s actually) and have them overlay one other sprite.

I can obtain that considerably with the code samples I’ve discovered however I’m not happy with the outcomes and I do know there’s a method to do that. In cell programming (BREW) we did it old fashioned and really checked every pixel for transparency earlier than rendering.

In this case it appears to render the sprite under it blended with the alpha above it. This could also be an artifact of how I’m rendering the feel however, as I stated earlier than, all examples level to this one method. Before I am going any additional I’ll go forward and paste my instance code.

    public void Draw(SpriteBatch batch, Camera digicam, float alpha)
    {
        int tileMapWidth = Width;
        int tileMapHeight = Height;

        batch.Begin(SpriteSortMode.Texture,
            BlendState.AlphaMix,
            SamplerState.PointWrap,
            DepthStencilState.Default,
            RasterizerState.CullNone,
            null, digicam.TransformMatrix);

        for (int x = 0; x < tileMapWidth; x++)
        {
            for (int y = 0; y < tileMapHeight; y++)
            {
                int tileIndex = _map[y, x];

                if (tileIndex != -1)
                {
                    Texture2D texture = _tileTextures[tileIndex];

                    batch.Draw(
                        texture,
                        new Rectangle(
                            (x * Engine.TileWidth),
                            (y * Engine.TileHeight),
                            Engine.TileWidth,
                            Engine.TileHeight),
                        new Color(new Vector4(1f, 1f, 1f, alpha )));
                }
            }
        }

        batch.End();

    }

As you possibly can see, on this code I’m utilizing the overloaded SpriteBatch.Begin methodology which takes, amongst different issues, a mix state. I’m virtually constructive that is my drawback. I do not wish to BLEND the sprites, I need them to be clear when alpha is 0. In this instance I can set alpha to 0 nevertheless it nonetheless renders each tiles, with the decrease z ordered sprite displaying by means of, discolored due to the mixing. This isn’t a desired impact, I need the upper z-ordered sprite to fade out and never impact the colour beneath it in such a fashion.

I could be method off right here as I’m pretty new to XNA improvement so be happy to steer me within the right route within the occasion I’m happening the mistaken rabbit gap.

TIA

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here