// AUTOGENERATED COPYRIGHT HEADER START
// Copyright (C) 2021-2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
// AUTOGENERATED COPYRIGHT HEADER END
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holder nor the names of its contributors
//    may be used to endorse or promote products derived from this software
//    without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

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

// Provided by Stream Effects
uniform texture2d InputA<
	bool automatic = true;
>;

uniform float _00_vignette_area<
	string name = "Vignetting Area";
	string field_type = "slider";
	float minimum = 0.;
	float maximum = 100.;
	float step = 0.01;
	float scale = 0.01;
> = 50.0;

uniform float4 _01_vignette_color<
	string name = "Vignette Color";
	string field_type = "slider";
	float4 minimum = {0., 0., 0., 0.};
	float4 maximum = {100., 100., 100., 100.};
	float4 step = {0.01, 0.01, 0.01, 0.01};
	float4 scale = {0.01, 0.01, 0.01, 0.01};
> = {0., 0., 0., 0.};

uniform float _02_vignette_strength<
	string name = "Vignette Strength";
	string field_type = "slider";
	float minimum = 0.;
	float maximum = 100.;
	float step = 0.01;
	float scale = 0.01;
> = 100.0;

#define PI		3.1415926f
#define TwoPI	6.2831853f
#define HalfPI	1.5707963f

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

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

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

float4 PSDefault(VertData v_in) : TARGET {
	static const float2 center = float2(.5, .5);
	float4 smp = InputA.Sample(def_sampler, v_in.uv);
	float borderFade = clamp(((1. - distance(v_in.uv, center)) - (_00_vignette_area - .5)), 0., 1.);

	float4 vignette_color = _01_vignette_color;
	return lerp(smp, vignette_color, (1. - borderFade) / max(1. - _02_vignette_strength, 0.0001));
}

technique Draw
{
	pass
	{
		vertex_shader = VSDefault(v_in);
		pixel_shader  = PSDefault(v_in);
	}
}

float4 PSRectangle(VertData v_in) : TARGET {
	static const float2 center = float2(.5, .5);
	float4 smp = InputA.Sample(def_sampler, v_in.uv);

	if (_00_vignette_area < 0.01)
		return smp;

	float2 borderArea = center * _00_vignette_area;
	float2 borderDistance = center;
	borderDistance -= abs(v_in.uv - center);
	borderDistance /= center;
	borderDistance -= center * _00_vignette_area;
	borderDistance = min(borderDistance, 0);
	borderDistance += borderArea;
	borderDistance /= borderArea;

	// Now apply a modifier so that we only get the border area.
	float borderFade = sin(borderDistance.x * HalfPI) * sin(borderDistance.y * HalfPI);

	return lerp(smp, _01_vignette_color, (1. - borderFade) * _02_vignette_strength);
}

technique Rectangle
{
	pass
	{
		vertex_shader = VSDefault(v_in);
		pixel_shader  = PSRectangle(v_in);
	}
}
