reduce draw calls on a procedurally generated scene

Hi everybody,

I am procedurally generating a scene, actually, a labyrinth.
I have an algorithm which generates a matrix full of 0 and 1. Then I iterate through the matrix and if I find 1, I put a wall. This caused a lot of problems: a lot of batches, low fps. So I switched to lines detection and I succeded in detecting lines of 1 that I convert to walls made of a scaled cube. Now I have 12 batches and about 145 saved by batching.

Using the last approach I tried to apply a texture to my walls, but the texture was scaled, so, to fix it, I modified the property renderer.material.mainTextureScale accordingly to the scaling of the wall. It works fine, but the batches now are 145 and 0 are saved by batching. Is there a way to do the same thing and reduce the batches (which I understood they are the Unity 4 Draw Calls, right?)?

Thanks a lot for your help.

Jymmy

Does that mean you instantiate a wall game object, or are you using DrawMeshNow to submit draw calls in immediate mode? I’d guess you’re doing the former.
What changes between each instantiation from a material stand-point? As long as you are just changing the transform, I’d expect Unity to dynamically batch these calls. If it doesn’t, could you ensure all of these instantiated objects share a common parent? There’s some talk of combining children, though for cubes, I don’t think that’s needed. In any case, look here

Reg batching vs draws – it’s not a 1:1 thing.
There’s a misconception that more draw calls is bad. What is really bad are pipeline state changes (different shaders, depth on/off, diff blend modes, etc) between the draws. The old (DX11, GL 4) drivers do a lot of state validation prior to submitting your draw call to the GPU. When state changes happen between several draws, that’s a lot of additonal CPU work. Batching refers to grouping geometry (meshes) that share the same material (shaders, shader resources, pipeline state) into a single draw call.
If you understand DX11, this would be a single call to DrawIndexed with big vertex and giant index buffers. There is CPU overhead to this, in that the vertices need to be transformed from model to world space on the CPU now, so that a common vertex shader does the ViewProjection part. Size of triangles batched (screen size covered v/s actual number of vertices can also matter). For more info, see BatchBatchBatch!

I can’t be sure because I’ve never scaled textures from script but it might be “instancing” (for the lack of a better word) a new material per object when you do that… meaning the objects will never be batched as it requires them to share the same material.

I’d use a script to combine all my meshes together into a single object, then apply the material to that new object. That’ll drop your draw calls.

Thanks a lot for your replies. I will try your solutions as soon as I can.
You are right when you say that I used the instantiate method to instantiate my objects. Can the use of DrawMeshNow bring me advantages? I am developing the application for mobile platforms, that is why I am caring about performances a lot.

I have read the Unity documentation about batches and it seems that Uni can batch morr object together only if they share the same material, but the call to renderer.material actually clones the material on the object. This is why I’ll look forward to combine the meshes together in a single mesh. And yes, they share the same parent.

I had a look at BatchBatchBatch (but I admit I didn’t have the time to read it carefully). It seems difficult to me: I have no prior knowledge of DirectX; I have only learned the basics of Unity. I am not so keen ln graphics, so I need some references (books, pdfs, documents). But I know the major programming languages (C#, C++, unfortunately Javascript, and some web languages), so I can understand programming well. Graphics management is my problem. Could you give me some more advanced references that can help me to understand the document and Unity background work (I am speaking about optimization - so how Unity works and, based on it, optimize your game -, more advanced graphics functions - such as DrawMeshNow - and so forth) if you have some?

Thank you a lot!!

Jymmy097