X形式のモデル表示
■Xフォーマットのモデルデータを表示するサンプルです。
簡単なBOXのXデータを用意しました。
「Box.x」
xof 0302txt 0064 Mesh { 8; -1.00000;1.00000;-1.00000;, 1.00000;1.00000;-1.00000;, 1.00000;-1.00000;-1.00000;, -1.00000;-1.00000;-1.00000;, 1.00000;1.00000;1.00000;, 1.00000;-1.00000;1.00000;, -1.00000;1.00000;1.00000;, -1.00000;-1.00000;1.00000;; 6; 4;0,1,2,3;, 4;1,4,5,2;, 4;4,6,7,5;, 4;6,0,3,7;, 4;6,4,1,0;, 4;3,2,5,7;; MeshTextureCoords { 8; 0.00000;0.00000;, 1.00000;0.00000;, 1.00000;1.00000;, 0.00000;1.00000;, 1.00000;0.00000;, 1.00000;1.00000;, 0.00000;0.00000;, 0.00000;1.00000;; } MeshMaterialList { 1; 6; 0, 0, 0, 0, 0, 0;; Material { 1.000000;1.000000;1.000000;1.000000;; 0.000000; 0.000000;0.000000;0.000000;; 0.000000;0.000000;0.000000;; TextureFilename { "mimi.png"; } } } }
テクスチャはいつものmimi.pngを使用しました。
(注意:mimi.pngは64X64の正方形に変形させました、そうしないとコンバート時にエラーになります。)
「Box.x」「mimi.png」をいつものとおり「Content」に登録します。
■プログラムは以下のとおりです。
namespace WindowsGame2_06 { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; private double dAng = 0; private Model model; private Matrix mainView; private Matrix mainProjection; private Matrix mainWorld; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { //モデル読み込み model = Content.Load<Model>("Box"); // mainProjection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45.0f), (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height, 1.0f, 100.0f ); mainWorld = Matrix.CreateTranslation(0, 0, 0); base.Initialize(); } protected override void Update(GameTime gameTime) { // ビューマトリックス mainView = Matrix.CreateLookAt( new Vector3((float)(10.0 * Math.Sin(dAng)), 5.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) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); //model描画設定 foreach (ModelMesh mesh in model.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.View = mainView; effect.Projection = mainProjection; effect.World = mainWorld; } mesh.Draw(); } base.Draw(gameTime); } } }