開発環境 Microsoft Visual C# 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 Windows Game (4.0)
プロジェクト名 HlslTexture

[ソリューションのディレクトリを作成]にチェックを付けないと.slnファイルが指定した場所の直下に作られる。

MyTexture.pngはペイントなどで作る。
1つは2x2の白黒模様。
もう1つは256x256くらいで白地に黒線を1ピクセル空けて何本か引くと拡大/縮小フィルタの効果が分かりやすい。

参考

Game1.cs
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
 
namespace HlslTexture
{
    class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        Effect effect;
        VertexPositionTexture[] vertices =
        {
            new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)),
            new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)),
            new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)),
            new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)),
        };
        float camDist = 4.0f;
        int camLat = 10;
        int camLong = 45;
 
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }
 
        protected override void LoadContent()
        {
            // ContentにMyEffect.fxを追加しておく
            effect = Content.Load<Effect>("MyEffect");
 
            Matrix projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 0.1f, 10);
 
            // ContentにMyTexture.pngを追加しておく
            Texture2D texture = Content.Load<Texture2D>("MyTexture");
 
            effect.Parameters["Projection"].SetValue(projection);
            effect.Parameters["MyTexture"].SetValue(texture);
 
            base.LoadContent();
        }
 
        protected override void Update(GameTime gameTime)
        {
            KeyboardState state = Keyboard.GetState();
            if (state[Keys.Escape] == KeyState.Down) Exit();
            if (state[Keys.Up] == KeyState.Down) camLat++;
            if (state[Keys.Down] == KeyState.Down) camLat--;
            if (state[Keys.Left] == KeyState.Down) camLong++;
            if (state[Keys.Right] == KeyState.Down) camLong--;
            if (state[Keys.PageUp] == KeyState.Down) camDist -= 0.1f;
            if (state[Keys.PageDown] == KeyState.Down) camDist += 0.1f;
 
            base.Update(gameTime);
        }
 
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
 
            float rad = MathHelper.ToRadians(camLat);
            float y = (float)Math.Sin(rad) * camDist;
            float r = (float)Math.Cos(rad) * camDist;
            rad = MathHelper.ToRadians(camLong);
            float x = (float)Math.Cos(rad) * r;
            float z = (float)Math.Sin(rad) * r;
 
            Matrix view = Matrix.CreateLookAt(new Vector3(x, y, z), Vector3.Zero, Vector3.Up);
            effect.Parameters["View"].SetValue(view);
 
            foreach (var pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(
                    PrimitiveType.TriangleStrip, vertices, 0, 2);
            }
 
            base.Draw(gameTime);
        }
    }
}
 

MyEffect.fx
// Effect File
 
float4x4 View;
float4x4 Projection;
 
texture MyTexture;
sampler mySampler = sampler_state {
	Texture = <MyTexture>;
	MagFilter = Point;
	//MagFilter = Linear;
	MinFilter = Point;
	//MinFilter = Linear;
};
 
struct VertexPositionTexture
{
	float4 Position : POSITION;
	float4 TextureCoordinate : TEXCOORD;
};
 
VertexPositionTexture MyVertexShader(VertexPositionTexture input)
{
	VertexPositionTexture output;
	output.Position = mul(input.Position, mul(View, Projection));
	output.TextureCoordinate = input.TextureCoordinate;
	return output;
}
 
float4 MyPixelShader(float2 textureCoordinate : TEXCOORD) : COLOR
{
	return tex2D(mySampler, textureCoordinate);
}
 
technique MyTechnique
{
	pass MyPass
	{
		VertexShader = compile vs_2_0 MyVertexShader();
		PixelShader = compile ps_2_0 MyPixelShader();
	}
}
 
最終更新:2012年11月30日 21:27