shader_type spatial;
render_mode unshaded, blend_mix, depth_draw_opaque, cull_back;

/* Wizardry + Tron aesthetic: neon brick wall effect. */

uniform float uv_scale : hint_range(0.01, 5.0) = 0.25;
uniform float trace_speed : hint_range(0.1, 8.0) = 2.5;
uniform float trace_brightness : hint_range(0.0, 4.0) = 1.2;
uniform float trace_length : hint_range(0.1, 10.0) = 2.5;
uniform float trace_density : hint_range(0.0, 1.0) = 0.55;
uniform vec3 trace_color : source_color = vec3(1.0, 0.55, 0.1);
uniform vec3 trace_head_color : source_color = vec3(1.0, 0.95, 0.7);

// Pseudo-random hash for randomizing traces per mortar segment
float hash(vec2 p) {
	return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}

void fragment() {
	// Scale UVs so bricks tile across the quad
	vec2 uv = UV * uv_scale;

	// Block grid for visual consistency
	vec2 block_size = vec2(0.4, 0.55);
	float mortar = 0.005;

	float row = floor(uv.y / block_size.y);
	float offset = mod(row, 2.0) * 0.5 * block_size.x;
	float bx = fract((uv.x + offset) / block_size.x);
	float by = fract(uv.y / block_size.y);

	// Distance to nearest mortar/edge line
	float dx = min(bx, 1.0 - bx);
	float dy = min(by, 1.0 - by);
	float d_mortar = min(dx, dy);

	// --- Tron light-cycle tracers flowing continuously along mortar lines ---
	// Masks: which mortar line we're currently on (horizontal vs vertical)
	float h_mask = step(dy, dx) * (1.0 - smoothstep(0.0, mortar * 2.0, dy));
	float v_mask = step(dx, dy) * (1.0 - smoothstep(0.0, mortar * 2.0, dx));

	float col = floor((uv.x + offset) / block_size.x);
	float period = trace_length * 3.0; // spacing between pulse heads along a line

	// Horizontal mortar: one pulse per row, traveling along x (direction randomized)
	float h_seed = hash(vec2(row, 17.3));
	float h_dir = (h_seed < 0.5) ? -1.0 : 1.0;
	float h_spd = trace_speed * (0.6 + h_seed * 0.8);
	float h_head = uv.x * h_dir - TIME * h_spd + h_seed * 50.0;
	float h_back = mod(h_head, period);                  // 0 at head, grows backward
	float h_tail = max(0.0, 1.0 - h_back / trace_length);
	h_tail *= h_tail;                                    // quadratic falloff
	float h_tip  = exp(-24.0 * h_back * h_back / (trace_length * trace_length));
	float h_gate = smoothstep(1.0 - trace_density, 1.0 - trace_density + 0.05,
	                          hash(vec2(row, 3.1)));

	// Vertical mortar: one pulse per column, traveling along y
	float v_seed = hash(vec2(col, 91.7));
	float v_dir = (v_seed < 0.5) ? -1.0 : 1.0;
	float v_spd = trace_speed * (0.6 + v_seed * 0.8);
	float v_head = uv.y * v_dir - TIME * v_spd + v_seed * 50.0;
	float v_back = mod(v_head, period);
	float v_tail = max(0.0, 1.0 - v_back / trace_length);
	v_tail *= v_tail;
	float v_tip  = exp(-24.0 * v_back * v_back / (trace_length * trace_length));
	float v_gate = smoothstep(1.0 - trace_density, 1.0 - trace_density + 0.05,
	                          hash(vec2(col, 57.8)));

	float trace_body = (h_tail * h_gate * h_mask) + (v_tail * v_gate * v_mask);
	float trace_head = (h_tip  * h_gate * h_mask) + (v_tip  * v_gate * v_mask);
	trace_body = clamp(trace_body, 0.0, 1.0);
	trace_head = clamp(trace_head, 0.0, 1.0);

	// Neon edge: bright core line + soft glow falloff
	float core = 1.0 - smoothstep(0.0, mortar, d_mortar);
	float glow = 1.0 - smoothstep(0.0, mortar * 5.0, d_mortar);

	// Tron colors: bright white-cyan core, blue glow halo
	vec3 core_color = vec3(0.9, 0.95, 1.0);
	vec3 glow_color = vec3(0.7, 0.2, 0.9);

	vec3 final_color = core * core_color + glow * glow_color * 0.9;

	ALPHA = 0.9 + 0.6 * core;
	// Trails add a warm body glow; heads pop with a bright hot tip
	final_color += trace_body * trace_color * trace_brightness;
	final_color += trace_head * trace_head_color * trace_brightness * 1.8;

	ALBEDO = final_color;
}