I was having a lot of issues with the spheregradient2d material function breaking lerping and figured out the culprit. The image shows how to recreate the issue. The simple solution is to replace the square root node with a power node that has a power of 0.5 because a square root is just another term for something raised to the 1/2 power.
Notice the bottom result and how it’s all masked in black, instead of red:
A bit of continuation on this topic: I’ve found that even using the power^0.5 node, it still bugs out for certain values of the scalar. At 0.2, it works fine, 0.3 doesn’t work, 0.31/32/33/34 work, but then 0.35/36 doesn’t work, 0.37 works, and so on.
I don’t know how UE handles roots, but is it possible that this could be some kind of lookup table error?
This is NOT a bug, just a misuse of material expressions.
Black area in resulting lerp corresponds to the area, where extracting square root from a negative number is attempted, which results in NaN.
There are no issues with power node either.
It is just the fact, that the area, you are expecting to give 0 input to ceil is not necessary 0-ish enough.
It’s definitely a floating point accuracy issue. It’s not the expression because it’s just a direct rip of the spheregradient2d material function. It’s breaking at the point where the two squares are subtracted from each other and are supposed to be equal to zero(when their values are the same and that would coincide with the red area that’s getting messed up). The FP precision is dipping below the zero threshold and that’s causing the number to go negative, which is causing the square root to go NaN.
The bandaid fix is to use an IF node right after the difference of the squares and it fixes it all. It’s very rare that I need to work with square roots in materials, so I guess I know what to keep in mind for the future.
EDIT: I propose that in cases of “NaN,” the engine should default them to zero for things like a lerp node. A simple “If the input is NaN, then the input equals zero” would suffice. Also, it’s using an IF, instead of a clamp, because the clamp still doesn’t properly clip it to zero. The only way it will properly reach zero is with the IF setup.