Voronoi

 avatar
unknown
glsl
a month ago
812 B
4
Indexable
float voronoiNoise(float2 uv, float offset)
{
	float2x2 m = float2x2(15.27, 47.63, 99.41, 89.98);
	uv = frac(sin(mul(uv, m)) * 46839.32);
	return float2(sin(uv.y * offset) * 0.5 + 0.5, cos(uv.x * offset) * 0.5 + 0.5);
}

float2 voronoi2(float2 uv, float angleOffset, float cellDensity)
{
	float2 g = floor(uv * cellDensity);
	float2 f = frac(uv * cellDensity);
	
	float3 res = float3(8.0, 8.0, 8.0);

	for(int y = -1; y <= 1; y++)
	for(int x = -1; x <= 1; x++)
	{
		float2 lattice = float2(x, y);
		float2 offset = voronoiNoise(g + lattice, angleOffset);
		float2 v = lattice + offset - f;
		float d = dot(v, v);

		if(d < res.x)
		{
			res.y = res.x;
			res.x = d;
			res.z = offset.x;
		} else if(d < res.y) {
			res.y = d;
		}
	}

	return float2(sqrt(res.x), sqrt(res.y));
}
Editor is loading...
Leave a Comment