Hi, I love to keep similar functionalities in one function and control them using static switches. Because they don’t affect runtime performance but affect the compilation time.
Example # 1: If I create an intensity function, then I will add both “Color Intensity” and “Normal Intensity” logic in one function and then add a switch to enable “Color” or “Normal” intensity.
Example # 2: If I create UVs function, then I will use switches for offset, rotation, and scale. It can add up a lot of switches in a material and I want to clear that I am not talking about the parameters just switches with default values or constant static bool to control the flow.
Recently, I read that every static switch creates 2 variations of material in the background. If I have 4 switches in material, then it will create around 32 variations. I want to know if it’s only true for “Static Switch Parameter” and “Static Bool Parmater” or is it the same for constant static switch and constant static bool?I also want to know if it’s ok to use that many static switches in material function.If there is a better way, then please let me know.
The short answer is that static switches don’t affect runtime performance, however, they can increase the compile time of your shaders and the size in memory and on disk. Also, if you have multiple switches nested into each other, they can cause the compile time to get worse exponentially. All that said, they’re called a static switch for a reason, so it indicates that they are static at runtime and do not add any extra cost.
But often times you don’t need to use a static switch. Parameters are a much better way to get functionality out of your material instances. In your example, adjusting the intensity is simply a matter of making the intensity multiplier a parameter and setting its default value to 1 which means there’s no change in intensity. Then setting it to any other value means you’re changing the intensity. There’s no need for a switch there. Same with your second use case. That’s just a parameter.
The only time you need a switch is if you’re trying to give the ability to switch between two different options such as using a World Position function (like wind effects for example) in a master material or not, or adding the ability to turn on or off extra functionality. If you want the smallest impact on your shader compile times, try not to put more than a few switches (I’d say 4 - 8) in your master materials. If you’re not bothered by waiting a while for shaders to compile, then there’s no real downside to using them targeting modern hardware, unless you’re working on a mobile game.