shader_type spatial;
render_mode unshaded;

uniform sampler2D dissolve_texture : hint_default_transparent;
uniform float threshold : hint_range(0,1);
uniform vec3 color : source_color;
uniform float alpha_add = 1;
uniform float speed : hint_range(0, 0.01) = 0.005;

float fresnel(vec3 Normal, vec3 ViewDir, float Power){
    return pow((1.0 - clamp(dot(normalize(Normal), normalize(ViewDir)), 0, 1)), Power);
}

float _fresnel(vec2 uv, float Power){
    return pow((clamp(sin(PI * uv.x) * sin(PI * uv.y), 0, 1)), Power);
}

vec2 loop(vec2 vec){
	vec2 res = vec;
	vec2 floors = floor(res);
	res.x -= floor(res.x);
	res.y -= floor(res.y);
	if(int(floors.x) % 2 == 0){
		res.x = 1.0 - res.x;
	}
	if(int(floors.y) % 2 == 0){
		res.y = 1.0 - res.y;
	}
	return res;
}

void fragment(){
    vec4 noise_texture = texture(dissolve_texture, loop(UV + vec2(TIME, TIME) * speed));
    noise_texture *= texture(dissolve_texture, loop(UV + vec2(-TIME, -TIME) * speed)) / 0.5;
    noise_texture *= texture(dissolve_texture, loop(UV + vec2(TIME, -TIME) * speed)) / 0.5;
    noise_texture *= texture(dissolve_texture, loop(UV + vec2(-TIME, TIME) * speed)) / 0.5;
    noise_texture *= texture(dissolve_texture, loop(UV * 2.0 + vec2(-TIME, TIME) * speed * 0.3)) / 0.5;
    noise_texture *= texture(dissolve_texture, loop(UV * 2.0 + vec2(TIME, -TIME) * speed * 0.5)) / 0.5;
    noise_texture *= texture(dissolve_texture, loop(UV * 2.0 + vec2(TIME, TIME) * speed * 0.8)) / 0.5;
    noise_texture *= texture(dissolve_texture, loop(UV * 2.0 + vec2(-TIME, -TIME) * speed)) / 0.5;
	
    ALPHA = clamp(noise_texture.x - threshold, 0, 1);
	ALPHA *= _fresnel(UV, 3) * alpha_add;
	ALPHA = clamp(ALPHA, 0, 1);
	ALBEDO = color;
}
