简体   繁体   中英

LibGDX 2D || Is calling draw() on offscreen Objects inefficient or are they automatically culled?

I'm making a simple 2D helicopter game as a way to learn libgdx. If I have an Object located offscreen, in this case numerous skyscrapers, does calling batch.draw() cause OpenGL to attempt to render the Object? It wouldn't be difficult to write a check to skip the draw call if so, I'm just wondering if it's necessary. I used a profiler to count calls/draw calls and the numbers haven't increased so I'm guessing it's okay?

Thanks in advance!

There is a balancing act. They are not culled, so they get sent to the GPU and their vertices are handled by the vertex shader. But after the vertex shader projects them and they are calculated to be outside the frustum, there will be no further calculations (no fragment shader runs for all the pixels).

So there is cost for drawing them, but it is not as high as it is for the ones that are visible. The CPU cost is equivalent for a visible or invisible sprite. The GPU does more work for sprites that are visible and it is almost linearly proportional to the number of pixels on screen that the sprite will occupy.

Of course, there is also some CPU cost to culling them because you would be comparing their positions to the camera. Probably not worth it if you are checking each specific one, but there can be significant savings by checking large blocks at a time, like if you know a pack of trees is grouped together, you keep a bounding box that contains all of them and check that instead of each individual tree.

If you have less than a couple thousand sprites in your game, none of this is worth worrying about even on a low end phone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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