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

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

vec3 applyMatrix( vec3 v, mat4 m )
{
  return ( m * vec4( v, 1.0 ) ).xyz;
}

vec3 applyMatrixWithoutTranslation( vec3 v, mat4 m )
{
  return ( m * vec4( v, 0.0 ) ).xyz;
}

vec3 applyMatrix( vec3 v, mat3 m )
{
  return ( m * v ).xyz;
}


vec3 localToWorld( vec3 local, mat4 _MODEL_MATRIX )
{
  return applyMatrix( local, _MODEL_MATRIX );
}

vec3 localToWorldDirection( vec3 local, mat4 _MODEL_MATRIX )
{
  return applyMatrixWithoutTranslation( local, _MODEL_MATRIX );
}

vec3 localToView( vec3 local, mat4 _MODELVIEW_MATRIX )
{
  return applyMatrix( local, _MODELVIEW_MATRIX );
}

vec3 localToViewDirection( vec3 local, mat4 _MODELVIEW_MATRIX )
{
  return applyMatrixWithoutTranslation( local, _MODELVIEW_MATRIX );
}

vec3 localToViewDirection( vec3 local, mat3 _MODELVIEW_NORMAL_MATRIX )
{
  return applyMatrix( local, _MODELVIEW_NORMAL_MATRIX );
}

vec3 worldToLocal( vec3 world, mat4 _MODEL_MATRIX )
{
  mat4 inversedMatrix = inverse( _MODEL_MATRIX );
  return applyMatrix( world, inversedMatrix );
}

vec3 worldToLocalDirection( vec3 world, mat4 _MODEL_MATRIX )
{
  mat4 inversedMatrix = inverse( _MODEL_MATRIX );
  return applyMatrixWithoutTranslation( world, inversedMatrix );
}


vec3 worldToView( vec3 world, mat4 _VIEW_MATRIX )
{  
  return applyMatrix( world, _VIEW_MATRIX );
}

vec3 worldToViewDirection( vec3 worldDirection, mat4 _VIEW_MATRIX )
{
  return applyMatrixWithoutTranslation( worldDirection, _VIEW_MATRIX );
}

vec3 viewToWorld( vec3 view, mat4 _INV_VIEW_MATRIX )
{
  return applyMatrix( view, _INV_VIEW_MATRIX );
}

vec3 viewToWorldDirection( vec3 viewDirection, mat4 _INV_VIEW_MATRIX )
{
  return applyMatrixWithoutTranslation( viewDirection, _INV_VIEW_MATRIX );
}

vec3 viewToLocal( vec3 view, mat4 _INV_VIEW_MATRIX, mat4 _MODEL_MATRIX )
{
  vec3 world = viewToWorld( view, _INV_VIEW_MATRIX );
  return worldToLocal( world, _MODEL_MATRIX );
}

vec3 viewToLocalDirection( vec3 view, mat4 _INV_VIEW_MATRIX, mat4 _MODEL_MATRIX )
{
  vec3 world = viewToWorldDirection( view, _INV_VIEW_MATRIX );
  return worldToLocalDirection( world, _MODEL_MATRIX );
}

vec4 viewToClip( vec3 view, mat4 _PROJECTION_MATRIX )
{
  vec4 clip = _PROJECTION_MATRIX * vec4( view, 1.0 ); 
  clip /= clip.w;
  return clip;
}

vec2 clipToScreen( vec4 clip )
{
  return ( clip.xy / clip.w ) * 0.5 + vec2( 0.5 );
}

vec4 screenToClip( vec2 screen, float z )
{
  return vec4( screen * 2.0 - 1.0, z, 1.0 );
}

vec4 clipToView( vec4 clip, mat4 _INV_PROJECTION_MATRIX )
{
  vec4 view = _INV_PROJECTION_MATRIX * clip;
  view /= view.w;
  return view;
}

vec4 screenToView( vec2 screen, float z, mat4 _INV_PROJECTION_MATRIX )
{
  vec4 clip = screenToClip( screen, z );
  vec4 view = clipToView( clip, _INV_PROJECTION_MATRIX );

  return view;
}

vec3 screenToWorld( vec2 screen, float z, mat4 _INV_PROJECTION_MATRIX, mat4 _INV_VIEW_MATRIX  )
{
  vec4 view = screenToView( screen, z, _INV_PROJECTION_MATRIX );

  return viewToWorld( view.xyz, _INV_VIEW_MATRIX );
}

vec2 viewToScreen( vec3 view, mat4 _PROJECTION_MATRIX )
{
  vec4 clip = viewToClip( view, _PROJECTION_MATRIX );

  return clipToScreen( clip );
}


vec2 worldToScreen( vec3 world, mat4 _VIEW_MATRIX, mat4 _PROJECTION_MATRIX )
{
  vec3 view   = worldToView( world, _VIEW_MATRIX );
  vec4 clip   = viewToClip( view, _PROJECTION_MATRIX );
  vec2 screen = clipToScreen( clip );

  return screen;
}


vec3 extractTranslation( mat4 matrix )
{
  return vec3( matrix[ 3 ][ 0 ], matrix[ 3 ][ 1 ], matrix[ 3 ][ 2 ] );
}

vec3 extractScale( mat3 matrix )
{
  mat3 m = matrix;
  
  float x = length( vec3( m[ 0 ][ 0 ], m[ 1 ][ 0 ], m[ 2 ][ 0 ] ) ); 
  float y = length( vec3( m[ 0 ][ 1 ], m[ 1 ][ 1 ], m[ 2 ][ 1 ] ) ); 
  float z = length( vec3( m[ 0 ][ 2 ], m[ 1 ][ 2 ], m[ 2 ][ 2 ] ) ); 

  return vec3( x, y, z );
}

vec3 extractScale( mat4 matrix )
{
  mat4 m = matrix;
  
  float x = length( vec3( m[ 0 ][ 0 ], m[ 1 ][ 0 ], m[ 2 ][ 0 ] ) ); 
  float y = length( vec3( m[ 0 ][ 1 ], m[ 1 ][ 1 ], m[ 2 ][ 1 ] ) ); 
  float z = length( vec3( m[ 0 ][ 2 ], m[ 1 ][ 2 ], m[ 2 ][ 2 ] ) ); 

  return vec3( x, y, z );
}

mat3 extractRotationMatrix( mat4 m )
{
  vec3 x = normalize( m[ 0 ].xyz ); 
  vec3 y = normalize( m[ 1 ].xyz ); 
  vec3 z = normalize( m[ 2 ].xyz ); 

  return mat3( x, y, z );
}

vec4 extractQuaternion( mat4 m )
{
  mat3 rotationMatrix = extractRotationMatrix( m );

  return quaternionFromMatrix( rotationMatrix );
}

float extractRotationX( mat4 matrix )
{  
  return atan( matrix[2][1], matrix[2][2] );
}

float extractRotationY( mat4 matrix )
{  
  return atan(-matrix[2][0], sqrt(matrix[2][1] * matrix[2][1] + matrix[2][2] * matrix[2][2]));
}

float extractRotationZ( mat4 matrix )
{  
  return atan(matrix[1][0], matrix[0][0]);
}

vec2 tilingOffset( vec2 uv, vec4 tilingOffset )
{
  uv *= tilingOffset.xy;
  uv += tilingOffset.zw;

  return uv;
}

vec2 tilingOffsetRepeat( vec2 uv, vec4 tilingOffset )
{
  uv *= tilingOffset.xy;
  uv += tilingOffset.zw;

  return mod( uv, vec2(1,1) );
}

vec3 billboardWorldOffset( vec2 _UV, mat4 _INV_VIEW_MATRIX, mat4 _MODEL_MATRIX  )
{
  vec2 mappedUV = mix( vec2(-1,1), vec2( 1, -1 ), _UV );
  vec4 offset = vec4( mappedUV.x, mappedUV.y, 0, 0 );

  offset = _INV_VIEW_MATRIX * offset;
  mat4 mw = _MODEL_MATRIX;
  mw[ 3 ][ 0 ] = 0.0;
  mw[ 3 ][ 1 ] = 0.0;
  mw[ 3 ][ 2 ] = 0.0;

  vec3 worldOffset = worldToLocal( offset.xyz, mw );
  worldOffset = normalize( worldOffset );

  return worldOffset;
}

vec3 billboardWorldOffsetWithSize( vec2 size, vec2 _UV, mat4 _INV_VIEW_MATRIX, mat4 _MODEL_MATRIX  )
{
  vec2 mappedUV = mix( vec2(-1,1), vec2( 1, -1 ), _UV );
  vec4 offset = vec4( mappedUV.x * size.x, mappedUV.y * size.y, 0, 0 );

  offset = _INV_VIEW_MATRIX * offset;
  mat4 mw = _MODEL_MATRIX;
  mw[ 3 ][ 0 ] = 0.0;
  mw[ 3 ][ 1 ] = 0.0;
  mw[ 3 ][ 2 ] = 0.0;

  vec3 worldOffset = worldToLocal( offset.xyz, mw );
  // worldOffset = normalize( worldOffset );

  return worldOffset;
}

vec2 rotate_v2( vec2 uv, float angle )
{
  float s = sin( angle );
  float c = cos( angle );

    
  float x = uv.x;
  float y = uv.y; 
 
  uv.x = c * x - s * y;
  uv.y = s * x + c * y;

  return uv;
}

vec2 rotateAround_v2( vec2 uv, float angle, vec2 pivot )
{
  uv -= pivot;
  uv =  rotate_v2( uv, angle );
  uv += pivot;

  return uv;
}

vec2 rotateAroundTexture_v2( vec2 uv, float angle, vec2 pivot, vec2 textureSize ) 
{

    vec2 p = uv;
    float aspect = textureSize.x / textureSize.y;
    
    float cosA = cos( angle );
    float sinA = sin( angle );
    
    mat2 rotMat = mat2( 
      vec2( cosA, -sinA ), 
      vec2( sinA, cosA )
     );

    rotMat = mat2( 
      vec2( cosA, -sinA ), 
      vec2( sinA, cosA )
     );


    p -= pivot;
    p.y *= 1.0 / aspect;
    p *= rotMat;
    
    p.y *= aspect;
    p += pivot;

    return p;

  }

vec3 cameraWorldPosition( mat4 _INV_VIEW_MATRIX )
{
  return (_INV_VIEW_MATRIX * vec4(vec3(0.0), 1.0)).xyz;
}

vec3 cameraWorldForward( mat4 _INV_VIEW_MATRIX )
{
  vec3 pos = cameraWorldPosition( _INV_VIEW_MATRIX );
  return normalize( (_INV_VIEW_MATRIX * vec4( vec3(0.0,0.0,1.0), 1.0)).xyz - pos );
}


mat3 identity_m3()
{
  return mat3(
    vec3( 1, 0, 0 ),
    vec3( 0, 1, 0 ),
    vec3( 0, 0, 1 )
  );
} 

mat3 translate_m3( vec2 translation )
{
  return mat3(
    vec3( 1, 0, translation.x ),
    vec3( 0, 1, translation.y ),
    vec3( 0, 0, 1 )
  );
}

mat3 scale_m3( vec2 scale )
{
  return mat3(
    vec3( scale.x, 0, 0 ),
    vec3( 0, scale.y, 0 ),
    vec3( 0, 0, 1 )
  );
}

mat3 scalePivot_m3( vec2 scale, vec2 pivot )
{
  return translate_m3( -pivot ) * scale_m3( scale ) * translate_m3( pivot );
}

mat3 rotate_m3( float radiansAngle )
{
  float c = cos( radiansAngle );
  float s = sin( radiansAngle );

  return mat3(
    vec3( c, -s, 0 ),
    vec3( s,  c, 0 ),
    vec3( 0,  0, 1 )
  );
}

mat3 rotatePivot_m3( float radiansAngle, vec2 pivot )
{
  return translate_m3( -pivot ) * rotate_m3( radiansAngle ) * translate_m3( pivot );
}

mat3 rotatePivotAspect_m3( float radiansAngle, vec2 pivot, float aspect )
{
  return translate_m3( -pivot ) * 
         scale_m3( vec2( 1, 1.0/aspect ) ) * 
         rotate_m3( radiansAngle ) * 
         scale_m3( vec2( 1, aspect ) ) *
         translate_m3( pivot );
}


vec2 applyMatrix_m3v2( mat3 matrix, vec2 point )
{
  float x = point.x;
  float y = point.y;

  // float ox = matrix[ 0 ] * x + matrix[ 3 ] * y + matrix[ 6 ];
	// float oy = matrix[ 1 ] * x + matrix[ 4 ] * y + matrix[ 7 ];
  
  // 0 1 2   0 3 6  
  // 3 4 5   1 4 7
  // 6 7 8   2 5 8

  vec3 mx = matrix[ 0 ];
  vec3 my = matrix[ 1 ];

  float ox = mx.x * x + mx.y * y + mx.z;
	float oy = my.x * x + my.y * y + my.z;

  return vec2( ox, oy );
}

vec2 applyMatrixBase_m3v2( mat3 matrix, vec2 point )
{
  float x = point.x;
  float y = point.y;

  vec3 mx = matrix[ 0 ];
  vec3 my = matrix[ 1 ];

  float ox = mx.x * x + mx.y * y;
	float oy = my.x * x + my.y * y;

  return vec2( ox, oy );
}


mat4 identity_m4()
{
  return mat4(
    vec4( 1, 0, 0, 0 ),
    vec4( 0, 1, 0, 0 ),
    vec4( 0, 0, 1, 0 ),
    vec4( 0, 0, 0, 1 )
  );
} 

mat4 translate_m4( vec3 translation )
{
  return mat4(
    vec4( 1, 0, 0, 0 ),
    vec4( 0, 1, 0, 0 ),
    vec4( 0, 0, 1, 0 ),
    vec4( translation.x , translation.y, translation.z, 1 )
  );
}



mat4 scale_m4( vec3 scale )
{
  return mat4(
    vec4( scale.x, 0, 0, 0 ),
    vec4( 0, scale.y, 0, 0 ),
    vec4( 0, 0, scale.z, 0 ),
    vec4( 0, 0, 0, 1 )
  );
}


mat4 rotationX_m4( float radiansAngle )
{
  float c = cos( radiansAngle );
  float s = sin( radiansAngle );

  return mat4(
    vec4( 1, 0, 0, 0 ),
    vec4( 0, c, s, 0 ),
    vec4( 0, -s, c, 0 ),
    vec4( 0, 0, 0, 1 )
  );
}

mat4 rotationY_m4( float radiansAngle )
{
  float c = cos( radiansAngle );
  float s = sin( radiansAngle );

  return mat4(
    vec4( c, 0,-s, 0 ),
    vec4( 0, 1, 0, 0 ),
    vec4( s, 0, c, 0 ),
    vec4( 0, 0, 0, 1 )
  );
}

mat4 rotationZ_m4( float radiansAngle )
{
  float c = cos( radiansAngle );
  float s = sin( radiansAngle );

  return mat4(
    vec4( c, s, 0, 0 ),
    vec4(-s, c, 0, 0 ),
    vec4( 0, 0, 1, 0 ),
    vec4( 0, 0, 0, 1 )
  );
}

mat3 quaternionToRotationMatrix( vec4 q )
{
  float x = q.x;
  float y = q.y;
  float z = q.z;
  float w = q.w;

  float x2 = x + x;
  float	y2 = y + y;
  float z2 = z + z;

  float xx = x * x2;
  float xy = x * y2;
  float xz = x * z2;

  float yy = y * y2;
  float yz = y * z2;
  float zz = z * z2;

  float wx = w * x2;
  float wy = w * y2;
  float wz = w * z2;


  mat3 matrix = mat3(

    vec3(  
      ( 1.0 - ( yy + zz ) ), 
      ( xy + wz ), 
      ( xz - wy )  
    ),
    
    vec3(
      ( xy - wz ),
      ( 1.0 - ( xx + zz ) ),
      ( yz + wx )
    ),

    vec3(
      ( xz + wy ),
      ( yz - wx ),
      ( 1.0  - ( xx + yy ) )
    )
  );

  return matrix;

}

mat4 TRS( vec3 translation, vec4 rotation, vec3 scale )
{
  return translate_m4( translation ) * mat4( quaternionToRotationMatrix( rotation ) ) * scale_m4( scale );
}

mat4 mixTRS( mat4 a, mat4 b, float t )
{
  vec3 oA = extractTranslation( a );
  vec3 oB = extractTranslation( b );
  
  vec3 sA = extractScale( a );
  vec3 sB = extractScale( b );

  vec4 rA = extractQuaternion( a );
  vec4 rB = extractQuaternion( b );

  vec3 o = mix( oA, oB, t );
  vec3 s = mix( sA, sB, t );
  vec4 r = quaternionSlerp( rA, rB, t );

  return TRS( o, r, s );
}