Dynamic Switch Param in material editor

Currently there is only static Switch Param

A Dynamic Switch Param so that it can be modified in blueprints, That would be great!

Do you mean that engine would generate both variants statically and when setting that parameter engine would swap material behind the curtains?
This can be also done quite easily with blueprints.
If you mean that switch is not statically compiled then just use If node with scalar parameter and create Material instanced dynamic.

1 Like

I would like to be able to change the texture of the mask using the blueprints, an authentic dinamic switch.

This example is very simple but more complex things, would save having to change all the parameters one by one.

You can just lerp between them with a zero or a one for that.

1 Like

^ that.

noticed the image and wanted to share two tutorials to optimize the material setup a bit:[UE4] How to merge three grayscale images into the RG and B channel of a texture. - YouTube

Thanks all for your feedbacks,

actually i change the scalar parameter, but I think that in many occasions a simple dinamic switch param that could be changed from a instanced material would save time.

@lous
You can see in the image that I merge the metal, oclussion and roughness in a only texture but for the mask I use only two greayscale textures, i think this is better that a rgb texture with a empty channel.

There is no benefit whatsoever to have that over a lerp. I could be wrong when it comes to the GPU (someone correct me if I am) but both a bool and a float are 32-bits and you still have to evaluate both branches to properly select the result. Having a lerp with a float that you set to 0 or 1 is identical to having a dynamic branch with just one “Select Float” BP node more.

1 Like

I could be a paranoid framerater and be completely wrong, but probably lerp function calculates both branches and then does the lerp inlcuso if the alpha is 0 or 1.
While a switch, which in the background is an if, only calculates a branch, if you have complex branches it can be a problem using lerp.

If and Lerp Node does always both branches. Static switch removes dead branch completely.

The switch is like a preprocessor directive, it’s evaluated at shader compile time so that the resulting shader has the least instructions and is therefore simpler and faster.

A dynamic switch would be useless, as both branches would be compiled into the resulting shader and still have to be evaluated - so you save nothing (and existing shaders would go up in instruction counts).

If you need to change at runtime, you have to use a Lerp node. There are several blend material functions if you want to drive multiple properties at once.

Mhm, I’d like to use this thread to ask some question :wink:

I heard that using If branching in shaders was quite expensive on GPU because its architecture, but the newer GPUs like GCN they don’t have this problem. Is that true?

Branching still is somewhat expensive, but a threshold, where it becomes justifiable, is indeed much lower on modern GPUs.

Do note however, that IF material expression in UE4 does not produce dynamic branch.

Dynamic branching in shaders is quite a large subject and doing dynamic branching in nodes is quite difficult architecture wise (what if both branches share parts of the network and / or cross-depend on each other?). As Deathrey mentioned too, the way GPUs are designed actually makes dynamic branching a loss of performance in many cases. It has to do with the fact that (simplifying a bit) GPU processors are separated into small clusters and if one processor in a cluster does branch A (long) and others do branch B (short) the others actually have to wait for A to finish so they can stay in sync. The GPU is designed with parallelism in mind and once you start giving different bits different tasks based on dynamic parameters things quickly go wrong.

I guess I’m a bit late but here is the answer.

1 Like

I’d still use a LERP over the clamp. Same result if you multiply by 0 that channel becomes dead plus gives you the option to fold it in better (confim someone?) as well as over-saturate if that’s what you want to do; LERP offers more flexibility vs any other node I can think of… I’d write entire materials out of LERP if I could :smiley:

Dynamic switch param does make alot of sense. Comparing this to another engine - Unity where this feature does exist.
Dynamic switches are something like shader_feature. Difference between Lerp and dynamic switch are:

  • lerp takes some small GPU time to process which value is used.
  • Dynamic switch essentially makes the engine create 2 versions of that shader with A and B versions (call them variants) and swapping the bool on that switch will replace the current shader variant A with the other variant B and vice-versa. This approach takes slightly more memory, slightly increases build time and build size, but does take no GPU overhead.

Now which is better is debatable as I do not have the exact numbers. But that is the reason why so many people would like a dynamic switch :slight_smile:

I take it as this feature does not exist I guess

I feel so stupid for not realizing I could use a lerp. lol Thank you :slight_smile: