// #include "res://addons/rokojori_action_library/Runtime/Shading/Library/Noise.gdshaderinc"

#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Math.gdshaderinc"

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

vec2 random_v2( vec2 uv ) 
{
  uv = vec2
  ( 
    dot(uv, vec2( 127.1,311.7 ) ),
    dot(uv, vec2( 269.5,183.3 ) ) 
  );
  
  return -1.0 + 2.0 * fract( sin( uv ) * 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 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 perlinOctaves( vec2 uv, int octaves, float scale, float gain )
{
  float s = 1.0;
  float g = 1.0;

  float v = perlin( uv * s ) * g;
  float n = 1.0;

  for ( int i = 0; i < octaves; i++ )
  {
    s *= scale;
    g *= gain;
     
    v += perlin( uv * s ) * g;
    n += g;
  }

  return v / n;
}

vec2 seamLessCoordinate( vec2 uv, vec2 seamRange, vec2 seamFade, vec2 type )
{
  return mod( uv - seamFade * type, seamRange );
}

float perlinOctavesSeamless( vec2 uv, int octaves, float scale, float gain, vec2 seamRange, vec2 seamFade )
{
  // uv = mod( uv , seamRange );
  seamFade *= seamRange;
  vec2 fading = mod( uv, seamRange ) ;
  fading.x = normalizeToRange01( fading.x, seamRange.x - seamFade.x, seamRange.x );
  fading.y = normalizeToRange01( fading.y, seamRange.y - seamFade.y, seamRange.y );


  // 0, seamRange - seamFade, seamRange
  //

  vec2 p00 = seamLessCoordinate( uv, seamRange, seamFade, vec2( 0.0, 0.0 ) );
  vec2 p10 = seamLessCoordinate( uv, seamRange, seamFade, vec2( 1.0, 0.0 ) );
  vec2 p01 = seamLessCoordinate( uv, seamRange, seamFade, vec2( 0.0, 1.0 ) );
  vec2 p11 = seamLessCoordinate( uv, seamRange, seamFade, vec2( 1.0, 1.0 ) );

  float n00 = perlinOctaves( p00, octaves, scale, gain );
  float n10 = perlinOctaves( p10, octaves, scale, gain );
  float n01 = perlinOctaves( p01, octaves, scale, gain );
  float n11 = perlinOctaves( p11, octaves, scale, gain );

  float n0 = mix( n00, n10, fading.x );
  float n1 = mix( n01, n11, fading.x );

  return mix( n0, n1, fading.y );
}



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;
}

