- Home /
Best practice to change vertex color on a large amount of quads?
Hi there! For a project of mine I need to change the vertex colors of a large amount of quad meshes (900+) individually. What I want to achieve is that every quad has it's own time offset saved in myAlphaOffset[] that determines when the alpha change for that quad begins (since I subtract that offset from the current progress in my lerp). When 'progress' has reached 1f, every quad, regardless of its offset should end up with a alpha of 0.
My code atm is something like this:
public override IEnumerator ChangeQuadAlphas ()
{
Color transColor = Color.white;
while(changingAlpha){
for(int r=0;r<rows;r++){
for(int c=0;c<cols;c++){
transColor.a = Mathf.Lerp(0f,1f,(progress-myAlphaOffset[c+r*cols])/duration);
quads[c+r*cols].mesh.colors = new Color[]{
transColor,
transColor,
transColor,
transColor
};
}
}
yield return null;
}
yield return null;
}
This code so far works fine but strains the cpu very much. I tried easing the load on the cpu by yielding after each row of the loop but then the alpha changes occur spread over multiple frames. This way I would end up with a lot of half visible quads when progress reaches 1f since there weren't enough frames for changing.
Now I don't know how to optimise that further. Would loop unrolling have much of an impact here? Would it be better to give each quadobject a lerptarget so that the mesh alpha would be changed in the quadobject's Update()? This is what I mean:
//Script attached to each quad
...
Color myColor = Color.white;
float myOffset = someValue;
void Update(){
myColor.a = Mathf.Lerp(0f, 1f, (progress-myOffset)/duration;
this.mesh.colors = new Color[]{
myColor,
myColor,
myColor,
myColor
}
}
I would guess that the overhead of 900+ Update()-functions might outweigh the benefits.
Maybe there is another approach that I might be unaware of so - what is the best (fastest) way of updating the mesh colors of a large amount of meshes individually?
Thanks in advance,
delstrega
Sounds interesting. Since I'm not that much into shaders, what are the possibilities here? Can you pass parameters to shaders and use that to adjust the vertex color there?
You can adjust colors, but it's a massively parallel operation which does the same thing to every vertex. Are the color offsets always the same, between quads? If that were the case, you could do this in the vertex shader. Otherwise, I'm not thinking of a way to do it outside of script.
Unfortunately the offsets differ from quad to quad :( Guess I'm out of luck then, but thanks for clearing that up :)
Answer by Jessy · Jun 17, 2012 at 07:57 AM
There is no fast way to update the colors for a ton of meshes. Combine the meshes. The rest of the stuff you're mentioning, like loop unrolling, is not the problem at all.
Thanks Jessy, I guess I'll have to bite the bullet and try to limit the number of quads then ;)
Your answer
Follow this Question
Related Questions
While loop issues in Update() vs. Start() 0 Answers
c# update loop. How to? 2 Answers
variables based on array.length not updating in JS 1 Answer