shader_type canvas_item;

uniform vec4 color_signal: source_color;
uniform float turn = 0.785;
uniform float inner_radius = 0.32;
uniform float speed = 0.0;

const float outer_radius = 0.109;
const float zoom = 50.0;
const float size = 5.0;
const float blur = 0.000001;
const float fill_ratio = 0.03;


float remap(float i_min, float i_max, float o_min, float o_max, float val) {
	float t = (val - i_min) / (i_max - i_min);
	return o_min + (o_max - o_min) * t;
}


float mask(vec2 uv, float value)
{
	float r = atan(uv.x, uv.y);
	r = remap(-PI, PI, 0.0, 1.0, r);
	r = step(r, value * 0.5);

	uv.x = uv.x + 1.0;
	uv.x = uv.x * -1.0;
	uv.x += 1.0;
	float l = atan(uv.x, uv.y);
	l = remap(-PI, PI, 0.0, 1.0, l);
	l = step(l, value * 0.5);

	return r + l ;
}


vec2 rotate_uv(vec2 uv, vec2 pivot, float rotation) {
    float cosa = cos(rotation);
    float sina = sin(rotation);
    uv -= pivot;
    return vec2(
        cosa * uv.x - sina * uv.y,
        cosa * uv.y + sina * uv.x
    ) + pivot;
}


float circle(vec2 uv, float value)
{
	float d = length(uv);
	float t = smoothstep(
		inner_radius + blur,
		inner_radius - blur,
		d
	) - smoothstep(
		outer_radius + blur,
		outer_radius - blur,
		d
	);

	return t;
}


void fragment(){
	float d = length((UV-0.5)*2.0);
	float t = pow(smoothstep(0.9,0.2,d),0.35);
	vec2 uv = (UV * 2.0) - 1.0;

	vec3 rainbow = 0.5*cos(TIME+UV.xyx+vec3(0,2,4));
	vec4 color = vec4(rainbow.rgb,1.0);
	vec2 origin = vec2(0.0, 0.0);
	color = vec4(color_signal.rgb,1.0);
    uv = rotate_uv(uv, origin, turn * TAU);
    float c = circle(uv, 0.5);
    float mask = mask(uv, fill_ratio);
	d = sin(zoom*d - speed*TIME);
	//d = abs(d);
	d = size/d;
	color *= d*t;

	COLOR = vec4(color * mask * c);
}