やっぱαブレンディングでしょ-その1

■ナマポリゴンにαブレンディングをかけてみたい
「もっとナマポリゴン-その2」では頂点のα値の影響を受けていなかったので、αを使用したサンプルを作成してみました。

namespace WindowsGame
{
   public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        ContentManager content;

        VertexPositionColor[] vertices =
        {
            new VertexPositionColor( new Vector3(  5.0f,  5.0f, 0.0f ),new Color( 255, 255, 255, 100 ) ),
            new VertexPositionColor( new Vector3( 95.0f,  5.0f, 0.0f ),new Color( 255, 255, 255, 255 ) ),
            new VertexPositionColor( new Vector3(  5.0f, 95.0f, 0.0f ),new Color( 255, 255, 255,   0 ) ),
            new VertexPositionColor( new Vector3( 95.0f, 95.0f, 0.0f ),new Color( 255, 255, 255,  80 ) ),
        };
        VertexDeclaration vdecl;
        BasicEffect basicEffect;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
        }

        protected override void Initialize()
        {
            vdecl = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionColor.VertexElements);
            basicEffect = new BasicEffect(graphics.GraphicsDevice, null);
            basicEffect.VertexColorEnabled = true;

            basicEffect.World = Matrix.Identity;
            basicEffect.View = Matrix.Identity;
            //原点( 0, 0 )を画面右上に設定
            basicEffect.Projection = Matrix.CreateOrthographicOffCenter(
                0.0f,                            // 左座標
                this.Window.ClientBounds.Width,  // 右座標 Window幅
                this.Window.ClientBounds.Height, // 下座標 Window高さ
                0.0f,              // 上座標
                0.0f,                            // ニアクリップの距離
                1.0f );                          // ファークリップの距離
            base.Initialize();
        }

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true;
            graphics.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
            graphics.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;

            basicEffect.Begin();
            graphics.GraphicsDevice.VertexDeclaration = vdecl;
            for (int iy = 0; iy < 6; iy++)
            {
                for (int ix = 0; ix < 8; ix++)
                {
                    basicEffect.CurrentTechnique.Passes[0].Begin();
                    basicEffect.View = Matrix.CreateTranslation(new Vector3(ix * 100, iy * 100, 0));
                    graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                        PrimitiveType.TriangleStrip, vertices, 0, 2);
                    basicEffect.CurrentTechnique.Passes[0].End();
                }
            }
            basicEffect.End();
            base.Draw(gameTime);
        }
    }
}

■ソース説明
4点ともすべて白色を設定し、α値(4つめの数値)のみを変更しています。

 VertexPositionColor[] vertices =
 {
     new VertexPositionColor( new Vector3(  5.0f,  5.0f, 0.0f ),new Color( 255, 255, 255, 100 ) ),
     new VertexPositionColor( new Vector3( 95.0f,  5.0f, 0.0f ),new Color( 255, 255, 255, 255 ) ),
     new VertexPositionColor( new Vector3(  5.0f, 95.0f, 0.0f ),new Color( 255, 255, 255,   0 ) ),
     new VertexPositionColor( new Vector3( 95.0f, 95.0f, 0.0f ),new Color( 255, 255, 255,  80 ) ),
 }

■αブレンドの設定をここでしています。

 graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true;
 graphics.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
 graphics.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;

■結果
わーい、α値が使用できるようになりました。