// AUTOGENERATED COPYRIGHT HEADER START
// Copyright (C) 2019-2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
// AUTOGENERATED COPYRIGHT HEADER END

// Always provided by OBS
uniform float4x4 ViewProj<
	bool visible = false;
	string name = "View Projection Matrix";
>;

// Provided by Stream Effects
uniform float4 Time<
	bool visible = false;
	string name = "Time Array";
	string description = "A float4 value containing the total time, rendering time and the time since the last tick. The last value is a random number between 0 and 1.";
>;
uniform float4x4 Random<
	bool visible = false;
	string name = "Random Array";
	string description = "A float4x4 value containing random values between 0 and 1";
>;
uniform float4 ViewSize<
	bool automatic = true;
>;
uniform texture2d InputA<
	bool automatic = true;
>;

// Shader Parameters
uniform float p_drunk_strength<
	bool visible = true;
	string field_type = "slider";
	string name = "Strength";
	float minimum = 0.;
	float maximum = 100.;
	float step = .01;
> = 25.0;
uniform float p_drunk_speed<
	bool visible = true;
	string field_type = "slider";
	string name = "Speed";
	float minimum = 0.;
	float maximum = 100.;
	float step = .01;
> = 2.0;

// ---------- Shader Code
sampler_state def_sampler {
	AddressU  = Clamp;
	AddressV  = Clamp;
	Filter    = Linear;
};

struct VertData {
	float4 pos : POSITION;
	float2 uv  : TEXCOORD0;
};

VertData VSDefault(VertData vtx) {
	vtx.pos = mul(float4(vtx.pos.xyz, 1.0), ViewProj);
	return vtx;
}

#define MAX_PTS 5
#define MAX_LINE 5.

float random_time_at(int x, int y) {
	const float ts[MAX_PTS + 1][MAX_PTS + 1] = {
		{.2, .8, -.2, .452, -.2832, .8},
		{-.28, -1, -.42, -.89, .72, -.29},
		{.75, .25, .33, .67, .98, .01},
		{-.28, 0.8, -.32, -.189, .11, .84},
		{-.48, 0.1, -.2323, -.555, .421, .23},
		{-.28, 0.3, -1.3333, 1.333, 4, 1},
	};
	
	return ts[x][y];
}

float2 mult_at(int x, int y) {
	float x2 = fmod(x, 2.);
	float y2 = fmod(y, 2.);
	
	float2 mult;
	mult.x = (x2 < 1.) ? -1. : 1.;
	mult.y = (y2 < 1.) ? -1. : 1.;
	
	return mult;
}

float4 PSDrunkStage1(VertData vtx) : TARGET {
	float2 uvs[MAX_PTS + 1][MAX_PTS + 1];
	for (int x = 0; x <= MAX_PTS; x++) {
		for (int y = 0; y <= MAX_PTS; y++) {
			float2 off = float2(0, 0);
			if ((x > 0) && (x < MAX_PTS)) {
				off.x = cos(Time.x * p_drunk_speed + random_time_at(x, y)) * ViewSize.z;
			}
			if ((y > 0) && (y < MAX_PTS)) {
				off.y = sin(Time.x * p_drunk_speed + random_time_at(x, y)) * ViewSize.w;
			}
			off *= (p_drunk_strength / 100.0) * ViewSize.xy * 0.5 * mult_at(x, y);
				
			uvs[x][y] = float2(x / MAX_LINE + off.x, y / MAX_LINE + off.y);
		}
	}
	
	float2 fade = frac(vtx.uv * MAX_LINE);
	fade = (sin((fade - 0.5) * 3.141) + 1.0) * 0.5;
	
	int2 _low = int2(floor(vtx.uv * MAX_LINE));
	int2 _hig = int2(ceil(vtx.uv * MAX_LINE));
	
	float2 uv = vtx.uv;
	float2 uv_tl = uvs[_low.x][_low.y];
	float2 uv_tr = uvs[_hig.x][_low.y];
	float2 uv_bl = uvs[_low.x][_hig.y];
	float2 uv_br = uvs[_hig.x][_hig.y];
	
	float2 uv_t = lerp(uv_tl, uv_tr, fade.x);
	float2 uv_b = lerp(uv_bl, uv_br, fade.x);
	uv = lerp(uv_t, uv_b, fade.y);
	
	return InputA.Sample(def_sampler, uv);
}

technique Draw
{
	pass
	{
		vertex_shader = VSDefault(vtx);
		pixel_shader  = PSDrunkStage1(vtx); 
	}
}
