shader_type spatial;
render_mode specular_disabled, cull_back;

// Awakened look armour / crowns / plates (scripts/awaken_looks.gd): vertex colours, the baked toon ramp of the heroes
// (toon.gdshader baked 1.0: lit 0.86 / shade 0.62) + a class-coloured rim and a small metal glint. `fade` 0..1 =
// materialise: the piece dissolves in / out through noise with a glowing edge (no transparency, no sorting).
#include "res://shaders/awaken_noise.gdshaderinc"
uniform float fade : hint_range(0.0, 1.0) = 1.0;
uniform vec4 edge_color : source_color = vec4(1.0, 0.8, 0.3, 1.0);
uniform vec4 rim_color : source_color = vec4(1.0, 0.9, 0.6, 1.0);
uniform float rim_amount = 0.55;
uniform float glint = 0.35;
varying vec3 v_p;

void vertex() {
	v_p = VERTEX;
}

void fragment() {
	float n = awk_dis(v_p);
	float th = fade * 1.12 - 0.06;
	if (n > th) {
		discard;
	}
	ALBEDO = awk_lin(COLOR.rgb);
	float edge = (1.0 - smoothstep(0.0, 0.08, th - n)) * (1.0 - step(0.999, fade));
	EMISSION = edge_color.rgb * edge * 3.0;
	ROUGHNESS = 1.0;
}

void light() {
	if (!LIGHT_IS_DIRECTIONAL) {
		DIFFUSE_LIGHT += smoothstep(-0.25, 0.6, dot(NORMAL, LIGHT)) * ATTENUATION * LIGHT_COLOR / PI;
	} else {
		float ndl = dot(NORMAL, LIGHT);
		float lit = smoothstep(-0.28, 0.28, ndl) * mix(1.0, smoothstep(0.25, 0.75, ATTENUATION), 0.85);
		vec3 band = mix(vec3(0.62, 0.61, 0.64), vec3(0.9), lit);
		float fres = 1.0 - clamp(dot(NORMAL, VIEW), 0.0, 1.0);
		band += rim_color.rgb * smoothstep(0.6, 0.74, fres) * rim_amount;
		vec3 hv = normalize(LIGHT + VIEW);
		float p = pow(max(dot(NORMAL, hv), 0.0), 26.0);
		band += vec3(smoothstep(0.3, 0.6, p) * glint * lit);
		DIFFUSE_LIGHT += band * LIGHT_COLOR / PI;
	}
}
