Can "if"-node material expression be used to selectively execute nodes

I’m trying to reduce shader instructions until a material has received at least one “hit” (Doing some texture splatting like operation) and using an If-node, but it doesn’t seem to branch as I would expect. Atleast the shader complexity view shows an unchanged complexity of the pixel shader when using the if-node (with the expression below, they should all simply pass Zero as the output instead of doing my computations I put into “A > B”.

Is this expected or is the material actually branching but not showing up like that in the shader complexity view?

  • Tom

That’s to create entirely different material permutations, you can’t set a bool param in code to dynamically switch inside the material (that’s why it’s called static switch parameter AFAIK)

I simply want the nodes I put inside “A > B” to only run if the comparison of A and B is indeed A greater than B. And otherwise only run the Zero I placed in A == B or A < B.

Do this means I cannot have my
“spheremask” logic using regular nodes
and plug it in, but instead need to
code all that inside of the custom
node too?
Yep. Exactly. In any case, the code to be branched needs to be inside { } section of the IF operator.
On a side, dynamic branch is justified arguably only if you are branching something heavy, like a hundred of instructions or several texture lookups. Branching logic for a single sphere mask is simple enough to be most likely a net performance loss for material with low overdraw.

been following this thread as well as the forum one. would be really handy to have an actual if-node that does all this things properly in the future :slight_smile:

Have you also tried using a static switch parameter, doing the “if” outside the material and passing in the Boolean result?

If material node is translated to ternary HLSL operator. It is unable of producing true branch. You need to do that in custom node with an if statement with [branch] attribute preferably. Do note, that simply plugging in parts of material graph into custom node with an if statement is not enough and you will have to put whole branch inside the custom node. Details in this thread. Shader complexity view will not show you if it is branching properly or not.

Thanks! That’s what I wanted to know, whether the shader complexity view would allow me to verify this. Too bad it does not.

Do this means I cannot have my “spheremask” logic using regular nodes and plug it in, but instead need to code all that inside of the custom node too?

Thanks for the info! This was most helpful (and disappointing haha)

Not sure if question or answer, but in some cases I just exchanged the whole material. Might be a pain to maintain in some cases, but I don’t see any other problems with that.

That was kind of my fall-back. I can place a static switch param to make two material variations, one that doesn’t support “splats” and one that does. It’s some extra work to set up, but if all else fails and I need the perf, than it’s worth the effort. Right now the material isn’t expensive enough to go this route I think so I am keeping it simple as it’s easier to develop with.

If anyone stumbles across this, try using “BlendedMaterialAtrributes” node. It worked for me.