Skip to content

Instantly share code, notes, and snippets.

@rodrigoueda
Last active November 15, 2023 11:37
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigoueda/934dd869779e12a5d90fe2be926c5285 to your computer and use it in GitHub Desktop.
Save rodrigoueda/934dd869779e12a5d90fe2be926c5285 to your computer and use it in GitHub Desktop.
Unity Shader Graph custom node for precise voronoi borders
$precision2 n = floor(UV * CellDensity);
$precision2 f = frac(UV * CellDensity);
// first pass: regular voronoi
$precision2 mg, mr;
$precision md = 8.0;
for (int j= -1; j <= 1; j++) {
for (int i= -1; i <= 1; i++) {
$precision2 g = $precision2(i,j);
$precision2x2 m = $precision2x2(15.27, 47.63, 99.41, 89.98);
$precision2 uvr = frac(sin(mul(g+n, m)) * 46839.32);
$precision2 o = $precision2(sin(uvr.y*+AngleOffset)*0.5+0.5, cos(uvr.x*AngleOffset)*0.5+0.5);
$precision2 r = g + o - f;
$precision d = dot(r,r);
if( d<md ) {
md = d;
mr = r;
mg = g;
}
}
}
// second pass: distance to borders
md = 8.0;
for (int j= -2; j <= 2; j++) {
for (int i= -2; i <= 2; i++) {
$precision2 g = mg + $precision2(i,j);
$precision2x2 m = $precision2x2(15.27, 47.63, 99.41, 89.98);
$precision2 uvr = frac(sin(mul(g+n, m)) * 46839.32);
$precision2 o = $precision2(sin(uvr.y*+AngleOffset)*0.5+0.5, cos(uvr.x*AngleOffset)*0.5+0.5);
$precision2 r = g + o - f;
if ( dot(mr-r,mr-r)>0.0001 ) {
md = min(md, dot( 0.5*(mr+r), normalize(r-mr) ));
}
}
}
Out = $precision3(md, mr.x, mr.y);
@plasticarm
Copy link

This is great! I was looking for a way to do something similar. I wanted to create UVs that I could use to place textures accurately in each cell so I could move textures along with the voronoi noise. This looks really close. How could you modify this to get a nice clean UV map for each cell? This looks more like you are generating this for normals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment