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

const float DegreesToRadians = 3.14159265358979/180.0;

float clamp01( float value )
{
  return clamp( value, 0.0, 1.0 );
} 

vec4 clamp01_v4( vec4 value )
{
  value.r = clamp01( value.r );
  value.g = clamp01( value.g );
  value.b = clamp01( value.b );
  value.a = clamp01( value.a );

  return value;
}

vec2 clamp01( vec2 value )
{
  return clamp( value, 0.0, 1.0 );
}

vec3 clamp01( vec3 value )
{
  return clamp( value, 0.0, 1.0 );
}

vec4 clamp01( vec4 value )
{
  return clamp( value, 0.0, 1.0 );
}

float normalizeToRange( float value, float min, float max )
{
  return ( value - min ) / ( max - min );
}

float normalizeToRange01( float value, float min, float max )
{
  return clamp01( normalizeToRange( value, min, max ) );
}

float map( float value, float inMin, float inMax, float outMin, float outMax )
{
  return mix( outMin, outMax, normalizeToRange( value, inMin, inMax ) );
}

vec2 map_v2( vec2 value, vec2 inMin, vec2 inMax, vec2 outMin, vec2 outMax )
{
  float x = map( value.x, inMin.x, inMax.x, outMin.x, outMax.x );
  float y = map( value.y, inMin.y, inMax.y, outMin.y, outMax.y ); 

  return vec2( x, y );
}

float mapClamped( float value, float inMin, float inMax, float outMin, float outMax )
{
  return mix( outMin, outMax, normalizeToRange01( value, inMin, inMax ) );
}

float triangle( float value )
{
  float inverted = 1.0 - value;

  return min( value, inverted ) * 2.0;
}

vec2 onCircle( float angle )
{
  return vec2( cos( angle ), sin( angle ) );
}

float normalizeAngle( float degrees )
{
  while ( degrees < 0.0 )
  {
    degrees += 360.0;
  }

  return degrees;
}

float angleDelta( float degreesA, float degreesB )
{
  float angleDelta = degreesB - degreesA;
  angleDelta = mod( angleDelta + 180.0, 360.0 ) - 180.0;
  return angleDelta;
}

float snapRounded( float value, float snappingDistance )
{
  return round( value / snappingDistance ) * snappingDistance;
}

vec3 snapRounded( vec3 value, float snappingDistance )
{
  return round( value / snappingDistance ) * snappingDistance;
}

float snapCeiled( float value, float snappingDistance )
{
  return ceil( value / snappingDistance ) * snappingDistance;
}

float snapFloored( float value, float snappingDistance )
{
  return floor( value / snappingDistance ) * snappingDistance;
}

float rangeAmount( float x, vec3 range)
{
  float a0 = range.x;
  float a1 = range.y;
  float a2 = range.z;

  float range01 = smoothstep( a0, a1, x );
  float range02 = 1.0 - smoothstep( a1, a2, x );

  return mix( range01, range02, step( a1, x) );
}