struct Uniforms {
	iResolution: vec2<f32>,
	time: f32,
	iMouse: vec2<f32>,
	size: f32,
	offset: vec2<f32>,
	beamwidth: f32,
	paddingtwo: f32
};

@group(0) @binding(0)
var<uniform> uni: Uniforms;

@vertex
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> @builtin(position) vec4<f32> {    
    var x = f32(i32((vertex_index << u32(1)) & u32(2)));
    var y = f32(i32(vertex_index & u32(2)));
    var uv = vec2<f32>(x, y);
    var out = 2.0 * uv - vec2<f32>(1.0, 1.0);
    return vec4<f32>(out.x, out.y, 0.0, 1.0);
}

fn modulo(a: f32, b: f32) -> f32 {
	return ((a % b) + b) % b;
}

@fragment
fn fs_main(@builtin(position) fragCoord: vec4<f32>) -> @location(0) vec4<f32> {
	let iMouse = uni.iMouse + uni.offset;
	let frag_coord = fragCoord + uni.offset.xyxy;
	var p: vec2<f32>;
	var m: vec2<f32>;
	if (uni.iResolution.x < uni.iResolution.y) {
		m = (2. * iMouse - uni.iResolution) / uni.iResolution.x;
		p = (2. * frag_coord.xy - uni.iResolution) / uni.iResolution.x;
	} else {
		m = (2. * iMouse - uni.iResolution) / uni.iResolution.y;
		p = (2. * frag_coord.xy - uni.iResolution) / uni.iResolution.y;
	}
	let tau: f32 = 3.1415927 * 2.;
	let a: f32 = atan2(p.x, p.y);
	let mousexy: f32 = atan2(m.x, m.y);
	var r: f32 = length(p) * uni.size;
	var l: f32 = length(m) * uni.size;
	var mouse: vec2<f32> = vec2<f32>(mousexy / tau, l);
	var uv: vec2<f32> = vec2<f32>(a / tau, r);
	var xCol: f32 = uv.x * 3.;
	xCol = modulo((xCol), (3.));
	var horColour: vec3<f32> = vec3<f32>(0.25, 0.25, 0.25);
	if (xCol < 1.) {
		horColour.r = horColour.r + (1. - xCol);
		horColour.g = horColour.g + (xCol);
	} else { 	if (xCol < 2.) {
		xCol = xCol - (1.);
		horColour.g = horColour.g + (1. - xCol);
		horColour.b = horColour.b + (xCol);
	} else { 
		xCol = xCol - (2.);
		horColour.b = horColour.b + (1. - xCol);
		horColour.r = horColour.r + (xCol);
	}
	}
	uv = 2. * uv - 1.;
	mouse = 2. * mouse - 1.;
	let beamWidth: f32 = (0.7 + 0.5 * sin((uv.x + (mouse.x - 0.2)) * 2. * tau * 0.15 * clamp(5., 0., 10.))) * abs(uni.beamwidth / (30. * uv.y));
	let horBeam: vec3<f32> = vec3<f32>(beamWidth);
    return vec4<f32>(horBeam * horColour, 1.);
}
