shader_type spatial;
render_mode cull_back;

uniform vec4 color_1: source_color = vec4(0.0, 0.03, 0.1, 1.0);
uniform vec4 color_2: source_color = vec4(0.0, 0.1, 0.3, 1.0);
uniform vec4 color_3: source_color = vec4(0.1, 0.35, 0.4, 1.0);

uniform highp float spin_rotation;
uniform highp vec2 offset = vec2(0.0, 0.0);
uniform highp float contrast = 2.0;
uniform highp float spin_amount = 0.36;
uniform highp float pixel_filter = 700.0;
#define SPIN_EASE 1.0

uniform float convolution_factor = 0.05;

uniform float roughness: hint_range(0.0, 1.0) = 0.15;
uniform float specular_contribution = 0.2;

// Used ONLY by the gunk, does not affect the gunk mask.
uniform vec2 uv_scale = vec2(1.0);

uniform float time_scale = 1.0;

uniform float edge_bleed = 0.1;

uniform sampler2D gunk_mask;


float brightness(vec3 color) {
	return 0.2 * color.r + 0.7 * color.g + 0.1 * color.b;
}

// Balatro-esque effect
vec4 effect(highp vec2 scaled_uv) {
	// Pixellate
	highp vec2 uv = floor(scaled_uv.xy * pixel_filter) / pixel_filter - 0.5 - offset;
	highp float uv_len = length(uv);

	// Adding in a center swirl, changes with time. Only applies meaningfully if `spin_amount` != 0
	highp float speed = 0.2 * SPIN_EASE * spin_rotation + 302.2;
	highp float new_px_angle = atan(uv.y, uv.x) + speed - 20.0 * SPIN_EASE * uv_len;
	uv = uv_len * vec2(cos(new_px_angle), sin(new_px_angle));

	// Now add the paint effect to the swirled uvec2
	uv *= 30.0;
	speed = TIME * time_scale;
	highp vec2 uv2 = vec2(uv.x + uv.y);

	for(int i = 0; i < 5; i++) {
		uv2 += sin(max(uv.x, uv.y)) + uv;
		uv += 0.5 * vec2(cos(5.1123314 + 0.353 * uv2.y + 0.131121 * speed), sin(uv2.x - 0.113 * speed));
		uv -= cos(uv.x + uv.y) - 1.0 * sin(0.711 * uv.x - uv.y);
	}

	// Adjust contrast & clamp on [0, 2]
	highp float contrast_mod = 0.25 * contrast + 0.5 * spin_amount + 1.2;
	highp float paint_res = clamp(0.035 * contrast_mod * length(uv), 0.0, 2.0);
	highp float c1p = max(0.0, 1.0 - contrast_mod * abs(1.0 - paint_res));
	highp float c2p = max(0.0, 1.0 - contrast_mod * abs(paint_res));
	highp float c3p = 1.0 - min(1.0, c1p + c2p);

	return 0.3 * color_1 / contrast + (color_1 * c1p + color_2 * c2p + vec4(c3p * color_3.rgb, c3p * color_1.a)) * (1.0 - 0.3 / contrast);
}

float hardstep(float value) {
	float x = clamp(value, 0.0, 1.0);
	return 0.5 * tanh( (20.0 * x - 10.0) * inversesqrt(x - x * x) ) + 0.5;
}

void fragment() {
	vec2 local_uv = UV * uv_scale;
	vec4 effect_color = effect(local_uv);
	float value = brightness(effect_color.rgb);
	float roughness_mix = value * roughness;

	ALBEDO = effect_color.rgb;
	ROUGHNESS = roughness_mix;
	SPECULAR = 0.5 * inversesqrt(specular_contribution);

	// Convolutional normal map
	float sample_step = convolution_factor / pixel_filter;
	float n = sqrt(brightness(effect(vec2(local_uv.x, local_uv.y - sample_step)).rgb));
	float s = sqrt(brightness(effect(vec2(local_uv.x, local_uv.y + sample_step)).rgb));
	float e = sqrt(brightness(effect(vec2(local_uv.x + sample_step, local_uv.y)).rgb));
	float w = sqrt(brightness(effect(vec2(local_uv.x - sample_step, local_uv.y)).rgb));
	float x = 0.5 * (w - e) + 0.5;
	float y = 0.5 * (s - n) + 0.5;
	NORMAL_MAP = vec3(x, y, clamp(1.0 - abs(x - 0.5) + abs(y - 0.5), 0.5, 1));


	float mask = texture(gunk_mask, UV).r;

	// soften edges
	NORMAL_MAP *= smoothstep(1.0, 0.0, mask);

	/*
	// Hard edge
	if(mask + edge_bleed < 0.5) {
		ALPHA = 1.0;
	} else {
		ALPHA = 0.0;
	}
	*/

	// Hardish edge
	ALPHA = hardstep(1.0 - mask + edge_bleed);
}