Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

voronoi lines

Discussion in 'Shaders' started by AnKOu, Feb 16, 2020.

  1. AnKOu

    AnKOu

    Joined:
    Aug 30, 2013
    Posts:
    123
    Hi,

    I'm creating a voronoi noise shader. I am to the point wher i'd like to display lines between cells.
    I've found a tutorial http://www.iquilezles.org/www/articles/voronoilines/voronoilines.htm

    I've got a problem. I want to animate the cells so that they move with time.
    Also, I wan't to be able to draw a grid instead of voronoi cell.
    For each of those cases, I can do it easily by controlling the random point inside each cell.
    random position between [0,1] for animation,
    [0.5,0.5] position for grid.

    The problem is that as soon as I modify the point position outside certain values, lines are completely in mess.
    I just don't get it.

    Example :


    Value is 1. No weird stuff


    Value is 0.999999, weird stuff (note how everything is exactly the same expected the new lines)

    Here is the code that generate the random point :

    Code (CSharp):
    1.  
    2.  
    3.             float2 getDirectionToCurrentPoint(int2 localCellIndex, int2 worldCellIndex, float2 relativePos)
    4.             {
    5.                 float2 cellLocalCenter = float2(0.5,0.5);
    6.  
    7.                 // Random position in the cell
    8.                 int2 coord = worldCellIndex + localCellIndex;
    9.                 float2 seed = float(_Seed) * float2(coord);
    10.                 float2 fractionalCoord = random2(seed);
    11.  
    12.                 //animation
    13.                 float2 animatedCoord =  saturate(cellLocalCenter + ( float(0.5) * sin( fractionalCoord * _Time.y * _AnimationSpeed)) * _AnimationStrength);
    14.                 float2 localCoord = lerp(fractionalCoord, animatedCoord, saturate(_AnimationAmount));
    15.              
    16.                 //voronoi amount
    17.                 float2 voronoiAmount = lerp(cellLocalCenter , localCoord, saturate(_VoronoiAmount));
    18.  
    19.                 //direction from current position to randomly generated point
    20.                 float2 dirToCurrentPoint = (float2(localCellIndex) + voronoiAmount) - relativePos;
    21.                 return dirToCurrentPoint;
    22.             }
    And the code that compute the distance to the closest point to draw the lines :
    Code (CSharp):
    1. // Scale
    2.                 float2 st = i.uv * _Scale;
    3.  
    4.                 // Tile the space
    5.                 int2 worldCellIndex = floor(st);
    6.                 float2 relativePos = frac(st);
    7.  
    8.                 int2 closestLocalCellIndex;
    9.                 int2 closestWorldCellIndex;
    10.                 float2 dirToclosestPoint;
    11.  
    12.                 float m_dist = -1;
    13.                 [unroll]for (int y= -1; y <= 1; y++)
    14.                 {
    15.                     [unroll]for (int x= -1; x <= 1; x++)
    16.                     {
    17.                         // neighbor place in the grid
    18.                         int2 localCellIndex = int2(x,y);
    19.                         float2 dirToCurrentPoint = getDirectionToCurrentPoint(localCellIndex, worldCellIndex, relativePos);
    20.                         float sqrDist = dot(dirToCurrentPoint, dirToCurrentPoint);
    21.  
    22.                         if( sqrDist < m_dist || m_dist == -1.)
    23.                         {
    24.                             m_dist = sqrDist;
    25.                             dirToclosestPoint = dirToCurrentPoint;
    26.                             closestLocalCellIndex = localCellIndex;
    27.                             closestWorldCellIndex = worldCellIndex + localCellIndex;
    28.                         }
    29.                     }
    30.                 }
    31.              
    32.                 m_dist = -1;
    33.                 [unroll]for( int j =- 2; j <= 2; j++ )
    34.                 {
    35.                     [unroll]for( int i =- 2; i <= 2; i++ )
    36.                     {
    37.                         int2 localCellIndex = closestLocalCellIndex + int2(i,j);
    38.                         float2 dirCurrentPoint = getDirectionToCurrentPoint(localCellIndex, worldCellIndex, relativePos);
    39.                         float d = sqrt( dot( dirToclosestPoint + dirCurrentPoint, normalize(dirCurrentPoint - dirToclosestPoint)));
    40.                      
    41.                         if( d < m_dist || m_dist == -1.)
    42.                         {
    43.                             m_dist = d;
    44.                         }
    45.                     }
    46.                 }
    47. //...do stuff with m_dist
    48.  
    I don't know in which direction to investigate so I ask here.
     
  2. AnKOu

    AnKOu

    Joined:
    Aug 30, 2013
    Posts:
    123
    ok, after hours investigating, I finaly post here.
    And 5 minutes later

    I tried this :


    Code (CSharp):
    1.                //float2 voronoiAmount = lerp(cellLocalCenter , localCoord, saturate(_VoronoiAmount));
    2.                float2 voronoiAmount =  (1. - _VoronoiAmount) * cellLocalCenter + _VoronoiAmount * localCoord;
    and it works.

    But why !?

    How the lerp works internaly to affect my code this way !?