x


How can I change the color of an object's material, not generate another instance of the material, and still keep batching?

Hi,

I'm trying to optimize my drawcalls on an iPhone project, and it's my understanding that changing the material color generates a new material instance and hence another drawcall.

I read in the forums that using a VertexLit shader and modifying vertex colors will let you change the color of an object and still keep it dynamically batched.

I don't know much about shaders, but in my code I am modifying the "material.color" property, thinking thats how you change the Vertex colors. It's still creating a new material though, so I must be doing something wrong.

So given two objects that share the same material, object A and object B. How can I make: A = red B = blue and still keep batching?

here is a code snippet of how i'm currently doing it

function Start()
{
    thisMaterial = renderer.material;
    origColor = thisMaterial.color;
    fadeToColor = origColor;

    if (unitType > 0)
       fadeToColor.b = 1;
    else if (unitType < 0)
       fadeToColor.r = 1;
}

function Update()
{
    if (unitType != 0)
       thisMaterial.color = Color.Lerp(origColor, fadeToColor, Mathf.PingPong(Time.time, 1));
}
more ▼

asked Apr 24 '10 at 12:19 PM

pyro gravatar image

pyro
760 4 5 23

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You have to change vertex colors by changing the actual colors of the vertex color array from a mesh. Changing material.color only changes the color of the material.

more ▼

answered Apr 24 '10 at 12:37 PM

Eric5h5 gravatar image

Eric5h5
80.2k 41 132 519

(comments are locked)
10|3000 characters needed characters left

You could render them with individual colors in one batch using Graphics.DrawMesh and MaterialPropertyBlock: http://unity3d.com/support/documentation/ScriptReference/MaterialPropertyBlock.html

more ▼

answered May 26 '10 at 07:29 PM

Dylan Fitterer gravatar image

Dylan Fitterer
33 1 1 8

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1999
x811
x506
x137
x115

asked: Apr 24 '10 at 12:19 PM

Seen: 7487 times

Last Updated: Apr 24 '10 at 12:19 PM