问题描述
在我的 XNA 3D 游戏中,由于某种原因,深度缓冲区被关闭并被忽略 - 即使我已经做了我能找到的一切来启用它(诚然,这并不多,但它应该很简单... 更不用说默认了)
In my XNA 3D game, for some reason, the depth buffer is off and ignored - even though I've done everything I can find to enable it (which admittedly isn't much, but it's supposed to be simple... not to mention default)
在渲染模型之前:
global.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
之前的某个地方:
graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;
graphics.ApplyChanges();
使用顶点/索引列表以及 device.DrawUserIndexedPrimitives 和 BasicEffect 渲染模型.
the models are rendered using a Vertex/Index list with device.DrawUserIndexedPrimitives and a BasicEffect.
结果是这样的:
紫色物体离相机很远,第一个绘制.
The purple object is very far from the camera, and is drawn 1st.
灰色物体离相机很近,画在第二个.
The gray object is very near the camera, and is drawn 2nd.
蓝色物体距离相机中等距离,绘制在第三个位置.
The blue object is medium-distance to the camera and is drawn 3rd.
灰色对象正在后面渲染蓝色对象 - 如果您不遵循绘制顺序,这是正确的,但我希望它按与相机的距离进行组织(使用深度缓冲区),在这种情况下,灰色对象应该绘制在蓝色对象的前面.
The gray object is rendering behind the blue object - that is correct if you're going off the draw order, but I want it to organize by distance from the camera (Using the depth buffer), in which case the gray object should draw in front of the blue object.
(而且,不,只是手动快速排序它们,虽然它可能提供临时解决方案,但不是解决此问题的方法)
(And, no, just quickly sorting them manually, while it may provide a temporary solution, is not the way to fix this problem)
- 更新:这与我渲染到 RenderTarget2D 而不是直接渲染到屏幕上的事实直接相关.(如果我在屏幕上渲染而不是渲染目标,则会正确计算深度.程序的其他部分需要渲染目标……或等效系统.)
推荐答案
好吧,我在随机搜索后找到了自己遗漏的东西:
Well I found what I missed on my own after searching randomly:
RenderTarget2D 默认禁用其 DepthBuffer.
A RenderTarget2D has its DepthBuffer disabled by default.
我只需要在我正在绘制的渲染目标上设置 DepthFormat.
I just had to set the DepthFormat on the rendertarget I was drawing too.
我知道我错过了一些明显的东西......
Knew I missed something obvious...
(当网站允许我时,我会在 4 小时内将其标记为绿色.)ED:也就是说,除非在那个时候,有人发布了一个很好的列表,列出了所有可以/应该设置的 XNA 深度缓冲区数据,以更好地帮助可能通过 google 找到此主题的人.
(I'll green-check-mark-this in 4 hours when the site lets me.)ED: that is, unless, in that time, somebody posts a nice list of everywhere XNA depth buffer data could/should be set, to better help people who might find this topic by google.
这篇关于深度缓冲区不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!