x


what's the best way to reduce draw calls?

Following on from "What is a reasonable number of draw calls relative to one hardware configuration ? @ http://answers.unity3d.com/questions/2117

what's the best way to reduce draw calls?

EDIT : Link updated to Qato link

more ▼

asked Apr 07, 2010 at 09:53 AM

David 3 gravatar image

David 3
774 21 32 45

(comments are locked)
10|3000 characters needed characters left

8 answers: sort voted first

Draw calls, broadly speaking, are similar to when a painter has to un-load their brush with one colour of paint, and load it up with another, before continuing to draw.

In GPU terms though, the different "paint colours" equates to different materials in your scene, and different models which can move independently.

Therefore, you can reduce these number of draw calls by designing the objects in your scene so that as much as possible can be drawn with each single "call".

Combining Meshes of the same material & texture.
If you have a number of scattered scenery objects in your scene which all use the same texture and material - for example, many buildings, rocks, fences, etc - which do not need to move independently, you can combine these objects together into a single large mesh which shares one single material even though the parts may be physically separate in space. This way, you incur one draw call for the whole set, rather than one per object.

You can automatically combine meshes in Unity using the CombineChildren script provided in the Standard Assets package.

Combining Meshes with different materials & textures.
If you have objects which use different textures and materials, it's still possible to combine them into a single mesh and thereby reduce draw calls, by creating what's known as a Texture Atlas. A texture atlas is a single large texture which contains each of the smaller textures used by a number of objects. Each object's UV coords is modified to fit the smaller area within the atlas. There are a couple of drawbacks to this technique, one of which is that all the objects must now use the same material, and therefore the same shader type (even though they keep their respective textures).

For example, if you had a building which uses different textures for the roof, doors, walls, beams, cornerstones, windows, etc, you might combine these into a single texture atlas. In doing this, you sacrifice the ability to use a different material for each item, but you gain the benefit of being able to collapse the various parts of the building into a single mesh with a single material, therefore enabling you to draw the entire building in 1 call, instead of many:

Example of a texture atlas

Typically though, the original "window" material might be transparent and reflective, and therefore wouldn't necessarily be suitable to be merged down into a single material with the other textures, so you might end up with two texture atlases for the final building. One for the opaque, non-reflective parts, and one for the transparent reflective parts.

If you then also have lots of buildings which use these same textures, you could combine these building meshes together too - so for example, you might end up with 10 buildings drawn with just two draw calls: one mesh (and one call) for the combined opaque parts of all 10 buildings, and another mesh & call for the combined transparent reflective parts of all 10 buildings.

This page on GamaSutra has an example of a tightly packed texture atlas which encompasses many objects into one texture: GamaSutra - Practical Texture Atlases

And here's a 3ds Max script which automatically generates texture atlases for you: Texture Atlas Generator. This is worth looking at even if you don't have 3ds Max, because the page has some good animated examples of how a texture atlas is formed.

Texture atlases are also commonly used in games which use 2D sprites for similar reasons, to avoid having to use different materials for each object, and to combine many frames of animation on to a single texture. In these cases they're sometimes referred to as Sprite Sheets.


Another point worth noting is that Unity iPhone supports an automatic batching feature, which automatically combines meshes which share the same material. This means that you can even reduce draw calls further by sharing materials between objects that are moving relative to each other in the scene! (Hopefully this feature will make it into the regular version of unity at some point, but that is just speculation for now!).

more ▼

answered Apr 07, 2010 at 10:35 AM

duck gravatar image

duck ♦♦
47.6k 129 189 457

wow lots of info - thanks - i am sure this will be useful to many people :)

Apr 07, 2010 at 10:49 AM David 3

Note: Unity 3 now implements this feature for all platforms, not just iPhone.

Sep 23, 2010 at 11:08 PM Bampf

another drawback is that combining identical objects to a big one might increase the file size.

Sep 23, 2010 at 11:24 PM alexnode

Does the new Auto-Batching feature in Unity 3 (was previously only on iPhone) basically mean you don't need to combine meshes with the same material manually and the docs just haven't been updated (where they say to "combine,combine,combine")? The meshes I'm talking about would be dynamic meshes in Unity Pro targeting PC Standalone.

Jan 31, 2011 at 01:40 AM brad_ict

Combining meshes is not all gain. For example, it defeats occlusion culling optimizations. Drawing the whole village while you're inside one building, for example, is clearly inefficient, regardless of how cleverly GPU-side culling is.

Apr 20, 2011 at 10:47 PM Waz
(comments are locked)
10|3000 characters needed characters left

Combine objects using the same material into one object (or at least fewer objects), either manually or with the CombineChildren script. If this isn't feasible because of having too many different materials, you can make fewer materials by combining different textures into texture atlases and then UV mapping objects which used those textures to take advantage of this. Then, with fewer materials, you'll have more objects that can be combined.

more ▼

answered Apr 07, 2010 at 10:00 AM

Eric5h5 gravatar image

Eric5h5
110k 55 181 659

if I do 2048 x 2048 textures combining my lightmaps will they be compatible with older graphic cards?

Apr 07, 2010 at 10:32 AM alexnode

How old is 'older'? I think cards have supported 2048x2048 since the the Radeon 9800, and that is now pretty 'old' hardware,

Apr 07, 2010 at 12:50 PM noradninja

@noradninja: Older than that...even the Radeon 7000 supports 2048x2048.

Apr 08, 2010 at 08:38 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Jul 16, 2013 at 09:46 AM

Kos Dvornik gravatar image

Kos Dvornik
1.9k 3 8 44

(comments are locked)
10|3000 characters needed characters left

all good!.. and dont forget to disable invisible objects in camera culling mask...

more ▼

answered Sep 23, 2010 at 10:34 PM

Mikeh gravatar image

Mikeh
13 1

(comments are locked)
10|3000 characters needed characters left

The Bob script ! It is a script I made (MIT license) that helps you reduce draw calls of dynamic and static ( atm click here for static ) meshes, reducing them to one draw call per shader. To my knowledge this is as low as it gets.

more ▼

answered Jun 01, 2011 at 05:25 PM

Ippokratis gravatar image

Ippokratis
569 7 10 24

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1100
x766
x231
x82

asked: Apr 07, 2010 at 09:53 AM

Seen: 66624 times

Last Updated: Sep 18, 2013 at 09:43 AM