shader_type spatial;

uniform sampler2D noise;
uniform sampler2D emissive_noise;
uniform sampler2D color_gradient;
uniform sampler2D DEPTH_TEXTURE : hint_depth_texture;
uniform vec4 emissive_color : source_color = vec4(1.0, 0.3, 1.0, 1.0);
uniform float noise_strength : hint_range(0.1,2) = 1;
uniform float edge_distance : hint_range(1, 10) = 1;
uniform vec2 oscillation_speed = vec2(1,1);

varying vec3 world_vertex;

void vertex() {
	// Called for every vertex the material is visible on.
	vec2 uv = UV + TIME * oscillation_speed;
	float noise_val = texture(noise, uv).r;
	VERTEX = VERTEX + NORMAL * noise_strength * noise_val;
	world_vertex = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
}

void fragment() {
	// Called for every pixel the material is visible on.
	vec3 color;
	vec3 e_col = emissive_color.rgb;
	vec2 uv = UV;
	float g_noise_val = texture(noise, vec2(uv.x + TIME * 0.2, 0.5)).r;
	g_noise_val *= g_noise_val;
	float e_noise_val = texture(noise, uv + TIME * 0.2).r;
	vec3 gradient_col = texture(color_gradient, vec2(g_noise_val, 0)).rgb;
	color = gradient_col;
	ALBEDO = color;
	e_col = e_col * smoothstep(COLOR.r,1.0, texture(emissive_noise, UV).r) * 20.0;
	EMISSION = e_col.rgb;
	
	float depth_tex = texture(DEPTH_TEXTURE, SCREEN_UV).r;
	float prox_factor = 1.0 - smoothstep(depth_tex, depth_tex + edge_distance, VERTEX.z);
	float a_noise_val = smoothstep(0, .8, texture(noise, uv + TIME * 0.1).r);
	ALPHA = COLOR.a * a_noise_val * prox_factor;
	
}

//void light() {
//	// Called for every pixel for every light affecting the material.
//	// Uncomment to replace the default light processing function with this one.
//}
