how does if-node branching work with materialAttributes?

I’m trying to create a LOD-able terrain shader which uses dynamic branches to swap in cheaper rendering for distant pixels. However I’m seeing unpredictable behavior with what seems like simple test code.

My un-lodded shader uses a LayerBlend node to combine several different map combinations – I’m using materialAttributes primarily to avoid a ton of redundant layer blend setup. Without the lod switch it works fine.

However when I tried testing by using a an if-node to swap in an alternate MaterialAttribute set (made with MakeMaterialAttributes) neither branch of the if renders correctly. However applying multiple copies of the same switch setup to individual material channels seems to produce the effect I’m after. This makes me think that the if node does not work properly with materialAttribute connections.

Is that the intended behavior? And if so, do the multiple redundant switches affect performance much?

This may or may not be related: shader complexity view does not reflect the difference between the complex foreground shader and the cheaper distant shader. Is that a known limitation?

edit:

Working through this I also found that (like other posts on branching) that the if node executes both branches, so it doesn’t help perf. I am able to get branching using a custom node with the [branch] attribute as is mentioned in this thread but there’s no obvious way to pipe materialAttributes through a custom node so the question still interests me

upvoted both.

IF node does not work as you would expect it to. It is implemented as ternary operator. Thus, all branching you attempt to do in material networks will be flattened.

To get dynamic branching working, you gotta use custom node, but as you’ve mentioned, you can’t use materialAttributes.

This is a significant issue, so please, vote for this and this, and feel free to post in the thread you’ve linked. For some reason this shortcoming is overlooked by most of the community.

UE-33876 is now the tracking number for this issue.