Search Unity

Bug Issue with how Polar Coordinates node works

Discussion in 'Shader Graph' started by orionsyndrome, Oct 6, 2022.

  1. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    This is intended only as a heads up.

    Unity implements the node as such
    Code (csharp):
    1. float2 delta = UV - Center;
    2. float radius = length(delta) * 2 * RadialScale;
    3. float angle = atan2(delta.x, delta.y) * 1.0/6.28 * LengthScale;
    4. Out = float2(radius, angle);
    If you want to keep your hair where it belongs (if you still have any), it should be implemented like this
    Code (csharp):
    1. float2 delta = UV - Center;
    2. float radius = length(delta) * 2.0 * RadialScale;
    3. float angle = atan2(delta.y, delta.x) * 1.0/6.28318 * LengthScale;
    4. Out = float2(radius, angle);
    Thankfully, it can be solved with Custom Function and pasting the above code.

    The first issue is the incredibly low precision of Tau, which is abysmally low, unless you really "want" the seam to be painfully apparent.

    In my case I wanted a "star burst" effect which I intended to achieve by dividing theta with a fixed angle, then doing frac with shaping, then doing asin(angle/length) to force the lines to remain straight the further they go.

    If I had any hair left, I'd lose all of it, because I really didn't expect the culprit to be a well-known constant treated so badly.

    The second issue is that atan2 is coded to accept x then y, while it's supposed to be the opposite
    https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-atan2
    I know young people are confused about this, but this is due to historical reasons of how atan was typically used (i.e.
    atan(y/x)
    ).

    Atan2(x, y) would output an angle rotated by 90 degrees which is not how atan2 is supposed to work, especially if you're trying to visually sync C# output with shader code.

    I hope this will be fixed, because it's much better not to have this node, than to have it in a state of weird malfunction.

    edit:
    v12.1.7 for the built-in rendering pipeline.
     
    Last edited: Oct 6, 2022
    MatheusDallaRT and sabint like this.
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    I have reported this issue back when I posted this, but then I was asked to recreate the whole project that showcased the issues. I couldn't do that because I had more pressing things to do at the time, and the solution for my project was to simply not use the node.

    Ultimately, after a long and quite ridiculous discussion/battle, only the argument swapping was confirmed as a bug, and as far as I can tell, the PI was left shortened for some reason.

    I'd just like to remind everyone that, while atan2 was used wrongly, the real issue with this node was how poorly PI was represented, and so the actual bug was outright rejected and won't be fixed, and the node remains mostly useless.
     
    MatheusDallaRT likes this.