- Home /
2 materials on one mesh = 2 draw calls?
Hi,
i have 2 materials for one mesh, my character, which has a jersey (the first material) and a number on the jersey (i used a second material to assign exactly the vertices and avoid the stretch effect, and each jersey will have its specific number). So i am not sure about the draw calls: does it mean unity call twice to paint each material on the same mesh?
thanks a lot
Answer by $$anonymous$$ · Feb 11, 2013 at 11:24 PM
You will get one draw call per material you use inside of Unity. So to avoid this, you could use one material that accepts two textures and combine then inside the shader, instead of using two shaders (two materials) to show the two textures.
Create a new Shader and paste this code in it. Then add one material to your mesh and choose 'TextureCombine_One_Drawcall' shader from the shader drop down.
Shader "TextureCombine_One_Drawcall" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_SecondTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Pass {
// Apply base texture
SetTexture [_MainTex] {
combine texture
}
// Blend in the _SecondTex texture using the lerp operator
SetTexture [_SecondTex] {
combine texture lerp (texture) previous
}
}
}
Fallback "VertexLit"
}
Obviously you could optimise this shader a lot. But this should be pretty decent in terms of performance.
Hope this help.
I wouldn't take this approach just to provide multiple textures for a mesh. It is usually better to use atlases. From a previous conversation on IRC it seems that mobile platforms are often limited in the maximum number of textures that can be added to a single material.
@hdsenevi thanks! so, how could i change dynamically the second texture, whenever i want? (the number on the jersey will be different for each player) @numberkruncher : thanks! okay i think i see how to gather the textures in one texture atlas, but i should put all the texture of the numbers, with the texture of the jersey? this last one is 1024x1024 already, and my number is the same size i think... (x 6,7 textures...) can they be all in the same texture atlas?
Of cause. But the question says that he needs to have two textures, not an atlas. So thats why I proposed this, as a quick fix. I'm well aware that this is not the most optimised way.
If you only have a given number of small permutations of the jersey and the number that can go on top of it, you could create an atlas with all the possible combinations of the number and jersey colour and refer only that in the single material. But I got the feeling that 'Pauls' needed more flexibility than that.
And just for your information, I've sometimes used three or four textures in some of the shaders that I've used in the past. I've tested them running well on iPhone 4 upwards.
And another thing. It's not worth just considering the draw call count alone, when you are trying to measure performance and trying to optimise things (in my experience). Don't get me wrong, draw calls matter. $$anonymous$$eeping them to a $$anonymous$$imum is good. But another important thing is the resolution and the compression of your textures that you use.
It's generally considered a good practice to have around 30 draw calls for a mobile game. But I've sometimes had more (draw calls) with good fps since I was using lower res, compressed textures rather than having fewer draw calls with high res, uncompressed (true colour) textures.
I'm no means an expert on the subject. These are just a couple of things that I've picked up along the way.
But you must be right 'agonumberkruncher', there must be a maximum number of textures that you can add to a single material on mobile platforms. Do yo know what that is on some popular mobile devices?.
Answer by bcullis · Feb 11, 2013 at 07:07 PM
A change in material means settings in the rendering pipeline need to change, so that will require a new drawcall with the new material active.
FWIW: I'm new to Unity (but not graphics in general) so I'm about 70% certain this is the case. For example, I'm not sure how efficient batching of materials might be under the hood, which might minimize the number of calls across the whole scene, but in the case of that particular mesh, each material needs to be "drawn", hence your two drawcalls.
thanks, and would you know how to gather 2 material in a single draw call?
@bcullius - the reference is for dynamic batching. That is multiple game objects with the same material. The original question is about a single mesh with multiple materials. In this case, I believe it uses the submesh array and the materials array. I though this resulted in a single draw call, but according to my searches in the last few $$anonymous$$utes, you are correct there is one draw call per unique material. There are some packages I use (like Vectrosity) that builds meshes and has unique colors for all the lines, yet draws in a single draw call. I'm not sure how they are getting their single draw call.
Yes, there is one draw call for each mesh for each material. Also note that applying the same material to multiple slots in the renderer will also incur additional draw calls. The previous fact may seem pointless, though it is certainly relevant in scenarios where some modelling packages do this for one reason or another. The best solution is to bundle as many textures as possible into a single atlas texture and then share the same material between objects. The asset store offers some tools which make this process easy, and the performance benefits are worth it, especially on mobile devices!
Your answer
Follow this Question
Related Questions
Does a mesh HAVE to have two UV sets? 1 Answer
Dynamic batching not working? 0 Answers
Character lighting issues 0 Answers
Material doesn't have a color property '_Color' 4 Answers
How to assign different colorsets to objects that are using the same mesh? 0 Answers