Graphics 在XNA/WP7中,在3D上绘制2D为我创建了一个不可靠的3D渲染
我正在尝试弄清楚XNA中的3D渲染是如何工作的,它似乎与DirectX非常相似,这使得它非常简单 但有一种行为是我在用雪碧画图时没有想到的 当我只绘制基本体(一个3D立方体)时,没有任何精灵,我得到以下结果: 但当我试图以以下方式在2D精灵(从SpriteBatch绘制)上绘制它时:Graphics 在XNA/WP7中,在3D上绘制2D为我创建了一个不可靠的3D渲染,graphics,windows-phone-7,3d,xna,sprite,Graphics,Windows Phone 7,3d,Xna,Sprite,我正在尝试弄清楚XNA中的3D渲染是如何工作的,它似乎与DirectX非常相似,这使得它非常简单 但有一种行为是我在用雪碧画图时没有想到的 当我只绘制基本体(一个3D立方体)时,没有任何精灵,我得到以下结果: 但当我试图以以下方式在2D精灵(从SpriteBatch绘制)上绘制它时: SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); base.Draw(gameTime);
SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
base.Draw(gameTime);
SpriteBatch.End();
shape.Draw(gameTime);
这是我得到的结果:
这显然是错误的。我的第一个想法是使用正交投影进行渲染(出于某种原因),但我不确定
这是我用来绘制立方体的代码,它几乎是XNA原语样本中的1:1(我还没有开始为游戏设计一个合适的3D框架),所以可能会很混乱
GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
// Create camera matrices, making the object spin.
float time = (float)gameTime.TotalGameTime.TotalSeconds;
Vector3 cameraPosition = new Vector3(0, 0, 2.5f);
float aspect = GraphicsDevice.Viewport.AspectRatio;
float yaw = 3.05f;
float pitch = 5.34f;
float roll = 8.396f;
Matrix world = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll);
Matrix view = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
Matrix projection = Matrix.CreatePerspectiveFieldOfView(1, aspect, 1, 10);
// currentPrimitive.Draw(world, view, projection, Color.Red);
// Reset the fill mode renderstate.
GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;
Color color = Color.Red;
// Set BasicEffect parameters.
basicEffect.World = world;
basicEffect.View = view;
basicEffect.Projection = projection;
basicEffect.DiffuseColor = color.ToVector3();
basicEffect.Alpha = 1.0f;// color.A / 255.0f;
GraphicsDevice device = basicEffect.GraphicsDevice;
device.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;
GraphicsDevice graphicsDevice = basicEffect.GraphicsDevice;
// Set our vertex declaration, vertex buffer, and index buffer.
graphicsDevice.SetVertexBuffer(vertexBuffer);
graphicsDevice.Indices = indexBuffer;
foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes)
{
effectPass.Apply();
int primitiveCount = indices.Count / 3;
graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0,
vertices.Count, 0, primitiveCount);
}
我还应该澄清,这不是spritebatch.begin/spritebatch.end的结果,如果我只是开始/结束而不绘制任何东西,最终结果是好的,但是如果我绘制字体或sprite,它会破坏立方体
有人有什么想法吗?我的建议是在执行
SpriteBatch.End()方法调用后检查图形设备的设置。使用SpriteBatch
时,它确实会改变图形设备中的某些渲染状态。可能值得在SpriteBatch.Begin()
之前存储图形设备的值,然后在SpriteBatch.End()
方法调用之后重置它们。这解决了问题,改变了消隐缠绕顺序。我遇到了一个类似的问题(),造成了很多挫折。