XNA是否会剔除SpriteBatch抽奖活动? [英] Are SpriteBatch drawcalls culled by XNA?

查看:84
本文介绍了XNA是否会剔除SpriteBatch抽奖活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对XNA有一个非常微妙的问题,特别是SpriteBatch. 在我的游戏中,我有一个Camera类.它可以平移视图(显然),也可以放大和缩小. 当我调用spritebatch实例的"Begin"功能(最后一个参数)时,将Camera应用于场景.

I have a very subtle problem with XNA, specifically the SpriteBatch. In my game I have a Camera class. It can Translate the view (obviously) and also zoom in and out. I apply the Camera to the scene when I call the "Begin" function of my spritebatch instance (the last parameter).

问题:当摄影机的Zoomfactor大于1.0f时,spritebatch停止绘制. 我尝试调试场景,但是找不到错误的地方.

The Problem: When the cameras Zoomfactor is bigger than 1.0f, the spritebatch stops drawing. I tried to debug my scene but I couldn't find the point where it goes wrong.

我尝试仅使用"Matrix.CreateScale(2.0f);"进行渲染作为开始"的最后一个参数. 所有其他参数均为​​null,第一个为"SpriteSortMode.Immediate",因此没有自定义着色器. 但是SpriteBatch仍然不想绘制.

I tried to just render with "Matrix.CreateScale(2.0f);" as the last parameter for "Begin". All other parameters were null and the first "SpriteSortMode.Immediate", so no custom shader or something. But SpriteBatch still didn't want to draw.

然后,我尝试仅调用"DrawString",并且DrawString在提供的刻度(2.0f)下可以完美地工作.

Then I tried to only call "DrawString" and DrawString worked flawlessly with the provided scale (2.0f).

但是,通过大量的试验和错误,我发现将ScaleMatrix乘以"Matrix.CreateTranslation(0,0,-1)"也会以某种方式将"safe"值更改为1.1f. 因此所有不超过1.1f的Scale值都可以使用.对于SpriteBatch以上的所有内容,在正常的绘制"调用中不会渲染单个像素. (DrawString仍然不受影响并且可以正常工作).

However, through a lot of trial and error, I found out that also multiplying the ScaleMatrix with "Matrix.CreateTranslation(0, 0, -1)" somehow changed the "safe" value to 1.1f. So all Scale values up to 1.1f worked. For everything above SpriteBatch does not render a single pixel in normal "Draw" calls. (DrawString still unaffected and working).

为什么会这样? 我没有设置任何视口或其他矩阵. 在我看来,这可能是某种奇怪的Near/Farclipping. 但是我通常只从3d东西中知道这些参数.

Why is this happening? I did not setup any viewport or other matrices. It appears to me that this could be some kind of strange Near/Farclipping. But I usually only know those parameters from 3d stuff.

如果不清楚,请询问!

推荐答案

是近/远削波.

您绘制的所有内容都将转换为投影空间,然后栅格化.该空间从屏幕左下方的(-1,-1)到右上方的(1,1).但这只是(X,Y)坐标.在Z坐标中,它从0到1(从前到后).该卷以外的所有内容都会被裁剪. (参考: 1 2

Everything you draw is transformed into and then rasterised in projection space. That space runs from (-1,-1) at the bottom left of the screen, to (1,1) at the top right. But that's just the (X,Y) coordinates. In Z coordinates it goes from 0 to 1 (front to back). Anything outside this volume is clipped. (References: 1, 2, 3.)

在3D模式下工作时,使用的投影矩阵将向下压缩Z坐标,以使近平面在投影空间中降落为0,而远平面在1中降落.

When you're working in 3D, the projection matrix you use will compress the Z coordinates down so that the near plane lands at 0 in projection space, and the far plane lands at 1.

在2D模式下工作时,通常使用Matrix.CreateOrthographic,它具有近平面参数和远平面参数,它们的功能完全相同.只是SpriteBatch指定了自己的矩阵,而将近平面和远平面保留为0和1.

When working in 2D you'd normally use Matrix.CreateOrthographic, which has near and far plane parameters that do exactly the same thing. It's just that SpriteBatch specifies its own matrix and leaves the near and far planes at 0 and 1.

SpriteBatch中的子图形顶点实际上具有Z坐标,即使通常不使用它也是如此.它由layerDepth参数指定.因此,如果您将图层深度设置为大于0.5,然后按2进行缩放,则Z坐标将超出0到1的有效范围,并且不会被渲染.

The vertices of sprites in a SpriteBatch do, in fact, have a Z-coordinate, even though it's not normally used. It is specified by the layerDepth parameter. So if you set a layer depth greater than 0.5, and then scale up by 2, the Z-coordinate will be outside the valid range of 0 to 1 and won't get rendered.

(文档说0到1是有效范围,但未指定应用转换矩阵时发生的情况.)

(The documentation says that 0 to 1 is the valid range, but does not specify what happens when you apply a transformation matrix.)

解决方案非常简单:不要缩放您的Z坐标.使用类似以下的缩放矩阵:

The solution is pretty simple: Don't scale your Z-coordinate. Use a scaling matrix like:

Matrix.CreateScale(2f, 2f, 1f)

这篇关于XNA是否会剔除SpriteBatch抽奖活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆