- Home /
Draw calls are adding up fast with asset packs
Hello,
For some time now I have been trying to wrap my head around how to use asset packs properly. What is worrying me are draw calls growing fast with every asset placed in a scene.
From what I remember, adviced draw call limit for mobile platforms is about 50, for PC maybe few hundreds, with general idea being the lower the better.
I have downloaded one of low poly asset packs from Kenney and setup a quick and simple scene, consisting of 18 objects, 1 directional light and 1 point light. Every material has only color set, no textures used. (see image)
This tiny setup is already using 123 batches and 64 draw calls, with more being added with every additional object...
I do know there are many, many ways to reduce draw call count. I could use a single material and apply different colors with shader instancing. I could combine all vertices to single mesh and flatten materials to vertex colors.
However, even if I make everything instanced and drawn with one material only, there is still tens of draw calls made for the purpose of light maps.
But most important thing is, if I am required to modify meshes or materials of ready to use packages I somehow feel I am using them wrong way... Or maybe if all geometry is so simple it is not a problem to have houndreds of draw calls even for mobile device?
Any tips welcome.
Thanks, Bartosz
Answer by highpockets · Apr 09, 2019 at 05:41 PM
Since you are just setting a color and no texture on most objects, I suggest that you use one material for all of them and use vertex colours to change the colours in the start method. All objects with that material will then just produce one draw call. I did make a shader that has vertex colors and also allows for a custom lightmap which in my case is just a lightmap texture baked in blender, if you want I can send you the shader over
I’ll extend this by saying the reason I made my lightmaps in blender is because I got 1 good quality 1024x1024 lightmap in blender when I was getting 4-5 1024x1024 lightmaps of lesser quality in unity. Since you are downloading assets, I’m not sure how hard it would be for you to export your scene into your favourite 3D modelling software to bake the scene in there, but this might be a place to start and then you can put the lightmap on the same material and thus your draw calls will be much lower
Thank you, I will probably end up doing exacly that. I love the idea to bake lightmaps in blender, somehow I forgot that this is even an option.
Sounds good, here is the shader if you want it. Note that it says supports lightmap, but I just put that in there and it's not supported by unity's lightmap, it's just a texture. You'll also note that there is a base texture, but you can remove it if you don't need it. I have a base texture for the first UV map as just a black and white texture to add details and then the lightmap for the 2nd UV map. Cheers,
Shader "Custom/VertexColor (Supports Lightmap)"
{
Properties
{
_$$anonymous$$ainTex("Base (RGB)", 2D) = "white" {}
_Color("$$anonymous$$ain Color", Color) = (1,1,1,1)
_Light$$anonymous$$ap ("Light$$anonymous$$ap", 2D) = "white" {}
}
Category
{
Tags{ "RenderType" = "Opaque" }
Lighting Off
SubShader
{
Pass
{
CGPROGRA$$anonymous$$
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 uv : TEXCOORD0;
float2 uv2 : TEXCOORD1;
};
struct v2f
{
float2 uv : TEXCOORD0;
float2 uv2 : TEXCOORD1;
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
};
sampler2D _$$anonymous$$ainTex;
float4 _$$anonymous$$ainTex_ST;
sampler2D _Light$$anonymous$$ap;
float4 _Light$$anonymous$$ap_ST;
fixed4 _Color;
v2f vert(appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFOR$$anonymous$$_TEX(v.uv, _$$anonymous$$ainTex);
o.uv2 = TRANSFOR$$anonymous$$_TEX(v.uv2, _Light$$anonymous$$ap);
o.color = v.color;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float4 col = tex2D(_$$anonymous$$ainTex, i.uv) * tex2D(_Light$$anonymous$$ap, i.uv2);
fixed4 vcol = i.color;
return col * vcol * _Color;
}
ENDCG
}
}
}
//FallBack "$$anonymous$$obile/Diffuse"
}
Answer by Zaeran · Apr 09, 2019 at 05:12 PM
Lighting and shadow is likely the largest cause of your draw calls. Two lights casting shadows and interacting with each other is definitely going to cause a performance hit.
If you don't need the lights to move or change, try baking a lightmap.