#T#PoiPostProcessProperties
//ifex _PostProcess==0
[HideInInspector] m_start_postprocess ("Post Processing--{reference_property:_PostProcess,button_help:{text:Tutorial,action:{type:URL,data:https://www.poiyomi.com/post-processing/main},hover:Documentation}}", Float) = 0
[HideInInspector][ThryToggle(POSTPROCESS)]_PostProcess ("Enable", Float) = 0
[sRGBWarning][ThryTexture] _PPMask ("Mask--{reference_properties:[_PPMaskPan, _PPMaskUV, _PPMaskChannel, _PPMaskInvert]}", 2D) = "white" { }
[HideInInspector][Vector2]_PPMaskPan ("Panning", Vector) = (0, 0, 0, 0)
[HideInInspector][Enum(R, 0, G, 1, B, 2, A, 3)]_PPMaskChannel ("Channel", Float) = 0
[HideInInspector][ToggleUI]_PPMaskInvert ("Invert", Float) = 0
[HideInInspector][ThryWideEnum(UV0, 0, UV1, 1, UV2, 2, UV3, 3, Panosphere, 4, World Pos XZ, 5, Polar UV, 6, Distorted UV, 7)]_PPMaskUV ("UV", Int) = 0

_PPHue ("Hue", Range(0, 1)) = 0
[HDR]_PPTint ("Tint", Color) = (1, 1, 1, 1)
[VectorLabel(R,G,B)]_PPRGB ("RGB", Vector) = (1, 1, 1, 1)
_PPContrast ("Contrast", Float) = 1
_PPSaturation ("Saturation", Float) = 1
_PPBrightness ("Brightness", Float) = 1
_PPLightness ("Lightness", Float) = 0
_PPHDR ("HDR", Float) = 0

[ThryToggleUI(true)]_PPPosterization ("<size=13><b>  Posterization</b></size>", Float) = 0
_PPPosterizationAmount ("Steps--{condition_showS:(_PPPosterization==1)}", Range(1, 30)) = 4

[HideInInspector] m_end_postprocess ("", Float) = 0
//endex

#T#PoiPostProcessKeywords
//ifex _PostProcess==0
#pragma shader_feature_local POSTPROCESS
//endex

#T#PoiPostProcessVariables
//ifex _PostProcess==0
#ifdef POSTPROCESS
	#if defined(PROP_PPMASK) || !defined(OPTIMIZER_ENABLED)
		Texture2D _PPMask;
	#endif
	float4 _PPMask_ST;
	float2 _PPMaskPan;
	float _PPMaskUV;
	float _PPMaskChannel;
	float _PPMaskInvert;

	float3 _PPTint;
	float3 _PPRGB;
	float _PPHue;
	float _PPContrast;
	float _PPSaturation;
	float _PPBrightness;
	float _PPLightness;
	float _PPHDR;

	float _PPPosterization;
	float _PPPosterizationAmount;
	const static float COLORS = 32;

#endif
//endex


#T#PoiPostProcessFunctions
//ifex _PostProcess==0
#ifdef POSTPROCESS
	float3 poiPosterize(float3 color, float steps)
	{
		float3 newColor = color;
        steps = floor(steps);
		newColor.r = floor(newColor.r * steps) / steps;
		newColor.g = floor(newColor.g * steps) / steps;
		newColor.b = floor(newColor.b * steps) / steps;
		return newColor;
	}

	float oetf_sRGB_scalar(float L)
	{
		float V = 1.055 * (pow(L, 1.0 / 2.4)) - 0.055;
		if (L <= 0.0031308)
			V = L * 12.92;
		return V;
	}

	float3 oetf_sRGB(float3 L)
	{
		return float3(oetf_sRGB_scalar(L.r), oetf_sRGB_scalar(L.g), oetf_sRGB_scalar(L.b));
	}

	float eotf_sRGB_scalar(float V)
	{
		float L = pow((V + 0.055) / 1.055, 2.4);
		if (V <= oetf_sRGB_scalar(0.0031308))
			L = V / 12.92;
		return L;
	}

	float3 GetHDR(float3 rgb)
	{
		return float3(eotf_sRGB_scalar(rgb.r), eotf_sRGB_scalar(rgb.g), eotf_sRGB_scalar(rgb.b));
	}

	float3 GetContrast(float3 col, float contrast)
	{
		return lerp(float3(0.5, 0.5, 0.5), col, contrast);
	}

	float3 GetSaturation(float3 col, float interpolator)
	{
		return lerp(dot(col, float3(0.3, 0.59, 0.11)), col, interpolator);
	}

	void applyPostProcessing(inout PoiFragData poiFragData, in PoiMesh poiMesh)
	{
		float3 col = poiFragData.finalColor;
		col = hueShift(col, _PPHue);
		col *= _PPTint;
		col *= _PPRGB;
		col = GetSaturation(col, _PPSaturation);
		col = lerp(col, GetHDR(col), _PPHDR);
		col = GetContrast(col, _PPContrast);
		col *= _PPBrightness;
		col += _PPLightness;

		float ppMask = 1;
		#if defined(PROP_PPMASK) || !defined(OPTIMIZER_ENABLED)
			ppMask = POI2D_SAMPLER_PAN(_PPMask, _MainTex, poiUV(poiMesh.uv[_PPMaskUV], _PPMask_ST), _PPMaskPan)[_PPMaskChannel];
			ppMask = lerp(ppMask, 1 - ppMask, _PPMaskInvert);
			col = lerp(poiFragData.finalColor, col, ppMask);
		#endif

		if (_PPPosterization)
		{
			col = lerp(col, poiPosterize(col, _PPPosterizationAmount), ppMask);
		}
		

		poiFragData.finalColor = col;
	}
#endif
//endex

#T#PoiPostProcessFunctionCalls
//ifex _PostProcess==0
#ifdef POSTPROCESS
	applyPostProcessing(poiFragData, poiMesh);
#endif
//endex