Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug 3/3 != 1 in ShaderGraph

Discussion in 'Shader Graph' started by seojihyuk, Jul 30, 2021.

  1. seojihyuk

    seojihyuk

    Joined:
    Sep 19, 2014
    Posts:
    30
    Hello, guys
    I think I found bug in ShaderGraph.
    I was using FlipBook node by width 3 and height 3.
    When I set TileNumber 3, it didn't show me 4th tile, but 1st tile.
    So I was wonder why, And after few hours of wasting my time, I found this.
    ShaderGraphError.png
    I can understand why this happens, it's 3/3=0.99999999... and caused by floating point accuracy problem.
    But it's still anooying, and I solve my problem by own custom flipbook.
    Before unity fix this issue, maybe someone need this solution like me, so I post my custom function on here.
    Caution : It's for horizontal FlipBook not vertical, cause I only needed this. But I think you can change a littlebit to use both cases.

    Code (CSharp):
    1. Tile = fmod(Tile, Width * Height);
    2. if(distance(frac(Tile/(Width * Height)),1)<0.000001){
    3. Tile = 0.0;
    4. }
    5. float2 tileCount = float2(1.0, 1.0) / float2(Width, Height);
    6. float tileX =  fmod(Tile, Width);
    7. if(distance(frac(Tile*tileCount.x),1)<0.000001){
    8. tileX =  0.0;
    9. }
    10. float tileY = Height - 1.0 - (Tile-tileX)/Width;
    11. Out = (UV + float2(tileX, tileY)) * tileCount;
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,753
    This not just shader.
    This is general floating point matter.
    Generally same aplies in C#, unless dividing int by int.
     
  3. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    I don't think that dividing by the same number gives rounding problems in c#, I will be surprised if this was the case in shaders.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,753
    If they are ints type, then not.
     
  5. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    In c#
    1.2 / 1.2 = 1
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,753
    Try 1.2f / 1.2f, none constants.
     
  7. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    Code below try on https://dotnetfiddle.net/
    It returns exact number.
    Code (csharp):
    1.  
    2. using System;
    3.                    
    4. public class Program
    5. {
    6.  
    7.    public static void Main()
    8.    {
    9.        float  a = 2.2222222f;
    10.        float  b = 1.1111111f;
    11.  
    12.        Console.WriteLine((float)Div(a,b)); //returns 2
    13.        Console.WriteLine(Div(a,b)); //returns 2
    14.        
    15.        
    16.        float  a2 = 3f;
    17.        float  b2 = 3f;
    18.  
    19.        Console.WriteLine((float)Div(a2,b2)); //returns 1
    20.        Console.WriteLine(Div(a2,b2)); //returns 1
    21.  
    22.    }
    23.    
    24.    public static float Div(float a,float div){
    25.        return a/div;
    26.    }
    27.        
    28. }
    29.