简体   繁体   English

如何防止在屏幕外绘制XNA组件?

[英]How to prevent drawing XNA component when it is off-screen?

I'm making a 2d game in XNA. 我在XNA做了一场2D比赛。 When using drawable game components which one is better for performance? 使用可绘制的游戏组件时,哪一个更适合性能?

1.When a component is not onscreen remove it from the components list and when its onscreen add it. 1.当组件未在屏幕上时,将其从组件列表中删除,并在其屏幕上添加它。

2.When its offscreen dont run its draw function (by using an "awake" bool field and an if statement around everything in the draw function) 2.当它的屏幕外不运行其绘制功能时(通过使用“清醒”bool字段和绘制函数中的所有内容的if语句)

I'm using method 2 at the moment and it works fine. 我现在正在使用方法2,它工作正常。 Its handy cus the draworder of components wont change (if I remove and re-add them it will, and I'll need more code to manage that) In fact it just occured to me that if its not in the components list it wont update and I'll need something else to keep track of whether its onscreen or not.. 它方便的cus组件的draworder不会改变(如果我删除并重新添加它们,我将需要更多的代码来管理它)事实上它只是发生在我身上,如果它不在组件列表中它不会更新我需要别的东西来跟踪它是否在屏幕上..

Also I dont have the fancy version of visual studio with the profiler so I'm just asking here what do people think from their experience 此外,我没有配置探测器的视觉工作室的花哨版本,所以我只是问这里人们从他们的经验中思考什么

A sometimes overlooked concept of CPU performance while using SpriteBatch is how you go about batching your sprites. 在使用SpriteBatch时,有时会忽略CPU性能的概念,就是如何批量精灵。 And using drawable game component doesn't lend itself easily to efficiently batching sprites. 并且使用可绘制的游戏组件并不容易有效地批量精灵。

Basically, The GPU drawing the sprites is not slow & the CPU organizing all the sprites into a batch is not slow. 基本上,绘制精灵的GPU并不慢,并且将所有精灵组织成批处理的CPU并不慢。 The slow part is when the CPU has to communicate to the GPU what it needs to draw (sending it the batch info). 缓慢的部分是当CPU必须与GPU通信时需要绘制的内容(向其发送批处理信息)。 This CPU to GPU communication does not happen when you call spriteBatch.Draw(). 调用spriteBatch.Draw()时,不会发生 CPU到GPU的通信。 It happens when you call spriteBatch.End(). 当你调用spriteBatch.End()时会发生这种情况。 So the low hanging fruit to efficiency is calling spriteBatch.End() less often. 因此,效率低下的结果是不太经常调用spriteBatch.End()。 (of course this means calling Begin() less often too). (当然这意味着不太经常调用Begin())。 Also, use spriteSortMode.Imediate very sparingly because it immediately causes the CPU to send each sprites info to the GPU (slow)). 另外,非常谨慎地使用spriteSortMode.Imediate,因为它会立即导致CPU将每个sprite信息发送到GPU(慢))。

So if you call Begin() & End() in each game component class, and have many components, you are costing yourself a lot of time unnecessarily and you will probably save more time coming up with a better batching scheme than worrying about offscreen sprites. 因此,如果你在每个游戏组件类中调用Begin()和End(),并且有很多组件,那么你自己花费了大量的时间进行不必要的工作,你可能会花费更多的时间来制定更好的批处理方案,而不是担心屏幕外的精灵。 。

Aside: The GPU automatically ignores offscreen sprites from its pixel shader anyway. 除此之外:GPU无论如何都会自动忽略其像素着色器中的屏幕外精灵。 So culling offscreen sprites on the CPU won't save GPU time... 所以剔除CPU上的屏幕外精灵不会节省GPU时间......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM