using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace BasicEffectTest1
{
///
/// This is the main type for your game
///
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// モデルの宣言
private Model myModel;
// テクスチャの宣言
private Texture2D myTexture;
// カスタムエフェクトの宣言
private Effect myEffect;
// カスタム頂点構造体の定義
public struct VertexPositionColorTextureCoordinateNormal
{
public Vector3 Position;
public float Color;
public Vector2 TextureCoordinate;
public Vector3 Normal;
public VertexPositionColorTextureCoordinateNormal(Vector3 position, float color, Vector2 textureCoordinate, Vector3 normal)
{
this.Position = position;
this.Color = color;
this.TextureCoordinate = textureCoordinate;
this.Normal = normal;
}
public static readonly VertexElement[] VertexElements
= {
new VertexElement(
0,
0,
VertexElementFormat.Vector3,
VertexElementMethod.Default,
VertexElementUsage.Position,
0
),
new VertexElement(
0,
sizeof(float) * 3,
VertexElementFormat.Color,
VertexElementMethod.Default,
VertexElementUsage.Color,
0
),
new VertexElement(
0,
sizeof(float) * (3 + 1),
VertexElementFormat.Vector2,
VertexElementMethod.Default,
VertexElementUsage.TextureCoordinate,
0
),
new VertexElement(
0,
sizeof(float) * (3 + 1 + 2),
VertexElementFormat.Vector3,
VertexElementMethod.Default,
VertexElementUsage.Normal,
0
)
};
}
// カスタム頂点の宣言
private VertexPositionColorTextureCoordinateNormal[] verData = new VertexPositionColorTextureCoordinateNormal[4];
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
///
/// 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()
{
// TODO: Add your initialization logic here
base.Initialize();
}
///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
// モデルの呼び出し
myModel = Content.Load("MyModel");
// テクスチャの呼び出し
myTexture = Content.Load("SpearMan01Attack01");
// カスタムエフェクトの呼び出し
myEffect = Content.Load("BasicEffect");
}
///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
///
/// 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 game to exit
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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
// ワールド座標変換行列
Matrix world = Matrix.CreateScale(100.0f) *
Matrix.CreateRotationX(MathHelper.ToRadians(90)) *
Matrix.CreateTranslation(Vector3.Zero);
// ビュー座標変換行列
Matrix view = Matrix.CreateLookAt(
new Vector3(0, 0, 400),
Vector3.Zero,
Vector3.Up);
// 射影変換行列
Viewport viewport = graphics.GraphicsDevice.Viewport;
float aspectRatio = (float)viewport.Width / (float)viewport.Height;
Matrix projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.PiOver4,
aspectRatio,
10, 1000);
// ワールド、ビュー、射影変換行列の設定
myEffect.Parameters["World"].SetValue(world);
myEffect.Parameters["View"].SetValue(view);
myEffect.Parameters["Projection"].SetValue(projection);
// 描画モードを「Textured」に設定(BasicEffect Shader付属ドキュメント参照)
myEffect.Parameters["ShaderIndex"].SetValue(2);
myEffect.Parameters["BasicTexture"].SetValue(myTexture);
// GraphicDeviceに頂点の型を設定する
graphics.GraphicsDevice.VertexDeclaration = new VertexDeclaration(
GraphicsDevice,
VertexPositionColorTextureCoordinateNormal.VertexElements);
foreach (ModelMesh mesh in myModel.Meshes)
{
// 頂点バッファデータのコピーを取得する
mesh.VertexBuffer.GetData(verData);
// テクスチャ座標を書き替える(テスト)
verData[0].TextureCoordinate = new Vector2(0.3f, 0.3f);
// 書き替えたデータをセットする(エラーになる)
// mesh.VertexBuffer.SetData(verData);
foreach (ModelMeshPart modelMeshPart in mesh.MeshParts)
{
// カスタムエフェクトを適用する
modelMeshPart.Effect = myEffect;
// エフェクトを開始する
myEffect.Begin();
foreach (EffectPass pass in myEffect.CurrentTechnique.Passes)
{
// エフェクトパスを開始する
pass.Begin();
// 頂点を描画する
graphics.GraphicsDevice.DrawUserPrimitives(
PrimitiveType.TriangleStrip,
verData,
modelMeshPart.StreamOffset,
modelMeshPart.PrimitiveCount);
// エフェクトパスを終了する
pass.End();
}
// エフェクトを終了する
myEffect.End();
}
// モデルを描画する
// mesh.Draw();
}
base.Draw(gameTime);
}
}
}