XNAを使用して、3Dモデル上に2Dメニューを作成する必要があります。現在、2D用のスプライトバッチと3Dモデルを作成しました。しかし、私が気づいたように、そして他の場所で言及されたように、zバッファの問題のためにモデルが正しく表示されていません。チュートリアルによると、drawメソッドでDepthBufferを再度有効にすることになっています。しかし、どういうわけか、私が使用するとき:
GraphicsDevice.DepthStencilState.DepthBufferEnable = true;
コードはデバッグ中にエラーをスローし、次のように言います。
読み取り専用のDepthStencilStateは変更できません。状態オブジェクトは、GraphicsDeviceに初めてバインドされたときに読み取り専用になります。プロパティ値を変更するには、新しいDepthStencilStateインスタンスを作成します。
今、私もDepthStencilStateの新しいインスタンスを作成しようとしましたが、それでも機能しないようです。ヘルプドキュメントに読み取り/書き込み値が示されている場合でも、常に同じエラーが発生します。
3Dモデルを正しく表示する方法を理解するのを手伝ってください。
参考までにDrawコードを示します。
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
Matrix[] transforms = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in myModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
//effect.DirectionalLight0.Enabled = true;
//effect.DirectionalLight0.DiffuseColor = Color.AntiqueWhite.ToVector3();
//effect.DirectionalLight0.Direction = new Vector3(0, 0, 0);
effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(myModelRotation) * Matrix.CreateTranslation(myModelPosition);
effect.View = Matrix.CreateLookAt(new Vector3(0, 0, 3000), Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f),
GraphicsDevice.Viewport.AspectRatio, 1, 5000);
}
mesh.Draw();
}
spriteBatch.Begin();
spriteBatch.Draw(myTexture, new Vector2(0, 0), Color.White);
spriteBatch.End();
DepthStencilState d = new DepthStencilState();
d.DepthBufferEnable = true;
GraphicsDevice.DepthStencilState = d;
base.Draw(gameTime);
}