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
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
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.