SpriteBatchとModel
■SpriteBatchとModelを同時に表示したら、ポリゴンのソートがおかしくなりました。
namespace WindowsGame2_13 { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; private double dAng = 0; private Model model; private Matrix mainView; private Matrix mainWorld; private Matrix mainProjection; private SpriteBatch sprite; private Texture2D texture; 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 ); base.Initialize(); } protected override void LoadContent() { sprite = new SpriteBatch(graphics.GraphicsDevice); texture = Content.Load<Texture2D>("mimi"); } 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); sprite.Begin(); Vector2 pos = new Vector2(0, 0); sprite.Draw(texture, pos, Color.White); sprite.End(); for (int j = -1; j < 2; j++) { for (int i = -1; i < 2; i++) { mainWorld = Matrix.CreateTranslation(i * 2.5f, 0, j * 2.5f); 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); } } }
デプスが無効化している事が問題なので
protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); sprite.Begin(); Vector2 pos = new Vector2(0, 0); sprite.Draw(texture, pos, Color.White); sprite.End(); graphics.GraphicsDevice.RenderState.DepthBufferEnable = true; for (int j = -1; j < 2; j++) { for (int i = -1; i < 2; i++) { mainWorld = Matrix.CreateTranslation(i * 2.5f, 0, j * 2.5f); 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); }
とし、この一行を入れる事で解決です。
graphics.GraphicsDevice.RenderState.DepthBufferEnable = true;
「SpriteBatch」の中で GraphicsDevice の設定を変えているのを思い出しました。詳しくは
http://blogs.msdn.com/ito/archive/2007/03/13/spritebatch-part2.aspx
参考をにしました。 感謝、感謝