shader_type canvas_item;

const int max_itter = 256;

const vec2 zoom_center = vec2(-0.74364388703, 0.13182590421);

int get_mandelbrot_count(vec2 uv, float zoom_factor){
	vec2 scaled_uv = (uv - vec2(0.5)) * 4.0 * zoom_factor + zoom_center;
	float c_re = scaled_uv.x;
	float c_im = scaled_uv.y;

	float re = 0.0;
	float im = 0.0;

	for (int i = 0; i < max_itter; i++){
		float t_re = re;
		re = re*re - im*im + c_re;
		im = 2.0*im*t_re + c_im;
		if (re*re + im*im > 4.0){
			return i;
		}
	}
	return 0;
}


void fragment() {
	float smooth_sine = (sin(TIME) * 0.5 + 0.5);
	float zoom_factor = mix(0.001, 1.0, smoothstep(0.0, 1.0, smooth_sine));
	int mandelbrot_count = get_mandelbrot_count(UV, zoom_factor);
	float m_cn = float(mandelbrot_count) / float(max_itter);
	COLOR.rgb = vec3(m_cn, m_cn*2.0, m_cn*3.0);
}
