Weird color conflict on if else branch

Hello,

I have a shader graph that display a texture.
It takes an input (x) that can be 0 , 1 , 2 , 3.

I made an If/else branch so :
If X = 0 , display the texture alone
If X = 1 , don’t use the texture, use COLOR BLACK
If X = 2 , don’t use the texture, use COLOR GREEN
If X = 3 , don’t use the texture, use COLOR RED

It’s working but there is weird black sprites that is the default “false” statement for both (if x=2) and (if x=3).

What am I missing ?


Creating braches for this use case is bad idea, but checking float equality is terrible idea.
Writing shaders is a bit different than writing normal C# code, use math instead of branches.
You might be intested in pinned post Branching in Shaders

Your problem is about precision, you “randomly” end up with values like 0.9999 what is clearly not equal to 1.
I don’t know what you try to do, I bet you are overcomplicating things, but if you really needed to do it this way, then at least check greater or equal with some additional treshold.

1 Like

My mesh is generated by script and I change material on it for every vertices. So I can’t control material data.
I must store data using “Mesh.SetUvs(int channel, Vector3 vec)” to change vertices indepentaly from others.

I change the logic, now my values will not be subhect to point floating point error. And it’s working as expected :

Now X can be {0 , 10 , 20 , 30}
And my logic check if :
X < 5
X > 15
X > 25

8248527--1079214--image_2022-07-02_111233144.png