- Home /
How to redner a single texture, multiple colors without increasing draw calls.
Here is some background: I'm using a single material and rendering it one of four colors depending on its tag. As a test, if I have 20 objects in my scene without a script attached, my draw calls are low because all objects are getting batched.
My problem arises after adding my script. I would expect my draw calls to increase by 4 (1 for each color). But the draw calls are over 350. I did some searching and saw that using renderer.sharedMaterial would lower the enormous amount of draw calls I have and using renderer.sharedMaterial on the first color seems to work. When I put it in my code for the rest of the colors, no matter what tag the object has, it is rendered the first color. So instead of 5/5/5/5 (5 objects for each color) I am getting 20/0/0/0. I'm hoping someone can see an error in my code or at least put me on the right path.
Also, before I forget, I am using a diffuse shader.
#pragma strict
static function RenderChange(first : Transform){
var allChildren = first.GetComponentsInChildren(Transform);
if (first.tag == "1"){
for (var child : Transform in allChildren) {
child.renderer.sharedMaterial.color = Color.cyan;
first.tag = "2";
}
}
else if (first.tag == "2"){
for (var child : Transform in allChildren) {
child.renderer.material.color = Color.magenta;
first.tag = "3";
}
}
else if (first.tag == "3"){
for (var child : Transform in allChildren) {
child.renderer.material.color = Color.red;
first.tag = "4";
}
}
else if (first.tag == "4"){
for (var child : Transform in allChildren) {
child.renderer.material.color = Color.green;
first.tag = "1";
}
}
first = null;
}
Update: I've created two separate objects and put a different material on each one. I was trying to see if I could get the code above to work with two different materials but the same problem occurs. Even with different materials, adding shared$$anonymous$$aterial in the code colors everything the same color.
You will need a custom shader and use control textures that use uv2 for each object to lookup the tint colour. It's pretty advanced stuff and I'm not at my computer right now but I could possibly help.
Do you know how to write shaders?
No, I'm pretty new to unity. I've gotten done some player control scripts, manipulated transforms and game objects, and some other basics. I'm now moving into optimization stuff like draw calls and occlusion culling for the first time and I'm kind of at a loss. I would appreciate any kind of help you could provide, I've been trying on and off for a day or two now and haven't been getting anywhere on this.
So I'm also new to shader program$$anonymous$$g too but I wrote a Noobs Guide on Unity Gems that could serve as a starting point to learning. Using Uv2 or vertex colours is the way to go, shaders aren't actually so hard when you get the $$anonymous$$dset. Check that out and ill post more when at the laptop.
So it's been a while, but I finally looked into this a bit more this weekend and my head almost exploded. I definitely don't know enough to write my own shader yet. I found a $$anonymous$$asked Tint - Vertex Lit shader online but wasn't able to get it working properly, but I'm thinking that its my best option at this point, I think I have an idea of how it works but I have no idea how to implement it based on tags as I mentioned in my question.
Answer by jogo13 · Dec 31, 2012 at 09:05 PM
renderer.sharedMaterial does lower the draw calls, but you can't set seperated material.color values because it's all the same single material (shared.)
To avoid needing seperate materials you could find/create a material that gets it's color from vertex color values. You'd need to set the vertex color values via script. They can be found at mesh.colors[];
This is a link to the shader that I used: http://wiki.unity3d.com/index.php/$$anonymous$$asked_Tint
I used the second shader, which is "$$anonymous$$asked Tint - Vertex Colors" without any modifications. Then using the script in my original question, when a tag was changed, I just set vertex colors ins$$anonymous$$d of changing the material color.
I now have 5 draw calls with 340 batched.