Search Unity

Static Batching Issues

Discussion in 'iOS and tvOS' started by AdsySingle, Sep 19, 2012.

  1. AdsySingle

    AdsySingle

    Joined:
    Jan 5, 2011
    Posts:
    116
    Hello everyone,

    We are building our fourth game for mobile with Unity, but this one is by far our most ambitious. Inspired by the visual quality of games like Shadowgun we amped up our target visuals. So we know what should be possible, but we have hit a drawcall snag. I have narrowed it down to a particular section. We have a long path in the game. This path is made up of modular platforms. Each platform uses the same material and so I would expect it to batch down to 1 drawcall (or 2 once with the lightmaps, is that correct?). The shader is simply the mobile/diffuse shader. Instead of getting a reasonably low number of drawcalls, when I remove everything else from the level and only render the path, I'm getting between 25 and 45 drawcalls.

    We have narrowed down a few possible reasons, but we can't find for sure if these things break the batching or not:

    Firstly, or levels are huge, world space wise. For instance, in the first level that we are trying to bring up to this quality level the first segment of platform is at 0,0,18 and the furthest segment in the path from there is at -9,-229, 1787, so basically a world space distance of 1780ish. The camera far plane is at 3000.

    Secondly, each platform has 2 LOD models, a high and low poly. I have no idea if this breaks batching, but both LODs are using the same material (everything to do with these platforms is using the same material).

    Thirdly, vert count. We have identified one particular segment type that doesn't batch at all. All other segments are batching, but not down into 1 draw call, this particular segment isn't even batching. It's exactly the same, except it's bigger than the others and has 310 verts. I had read that dynamic batching had a 300 vert limit, but I was under the impression this didn't apply to static batching. Even when we turn off the rendering of these platforms, the path gets as many as 40 drawcalls with nothing else in the scene being rendered.

    It seems as if we are missing something fundamental here. The levels are large, but they are modular and take advantage of texture atlases to minimise the number of materials being used and they should be producing quite low drawcalls as far as I can tell. Any help on this subject would be greatly appreciated.

    Adsy.
     
    Last edited: Sep 19, 2012
  2. Jtbentley_v2

    Jtbentley_v2

    Joined:
    Sep 5, 2012
    Posts:
    174
    Static batching, to my knowledge, is unaffected by vertex counts. However, LOD versions isn't really going to be helping your cause. Have you tried dropping the high or low version, to keep everything totally uniform?

    Also, you aren't spawning these on demand are you? Static batching only supports objects in the scene, you can't apply it to run-time instantiated items.
     
  3. AdsySingle

    AdsySingle

    Joined:
    Jan 5, 2011
    Posts:
    116
    No, we aren't spawning on demand. But we may have found an issue. We are using a spline, and on awake all the platforms are put on the spline (to make sure they are properly aligned). This might be breaking the batching somehow.

    I don't think it's the LOD, I set up a scene with nearly 700 platform segments in it with the same camera frustum size and spaced them all the way out , I got 3 draw calls and 600+ batched ones. The only difference I can see is that this test scene didn't have the spline in it. We are using the Super Splines plugin from the Asset Store so I could contact the author of that plugin and see if they have seen anything like that happening before.

    Technically the platforms are all in the right position because they hold their positions after being placed on the spline. So they aren't actually changing position at load up because they check to see if they are already on the position. Since they are, there is no transform.position assignment actually happening. So I'm actually still a little stumped. It seems like the spline has something to do with it, but I don't know how.


    EDIT*
    Although the platforms appear to be in the correct positions in editor and at runtime (so the code should be saying it's in position and not positioning it), I added a debug line and it definitely is being called. So they are all having their positions set at load time. Not sure if that is breaking batching though

    NEW EDIT*
    I disabled this section of code so that the platforms aren't positioned at load time at all. Made exactly 0 difference to the drawcalls. So there is something fundamentally different between the 2 scenes, something else that I'm missing.
     
    Last edited: Sep 19, 2012
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    what materials are you using?
    There are materials that won't batch no matter what, simply because its not technically possible. (generally speaking alpha - masking fall into that category)

    Also, do you test it with the same light setup? Pixel lights cause mesh redraws outside deferred rendering which is visualized as 'draw calls' too
     
  5. Jtbentley_v2

    Jtbentley_v2

    Joined:
    Sep 5, 2012
    Posts:
    174
    OH MY GOD! I had no idea this was a thing, I knew it!
     
  6. AdsySingle

    AdsySingle

    Joined:
    Jan 5, 2011
    Posts:
    116
    We are using a mobile diffuse shader with no lights on for this test to narrow down the issue.

    To add some more confusion we were looking at redesigning the layout of this level. So one of our designers just finished the new layout in a new scene. It's batching almost perfectly, just a few drawcalls coming from the entire path. As far as I can tell, absolutely nothing is different. He built it the same way he did the first one. It's the same prefabs being placed in each level and I can switch from one to the other and see the difference in drawcalls. Although this is good news, it means we broke something in the first scene somehow and we don't know what, which means we could do it again, and if we did it would mean starting a new scene from scratch and hoping we didn't just replicate the mistake as well.

    Originally we had two directional lights in the scene and were baking the lighting into lightmaps. We were getting 6 lightmaps and I thought that might be causing the breaking, so I cleared the bake and left the lights off. But I was still getting too many drawcalls so I figured it wasn't that. Or did I not clear the data correctly some how? Even though the lightmaps weren't visibly rendering was something still happening there?
     
  7. AdsySingle

    AdsySingle

    Joined:
    Jan 5, 2011
    Posts:
    116
    While on the subject, can someone define pixel light for me? And what other lights are there?
     
  8. duck

    duck

    Unity Technologies

    Joined:
    Oct 21, 2008
    Posts:
    358
  9. AdsySingle

    AdsySingle

    Joined:
    Jan 5, 2011
    Posts:
    116
    Fantastic, thanks for the reply. I got me some reading to do.
     
  10. roryo

    roryo

    Joined:
    May 21, 2009
    Posts:
    1,479
    To see accurate stats on which static objects will batch, you have to click Play. In the editor, only dynamic batching stats are shown. That means that in edit mode, a static object with more than 300 verts will appear not to batch. Once you hit play, the stats will update to show the static batching.