Search Unity

Can someone explain batching? and what I'm not understanding?

Discussion in 'Editor & General Support' started by bumble864, Feb 10, 2012.

  1. bumble864

    bumble864

    Joined:
    Jan 27, 2011
    Posts:
    128
    Hiya,
    So I'm trying to optimize for mobile and I just can't figure out batching. I've read the literature I could find, and thought I knew what I was doing but it's not working. I started doing some tests and the end result is this
    $BatchRendering.png
    I have the same ship with the only difference being the one on the right has one more piece as part of the mesh exported from Studio Max into FBX files. The ship both ships fall under what I read was the 300 vert limit of batch rendering but the ones on the right do not batch while the ones on the left do batch. So that's bugging the crap outta me because I thought I had 300 verts per object.

    So yeah, I'm actually gonna end this one here because the other problem is with FBX files.
    Any help or explanation anyone has would be fantastic.
     
  2. larvantholos

    larvantholos

    Joined:
    Oct 29, 2009
    Posts:
    668
    Are they both set to static? It wont batch them automatically unless its a static object, and likely one of them is, and one of them isn't. You can't batch dynamic objects, at least not the same way. It can still batch your materials for instances - all batching does it combine objects (meshes, materials, etc) to reduce the system load, by instantiating the multiple objects into one source.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes you can, that's the point of dynamic batching. Also static batching doesn't have any polygon limits, plus static objects can't move.

    Anyway, dynamic batching has pretty severe limits for number of polygons in the model, and it's not entirely clear what the limits are, so keep reducing until it works.

    --Eric
     
  4. bumble864

    bumble864

    Joined:
    Jan 27, 2011
    Posts:
    128
    larvantholos, I'm not looking for static batching, these ships are going to be moving through the scene. According to Unity's help Dynamic batching happens automatically with objects smaller than 300 tri's, even if they are moving (as I understand it). For example, the bullets my enemy ships shoot. They are a flat plane. A 2 triangles plane object that I exported out of Studio Max, they all have a shared texture, each bullet moves independently but even if there are 100 of them on screen they all batch into one single draw call.
    According to documentation all objects under the 300 triangle limit with the same texture will batch render and take up only one draw call so I've started modifying my enemy ships to fall under the 300 triangle limit and all use the same texture atlas, but Unity doesn't want to batch my ships and I can't figure out why.
    The objects in my image above are nothing more than FBX's I placed into a scene. I even over-wrote the FBX file when I was adding and removing parts from the mesh to find that sweet spot between batching and not.

    OK, another conundrum has arisen in my attempt to optimize, and understand optimization. Can anyone tell me what a shaders effect is on Batch Rendering? Just for gits and shiggles I went into studio max and exported the full ship. Studio Max has the ship listed at 172 verts and 168 tris, Unity has the ship listed at 342 verts and 168tri's. With my previous tests I was assuming it would not batch, so I put three into a blank scene and they didn't, until I changed the shader. If the material has a diffuse shader the ships do not batch, but if it has an Unlit shader the ships all batch just fine. I thought they just needed to have the same shader, and the same textures, not specific shaders. But if I put the material that my bullets use onto the ships they do not batch even though my bullets do.

    This segways into my other problem I just started looking into. In the above picture the ships that Unity is batching are listed at 224 verts and 126tri's when in studio max it is only 124 verts and 126 tri's. Where's double the verts coming?
     
  5. bumble864

    bumble864

    Joined:
    Jan 27, 2011
    Posts:
    128
    Thanks for the tip Eric, there doesn't seem to be any specific, hard tech specs about batching that I can find. At this point I'm thinking about going back to studio max. Duplicate the enemy ship a dozen or so times, merge them all into one mesh and just skin them to individual dummy objects.
     
  6. lakehaze

    lakehaze

    Joined:
    Mar 7, 2011
    Posts:
    24
    Yeah, I've seen that shader-specific craziness too. I'm amazed that there is no real documentation on dynamic batching; there certainly is more going on than what is listed.
    I have some objects that will batch with any shader, and other objects that will only batch with an unlit shader.
    Some of my objects will batch with non-uniform scale adjustments, some will batch with uniform scale adjustments, and some will only batch if the scale is 1,1,1.
    Some skinned meshes will batch, most won't.
    I've given up trying to understand it.
    I can be pretty sure that if I use a shared unlit material, a static 1,1,1 scale value, and no skinning, they will batch. From there I start pushing to see if I can get away with anything more...
     
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Just add them all to one skinned mesh. Ippokratis batching tools http://forum.unity3d.com/threads/113508-Batching-Tools allows you to build your own dynamic batching system via skinned meshes. Clever eh?

    You'll be needing 1.4 of batching tools for this to work properly.

    The alternative is of course skinning a bunch of them yourself manually, could get a bit tricky. Regardless, using batching tools, simply move the object very far from the world (-9999 etc) to "cull it. There is still the vertex processing overhead but not the pixel shading overhead or the fill rate and so on. Usually vertex processing isn't something that is slowing down iOS.

    Personally I do not advocate unity's dynamic batching for anything except minor debris, quads and so on (which I think kinda was the point of it).

    Dynamic batching also breaks under many circumstances such as scaling a mesh. So check out your own skinned mesh solution, bear in mind to set bone influence count to 1.
     
  8. bumble864

    bumble864

    Joined:
    Jan 27, 2011
    Posts:
    128
    thanks lakehaze, I it looks like just keep playing around till I find something that works.

    hippocoder, I'm reading about this now, if it works/does what I'm looking at now I think I'm going to jump through this monitor and kiss you ;-)
     
  9. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Thanks for the kiss. I hope you brushed your teeth first though.

    You will need 1.4 - I am not sure ippo has that available yet (it should be - in any case just ask him :) )