■XNAでアルファブレンド | Prev Top Next |
今回はアルファブレンドです。ポリゴンを半透明にしたり、くり抜いたりします。
namespace Tutrial { /// /// This is the main type for your game /// public class Game1 : Microsoft.Xna.Framework.Game { private GraphicsDeviceManager graphics = null; private ContentManager content = null; //**************************************************************** //オブジェクトの定義 //**************************************************************** private VertexDeclaration vertexDeclaration = null; //頂点データを定義 private BasicEffect basicEffect = null; //単純なエフェクト。固定機能パイプラインの感覚で使用できると思う。 private VertexBuffer vertexBuffer = null; //頂点バッファ private Texture2D[] texture2D = new Texture2D[2]; //テクスチャー public Game1() { this.graphics = new GraphicsDeviceManager(this); //リソースフォルダのルートパスを指定 this.content = new ContentManager(this.Services, "Resource"); } /// /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// protected override void Initialize() { //**************************************************************** //オブジェクトの初期化 //**************************************************************** // 頂点データを頂点座標と頂点カラーとテクセル座標で定義する this.vertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionColorTexture.VertexElements); //エフェクトを作成 this.basicEffect = new BasicEffect(graphics.GraphicsDevice, null); //頂点バッファ作成 this.vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, VertexPositionColorTexture.SizeInBytes * 4, ResourceUsage.None); //頂点データ(□ポリゴン)を作成する VertexPositionColorTexture[] vertices = new VertexPositionColorTexture[4]; vertices[0] = new VertexPositionColorTexture(new Vector3(-2.0f, 2.0f, 0.0f), Color.White, new Vector2(0.0f, 0.0f)); vertices[1] = new VertexPositionColorTexture(new Vector3(2.0f, 2.0f, 0.0f), Color.White, new Vector2(1.0f, 0.0f)); vertices[2] = new VertexPositionColorTexture(new Vector3(-2.0f, -2.0f, 0.0f), Color.Black, new Vector2(0.0f, 1.0f)); vertices[3] = new VertexPositionColorTexture(new Vector3(2.0f, -2.0f, 0.0f), Color.Black, new Vector2(1.0f, 1.0f)); //頂点データを頂点バッファにセットする this.vertexBuffer.SetData(vertices); //テクスチャーの読み込み this.texture2D[0] = this.content.Load ("Texture"); this.texture2D[1] = this.content.Load ("Texture2"); base.Initialize(); } /// /// Load your graphics content. If loadAllContent is true, you should /// load content from both ResourceManagementMode pools. Otherwise, just /// load ResourceManagementMode.Manual content. /// /// Which type of content to load. protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { // TODO: Load any ResourceManagementMode.Automatic content } // TODO: Load any ResourceManagementMode.Manual content } ////// Unload your graphics content. If unloadAllContent is true, you should /// unload content from both ResourceManagementMode pools. Otherwise, just /// unload ResourceManagementMode.Manual content. Manual content will get /// Disposed by the GraphicsDevice during a Reset. /// /// Which type of content to unload. protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent == true) { content.Unload(); } } ////// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input and playing audio. /// /// Provides a snapshot of timing values. protected override void Update(GameTime gameTime) { // Allows the default game to exit on Xbox 360 and Windows if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here base.Update(gameTime); } ////// This is called when the game should draw itself. /// /// Provides a snapshot of timing values. protected override void Draw(GameTime gameTime) { //背景色とZバッファの初期化 this.graphics.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, new Vector4(0.2f, 0.2f, 0.2f, 1.0f), 1.0f, 0 ); //射影座標変換 this.basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45.0f), //視野角 (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height, //スクリーンサイズのアスペクト比 1.0f, //前方クリップ位置 100.0f //後方クリップ位置 ); //ビュー座標変換 this.basicEffect.View = Matrix.CreateLookAt( new Vector3(0.0f, 0.0f, 10.0f), //カメラ位置 Vector3.Zero, //カメラの注視点 Vector3.Up //カメラの上方向 ); //テクスチャーを有効にする this.basicEffect.TextureEnabled = true; //頂点カラーを有効にする this.basicEffect.VertexColorEnabled = true; //Zバッファを有効にする this.graphics.GraphicsDevice.RenderState.DepthBufferEnable = true; //アルファブレンドを有効にする this.graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true; //アルファブレンドの合成方法を設定する(注意2) this.graphics.GraphicsDevice.RenderState.SourceBlend = Blend.One; this.graphics.GraphicsDevice.RenderState.DestinationBlend = Blend.One; //描画する頂点データの定義を設定 this.graphics.GraphicsDevice.VertexDeclaration = this.vertexDeclaration; //頂点バッファをセット this.graphics.GraphicsDevice.Vertices[0].SetSource( this.vertexBuffer, 0, VertexPositionColorTexture.SizeInBytes); //エフェクトの使用を開始 this.basicEffect.Begin(); //パスの数だけ繰り返し描画 foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes) { //パスの開始 pass.Begin(); //**************************************************************** //雲テクスチャーを貼り付けたポリゴンをレンダリング //**************************************************************** //ワールド座標変換 this.basicEffect.World = Matrix.CreateTranslation(1.0f, -1.0f, -1.0f); //エフェクトにテクスチャーをセット this.basicEffect.Texture = this.texture2D[1]; //エフェクトの内容を変更したことをデバイスに通知する this.basicEffect.CommitChanges(); //ポリゴン描画する graphics.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); //**************************************************************** //Textureの文字のテクスチャーを貼り付けたポリゴンをレンダリング //**************************************************************** //ワールド座標変換 this.basicEffect.World = Matrix.CreateTranslation(0.0f, 0.0f, 0.0f); //エフェクトにテクスチャーをセット this.basicEffect.Texture = this.texture2D[0]; //エフェクトの内容を変更したことをデバイスに通知する this.basicEffect.CommitChanges(); //ポリゴン描画する graphics.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); //パスの終了 pass.End(); } //エフェクトの使用を終了 this.basicEffect.End(); base.Draw(gameTime); } } }
アルファブレンドを使用した半透明ポリゴンが表示されました。光などのエフェクトの表現でアルファブレンドは重宝します。
また、ここで紹介した使用方法以外にもポリゴンのくり抜き処理などでもアルファブレンドを使用します。
(注意1) ポリゴンの頂点カラーについて、上側を白、下側を黒に指定します。レンダリング結果としてポリゴンの下側が薄くなってます。 これでポリゴンのフェードアウト(徐々に薄くなる)効果が表現できます。
(注意2) アルファブレンドの合成方法を設定してします。ここでは加算方法で合成しています。