I was wondering if anyone had any advice using shader graph to flip between more than 2 states. Like in a Branch node.
Basically, I have a default material state, and then 4 different potential things that can happen to it. I’d like to just feed some node an int/float that is 0, 1, 2, 3, and 4 by code. To switch between them. Currently, I have 2 modes, and am just using a bool + branch node, and feeding it 0 or 1 by code.
I’m about to start figuring out a complicated chain of branches and bools to achieve all the options, but thought I’d ask here first…
If you’re going to use branches to create distinct shader variants and need more than two states, you might look at using an enum (Keyword > Enum) which will at least simplify the chain of bools down to a single node. There’s some shader variant and batching implications mixed in with this process you might look into for more context on how to do your branching.
You could also use the Compare node to create a chain of A:B comparisons using a number rather than a bool input. That would let you continue using a float as an input, but would use the same extended chain of nodes. Unsure whether that node creates true branching in the shader. Check all of the logic nodes while you’re exploring.
Enums create shader variants, increasing build time and bigger shaders in build. If you don’t want to create new variants, an easy trick I use is this: I use an int which goes from 0 to 3 and a vector4 which is set by script (I used custom shader GUIs for my shaders) which is set like this:
If float = 0 vec = 1,0,0,0
If float = 1 vec = 0,1,0,0
…
In shader I multiply each option with one channel of the vector and then add all results together:
But I only use this for selecting vertex color or other cheap math, if you want to switch between textures for instance, using a keyword enum is still a better idea. And this approch only works for 4 options.