float random( vec2 uv ) 
{
  return fract( sin( dot( uv.xy, vec2( 12.9898, 78.233 ) ) ) * 43758.5453123 );
}

vec2 random_v2( vec2 uv ) 
{
	return vec2( fract( sin( dot( uv.xy, vec2( 12.9898,78.233 ) ) ) * 43758.5453123 ) );
}

vec3 random_v3( vec3 uvw )
{
	
  uvw = vec3( dot(uvw, vec3(127.1,311.7, 513.7) ),
              dot(uvw, vec3(269.5,183.3, 396.5) ),
        dot(uvw, vec3(421.3,314.1, 119.7) ) );
    
  return -1.0 + 2.0 * fract(sin(uvw) * 43758.5453123);
}

float perlin( vec2 uv ) 
{
  vec2 uv_index = floor(uv);
  vec2 uv_fract = fract(uv);

  vec2 blur = smoothstep(0.0, 1.0, uv_fract);

  return mix( mix( dot( random_v2(uv_index + vec2(0.0,0.0) ), uv_fract - vec2(0.0,0.0) ),
                    dot( random_v2(uv_index + vec2(1.0,0.0) ), uv_fract - vec2(1.0,0.0) ), blur.x),
              mix( dot( random_v2(uv_index + vec2(0.0,1.0) ), uv_fract - vec2(0.0,1.0) ),
                    dot( random_v2(uv_index + vec2(1.0,1.0) ), uv_fract - vec2(1.0,1.0) ), blur.x), blur.y) + 0.5;
}

float perlin3D( vec3 uvw )
{	
	vec3 gridIndex = floor( uvw ); 
	vec3 gridFract = fract( uvw );
	
	vec3 blur = smoothstep( 0.0, 1.0, gridFract );
	
	vec3 blb = gridIndex + vec3(0.0, 0.0, 0.0);
	vec3 brb = gridIndex + vec3(1.0, 0.0, 0.0);
	vec3 tlb = gridIndex + vec3(0.0, 1.0, 0.0);
	vec3 trb = gridIndex + vec3(1.0, 1.0, 0.0);
	vec3 blf = gridIndex + vec3(0.0, 0.0, 1.0);
	vec3 brf = gridIndex + vec3(1.0, 0.0, 1.0);
	vec3 tlf = gridIndex + vec3(0.0, 1.0, 1.0);
	vec3 trf = gridIndex + vec3(1.0, 1.0, 1.0);
	
	vec3 gradBLB = random_v3( blb ); 
	vec3 gradBRB = random_v3( brb );
	vec3 gradTLB = random_v3( tlb );
	vec3 gradTRB = random_v3( trb );
	vec3 gradBLF = random_v3( blf );
	vec3 gradBRF = random_v3( brf );
	vec3 gradTLF = random_v3( tlf );
	vec3 gradTRF = random_v3( trf );
	
	
	vec3 distToPixelFromBLB = gridFract - vec3( 0.0, 0.0, 0.0 );
	vec3 distToPixelFromBRB = gridFract - vec3( 1.0, 0.0, 0.0 );
	vec3 distToPixelFromTLB = gridFract - vec3( 0.0, 1.0, 0.0 );
	vec3 distToPixelFromTRB = gridFract - vec3( 1.0, 1.0, 0.0 );
	vec3 distToPixelFromBLF = gridFract - vec3( 0.0, 0.0, 1.0 );
	vec3 distToPixelFromBRF = gridFract - vec3( 1.0, 0.0, 1.0 );
	vec3 distToPixelFromTLF = gridFract - vec3( 0.0, 1.0, 1.0 );
	vec3 distToPixelFromTRF = gridFract - vec3( 1.0, 1.0, 1.0 );
	
	float dotBLB = dot( gradBLB, distToPixelFromBLB );
	float dotBRB = dot( gradBRB, distToPixelFromBRB );
	float dotTLB = dot( gradTLB, distToPixelFromTLB );
	float dotTRB = dot( gradTRB, distToPixelFromTRB );
	float dotBLF = dot( gradBLF, distToPixelFromBLF );
	float dotBRF = dot( gradBRF, distToPixelFromBRF );
	float dotTLF = dot( gradTLF, distToPixelFromTLF );
	float dotTRF = dot( gradTRF, distToPixelFromTRF );
	
	
	return mix(
		mix( mix(dotBLB, dotBRB, blur.x),	mix(dotTLB, dotTRB, blur.x), blur.y	),
		mix( mix(dotBLF, dotBRF, blur.x),	mix(dotTLF, dotTRF, blur.x), blur.y ), 
    blur.z
	) + 0.5;
}


float perlinPolar( vec2 uv )
{
  return perlin( uv ) * 2.0 - 1.0;
}


float worley( vec2 uv, float columns, float rows ) 
{
	
	vec2 index_uv = floor( vec2( uv.x * columns, uv.y * rows ) );
	vec2 fract_uv = fract( vec2( uv.x * columns, uv.y * rows ) );
	
	float minimum_dist = 1.0;  
	
	for ( int y= -1; y <= 1; y++ ) 
  {
		for ( int x= -1; x <= 1; x++ ) 
    {
			vec2 neighbor = vec2( float( x ), float( y ) );
			vec2 point = random_v2( index_uv + neighbor );
			
			vec2 diff = neighbor + point - fract_uv;
			float dist = length (diff );

			minimum_dist = min( minimum_dist, dist );
		}
	}
	
	return minimum_dist;
}

vec2 voronoi( vec2 uv, float columns, float rows ) 
{	
	vec2 index_uv = floor( vec2( uv.x * columns, uv.y * rows ) );
	vec2 fract_uv = fract( vec2( uv.x * columns, uv.y * rows ) );
	
	float minimum_dist = 1.0;  
	vec2 minimum_point;

	for ( int y= -1; y <= 1; y++ ) 
  {
		for ( int x= -1; x <= 1; x++ ) 
    {
			vec2 neighbor = vec2( float( x ), float( y ) );
			vec2 point = random_v2( index_uv + neighbor );

			vec2 diff = neighbor + point - fract_uv;
			float dist = length( diff );
  
			if ( dist < minimum_dist )
      {
				minimum_dist = dist;
				minimum_point = point;
			}
		}
	}


	return minimum_point;
}

