開発環境 |
Microsoft Visual C# 2010 Express (SP1) |
実行環境 |
Microsoft Windows XP Home Edition (SP3) |
プロジェクトの種類 |
Windows Game (4.0) |
プロジェクト名 |
XnaShader |
参考
Program_2.cs
// #2
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
class Game1 : Game
{
GraphicsDeviceManager graphics;
VertexBuffer vb;
Effect effect;
Vector2 offset = new Vector2(-0.25f, 0.0f);
float zoom = 3.0f;
// 頂点構造体
struct MyVertex : IVertexType
{
Vector2 Position;
Vector2 UV;
public MyVertex(Vector2 position, Vector2 uv)
{
Position = position;
UV = uv;
}
public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration(
new VertexElement[] {
new VertexElement(0, VertexElementFormat.Vector2,
VertexElementUsage.Position, 0),
new VertexElement(8, VertexElementFormat.Vector2,
VertexElementUsage.TextureCoordinate, 0),
});
VertexDeclaration IVertexType.VertexDeclaration { get { return VertexDeclaration;} }
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
Window.AllowUserResizing = true;
}
protected override void LoadContent()
{
const int numVertices = 4;
vb = new VertexBuffer(GraphicsDevice, typeof(MyVertex), numVertices,
BufferUsage.None);
MyVertex[] vertices = new MyVertex[numVertices] {
new MyVertex(new Vector2(-1.0f, 1.0f), new Vector2(0.0f, 1.0f)), // LT
new MyVertex(new Vector2( 1.0f, 1.0f), new Vector2(1.0f, 1.0f)), // RT
new MyVertex(new Vector2(-1.0f, -1.0f), new Vector2(0.0f, 0.0f)), // LB
new MyVertex(new Vector2( 1.0f, -1.0f), new Vector2(1.0f, 0.0f)), // RB
};
vb.SetData(vertices);
// ContentにMandelbrot.fxを追加しておく
effect = Content.Load<Effect>("Mandelbrot");
base.LoadContent();
}
protected override void Update(GameTime gameTime)
{
// 移動量
float offsetMove = 0.01f * (float)Math.Log(zoom + 1.0f);
KeyboardState key = Keyboard.GetState();
if (key.IsKeyDown(Keys.Left)) {
offset.X -= offsetMove;
}
if (key.IsKeyDown(Keys.Right)) {
offset.X += offsetMove;
}
if (key.IsKeyDown(Keys.Down)) {
offset.Y -= offsetMove;
}
if (key.IsKeyDown(Keys.Up)) {
offset.Y += offsetMove;
}
if (key.IsKeyDown(Keys.PageDown)) {
zoom *= 1.05f;
}
if (key.IsKeyDown(Keys.PageUp))
{
zoom /= 1.05f;
}
if (key.IsKeyDown(Keys.Escape)) {
Exit();
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// エフェクトのパラメータ更新
effect.Parameters["Zoom"].SetValue(zoom);
effect.Parameters["Aspect"].SetValue(GraphicsDevice.Viewport.AspectRatio);
effect.Parameters["Offset"].SetValue(offset);
GraphicsDevice.SetVertexBuffer(vb);
foreach(EffectPass pass in effect.CurrentTechnique.Passes) {
pass.Apply();
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
}
base.Draw(gameTime);
}
}
class Program
{
static void Main()
{
using (Game1 game = new Game1()) {
game.IsMouseVisible = true;
game.Run();
}
}
}
Mandelbrot_2.fx
float Aspect = 1.0f;
float Zoom = 1.0f;
float2 Offset = float2(0.0f, 0.0f);
// 頂点シェーダ
struct VertexIn
{
float2 Pos : POSITION;
float2 UV : TEXCOORD0;
};
struct VertexOut
{
float4 ScreenPos : POSITION;
float2 UV : TEXCOORD0;
};
VertexOut MyVertexShader(VertexIn input)
{
VertexOut output;
output.ScreenPos = float4(input.Pos, 0.0f, 1.0f);
output.UV = input.UV;
return output;
}
// ピクセルシェーダ
struct PixelIn
{
float2 UV : TEXCOORD0;
};
struct PixelOut
{
float4 Color : COLOR0;
};
PixelOut MandelbrotShader(PixelIn input, uniform int MaxIterate)
{
const float2 c = (input.UV - 0.5f) * float2(1.0f, Aspect) * Zoom + Offset;
float2 p = 0;
int n;
for (n = 0; n < MaxIterate && dot(p, p) < 4.0f; n++) {
p = float2(p.x * p.x - p.y * p.y, 2.0f * p.x * p.y) + c;
}
PixelOut output;
if (n < MaxIterate) {
float u = (float)n / (float)MaxIterate;
output.Color.rgb = float3(u, u, 1.0f - u);
} else {
output.Color.rgb = 0.0f;
}
output.Color.a = 1.0f;
return output;
}
// レンダー
technique Render
{
pass Pass0
{
VertexShader = compile vs_3_0 MyVertexShader();
PixelShader = compile ps_3_0 MandelbrotShader(254);
}
}
最終更新:2012年11月07日 21:43