Material 'Lerp' vs 'If' performance ?
I have a material that needs to apply different logic depending on vertex color, but in my case vertex color can be only 100% Red, 100% Green or 100% Blue. So if vertex has R channel == 1.0, it will do things exclusive to vertexes with (1,0,0) color, and so on. For now it works like this:
It works OK, but knowing that our vertexes can only have 3 values [(1,0,0), (0,1,0) or (0,0,1)] is there a better, more optimized way to do this? What about 'If' statement, would it be more performant in this case?
(e.g. if Blue channel == 1.0, do logic for blue vertexes, else check if Green channel == 1.0, and so on...)
I will be grateful for some hints! :)
Good day Slavq,
It depends on a good deal of factors, including your target hardware, and the logic you want to put under each block, and number of adjacent pixels on screen, sharing same vertex color. What you are speaking about here is commonly referenced as dynamic shader branching. IF statement will only skip the block, if it contains no gradient functions,otherwise both blocks will be executed, and one result will be discarded. As a general rough rule, using IF statement will benefit you, if your blocks contain at least 40~50 instructions or a texture sampler.
However, do note, that presently, IF node in material editor will not let you achieve dynamic branching, thus it won't give you any advantages over lerps.
I've covered the issue in detail in **this thread**. There is also an issue tracker link in the first post. Feel free to toss a vote there.
Follow this question
Once you sign in you will be able to subscribe for any updates here