- Home /
Changing vertex colors break batching?
Hello everyone, i have to recolor some meshes on my scene, and since shader i use can work with vertex color, so i decided to try just change colors of vertexes runtime. But then i faced with increasing batches. Recolored mesh doesn't batch with others, despite they share one material. I decided to check it again and create empty project with two standart unity's quads. Material use shader (Particles/AlphaBlended) and "Default particle" as Texture. Then i attached to one of quads script, that change its colors.
void Start () {
mesh = this.GetComponent<MeshFilter>().mesh;
Color[] colors = new Color[mesh.vertexCount];
for (int i = 0; i < colors.Length; i++)
colors[i] = Color.black;
mesh.colors = colors;
}
But after all i get 2 batches, instead of 1. Enable script = 2 batches, disable script = 1batch. Same material, only 4 verts in each object. What have i missed? Where is my mistake? Shouldn't it be batched?
UPDATE I managed to get quads batched. First of all i changed in script mesh property, to sharedMesh, which is according to Unitydocs actuall asset in library. After that, as expected, all my quads became black. Then i changed script again and put mesh thing back. And here we are, 1 batch for at least 6 squads colored with different colors. Anybody have a suggestions why is it works like that? I suspect, that i forget to turn off some checkbox somewhere, or read some manual article. Looks like, that changing shared mesh colors change something else as well. Any help?
That's.... weird! $$anonymous$$aybe changing to shared$$anonymous$$esh and back again to mesh woke up something in the rendering pipeline? Can you recreate that fix in a new project?
Answer by tanoshimi · Oct 19, 2016 at 06:26 PM
There's nothing wrong with your understanding or your code. Vertex colours are assigned to the mesh, not material properties, so you can batch together meshes with different coloured verts just fine.
I can produce any number of different coloured quads using the steps you describe, as shown below (note that in order to get down to just one batch, I had to set the camera flag to "Don't Clear" - drawing the background/skybox takes at least one batch on its own - I wonder if that is skewing your figures?)
Also be sure to have batching enabled under Project -> Player Settings.
Hi, thank you for answer. I used camera flag background, but with using "don't clear" i get same results. Only if change flag to skybox i see additional drawcall. Batching is enabled. Here what i get.
But you will get additional drawcalls for the skybox - that's to be expected. But, choose a solid colour background, say, and you'll get two batches - one for the background, and one for as many vertex-coloured meshes as share the same material as you want.
I chose don't clear and get 2 batches as well, that what i wanted to say. And 3 batches, if i choose skybox, since skybox also do drawcall, as you mentioned above. Sorry for misunderstanding
$$anonymous$$ore than that. This recolored quads don't even want to batch with each other. Each additional quad adds 1 drawcall. Enabling and disabling dynamic batching in player settings affects immediately with 2 drawcalls for non-colored quads.
If I recreate that scene (I'm using 5.3.3f1) I get 2 batches as expected, so there must be something else different about your scene, project settings, or something that's preventing batching occurring.
I also tried to recreate same situation in Unity 4.6 and of course it works perfectly well there. 1 drawcall for all quads. In Unity 5.4.0 problem remains. Well, anyway, thank you for help and patience
Answer by flaviusxvii · Oct 19, 2016 at 05:08 PM
Ignore these lies.. ---Changing the vertex colors is building a new set of data that has to be sent to the gpu separately. That means a new batch.---
Huh? No it doesn't. It's just adding properties to vertices of the existing mesh data.
You're right. I was remembering this: http://answers.unity3d.com/questions/525327/batch-drawcalls-of-gameobjects-with-a-script-attac.html incorrectly.
I saw related topics here, where people said that it should work well.
Answer by Lilius · Oct 19, 2016 at 05:30 PM
You can still batch them after you have done changing vertex colors:
https://docs.unity3d.com/ScriptReference/StaticBatchingUtility.html
Thanks for answer. This is quite useful tool, but unfortunately they said it blocks transform for objects, which is not acceptable for me :(
It's sad that every approach works only when the objects are static, as in Unity Cookies Tutorial.