0
\$\begingroup\$

I noticed after I made a minimap for my 2d strategy game that there was significant lag - only to realize that every single hex is getting its own draw call/batch. I've been messing around for about a day trying to figure this out; trying different batching types, gpu instancing, etc

Effectively it seems that my 2d objects meet the SRP batch requirement to fall into the same batch, however are requiring different draw calls (meaning I'm not really getting the benefit). Reading another post on here maybe this means that I don't have anything to do with regards to batching, and my lag is a result of needing to (instead) optimize the minimap situation. That said, because I can get them to a single draw call with Sprites-Default I assume that I can eventually figure out how to do that with more interesting shaders.

As a test I have a very simple setup: a bunch of hexes with a simple (shadergraph) shader. I can get them to batch with Sprites-Default and have a single draw call, but for the life of me can't get them to for any material. The hexes have no scripts attached to them nor are they touched programmatically, nor are they using (instance) vs just.

Note: I'm using Sprites/Sprite Renderers. I did see the related 3D question from a few years ago but that solution regards merging meshes I believe, and I'm hoping not to generate or merge meshes here. The linked post also seems to be talking of meshes rather than Sprites.

image showing draw calls

I have noticed that if I use Sprite-Lit-Default that I get the same issue. I suspect it has to do with sprites that have normals and/or interact with lighting data.

\$\endgroup\$
3
  • \$\begingroup\$ 16 is not a lot of draw calls and 39 is not a lot of batches. If you're getting 0.6 FPS as shown in your screenshot, something else is wrong (most likely a flaw in your shader or C# code). \$\endgroup\$ Commented Dec 2 at 19:01
  • \$\begingroup\$ If you're not already fairly certain exactly what's wrong, it's best to use profiling tools to narrow down what's actually causing your performance issues. See docs.unity3d.com/6000.2/Documentation/Manual/… for profiling rendering and docs.unity3d.com/6000.2/Documentation/Manual/Profiler.html for profiling CPU. If it's a shader problem, sometimes you can narrow it down just by swapping out the materials using that shader for materials using one of Unity's regular shaders. \$\endgroup\$ Commented Dec 2 at 19:32
  • 1
    \$\begingroup\$ @Kevin The above is just me testing things. The batches/draw calls are going up linearly with hexes. My rendercam for the minimap is causing 2500 draw calls, so I'm attempting to figure it out with something more managable. \$\endgroup\$ Commented Dec 2 at 19:49

1 Answer 1

0
\$\begingroup\$

If your map has hundreds or thousands of hexes, I would not recommend trying to render all of them in the mini-map; whether or not they can be batched, this is adding too much complexity/overhead to the minimap. Instead, consider rendering the map once to a render texture and displaying this texture in the minimap. Any dynamic content such as icons or markers should generally be drawn separately above the texture, not rendered into the texture.

As for how to render the texture, generally you would use a separate camera positioned above the map so that it can see the whole map, configured to only see the layer(s) that the map tiles are on. Make sure to disable this camera after the texture is rendered! If the minimap has a separate visual style, you'll need a separate copy of the map for rendering the minimap.

There are a few possible approaches, depending on how your minimap works:

Static Minimap

If the minimap shows the entire map at once, and the map tiles do not change, then you only need to render the map once, at the same resolution as the minimap. If the tiles change over time, then you might need to periodically re-render the map (this could potentially cause a noticeable momentary stutter, so you might need to be careful about when you do it).

Panning Minimap

If the minimap shows a limited portion of the overall map, you may be able to render the entire map into a texture and pan this around in the minimap window, masking the portion that is outside of the minimap's window. For example, if your minimap is 200x200 pixels and shows 10% of the overall map, then you could render the entire map at 2000x2000. Again, if your map tiles do not change, you would only need to render once, but if they change over time then you might need to re-render some or all of the minimap.

This approach may not be practical if the map is extremely large and a render of the full map would consume excessive texture memory.

Zoomable panning minimap

Some games with a panning minimap also let the user zoom the minimap in and out. This adds an additional layer of complexity. Depending on the needs of your game and how your zoom function works, you could potentially render the minimap at multiple resolutions, or render at the highest resolution and scale it up and down depending on the zoom level.


Additional tips

  • If your game has fog of war, this might constantly change the appearance of the minimap. In this case, it may be better to render the fog of war as a separate effect above the map texture.
\$\endgroup\$
2
  • \$\begingroup\$ This is not a minimap question, this is a question regarding understanding SRP batching and its restrictions. I have a workaround in place that has eliminated the lag/draw calls. Fully agree on all points presented as a reasonable bypass of the symptom (minimap causing lag), but my question is about how to reduce Draw Calls within SRP batches so that I better understand these issues in the future. That said, thank you for your answer/solution. \$\endgroup\$ Commented Dec 3 at 13:09
  • \$\begingroup\$ @user1765812 Your question starts off talking about your minimap. It sounds to me like you are trying to render a minimap made of thousands of individual tiles in real-time. Batching reduces but does not eliminate the overhead of such a complex map; while batching is a useful feature to understand, IMO it's not the right tool for the job here (unless I've misunderstood your setup). Anyway, if you have figured out a solution, please post it as an answer to help future readers. \$\endgroup\$ Commented Dec 4 at 0:00

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.