Mobile efficiency: this.renderer.material.color vs vertex shaders for changing object color.

I currently have 3 different types of enemies spawning into a scene.

Each of these three enemy types are spawned based off of a prefab; enemy1, enemy2, enemy3.

Each enemy takes damage individually although there may be 5 of enemy 1, 5 of enemy2, and 5 of enemy3 in the scene at any given time (often even more than 5 of each).

At the moment when the enemies take damage I have the individual enemy change colors (using: this.renderer.material.color) from green to yellow to red as they get closer to death (these colors were just a proof of concept for myself).

I am concerned about overall performance, as this game is being designed for a mobile platform, and was recently told that somehow changing individual colors of my spawned enemies would cut down the graphical performance of the game and that I should use vertex shaders in place of “this.renderer.material.color.”

I do not understand why the performance would be better (and specifically for a mobile platform) if I made the replacement. Can someone attempt to explain this or point me in the right direction?

Thank you.

My guess is that whoever told you this was referring to the instancing of materials. When you change the property of a material, Unity makes a new instance of a material to accommodate the change. This way only the individual object changes. But the biggest drawback is that the object won’t be batched anymore, and that adds draw calls which is a problem on mobile platforms.

Vertex colors could be what this person meant by vertex shaders. You can use a vertex colored shader, then change the vertex colors of the mesh. You need this line in your fixed-function shader:

ColorMaterial AmbientAndDiffuse

If you don’t understand shaders, then there are several built-in vertex colored shaders that will do what you want.


Then you need to get the mesh data of your model and change its vertex colors.

 var mesh : Mesh = GetComponent.<MeshFilter>().mesh;
 var colors = mesh.colors;
 for (var i = 0; i < colors.Length ; i++){
        colors *= Color.Red;*

//just for example.
}
mesh.colors = colors;
I haven’t done side by side testing so I can’t say which way is faster. If you don’t have that many characters, it really doesn’t make much difference. If you have lots of objects then the second choice might be faster.