shader_type canvas_item;

#include "res://shaders/tools.gdshaderinc"

uniform vec2 point_1;
uniform vec2 point_2;
uniform vec2 point_3;
uniform vec2 point_4;
uniform vec2 point_5;
uniform vec2 point_6;
uniform vec2 point_7;
uniform vec2 point_8;
uniform float len:  hint_range(0.0, 1.0) = 0.5;
const vec2 center = vec2(0.125, 0.125);
const float DSS = 0.002525;
const float radius = 0.125;
uniform float speed:  hint_range(0.0, 1024.0) = 0.0;


float line(vec2 p1, vec2 p2, float width, vec2 uv)
{
	float dist = distance(p1, p2); // Дистанция между точками
	float dist_uv = distance(p1, uv); // Дистанция между точкой и текущим пикселем
	return 1.0 - floor(1.0 - (0.001 * width) + distance (mix(p1, p2, clamp(dist_uv / dist, 0.0, 1.0)),  uv));
}


void get_line_points(inout float px0, inout float px1, inout float py0 ,inout float py1, float ang)
{
    px0 = (sin(ang*PI/180.0)/4.0);
    py0 = (cos(ang*PI/180.0)/4.0);
    px1 = px0 * len;
    py1 = py0 * len;
    px0 +=0.125;
    py0 +=0.125;
    px1 +=0.125;
    py1 +=0.125;
}


vec2 rotateUVmatrinx(vec2 uv, vec2 pivot, float rotation)
{
    mat2 rotation_matrix=mat2(  vec2(sin(rotation),-cos(rotation)),
                                vec2(cos(rotation),sin(rotation))
                                );
    uv -= pivot;
    uv *= rotation_matrix;
    uv += pivot;
    return uv;
}


void vertex()
{
    float dir = 0.0;
    vec2 offset = vec2(0.125, 0.125);
    dir -= speed * TIME;
    VERTEX = rotateUVmatrinx(VERTEX, center - offset, radians(dir));
}


void fragment()
{
    float px0 = 0.0;
    float px1 = 0.0;
    float py0 = 0.0;
    float py1 = 0.0;
    COLOR = vec4(1.0, 0.0, 0.0, 0.0); //texture(TEXTURE, UV);  // Цвет текущей точки
    get_line_points(px0, px1, py0, py1, 330.0);
    COLOR.a += line(vec2(px0, py0), vec2(px1, py1), 15.0, UV);
    get_line_points(px0, px1, py0, py1, 90.0);
    COLOR.a += line(vec2(px0, py0), vec2(px1, py1), 15.0, UV);
    get_line_points(px0, px1, py0, py1, 210.0);
    COLOR.a += line(vec2(px0, py0), vec2(px1, py1), 15.0, UV);
    COLOR *= smooth_px(length(UV-center), radius, DSS);
}
