Texture2Dでもカラーキー
■前回モデルデータにカラーキーを設定する話できたので、今回は「Texture2」に対しての設定です。
といっても、結局やることは一緒で「tree.png」に対してカラーキーを設定します。
「Color Key Enabled」を「true」にしてください。
今回も赤色を抜き色にしますので。「Color Key Color」の設定は「255.0.0.255」です。
■ソースです
namespace WindowsGame2_12 { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; private VertexDeclaration vertexDeclaration = null; private VertexPositionTexture[] vertices = null; private BasicEffect basicEffect = null; private double dAng; private Texture2D texture = null; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { // エフェクトを作成 basicEffect = new BasicEffect(graphics.GraphicsDevice, null); // ビューマトリックス basicEffect.View = Matrix.CreateLookAt( new Vector3((float)(10.0 * Math.Sin(0)), 5.0f, (float)(10.0 * Math.Cos(0))), new Vector3(0, 0, 0), Vector3.Up ); // ワールドマトリックス basicEffect.World = Matrix.CreateTranslation(0, 0, 0); // プロジェクションマトリックス basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45.0f), (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height, 1.0f, 100.0f ); // 頂点カラーを有効にする basicEffect.VertexColorEnabled = false; basicEffect.TextureEnabled = true; // 頂点データ定義を作成 vertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionTexture.VertexElements); // 頂点データを作成する vertices = new VertexPositionTexture[12]; vertices[0] = new VertexPositionTexture(new Vector3(-1.0f, 2.0f, 0.0f), new Vector2(0.0f, 0.01f)); vertices[1] = new VertexPositionTexture(new Vector3(1.0f, 2.0f, 0.0f), new Vector2(1.0f, 0.01f)); vertices[2] = new VertexPositionTexture(new Vector3(1.0f, -1.0f, 0.0f), new Vector2(1.0f, 1.0f)); vertices[3] = new VertexPositionTexture(new Vector3(-1.0f, 2.0f, 0.0f), new Vector2(0.0f, 0.01f)); vertices[4] = new VertexPositionTexture(new Vector3( 1.0f,-1.0f, 0.0f), new Vector2(1.0f, 1.0f)); vertices[5] = new VertexPositionTexture(new Vector3(-1.0f,-1.0f, 0.0f), new Vector2(0.0f, 1.0f)); vertices[6] = new VertexPositionTexture(new Vector3(0.0f, -1.0f, 1.0f), new Vector2(0.0f, 1.0f)); vertices[7] = new VertexPositionTexture(new Vector3(0.0f, 2.0f, 1.0f), new Vector2(0.0f, 0.01f)); vertices[8] = new VertexPositionTexture(new Vector3(0.0f, 2.0f,-1.0f), new Vector2(1.0f, 0.01f)); vertices[9] = new VertexPositionTexture(new Vector3(0.0f, -1.0f, 1.0f), new Vector2(0.0f, 1.0f)); vertices[10] = new VertexPositionTexture(new Vector3(0.0f, 2.0f,-1.0f), new Vector2(1.0f, 0.01f)); vertices[11] = new VertexPositionTexture(new Vector3(0.0f,-1.0f,-1.0f), new Vector2(1.0f, 1.0f)); dAng = 0; base.Initialize(); } protected override void LoadContent() { texture = Content.Load<Texture2D>("tree"); } protected override void Update(GameTime gameTime) { // ビューマトリックス basicEffect.View = Matrix.CreateLookAt( new Vector3((float)(10.0 * Math.Sin(dAng)), 2.0f, (float)(10.0 * Math.Cos(dAng))), new Vector3(0, 0, 0), Vector3.Up ); dAng += 0.01; if (dAng > 3.141592 * 2) dAng -= 3.141592 * 2; base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.RenderState.AlphaTestEnable = true; GraphicsDevice.RenderState.AlphaFunction = CompareFunction.Greater; GraphicsDevice.RenderState.CullMode = CullMode.None; basicEffect.Begin(); for (int j = -4; j < 5; j++) { for (int i = -4; i < 5; i++) { basicEffect.CurrentTechnique.Passes[0].Begin(); basicEffect.World = Matrix.CreateTranslation(i*2, 0, j*2); GraphicsDevice.Textures[0] = texture; GraphicsDevice.VertexDeclaration = vertexDeclaration; GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, vertices, 0, 4); basicEffect.CurrentTechnique.Passes[0].End(); } } basicEffect.End(); base.Draw(gameTime); } } }
これで同じ前回と同じ画面が出ます
■ほかにも2Dのスプライトでも使用が可能なので、いろいろと使ってみてください。
■今日はテニスコートを借りてテニスをしたのでへとへとです。
運動の後のビールは美味しいですが・・・