やっぱαブレンディングでしょ-その2
■テクスチャにもブレンド
まずは簡単にテクスチャへブレンドをかけようと思っていたのですが、「SpriteBatch」に一生懸命ブレンドを設定したところ、設定できなかったので違う方法でアプローチにしました。
namespace WindowsGame { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; ContentManager content; VertexPositionTexture[] vertices = { new VertexPositionTexture( new Vector3( 0.0f, 0.0f, 0.0f ),new Vector2( 0.0f, 0.0f ) ), new VertexPositionTexture( new Vector3( 56.0f, 0.0f, 0.0f ),new Vector2( 1.0f, 0.0f ) ), new VertexPositionTexture( new Vector3( 0.0f, 69.0f, 0.0f ),new Vector2( 0.0f, 1.0f ) ), new VertexPositionTexture( new Vector3( 56.0f, 69.0f, 0.0f ),new Vector2( 1.0f, 1.0f ) ) }; Texture2D texture; VertexDeclaration vdecl; BasicEffect basicEffect; public Game1() { graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); } protected override void Initialize() { vdecl = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionTexture.VertexElements); basicEffect = new BasicEffect(graphics.GraphicsDevice, null); basicEffect.TextureEnabled = 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 LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { texture = content.Load<Texture2D>("mimi"); } } 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; basicEffect.CurrentTechnique.Passes[0].Begin(); graphics.GraphicsDevice.Textures[0] = texture; graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>( PrimitiveType.TriangleStrip, vertices, 0, 2); basicEffect.CurrentTechnique.Passes[0].End(); basicEffect.End(); base.Draw(gameTime); } } }
■ソース説明
ブレンドの設定、何を設定しているのかは、後々、説明しようと思います
graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true;
graphics.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
graphics.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
テクスチャの設定、DirectXだと「lpD3DDEV->SetTexture( 0, texture);」を設定をする部分です。
graphics.GraphicsDevice.Textures[0] = texture;