- Home /
How can I blend between 5 different materials?
Hi there. I've been taking a look at the unity scripting reference for material.Lerp, but it only seems to list an example where you might want to blend between 2 materials.
In my scene i need to blend between 5 different materials at a constant rate using either PingPong or Loop, over a cyclical period of 10 seconds.
Can anyone help please with some kind of example/link as to how I might go about this?
(P.S. Ideally if you could show an example in C# that would be great, but I don't mind if it's in Javascript).
edit(moved from answer-comment)
Ah! good point. I didnt clarify that. Im thinking lerp gradually from A-B, then B-C, then C-D, then D-E, then E back to A
Do you want to blend between A, B, C, D, E at the same time (something like lerp( lerp( lerp ( lerp( A, B, t5), C, t3), D, t2), E, t1)), or do you want to lerp between A and B, then B and C, then C and D ... ?
Answer by Wolfram · Jun 01, 2012 at 06:47 PM
You can split up your lerp parameter over the array of materials:
// assuming "blend" is your interpolation parameter, moving from 0 to 1 in 10 seconds
if(myMaterialArray.Length>=2){
float scaledBlend=blend*(myMaterialArray.Length-1);
int index=Mathf.FloorToInt(scaledBlend);
if(blend==1){
// special treatment/workaround for maximum, to prevent out-of-bounds overflow
index--;
}
float partial01blend=scaledBlend-index; // the fraction of scaledBlend
renderer.material.Lerp(myMaterialArray[index],myMaterialArray[index+1],partial01blend);
}
EDIT: Oh, this does not lerp E back to A. So that's left as an exercise for the reader ;-)
Answer by Berenger · Jun 01, 2012 at 10:16 PM
I'm not sure what would be the best performance-wise, to declare 2 texture in you shader and use SetTexture in your script to assign them correctly, or to declare them all and use an int to choose the combinaison for the lerp inside the shader. Sort of if( i == 0 ) lerp( A, B, t ) etc. But both are correct.
Your answer
Follow this Question
Related Questions
Material lerp problem 1 Answer
Way to get a material to alpha-fade along the texture? 0 Answers
Vertex Alpha value doesn't update? 1 Answer
Lerping/Fading between multiple colours 1 Answer
Fade an object out 0 Answers