Search Unity

Resolved Mix voronoi with a texture

Discussion in 'Shader Graph' started by Racines, Dec 3, 2019.

  1. Racines

    Racines

    Joined:
    Jan 20, 2014
    Posts:
    35
    Hi,

    I want to make this kind of effect in shader graph:

    https://github.com/keijiro/KinoVoronoi

    I want that each voronoi cells show the color of an image at the center of the voronoi's cell.
    upload_2019-12-3_12-16-49.png
    I want that the cell in the A node have the color of the B node (uv from cell's center)
    Have you an idea on how to retrieve the center of the voronoi's cell for the entire cell ?

    Thanks
     
  2. Racines

    Racines

    Joined:
    Jan 20, 2014
    Posts:
    35
    I finally found a way:
    1) Right click on Voronoi node and select "Show generated code".
    2) Make a custom function node of this
    3) Found the center formula:
    Code (CSharp):
    1. Center = (offset + lattice + g) / CellDensity;
    upload_2019-12-3_19-2-40.png

    Here the full code of the custom function node:
    Code (CSharp):
    1. float2 g = floor(UV * CellDensity);
    2. float2 f = frac(UV * CellDensity);
    3. float t = 8.0;
    4. float3 res = float3(8.0, 0.0, 0.0);
    5.  
    6. for(int y=-1; y<=1; y++)
    7. {
    8.     for(int x=-1; x<=1; x++)
    9.     {
    10.         float2 lattice = float2(x,y);
    11.  
    12.         float2 sUV = lattice + g;
    13.         float2x2 m = float2x2(15.27, 47.63, 99.41, 89.98);
    14.         sUV = frac(sin(mul(sUV, m)) * 46839.32);
    15.         float2 offset = float2(sin(sUV.y*+AngleOffset)*0.5+0.5, cos(sUV.x*AngleOffset)*0.5+0.5);
    16.         float d = distance(lattice + offset, f);
    17.         float2 sign = (lattice + offset) - f;
    18.  
    19.         if(d < res.x)
    20.         {
    21.             res = float3(d, offset.x, offset.y);
    22.             Out = res.x;
    23.             Cells = res.y;
    24.             Center = (offset + lattice + g) / CellDensity;
    25.         }
    26.     }
    27. }
     
  3. mrFrancois

    mrFrancois

    Joined:
    Sep 4, 2020
    Posts:
    4
    Hello @Racines
    thx for your post it's exactly what I want to test.
    I've got just one pobs, nothings hapen when I select "Show generated code"on my Voronoi Node.
    My project is HDRP with the 2020.1.5f1 unity version
    thx for your reply
     
  4. Racines

    Racines

    Joined:
    Jan 20, 2014
    Posts:
    35
    Hello @mrFrancois
    On Unity 2019.3.13f1 this is working well, opening visual studio and show me the code.

    I made a file of the generated code for you
     

    Attached Files:

  5. mrFrancois

    mrFrancois

    Joined:
    Sep 4, 2020
    Posts:
    4
    Really thank for your kindness
    It doesn't work on my Unity 2019.3.4, but I'm on a macbook, maybe that's the problem ?

    For your file, I can read it on visual studio, but I don't know how I can import it on my project or where ? I try to package it, to drag and drop on my scene ...
     
  6. Racines

    Racines

    Joined:
    Jan 20, 2014
    Posts:
    35
    Maybe I am using Windows.

    What do you want to achieve exactly?
     
  7. mrFrancois

    mrFrancois

    Joined:
    Sep 4, 2020
    Posts:
    4
    Like you, I try to recover the color behind the central point of the vorodoi to apply it to its same cell.
    I'm a designer and I'm playing with Shader ans VFX to creat emotional experiences.
    I uninstalled visual studio code to visual studio for mac. on Package manager the same.
    But it still doesn't work :)
    Thanx for your answers and your time, I will continue to explore the possibilities and keep you posted
     
  8. Racines

    Racines

    Joined:
    Jan 20, 2014
    Posts:
    35
    Ok so if you want to recreate this then you can do what I explained in my original post: https://forum.unity.com/threads/mix-voronoi-with-a-texture.787517/#post-5241860

    Here is a more detailed description of the steps:
    1. Create a node "Custom Function" in shader graph
      1. Click on settings icon
      2. Add inputs
        1. UV - Vector2
        2. AngleOffset - Vector1
        3. CellDensity - Vector1
      3. Add outputs
        1. Out - Vector1
        2. Cells - Vector1
        3. Center - Vector2
      4. Select string in Type dropdown
      5. Add a name in Name section
      6. Past the code below in the Body section
    2. In shadergraph use the Center ouput to retrieve the cells UV
    Code (CSharp):
    1. float2 g = floor(UV * CellDensity);
    2. float2 f = frac(UV * CellDensity);
    3. float t = 8.0;
    4. float3 res = float3(8.0, 0.0, 0.0);
    5. for(int y=-1; y<=1; y++)
    6. {
    7.     for(int x=-1; x<=1; x++)
    8.     {
    9.         float2 lattice = float2(x,y);
    10.         float2 sUV = lattice + g;
    11.         float2x2 m = float2x2(15.27, 47.63, 99.41, 89.98);
    12.         sUV = frac(sin(mul(sUV, m)) * 46839.32);
    13.         float2 offset = float2(sin(sUV.y*+AngleOffset)*0.5+0.5, cos(sUV.x*AngleOffset)*0.5+0.5);
    14.         float d = distance(lattice + offset, f);
    15.         float2 sign = (lattice + offset) - f;
    16.         if(d < res.x)
    17.         {
    18.             res = float3(d, offset.x, offset.y);
    19.             Out = res.x;
    20.             Cells = res.y;
    21.             Center = (offset + lattice + g) / CellDensity;
    22.         }
    23.     }
    24. }
     
  9. mrFrancois

    mrFrancois

    Joined:
    Sep 4, 2020
    Posts:
    4
    MAN ! Girl ?
    YOU SAVE MY DAY !!!!
    I owe you champagne :)
    Really thanks a lot for your time and your answers
     
  10. Racines

    Racines

    Joined:
    Jan 20, 2014
    Posts:
    35
    You are welcome
     
  11. m4tar

    m4tar

    Joined:
    Nov 2, 2023
    Posts:
    1
    Very nice.