開発環境 |
Microsoft Visual C# 2010 Express (SP1) |
実行環境 |
Microsoft Windows XP Home Edition (SP3) |
プロジェクトの種類 |
Windows Game (4.0) |
プロジェクト名 |
XnaTexture2D |
テクスチャの各種表示方法
- スプライト(2D)
- プリミティブ(3D)
- HLSL
参考
Game1.cs
// XnaTexture2D 1.スプライト(2D)
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace XnaTexture2D
{
class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch sprite;
Texture2D texture;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1000;
graphics.PreferredBackBufferHeight = 500;
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void LoadContent()
{
sprite = new SpriteBatch(GraphicsDevice);
texture = Content.Load<Texture2D>("earthmap1k");
base.LoadContent();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
sprite.Begin();
sprite.Draw(texture, Vector2.Zero, Color.White);
sprite.End();
base.Draw(gameTime);
}
}
}
Game1.cs
// XnaTexture2D 2.プリミティブ(3D)
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace XnaTexture2D
{
class Game1 : Game
{
GraphicsDeviceManager graphics;
VertexBuffer vertexBuffer;
BasicEffect effect;
Texture2D texture;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void LoadContent()
{
texture = Content.Load<Texture2D>("earthmap1k");
effect = new BasicEffect(GraphicsDevice);
effect.TextureEnabled = true;
effect.Texture = texture;
VertexPositionTexture[] vertices = new VertexPositionTexture[4];
vertices[0] = new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0));
vertices[1] = new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0));
vertices[2] = new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1));
vertices[3] = new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1));
vertexBuffer = new VertexBuffer(GraphicsDevice,
typeof(VertexPositionTexture), vertices.Length, BufferUsage.WriteOnly);
vertexBuffer.SetData(vertices);
base.LoadContent();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
GraphicsDevice.SetVertexBuffer(vertexBuffer);
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
}
base.Draw(gameTime);
}
}
}
Game1.cs
// XnaTexture2D 3.HLSL
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace XnaTexture2D
{
class Game1 : Game
{
GraphicsDeviceManager graphics;
VertexBuffer vertexBuffer;
Effect effect;
Texture2D texture;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void LoadContent()
{
texture = Content.Load<Texture2D>("earthmap1k");
effect = Content.Load<Effect>("Effect1");
effect.Parameters["WorldMap"].SetValue(texture);
VertexPositionTexture[] vertices = new VertexPositionTexture[4];
vertices[0] = new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0));
vertices[1] = new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0));
vertices[2] = new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1));
vertices[3] = new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1));
vertexBuffer = new VertexBuffer(GraphicsDevice,
typeof(VertexPositionTexture), vertices.Length, BufferUsage.WriteOnly);
vertexBuffer.SetData(vertices);
base.LoadContent();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
GraphicsDevice.SetVertexBuffer(vertexBuffer);
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
}
base.Draw(gameTime);
}
}
}
Effect1.fx
texture WorldMap;
sampler TextureSampler = sampler_state
{
texture = <WorldMap>;
minfilter = linear;
magfilter = linear;
};
struct VertexShaderInput
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
output.Position = input.Position;
output.TexCoord = input.TexCoord;
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
return tex2D(TextureSampler, input.TexCoord);
}
technique Technique1
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
最終更新:2012年12月18日 05:30