Unity 4 static/dynamic batching + deferred lighting

Hi,

I’ve all my static objects set to static and they share a lot of materials, yet I cannot see any static/dynamic batching. The “saved by batching” counter is always 0…

Is batching actually active?

I have a pro version btw…

Honestly?!? No one? Not even Unity Devs?!?

Hmmm, that’s a tough one. I can only say that mine works (deferred lighting, static objects sharing the same materials). E.G: Draw Calls - 398, Saved by Batching - 2910

Are you using baked lighting? When you bake lights, it adds a (hidden) material, which basically means nothing would actually batch. At least as I understand it (I don’t use baked lighting, all my lights are dynamic). So maybe that is the culprit?

Static batching works perfectly fine with lightmapping, deferred rendering, etc. There must be another variable that you’re not considering. The first obvious question is whether or not static batching is turned on in the player settings. After that you will need to provide more information about your particular scene/project.

Really? Then I don’t understand what this quote from the docs is referring to. Can you clarify what this means?

“Objects with lightmaps have additional (hidden) material parameter: offset/scale in lightmap, so lightmapped objects won’t be batched (unless they point to same portions of lightmap)”

I take that to mean that lightmapping and batching are pretty much incompatible. Am I wrong? I guess I must be misunderstanding something.

Ah yeah, that’s a good place to start :slight_smile:

It means that even if all your objects used the same material if they access different lightmaps they can’t be batched together.

For example (slightly simplified):

If you had a scene with 500 objects that all used the same material, but the scene consisted of 25 lightmaps then the best it could ever be batched into would be 25 draw calls.

But if you had a scene with 500 objects that all used the same material, and the scene consisted of 1 lightmap then the objects could potentially be batched into 1 draw call.

Counter checked multiple times: All objects are set to static, I’d say at most there are 4-5 materials, 1491 draw calls 0 saves, and the player has both (static and dynamic) batching flags ticked…

No error is given…

Anything else I should look at?

Oh perhaps this: I’ve generated the level through a editor script (script has instanced the objects) and all objects have a “(clone)” marker… I have saved the generated scene though and no object (but the player character) is generated at runtime.

How many lights with realtime shadows turned on are you using? Also, are you sure materials weren’t cloned with your scene generator?

Ah, I see. That makes sense, thanks for putting me straight, Antenna, much appreciated.

Perhaps the docs ought to be clarified on that point? Because that’s not really what it says. It’s kind of confusing, to me at least. Maybe it’s just me :slight_smile:

Hope you get it worked out Metron.

Yeah, it’s the “unless they point to same portions of lightmap”… it should simply say “unless they point to the same lightmap”.

There’s only 1 directional light…

how can I see if the materials have been cloned?

No one?

Btw. I have found how to avoid the “(clone)” when generating the level through my level editor. I have to use

newGo = (GameObject)PrefabUtility.InstantiatePrefab(ti.prefab as GameObject);
newGo.transform.position = new Vector3(x * 4, 0.0f, y * 4);
newGo.transform.rotation = rot;

instead of

newGo = GameObject.Instantiate(ti.prefab as GameObject, new Vector3(x * 4, 0.0f, y * 4), rot) as GameObject;

but this does not solve the problem with the batching. The materials don’t seem to be cloned because when I change something in one material of a game object, it’s mirrored in all other game objects.

Ok… bumping up this thread…

I’ve tested this and the batching is done ONLY when I switch to forward rendering. When using deferred rendering, nothing get’s batched. V4.0.1f2

bump

so besides making the editor seting turned on into static batching did you made shure to set your objects to static? and what shaders are you using? any custom ones out there?

All objects are static, the animation component is disabled on all objects, the scene is entirely created out of prefabs with only 5 different objects (4 wall parts, 1 floor part), no custom shaders, 1 directional light in the scene, nothing special…

as I said, switching to forward rendering actually batches…

but when I enable deferred rendering, it doesn’t batch anymore.

Does anyone know if this is a bug or has dynamic batching never worked with deferred lighting?

Hi,

dynamic batching works. I had quite some trouble with it, but I finally got it working. Some things to keep an eye on:

a) Make sure that dynamic and static batching is ticked in the player settings (i.e. in the settings for PC, Mac Linux Standalone).

b) Make sure that the rendering path is set to deferred lighting.

c) Make sure that you have set the correct build settings (don’t look at PC/Mac when you’re working with Web builds)

d) Make sure that you have set the correct quality settings for the Editor.

e) Make sure that the rendering path for the camera says “use player settings”

f) Mark all objects that are static as being static. Also make sure that the sub-objects are marked static.

g) Make sure that all dynamic objects actually have unticked the static checkbox. Do this for all sub-objects.

h) Make sure that the objects that, in your opinion, should be batched, use the same materials and no cloned materials or materials altered in runtime.

i) Make sure that the objects that should be batched don’t have too much vertices. AFAIK there is a vertex limit dependent on the number of vertex information elements (position, normal, UV1, UV2, …). I think this limit is around 900 vertices.

Those are the “first points” that spring me to mind when thinking again of the batching…

Hope this helps.

Hi Metron, this may be a silly question but, do you know if dynamic batching is supposed to work with realtime shadows?

I started a new project in unity 4.1.2, and made a simple scene, just a few cubes and a directional light. With forward rendering, dynamic batching only works if the light is set to no shadows. With deferred rendering, it wont work at all, even with no lights in the scene?

Yeah, it should work… doing this all the time… I’ll check that with new project generation… just a sec :slight_smile: