shader_type canvas_item;

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

uniform float wipeState:hint_range(0,1) = 0;
uniform vec2 outputViewSize = vec2(1920,1080);


uniform sampler2D wipeTexture : source_color, filter_linear_mipmap, repeat_enable;
uniform vec2 wipeTextureSize = vec2( 1,1 );
uniform vec4 wipeTextureTint : source_color = vec4(0,0,0,1);

uniform float uvRotation = 0;
uniform vec2 uvRotationCenter = vec2(0 ,0);

uniform float uvScale = 1;
uniform vec2 uvScaleCenter = vec2(0 ,0);

uniform vec2 uvTranslation = vec2(0,0);

varying vec2 textureUV;
varying vec4 animatedTint;

void vertex()
{
  mat3 matrix = identity_m3();

  float textureAspect = wipeTextureSize.x / wipeTextureSize.y;
  float viewAspect = outputViewSize.x / outputViewSize.y;
  float viewRatio = viewAspect / textureAspect;

  matrix *= rotatePivotAspect_m3( uvRotation * DegreesToRadians , uvRotationCenter, textureAspect * viewRatio );

  vec2 rotatedScaleCenter = applyMatrix_m3v2( rotatePivot_m3( uvRotation, vec2( 0.5,0.5)), uvScaleCenter );
  matrix *= scalePivot_m3( uvScale * vec2( viewRatio,1), rotatedScaleCenter );


  matrix *= translate_m3( applyMatrixBase_m3v2( matrix, -uvTranslation ) );

  vec2 uv = applyMatrix_m3v2( matrix, UV );
  textureUV = uv;

  animatedTint = wipeTextureTint;
  animatedTint.a *= wipeState;
}

void fragment()
{
  COLOR = texture( wipeTexture, textureUV ) * animatedTint;
}
