#T#PoiGlobalHelperFunctions
// glsl_mod behaves better on negative numbers, and
// in some situations actually outperforms HLSL's fmod()
#ifndef glsl_mod
#define glsl_mod(x, y) (((x) - (y) * floor((x) / (y))))
#endif

// sin/cos handling for large values
#ifndef mod2pi
#define mod2pi(x) (frac((x) / UNITY_TWO_PI) * UNITY_TWO_PI)
#endif

uniform float random_uniform_float_only_used_to_stop_compiler_warnings = 0.0f;

// Missing functions for URP
#if POI_PIPE == POI_URP
	inline float Linear01Depth( float z )
	{
		return 1.0 / (_ZBufferParams.x * z + _ZBufferParams.y);
	}

	inline half3 LinearToGammaSpace (half3 linRGB)
	{
		linRGB = max(linRGB, half3(0.h, 0.h, 0.h));
		// An almost-perfect approximation from http://chilliant.blogspot.com.au/2012/08/srgb-approximations-for-hlsl.html?m=1
		return max(1.055h * pow(linRGB, 0.416666667h) - 0.055h, 0.h);

		// Exact version, useful for debugging.
		//return half3(LinearToGammaSpaceExact(linRGB.r), LinearToGammaSpaceExact(linRGB.g), LinearToGammaSpaceExact(linRGB.b));
	}

	inline half3 GammaToLinearSpace (half3 sRGB)
	{
		// Approximate version from http://chilliant.blogspot.com.au/2012/08/srgb-approximations-for-hlsl.html?m=1
		return sRGB * (sRGB * (sRGB * 0.305306011h + 0.682171111h) + 0.012522878h);

		// Precise version, useful for debugging.
		//return half3(GammaToLinearSpaceExact(sRGB.r), GammaToLinearSpaceExact(sRGB.g), GammaToLinearSpaceExact(sRGB.b));
	}

	half4 PoiCalculateShadowMask(float2 shadowCoords)
	{
		// To ensure backward compatibility we have to avoid using shadowMask input, as it is not present in older shaders
		#if defined(SHADOWS_SHADOWMASK) && defined(LIGHTMAP_ON)
		half4 shadowMask = SAMPLE_SHADOWMASK(shadowCoords); // Shadowmask was sampled from lightmap
		#elif !defined(LIGHTMAP_ON) && (defined(PROBE_VOLUMES_L1) || defined(PROBE_VOLUMES_L2))
		half4 shadowMask = SAMPLE_SHADOWMASK(shadowCoords); // Shadowmask (probe occlusion) was sampled from APV
		#elif !defined (LIGHTMAP_ON)
		half4 shadowMask = unity_ProbesOcclusion; // Sample shadowmask (probe occlusion) from legacy probes
		#else
		half4 shadowMask = half4(1, 1, 1, 1); // Fallback shadowmask, fully unoccluded
		#endif

		return shadowMask;
	}

	// normal should be normalized, w=1.0
	half3 SHEvalLinearL2 (half4 normal)
	{
		half3 x1, x2;
		// 4 of the quadratic (L2) polynomials
		half4 vB = normal.xyzz * normal.yzzx;

		x1.r = dot(PoiSHBr,vB);
		x1.g = dot(PoiSHBg,vB);
		x1.b = dot(PoiSHBb,vB);

		// Final (5th) quadratic (L2) polynomial
		half vC = normal.x*normal.x - normal.y*normal.y;

		x2 = PoiSHC.rgb * vC;

		return x1 + x2;
	}

	half3 PoiGlossyEnvironmentReflection(half3 reflectVector, float3 positionWS, half perceptualRoughness, half occlusion, float2 normalizedScreenSpaceUV)
	{
		#if UNITY_VERSION >= 202200
			return GlossyEnvironmentReflection(reflectVector, positionWS, perceptualRoughness, occlusion, normalizedScreenSpaceUV);
		#elif UNITY_VERSION >= 202100
			return GlossyEnvironmentReflection(reflectVector, positionWS, perceptualRoughness, occlusion);
		#else
			return GlossyEnvironmentReflection(reflectVector, perceptualRoughness, occlusion);
		#endif
	}

	half3 PoiGlobalIllumination(BRDFData brdfData, BRDFData brdfDataClearCoat, float clearCoatMask,
		half3 bakedGI, half occlusion, float3 positionWS,
		half3 normalWS, half3 viewDirectionWS, float2 normalizedScreenSpaceUV)
	{
		#if UNITY_VERSION >= 202200
			return GlobalIllumination(brdfData, brdfDataClearCoat, clearCoatMask, bakedGI, occlusion, positionWS, normalWS, viewDirectionWS, normalizedScreenSpaceUV);
		#else
			return GlobalIllumination(brdfData, brdfDataClearCoat, clearCoatMask, bakedGI, occlusion, normalWS, viewDirectionWS);
		#endif
	}
#endif

float3 PoiUnpackNormalScale(float4 packedNormal, float bumpScale)
{
	#if POI_PIPE == POI_BIRP
		return UnpackScaleNormal(packedNormal, bumpScale);
	#elif POI_PIPE == POI_URP
		return UnpackNormalScale(packedNormal, bumpScale);
	#endif
}

float4 PoiObjectToClipPos(in float3 pos)
{
	#if POI_PIPE == POI_BIRP
		return UnityObjectToClipPos(pos);
	#elif POI_PIPE == POI_URP
		return TransformObjectToHClip(pos);
	#endif
}

float4 PoiWorldToClipPos(in float3 pos)
{
	#if POI_PIPE == POI_BIRP
		return UnityWorldToClipPos(pos);
	#elif POI_PIPE == POI_URP
		return TransformWorldToHClip(pos);
	#endif
}

float4 PoiComputeScreenPos(in float4 pos)
{
	#if POI_PIPE == POI_BIRP
		return ComputeGrabScreenPos(pos);
	#elif POI_PIPE == POI_URP
		return ComputeScreenPos(pos);
	#endif
}

float3 PoiDecodeHDR(float4 data, float4 decodeInstructions)
{
	#if POI_PIPE == POI_BIRP
		return DecodeHDR(data, decodeInstructions);
	#elif POI_PIPE == POI_URP
		return DecodeHDREnvironment(data, decodeInstructions);
	#endif
}

float3 PoiWorldSpaceViewDir(float3 worldPos)
{
	#if POI_PIPE == POI_BIRP
		return UnityWorldSpaceViewDir(worldPos);
	#elif POI_PIPE == POI_URP
		return GetWorldSpaceViewDir(worldPos);
	#endif
}

float2 poiUV(float2 uv, float4 tex_st)
{
	return uv * tex_st.xy + tex_st.zw;
}

//Lighting Helpers
float CalculateLuminance(float3 color)
{
	return color.r * 0.299 + color.g * 0.587 + color.b * 0.114;
}

float AverageLuminance(float3 color)
{
	return (color.r + color.g + color.b) / 3.0;
}

float dotToDegrees(float dot)
{
	dot = clamp(dot, -1.0, 1.0);
	return degrees(acos(dot));
}

// Convenience overload: compute dot for you
float dotToDegrees(float3 a, float3 b)
{
	return dotToDegrees(dot(normalize(a), normalize(b)));
}

// Fast approximation of arctangent function
// From Heitz et al. 2016, "Real-Time Polygonal-Light Shading with Linearly Transformed Cosines"
float poiFastAtan(float x)
{
	return x * (abs(x) * (1.5707963 * abs(x) - 0.00507668) + 0.420691) /
		   (abs(x) * (abs(x) * (0.633387806 + abs(x)) + 0.671041944) + 0.215192627);
}

// Set by VRChat (as of open beta 1245)
// _VRChatCameraMode: 0 => Normal, 1 => VR HandCam, 2 => Desktop Handcam, 3 => Screenshot/Photo
// _VRChatMirrorMode: 0 => Normal, 1 => Mirror (VR), 2 => Mirror (Deskie)
float _VRChatCameraMode;
float _VRChatMirrorMode;

// VRChat Time Variables
uint _VRChatTimeNetworkMs;
uint _VRChatTimeUTCUnixSeconds;

// Time Source: 0 = Unity Default, 1 = VRChat Network Time, 2 = VRChat UTC
// VRChat UTC updates too slowly for smooth animations, not used
// Falls back to _Time when VRChat variables are not populated (e.g. in Editor)
// d4rkpl4y3r suggests removing top 6 bits for uint -> fp precision 
float _PoiTimeSource;
static float4 POI_TIME = (_PoiTimeSource == 1 && _VRChatTimeNetworkMs != 0) ? (((_VRChatTimeNetworkMs << 6) >> 6) * float4(0.00005, 0.001, 0.002, 0.003))
			 			: _Time;

float VRCCameraMode()
{
	return _VRChatCameraMode;
}

float VRCMirrorMode()
{
	return _VRChatMirrorMode;
}

bool IsInMirror()
{
	return unity_CameraProjection[2][0] != 0.f || unity_CameraProjection[2][1] != 0.f;
}

bool IsOrthographicCamera()
{
	return unity_OrthoParams.w == 1 || UNITY_MATRIX_P[3][3] == 1;
}

float shEvaluateDiffuseL1Geomerics_local(float L0, float3 L1, float3 n)
{
	// average energy
	float R0 = max(0, L0);
	
	// avg direction of incoming light
	float3 R1 = 0.5f * L1;
	
	// directional brightness
	float lenR1 = length(R1);
	
	// linear angle between normal and direction 0-1
	//float q = 0.5f * (1.0f + dot(R1 / lenR1, n));
	//float q = dot(R1 / lenR1, n) * 0.5 + 0.5;
	float q = dot(normalize(R1), n) * 0.5 + 0.5;
	q = saturate(q); // Thanks to ScruffyRuffles for the bug identity.
	
	// power for q
	// lerps from 1 (linear) to 3 (cubic) based on directionality
	float p = 1.0f + 2.0f * lenR1 / R0;
	
	// dynamic range constant
	// should vary between 4 (highly directional) and 0 (ambient)
	float a = (1.0f - lenR1 / R0) / (1.0f + lenR1 / R0);
	
	return R0 * (a + (1.0f - a) * (p + 1.0f) * pow(q, p));
}

half3 BetterSH9(half4 normal)
{
	float3 indirect;
	float3 L0 = float3(PoiSHAr.w, PoiSHAg.w, PoiSHAb.w) + float3(PoiSHBr.z, PoiSHBg.z, PoiSHBb.z) / 3.0;
	indirect.r = shEvaluateDiffuseL1Geomerics_local(L0.r, PoiSHAr.xyz, normal.xyz);
	indirect.g = shEvaluateDiffuseL1Geomerics_local(L0.g, PoiSHAg.xyz, normal.xyz);
	indirect.b = shEvaluateDiffuseL1Geomerics_local(L0.b, PoiSHAb.xyz, normal.xyz);
	indirect = max(0, indirect);
	indirect += SHEvalLinearL2(normal);
	return indirect;
}

// Silent's code ends here

float3 BetterSH9(float3 normal)
{
	return BetterSH9(float4(normal, 1));
}

float3 getCameraForward()
{
	#if UNITY_SINGLE_PASS_STEREO
		float3 p1 = mul(unity_StereoCameraToWorld[0], float4(0, 0, 1, 1));
		float3 p2 = mul(unity_StereoCameraToWorld[0], float4(0, 0, 0, 1));
	#else
		float3 p1 = mul(unity_CameraToWorld, float4(0, 0, 1, 1)).xyz;
		float3 p2 = mul(unity_CameraToWorld, float4(0, 0, 0, 1)).xyz;
	#endif
	return normalize(p2 - p1);
}

half3 GetSHLength()
{
	half3 x, x1;
	x.r = length(PoiSHAr);
	x.g = length(PoiSHAg);
	x.b = length(PoiSHAb);
	x1.r = length(PoiSHBr);
	x1.g = length(PoiSHBg);
	x1.b = length(PoiSHBb);
	return x + x1;
}

float3 BoxProjection(float3 direction, float3 position, float4 cubemapPosition, float3 boxMin, float3 boxMax)
{
	#if UNITY_SPECCUBE_BOX_PROJECTION
		//UNITY_BRANCH
		if (cubemapPosition.w > 0)
		{
			float3 factors = ((direction > 0 ? boxMax : boxMin) - position) / direction;
			float scalar = min(min(factors.x, factors.y), factors.z);
			direction = direction * scalar + (position - cubemapPosition.xyz);
		}
	#endif
	return direction;
}

float poiMax(float2 i)
{
	return max(i.x, i.y);
}

float poiMax(float3 i)
{
	return max(max(i.x, i.y), i.z);
}

float poiMax(float4 i)
{
	return max(max(max(i.x, i.y), i.z), i.w);
}

float3 calculateNormal(in float3 baseNormal, in PoiMesh poiMesh, in Texture2D normalTexture, in SamplerState mainSampler, in float4 normal_ST, in float2 normalPan, in float normalUV, in float normalIntensity)
{
	float3 normal = PoiUnpackNormalScale(normalTexture.Sample(mainSampler, POI_PAN_UV(poiUV(poiMesh.uv[normalUV], normal_ST), normalPan)), normalIntensity);
	return normalize(
		normal.x * poiMesh.tangent[0] +
		normal.y * poiMesh.binormal[0] +
		normal.z * baseNormal
	);
}

float remap(float x, float minOld, float maxOld, float minNew = 0, float maxNew = 1)
{
	return minNew + (x - minOld) * (maxNew - minNew) / (maxOld - minOld);
}

float2 remap(float2 x, float2 minOld, float2 maxOld, float2 minNew = 0, float2 maxNew = 1)
{
	return minNew + (x - minOld) * (maxNew - minNew) / (maxOld - minOld);
}

float3 remap(float3 x, float3 minOld, float3 maxOld, float3 minNew = 0, float3 maxNew = 1)
{
	return minNew + (x - minOld) * (maxNew - minNew) / (maxOld - minOld);
}

float4 remap(float4 x, float4 minOld, float4 maxOld, float4 minNew = 0, float4 maxNew = 1)
{
	return minNew + (x - minOld) * (maxNew - minNew) / (maxOld - minOld);
}

float remapClamped(float minOld, float maxOld, float x, float minNew = 0, float maxNew = 1)
{
	return clamp(minNew + (x - minOld) * (maxNew - minNew) / (maxOld - minOld), minNew, maxNew);
}

float2 remapClamped(float2 minOld, float2 maxOld, float2 x, float2 minNew, float2 maxNew)
{
	return clamp(minNew + (x - minOld) * (maxNew - minNew) / (maxOld - minOld), minNew, maxNew);
}

float3 remapClamped(float3 minOld, float3 maxOld, float3 x, float3 minNew, float3 maxNew)
{
	return clamp(minNew + (x - minOld) * (maxNew - minNew) / (maxOld - minOld), minNew, maxNew);
}

float4 remapClamped(float4 minOld, float4 maxOld, float4 x, float4 minNew, float4 maxNew)
{
	return clamp(minNew + (x - minOld) * (maxNew - minNew) / (maxOld - minOld), minNew, maxNew);
}
float2 calcParallax(in float height, in PoiCam poiCam)
{
	return ((height * - 1) + 1) * (poiCam.tangentViewDir.xy / poiCam.tangentViewDir.z);
}


/*
0: Zero	                float4(0.0, 0.0, 0.0, 0.0),
1: One	                float4(1.0, 1.0, 1.0, 1.0),
2: DstColor	            destinationColor,
3: SrcColor	            sourceColor,
4: OneMinusDstColor	    float4(1.0, 1.0, 1.0, 1.0) - destinationColor,
5: SrcAlpha	            sourceColor.aaaa,
6: OneMinusSrcColor	    float4(1.0, 1.0, 1.0, 1.0) - sourceColor,
7: DstAlpha	            destinationColor.aaaa,
8: OneMinusDstAlpha	    float4(1.0, 1.0, 1.0, 1.0) - destinationColor.,
9: SrcAlphaSaturate     saturate(sourceColor.aaaa),
10: OneMinusSrcAlpha	float4(1.0, 1.0, 1.0, 1.0) - sourceColor.aaaa,
*/

float4 poiBlend(const float sourceFactor, const  float4 sourceColor, const  float destinationFactor, const  float4 destinationColor, const float4 blendFactor)
{
	float4 sA = 1 - blendFactor;
	const float4 blendData[11] = {
		float4(0.0, 0.0, 0.0, 0.0),
		float4(1.0, 1.0, 1.0, 1.0),
		destinationColor,
		sourceColor,
		float4(1.0, 1.0, 1.0, 1.0) - destinationColor,
		sA,
		float4(1.0, 1.0, 1.0, 1.0) - sourceColor,
		sA,
		float4(1.0, 1.0, 1.0, 1.0) - sA,
		saturate(sourceColor.aaaa),
		1 - sA,
	};
	
	return lerp(blendData[sourceFactor] * sourceColor + blendData[destinationFactor] * destinationColor, sourceColor, sA);
}

// Color burn
float blendColorBurn(float base, float blend)
{
	return (blend == 0.0) ? blend : max((1.0 - ((1.0 - base) * rcp(random_uniform_float_only_used_to_stop_compiler_warnings + blend))), 0.0);
}

float3 blendColorBurn(float3 base, float3 blend)
{
	return float3(blendColorBurn(base.r, blend.r), blendColorBurn(base.g, blend.g), blendColorBurn(base.b, blend.b));
}

// Color Dodge
float blendColorDodge(float base, float blend)
{
	return (blend == 1.0) ? blend : min(base / (1.0 - blend), 1.0);
}

float3 blendColorDodge(float3 base, float3 blend)
{
	return float3(blendColorDodge(base.r, blend.r), blendColorDodge(base.g, blend.g), blendColorDodge(base.b, blend.b));
}

// Darken
float blendDarken(float base, float blend)
{
	return min(blend, base);
}

float3 blendDarken(float3 base, float3 blend)
{
	return float3(blendDarken(base.r, blend.r), blendDarken(base.g, blend.g), blendDarken(base.b, blend.b));
}

// Overlay
float blendOverlay(float base, float blend)
{
	return base < 0.5 ? (2.0 * base * blend) : (1.0 - 2.0 * (1.0 - base) * (1.0 - blend));
}

float3 blendOverlay(float3 base, float3 blend)
{
	return float3(blendOverlay(base.r, blend.r), blendOverlay(base.g, blend.g), blendOverlay(base.b, blend.b));
}

// Lighten
float blendLighten(float base, float blend)
{
	return max(blend, base);
}

float3 blendLighten(float3 base, float3 blend)
{
	return float3(blendLighten(base.r, blend.r), blendLighten(base.g, blend.g), blendLighten(base.b, blend.b));
}

// Linear Dodge
float blendLinearDodge(float base, float blend)
{
	// Note : Same implementation as BlendAddf
	return min(base + blend, 1.0);
}

float3 blendLinearDodge(float3 base, float3 blend)
{
	// Note : Same implementation as BlendAdd
	return base + blend;
}
// Multiply
float blendMultiply(float base, float blend)
{
	return base * blend;
}
float3 blendMultiply(float3 base, float3 blend)
{
	return base * blend;
}
// Normal
float blendNormal(float base, float blend)
{
	return blend;
}
float3 blendNormal(float3 base, float3 blend)
{
	return blend;
}

// Screen
float blendScreen(float base, float blend)
{
	return 1.0 - ((1.0 - base) * (1.0 - blend));
}

float3 blendScreen(float3 base, float3 blend)
{
	return float3(blendScreen(base.r, blend.r), blendScreen(base.g, blend.g), blendScreen(base.b, blend.b));
}

// Subtract
float blendSubtract(float base, float blend)
{
	return max(base - blend, 0.0);
}

float3 blendSubtract(float3 base, float3 blend)
{
	return max(base - blend, 0.0);
}

float blendMixed(float base, float blend)
{
	return base + base * blend;
}

float3 blendMixed(float3 base, float3 blend)
{
	return base + base * blend;
}

float3 customBlend(float3 base, float3 blend, float blendType, float alpha = 1)
{
	// Compute all blend results
	float3 r0 = lerp(base, blend, alpha);                        // Normal (0)
	float3 r1 = lerp(base, blendDarken(base, blend), alpha);     // Darken (1)
	float3 r2 = base * lerp(1, blend, alpha);                    // Multiply (2)
	float3 r5 = lerp(base, blendLighten(base, blend), alpha);    // Lighten (5)
	float3 r6 = lerp(base, blendScreen(base, blend), alpha);     // Screen (6)
	float3 r7 = blendSubtract(base, blend * alpha);              // Subtract (7)
	float3 r8 = lerp(base, blendLinearDodge(base, blend), alpha);// Linear Dodge (8)
	float3 r9 = lerp(base, blendOverlay(base, blend), alpha);    // Overlay (9)
	float3 r20 = lerp(base, blendMixed(base, blend), alpha);     // Mixed (20)
	
	// Branchless selection
	float t = blendType;
	float w0 = saturate(1 - abs(t - 0));
	float w1 = saturate(1 - abs(t - 1));
	float w2 = saturate(1 - abs(t - 2));
	float w5 = saturate(1 - abs(t - 5));
	float w6 = saturate(1 - abs(t - 6));
	float w7 = saturate(1 - abs(t - 7));
	float w8 = saturate(1 - abs(t - 8));
	float w9 = saturate(1 - abs(t - 9));
	float w20 = saturate(1 - abs(t - 20));
	
	return r0*w0 + r1*w1 + r2*w2 + r5*w5 + r6*w6 + r7*w7 + r8*w8 + r9*w9 + r20*w20;
}

float customBlend(float base, float blend, float blendType, float alpha = 1)
{
	// Compute all blend results
	float r0 = lerp(base, blend, alpha);                        // Normal (0)
	float r2 = base * lerp(1, blend, alpha);                    // Multiply (2)
	float r5 = lerp(base, blendLighten(base, blend), alpha);    // Lighten (5)
	float r6 = lerp(base, blendScreen(base, blend), alpha);     // Screen (6)
	float r7 = blendSubtract(base, blend * alpha);              // Subtract (7)
	float r8 = lerp(base, blendLinearDodge(base, blend), alpha);// Linear Dodge (8)
	float r9 = lerp(base, blendOverlay(base, blend), alpha);    // Overlay (9)
	float r20 = lerp(base, blendMixed(base, blend), alpha);     // Mixed (20)
	
	// Branchless selection
	float t = blendType;
	float w0 = saturate(1 - abs(t - 0));
	float w2 = saturate(1 - abs(t - 2));
	float w5 = saturate(1 - abs(t - 5));
	float w6 = saturate(1 - abs(t - 6));
	float w7 = saturate(1 - abs(t - 7));
	float w8 = saturate(1 - abs(t - 8));
	float w9 = saturate(1 - abs(t - 9));
	float w20 = saturate(1 - abs(t - 20));
	
	return r0*w0 + r2*w2 + r5*w5 + r6*w6 + r7*w7 + r8*w8 + r9*w9 + r20*w20;
}

#define REPLACE 0
#define SUBSTRACT 1
#define MULTIPLY 2
#define DIVIDE 3
#define MIN 4
#define MAX 5
#define AVERAGE 6
#define ADD 7

float maskBlend(float baseMask, float blendMask, float blendType)
{
	// Compute all blend results (GPU parallelism)
	float replace = blendMask;
	float subtract = baseMask - blendMask;
	float multiply = baseMask * blendMask;
	float divide = baseMask / max(blendMask, 0.0001);
	float minVal = min(baseMask, blendMask);
	float maxVal = max(baseMask, blendMask);
	float average = (baseMask + blendMask) * 0.5;
	float add = baseMask + blendMask;
	
	// Branchless selection using step functions
	float t = blendType + 0.5;
	float w0 = step(t, 1);
	float w1 = step(1, t) * step(t, 2);
	float w2 = step(2, t) * step(t, 3);
	float w3 = step(3, t) * step(t, 4);
	float w4 = step(4, t) * step(t, 5);
	float w5 = step(5, t) * step(t, 6);
	float w6 = step(6, t) * step(t, 7);
	float w7 = step(7, t);
	
	float output = replace * w0 + subtract * w1 + multiply * w2 + divide * w3 + minVal * w4 + maxVal * w5 + average * w6 + add * w7;
	return saturate(output);
}

float globalMaskBlend(float baseMask, float globalMaskIndex, float blendType, PoiMods poiMods)
{
	if (globalMaskIndex == 0)
	{
		return baseMask;
	}
	else
	{
		return maskBlend(baseMask, poiMods.globalMask[globalMaskIndex - 1], blendType);
	}
}

// Standardized mask processing with channel blending, remapping, gamma, and inversion
float poiSampleMask(float4 sampledTexture, float4 channelStrengths, float blendMode, float invert, float2 remapMinMax, float gamma, float strength, float offset)
{
	// Start with R channel weighted by its strength
	float mask = sampledTexture.r * channelStrengths.x;

	// Sequentially blend G, B, A channels
	mask = lerp(mask, maskBlend(mask, sampledTexture.g, blendMode), channelStrengths.y);
	mask = lerp(mask, maskBlend(mask, sampledTexture.b, blendMode), channelStrengths.z);
	mask = lerp(mask, maskBlend(mask, sampledTexture.a, blendMode), channelStrengths.w);

	// Min/Max remapping (remap [min,max] to [0,1])
	mask = saturate((mask - remapMinMax.x) / max(remapMinMax.y - remapMinMax.x, 0.0001));

	// Gamma curve
	mask = pow(mask, gamma);

	// Strength and offset
	mask = saturate(mask * strength + offset);

	// Branchless inversion
	mask = lerp(mask, 1.0 - mask, invert);

	return mask;
}

// Single channel selection overload (backward compatible)
float poiSampleMask(float4 sampledTexture, float channel, float invert)
{
	float4 channelStrengths = float4(
		channel < 0.5 ? 1 : 0,
		channel >= 0.5 && channel < 1.5 ? 1 : 0,
		channel >= 1.5 && channel < 2.5 ? 1 : 0,
		channel >= 2.5 ? 1 : 0
	);
	return poiSampleMask(sampledTexture, channelStrengths, 0, invert, float2(0, 1), 1.0, 1.0, 0.0);
}

// Single channel with strength and offset overload
float poiSampleMask(float4 sampledTexture, float channel, float invert, float strength, float offset)
{
	float4 channelStrengths = float4(
		channel < 0.5 ? 1 : 0,
		channel >= 0.5 && channel < 1.5 ? 1 : 0,
		channel >= 1.5 && channel < 2.5 ? 1 : 0,
		channel >= 2.5 ? 1 : 0
	);
	return poiSampleMask(sampledTexture, channelStrengths, 0, invert, float2(0, 1), 1.0, strength, offset);
}

// Removed legacy random/random2/random3/randomFloat3/Range helpers after migration.
// Use poiRand/poiRand2/poiRand3/poiRand4/poiRand3Range instead.


// Generic Poiyomi RNG helpers (deterministic across stages)
inline float poiRand(float2 co)
{
	float3 p3 = frac(float3(co.xyx) * 0.1031);
	p3 += dot(p3, p3.yzx + 33.33);
	return frac((p3.x + p3.y) * p3.z);
}

inline float4 poiRand4(float2 seed)
{
	float3 p3 = frac(float3(seed.xyx) * 0.1031);
	p3 += dot(p3, p3.yzx + 33.33);
	float2 a = frac((p3.xx + p3.yz) * p3.zy);
	float2 s2 = seed + 37.0;
	float3 q3 = frac(float3(s2.xyx) * 0.1031);
	q3 += dot(q3, q3.yzx + 33.33);
	float2 b = frac((q3.xx + q3.yz) * q3.zy);
	return float4(a, b);
}

inline float2 poiRand2(float seed)
{
	float2 x = float2(seed, seed * 1.3);
	float3 p3 = frac(float3(x.xyx) * 0.1031);
	p3 += dot(p3, p3.yzx + 33.33);
	return frac((p3.xx + p3.yz) * p3.zy);
}

inline float2 poiRand2(float2 seed)
{
	float3 p3 = frac(float3(seed.xyx) * 0.1031);
	p3 += dot(p3, p3.yzx + 33.33);
	return frac((p3.xx + p3.yz) * p3.zy);
}

inline float poiRand3(float seed)
{
	float p = frac(seed * 0.1031);
	p *= p + 33.33;
	p *= p + p;
	return frac(p);
}

inline float3 poiRand3(float2 seed)
{
	float3 p3 = frac(float3(seed.xyx) * 0.1031);
	p3 += dot(p3, p3.yzx + 33.33);
	return frac((p3.xxy + p3.yzz) * p3.zyx);
}

inline float3 poiRand3(float3 seed)
{
	float3 p3 = frac(seed * 0.1031);
	p3 += dot(p3, p3.zyx + 31.32);
	return frac((p3.xxy + p3.yzz) * p3.zyx);
}

inline float3 poiRand3Range(float2 Seed, float Range)
{
	float3 r = poiRand3(Seed);
	return (r * 2.0 - 1.0) * Range;
}



// Deprecated: use poiRand3Range instead of randomFloat3Range
float3 randomFloat3WiggleRange(float2 Seed, float Range, float wiggleSpeed, float timeOffset)
{
	float3 rando = (float3(
		frac(sin(dot(Seed.xy, float2(12.9898, 78.233))) * 43758.5453),
		frac(sin(dot(Seed.yx, float2(12.9898, 78.233))) * 43758.5453),
		frac(sin(dot(float2(Seed.x * Seed.y, Seed.y + Seed.x), float2(12.9898, 78.233))) * 43758.5453)
	) * 2 - 1);
	float speed = 1 + wiggleSpeed;
	return float3(sin(((POI_TIME.x + timeOffset) + rando.x * UNITY_PI) * speed), sin(((POI_TIME.x + timeOffset) + rando.y * UNITY_PI) * speed), sin(((POI_TIME.x + timeOffset) + rando.z * UNITY_PI) * speed)) * Range;
}


// The weights of RGB contributions to luminance.
// Should sum to unity.
static const float3 HCYwts = float3(0.299, 0.587, 0.114);
static const float HCLgamma = 3;
static const float HCLy0 = 100;
static const float HCLmaxL = 0.530454533953517; // == exp(HCLgamma / HCLy0) - 0.5
static const float3 wref = float3(1.0, 1.0, 1.0);
#define TAU 6.28318531

float3 HUEtoRGB(in float H)
{
	float R = abs(H * 6 - 3) - 1;
	float G = 2 - abs(H * 6 - 2);
	float B = 2 - abs(H * 6 - 4);
	return saturate(float3(R, G, B));
}

float3 RGBtoHCV(in float3 RGB)
{
	// Based on work by Sam Hocevar and Emil Persson
	float4 P = (RGB.g < RGB.b) ? float4(RGB.bg, -1.0, 2.0 / 3.0) : float4(RGB.gb, 0.0, -1.0 / 3.0);
	float4 Q = (RGB.r < P.x) ? float4(P.xyw, RGB.r) : float4(RGB.r, P.yzx);
	float C = Q.x - min(Q.w, Q.y);
	float H = abs((Q.w - Q.y) / (6 * C + EPSILON) + Q.z);
	return float3(H, C, Q.x);
}

float3 RGBtoHSV(float3 c)
{
	float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
	float4 p = lerp(float4(c.bg, K.wz), float4(c.gb, K.xy), step(c.b, c.g));
	float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r));

	float d = q.x - min(q.w, q.y);
	float e = 1.0e-10;
	return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

float3 HSVtoRGB(float3 c)
{
	float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
	float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
	return c.z * lerp(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

void DecomposeHDRColor(in float3 linearColorHDR, out float3 baseLinearColor, out float exposure)
{
	// Optimization/adaptation of https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/GUI/ColorMutator.cs#L23 but skips weird photoshop stuff
	float maxColorComponent = max(linearColorHDR.r, max(linearColorHDR.g, linearColorHDR.b));
	bool isSDR = maxColorComponent <= 1.0;

	float scaleFactor = isSDR ? 1.0 : (1.0 / maxColorComponent);
	exposure = isSDR ? 0.0 : log(maxColorComponent) * 1.44269504089; // ln(2)
	
	baseLinearColor = scaleFactor * linearColorHDR;
}

float3 ApplyHDRExposure(float3 linearColor, float exposure)
{
	return linearColor * pow(2, exposure);
}

// Transforms an RGB color using a matrix. Note that S and V are absolute values here
float3 ModifyViaHSV(float3 color, float h, float s, float v)
{
	float3 colorHSV = RGBtoHSV(color);
	colorHSV.x = frac(colorHSV.x + h);
	colorHSV.y = saturate(colorHSV.y + s);
	colorHSV.z = saturate(colorHSV.z + v);
	return HSVtoRGB(colorHSV);
}

float3 ModifyViaHSV(float3 color, float3 HSVMod)
{
	return ModifyViaHSV(color, HSVMod.x, HSVMod.y, HSVMod.z);
}

float4x4 brightnessMatrix(float brightness)
{
	return float4x4(
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		brightness, brightness, brightness, 1
	);
}

float4x4 contrastMatrix(float contrast)
{
	float t = (1.0 - contrast) / 2.0;
	
	return float4x4(
		contrast, 0, 0, 0,
		0, contrast, 0, 0,
		0, 0, contrast, 0,
		t, t, t, 1
	);
}

float4x4 saturationMatrix(float saturation)
{
	float3 luminance = float3(0.3086, 0.6094, 0.0820);
	
	float oneMinusSat = 1.0 - saturation;
	
	float3 red = luminance.x * oneMinusSat;
	red += float3(saturation, 0, 0);
	
	float3 green = luminance.y * oneMinusSat;
	green += float3(0, saturation, 0);
	
	float3 blue = luminance.z * oneMinusSat;
	blue += float3(0, 0, saturation);
	
	return float4x4(
		red, 0,
		green, 0,
		blue, 0,
		0, 0, 0, 1
	);
}

float4 PoiColorBCS(float4 color, float brightness, float contrast, float saturation)
{
	return mul(color, mul(brightnessMatrix(brightness), mul(contrastMatrix(contrast), saturationMatrix(saturation))));
}
float3 PoiColorBCS(float3 color, float brightness, float contrast, float saturation)
{
	return mul(float4(color, 1), mul(brightnessMatrix(brightness), mul(contrastMatrix(contrast), saturationMatrix(saturation)))).rgb;
}


float3 linear_srgb_to_oklab(float3 c)
{
	float l = 0.4122214708 * c.x + 0.5363325363 * c.y + 0.0514459929 * c.z;
	float m = 0.2119034982 * c.x + 0.6806995451 * c.y + 0.1073969566 * c.z;
	float s = 0.0883024619 * c.x + 0.2817188376 * c.y + 0.6299787005 * c.z;

	float l_ = pow(l, 1.0 / 3.0);
	float m_ = pow(m, 1.0 / 3.0);
	float s_ = pow(s, 1.0 / 3.0);

	return float3(
		0.2104542553 * l_ + 0.7936177850 * m_ - 0.0040720468 * s_,
		1.9779984951 * l_ - 2.4285922050 * m_ + 0.4505937099 * s_,
		0.0259040371 * l_ + 0.7827717662 * m_ - 0.8086757660 * s_
	);
}

float3 oklab_to_linear_srgb(float3 c)
{
	float l_ = c.x + 0.3963377774 * c.y + 0.2158037573 * c.z;
	float m_ = c.x - 0.1055613458 * c.y - 0.0638541728 * c.z;
	float s_ = c.x - 0.0894841775 * c.y - 1.2914855480 * c.z;

	float l = l_ * l_ * l_;
	float m = m_ * m_ * m_;
	float s = s_ * s_ * s_;

	return float3(
		+ 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
		- 1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
		- 0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s
	);
}

float3 hueShiftOKLab(float3 color, float shift, float selectOrShift)
{
	float3 oklab = linear_srgb_to_oklab(color);
	float chroma = length(oklab.yz);
	
	oklab.y = selectOrShift > 0.5 ? oklab.y : chroma;
	oklab.z = selectOrShift > 0.5 ? oklab.z : 0;
	float s, c;
	sincos(shift * UNITY_TWO_PI, s, c);
	oklab.yz = float2(c * oklab.y - s * oklab.z, s * oklab.y + c * oklab.z);
	
	return oklab_to_linear_srgb(oklab);
}

float3 hueShiftHSV(float3 color, float hueOffset, float selectOrShift)
{
	float3 hsvCol = RGBtoHSV(color);
	hsvCol.x = hsvCol.x * selectOrShift + hueOffset;
	return HSVtoRGB(hsvCol);
}

float3 hueShift(float3 color, float shift, float ColorSpace, float selectOrShift)
{
	float3 oklab = hueShiftOKLab(color, shift, selectOrShift);
	float3 hsv = hueShiftHSV(color, shift, selectOrShift);
	float w = saturate(ColorSpace);
	return lerp(oklab, hsv, w);
}

float4 hueShift(float4 color, float shift, float ColorSpace, float selectOrShift)
{
	return float4(hueShift(color.rgb, shift, ColorSpace, selectOrShift), color.a);
}


float4x4 poiRotationMatrixFromAngles(float x, float y, float z)
{
	float angleX = radians(x);
	float c = cos(angleX);
	float s = sin(angleX);
	float4x4 rotateXMatrix = float4x4(1, 0, 0, 0,
	0, c, -s, 0,
	0, s, c, 0,
	0, 0, 0, 1);
	
	float angleY = radians(y);
	c = cos(angleY);
	s = sin(angleY);
	float4x4 rotateYMatrix = float4x4(c, 0, s, 0,
	0, 1, 0, 0,
	- s, 0, c, 0,
	0, 0, 0, 1);
	
	float angleZ = radians(z);
	c = cos(angleZ);
	s = sin(angleZ);
	float4x4 rotateZMatrix = float4x4(c, -s, 0, 0,
	s, c, 0, 0,
	0, 0, 1, 0,
	0, 0, 0, 1);
	
	return mul(mul(rotateXMatrix, rotateYMatrix), rotateZMatrix);
}

float4x4 poiRotationMatrixFromAngles(float3 angles)
{
	float angleX = radians(angles.x);
	float c = cos(angleX);
	float s = sin(angleX);
	float4x4 rotateXMatrix = float4x4(1, 0, 0, 0,
	0, c, -s, 0,
	0, s, c, 0,
	0, 0, 0, 1);
	
	float angleY = radians(angles.y);
	c = cos(angleY);
	s = sin(angleY);
	float4x4 rotateYMatrix = float4x4(c, 0, s, 0,
	0, 1, 0, 0,
	- s, 0, c, 0,
	0, 0, 0, 1);
	
	float angleZ = radians(angles.z);
	c = cos(angleZ);
	s = sin(angleZ);
	float4x4 rotateZMatrix = float4x4(c, -s, 0, 0,
	s, c, 0, 0,
	0, 0, 1, 0,
	0, 0, 0, 1);
	
	return mul(mul(rotateXMatrix, rotateYMatrix), rotateZMatrix);
}

float3 _VRChatMirrorCameraPos;
float3 getCameraPosition(bool useCenterEye = true)
{
	float3 camPosWS = _VRChatMirrorMode == 1 ? _VRChatMirrorCameraPos : _WorldSpaceCameraPos;
	#ifdef USING_STEREO_MATRICES
		float3 centerEyePos = unity_StereoWorldSpaceCameraPos[0] * .5 + unity_StereoWorldSpaceCameraPos[1] * .5;
		camPosWS = (useCenterEye) ? centerEyePos : camPosWS;
	#endif
	return camPosWS;
}

float2 calcPixelScreenUVs(half4 grabPos)
{
	half2 uv = grabPos.xy / (grabPos.w + 0.0000000001);
	#if UNITY_SINGLE_PASS_STEREO
		uv.xy *= half2(_ScreenParams.x * 2, _ScreenParams.y);
	#else
		uv.xy *= _ScreenParams.xy;
	#endif
	
	return uv;
}

float CalcMipLevel(float2 texture_coord)
{
	float2 dx = ddx(texture_coord);
	float2 dy = ddy(texture_coord);
	float delta_max_sqr = max(dot(dx, dx), dot(dy, dy));
	
	return 0.5 * log2(delta_max_sqr);
}

float inverseLerp(float A, float B, float T)
{
	return (T - A) / (B - A);
}

float inverseLerp2(float2 a, float2 b, float2 value)
{
	float2 AB = b - a;
	float2 AV = value - a;
	return dot(AV, AB) / dot(AB, AB);
}

float inverseLerp3(float3 a, float3 b, float3 value)
{
	float3 AB = b - a;
	float3 AV = value - a;
	return dot(AV, AB) / dot(AB, AB);
}

float inverseLerp4(float4 a, float4 b, float4 value)
{
	float4 AB = b - a;
	float4 AV = value - a;
	return dot(AV, AB) / dot(AB, AB);
}

float4 QuaternionFromMatrix(
	float m00, float m01, float m02,
	float m10, float m11, float m12,
	float m20, float m21, float m22)
{
	float4 q;
	float trace = m00 + m11 + m22;
	if (trace > 0)
	{
		float s = sqrt(trace + 1) * 2;
		q.w = 0.25 * s;
		q.x = (m21 - m12) / s;
		q.y = (m02 - m20) / s;
		q.z = (m10 - m01) / s;
	}
	else if (m00 > m11 && m00 > m22)
	{
		float s = sqrt(1 + m00 - m11 - m22) * 2;
		q.w = (m21 - m12) / s;
		q.x = 0.25 * s;
		q.y = (m01 + m10) / s;
		q.z = (m02 + m20) / s;
	}
	else if (m11 > m22)
	{
		float s = sqrt(1 + m11 - m00 - m22) * 2;
		q.w = (m02 - m20) / s;
		q.x = (m01 + m10) / s;
		q.y = 0.25 * s;
		q.z = (m12 + m21) / s;
	}
	else
	{
		float s = sqrt(1 + m22 - m00 - m11) * 2;
		q.w = (m10 - m01) / s;
		q.x = (m02 + m20) / s;
		q.y = (m12 + m21) / s;
		q.z = 0.25 * s;
	}
	return q;
}

float4 MulQuat(float4 a, float4 b)
{
	return float4(
		a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
		a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
		a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
		a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
	);
}

float4 QuaternionFromBasis(float3 sx, float3 sy, float3 sz)
{
	return QuaternionFromMatrix(
		sx.x, sy.x, sz.x,
		sx.y, sy.y, sz.y,
		sx.z, sy.z, sz.z
	);
}

float4 BuildQuatFromForwardUp(float3 forward, float3 up)
{
	float3 f = normalize(forward);
	float3 u = normalize(up);
	float3 x = normalize(cross(u, f));
	float3 y = cross(f, x);
	return QuaternionFromBasis(x, y, f);
}

float3 QuaternionToEuler(float4 q)
{
	float3 euler;
	
	float sinr_cosp = 2 * (q.w * q.z + q.x * q.y);
	float cosr_cosp = 1 - 2 * (q.z * q.z + q.x * q.x);
	euler.z = atan2(sinr_cosp, cosr_cosp) * 57.2958;
	
	float sinp = 2 * (q.w * q.x - q.y * q.z);
	if (abs(sinp) >= 1)
		euler.x = (sinp >= 0 ? 1 : - 1) * 90;
	else
		euler.x = asin(sinp) * 57.2958;
	
	float siny_cosp = 2 * (q.w * q.y + q.z * q.x);
	float cosy_cosp = 1 - 2 * (q.x * q.x + q.y * q.y);
	euler.y = atan2(siny_cosp, cosy_cosp) * 57.2958;
	
	return euler;
}

float4 EulerToQuaternion(float3 euler)
{
	float3 eulerRad = euler * 0.0174533;
	
	float cx = cos(eulerRad.x * 0.5);
	float sx = sin(eulerRad.x * 0.5);
	float cy = cos(eulerRad.y * 0.5);
	float sy = sin(eulerRad.y * 0.5);
	float cz = cos(eulerRad.z * 0.5);
	float sz = sin(eulerRad.z * 0.5);
	
	float4 q;
	q.w = cx * cy * cz + sx * sy * sz;
	q.x = sx * cy * cz - cx * sy * sz;
	q.y = cx * sy * cz + sx * cy * sz;
	q.z = cx * cy * sz - sx * sy * cz;
	
	return q;
}

/*
MIT License

Copyright (c) 2019 wraikny

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

VertexTransformShader is dependent on:
*/

float4 quaternion_conjugate(float4 v)
{
	return float4(
		v.x, -v.yzw
	);
}

float4 quaternion_mul(float4 v1, float4 v2)
{
	float4 result1 = (v1.x * v2 + v1 * v2.x);
	
	float4 result2 = float4(
		- dot(v1.yzw, v2.yzw),
		cross(v1.yzw, v2.yzw)
	);
	
	return float4(result1 + result2);
}

// angle : radians
float4 get_quaternion_from_angle(float3 axis, float angle)
{
	float sn = sin(angle * 0.5);
	float cs = cos(angle * 0.5);
	return float4(axis * sn, cs);
}

float4 quaternion_from_vector(float3 inVec)
{
	return float4(0.0, inVec);
}

float degree_to_radius(float degree)
{
	return (
		degree / 180.0 * UNITY_PI
	);
}

float3 rotate_with_quaternion(float3 inVec, float3 rotation)
{
	float4 qx = get_quaternion_from_angle(float3(1, 0, 0), radians(rotation.x));
	float4 qy = get_quaternion_from_angle(float3(0, 1, 0), radians(rotation.y));
	float4 qz = get_quaternion_from_angle(float3(0, 0, 1), radians(rotation.z));
	
	#define MUL3(A, B, C) quaternion_mul(quaternion_mul((A), (B)), (C))
	float4 quaternion = normalize(MUL3(qx, qy, qz));
	float4 conjugate = quaternion_conjugate(quaternion);
	
	float4 inVecQ = quaternion_from_vector(inVec);
	
	float3 rotated = (
		MUL3(quaternion, inVecQ, conjugate)
	).yzw;
	
	return rotated;
}

float3 RotateByQuaternion(float4 q, float3 v)
{
	float3 u = q.xyz;
	float s = q.w;
	return 2.0 * dot(u, v) * u
	+ (s * s - dot(u, u)) * v
	+ 2.0 * s * cross(u, v);
}




float4 SlerpQuaternion(float4 qa, float4 qb, float t)
{
	float cosHalfTheta = dot(qa, qb);
	// take shortest path
	if (cosHalfTheta < 0.0)
	{
		qb = -qb;
		cosHalfTheta = -cosHalfTheta;
	}
	if (cosHalfTheta > 0.9995)
	{
		// almost parallel — use lerp
		float4 qr = normalize(qa * (1 - t) + qb * t);
		return qr;
	}
	float halfTheta = acos(cosHalfTheta);
	float sinHalfTheta = sqrt(1.0 - cosHalfTheta * cosHalfTheta);
	float a = sin((1 - t) * halfTheta) / sinHalfTheta;
	float b = sin(t * halfTheta) / sinHalfTheta;
	return qa * a + qb * b;
}

float4 transform(float4 input, float4 pos, float4 rotation, float4 scale)
{
	input.rgb *= (scale.xyz * scale.w);
	input = float4(rotate_with_quaternion(input.xyz, rotation.xyz * rotation.w) + (pos.xyz * pos.w), input.w);
	return input;
}
/*
MIT END
*/

float2 RotateUV(float2 _uv, float _radian, float2 _piv, float _time)
{
	float RotateUV_ang = _radian;
	float RotateUV_cos = cos(_time * RotateUV_ang);
	float RotateUV_sin = sin(_time * RotateUV_ang);
	return (mul(_uv - _piv, float2x2(RotateUV_cos, -RotateUV_sin, RotateUV_sin, RotateUV_cos)) + _piv);
}

float3 RotateAroundAxis(float3 original, float3 axis, float radian)
{
	float s = sin(radian);
	float c = cos(radian);
	float one_minus_c = 1.0 - c;

	axis = normalize(axis);
	float3x3 rot_mat = {
		one_minus_c * axis.x * axis.x + c, one_minus_c * axis.x * axis.y - axis.z * s, one_minus_c * axis.z * axis.x + axis.y * s,
		one_minus_c * axis.x * axis.y + axis.z * s, one_minus_c * axis.y * axis.y + c, one_minus_c * axis.y * axis.z - axis.x * s,
		one_minus_c * axis.z * axis.x - axis.y * s, one_minus_c * axis.y * axis.z + axis.x * s, one_minus_c * axis.z * axis.z + c
	};
	return mul(rot_mat, original);
}

float3 lilToneCorrection(float3 c, float4 hsvg)
{
	// gamma
	c = pow(abs(c), hsvg.w);
	// rgb - > hsv
	float4 p = (c.b > c.g) ? float4(c.bg, -1.0, 2.0 / 3.0) : float4(c.gb, 0.0, -1.0 / 3.0);
	float4 q = (p.x > c.r) ? float4(p.xyw, c.r) : float4(c.r, p.yzx);
	float d = q.x - min(q.w, q.y);
	float e = 1.0e-10;
	float3 hsv = float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
	// shift
	hsv = float3(hsv.x + hsvg.x, saturate(hsv.y * hsvg.y), saturate(hsv.z * hsvg.z));
	// hsv - > rgb
	return hsv.z - hsv.z * hsv.y + hsv.z * hsv.y * saturate(abs(frac(hsv.x + float3(1.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - 3.0) - 1.0);
}

float3 lilBlendColor(float3 dstCol, float3 srcCol, float3 srcA, int blendMode)
{
	float3 ad = dstCol + srcCol;
	float3 mu = dstCol * srcCol;
	float3 outCol = float3(0, 0, 0);
	if (blendMode == 0) outCol = srcCol; // Normal
	if (blendMode == 1) outCol = ad; // Add
	if (blendMode == 2) outCol = max(ad - mu, dstCol); // Screen
	if (blendMode == 3) outCol = mu; // Multiply
	return lerp(dstCol, outCol, srcA);
}

float lilIsIn0to1(float f)
{
	float value = 0.5 - abs(f - 0.5);
	return saturate(value / clamp(fwidth(value), 0.0001, 1.0));
}

float lilIsIn0to1(float f, float nv)
{
	float value = 0.5 - abs(f - 0.5);
	return saturate(value / clamp(fwidth(value), 0.0001, nv));
}

float poiEdgeLinearNoSaturate(float value, float border)
{
	return (value - border) / clamp(fwidth(value), 0.0001, 1.0);
}

float3 poiEdgeLinearNoSaturate(float value, float3 border)
{
	return float3(
		(value - border.x) / clamp(fwidth(value), 0.0001, 1.0),
		(value - border.y) / clamp(fwidth(value), 0.0001, 1.0),
		(value - border.z) / clamp(fwidth(value), 0.0001, 1.0)
	);
}

float poiEdgeLinearNoSaturate(float value, float border, float blur)
{
	float borderMin = saturate(border - blur * 0.5);
	float borderMax = saturate(border + blur * 0.5);
	return (value - borderMin) / max(saturate(borderMax - borderMin + fwidth(value)), .0001);
}

float poiEdgeLinearNoSaturate(float value, float border, float blur, float borderRange)
{
	float borderMin = saturate(border - blur * 0.5 - borderRange);
	float borderMax = saturate(border + blur * 0.5);
	return (value - borderMin) / max(saturate(borderMax - borderMin + fwidth(value)), .0001);
}

float poiEdgeNonLinearNoSaturate(float value, float border)
{
	// return (value - border) / clamp(fwidth(value), 0.0001, 1.0);

	float fwidthValue = fwidth(value);
	return smoothstep(border - fwidthValue, border + fwidthValue, value);
}

float poiEdgeNonLinearNoSaturate(float value, float border, float blur)
{
	float fwidthValue = fwidth(value);
	float borderMin = saturate(border - blur * 0.5);
	float borderMax = saturate(border + blur * 0.5);
	return smoothstep(borderMin - fwidthValue, borderMax + fwidthValue, value);
}

float poiEdgeNonLinearNoSaturate(float value, float border, float blur, float borderRange)
{
	float fwidthValue = fwidth(value);
	float borderMin = saturate(border - blur * 0.5 - borderRange);
	float borderMax = saturate(border + blur * 0.5);
	return smoothstep(borderMin - fwidthValue, borderMax + fwidthValue, value);
}

float poiEdgeNonLinear(float value, float border)
{
	return saturate(poiEdgeNonLinearNoSaturate(value, border));
}

float poiEdgeNonLinear(float value, float border, float blur)
{
	return saturate(poiEdgeNonLinearNoSaturate(value, border, blur));
}

float poiEdgeNonLinear(float value, float border, float blur, float borderRange)
{
	return saturate(poiEdgeNonLinearNoSaturate(value, border, blur, borderRange));
}

float poiEdgeLinear(float value, float border)
{
	return saturate(poiEdgeLinearNoSaturate(value, border));
}

float poiEdgeLinear(float value, float border, float blur)
{
	return saturate(poiEdgeLinearNoSaturate(value, border, blur));
}

float poiEdgeLinear(float value, float border, float blur, float borderRange)
{
	return saturate(poiEdgeLinearNoSaturate(value, border, blur, borderRange));
}
// From https : // github.com / lilxyzw / OpenLit / blob / main / Assets / OpenLit / core.hlsl
float3 OpenLitLinearToSRGB(float3 col)
{
	return LinearToGammaSpace(col);
}

float3 OpenLitSRGBToLinear(float3 col)
{
	return GammaToLinearSpace(col);
}

float OpenLitLuminance(float3 rgb)
{
	#if defined(UNITY_COLORSPACE_GAMMA)
		return dot(rgb, float3(0.22, 0.707, 0.071));
	#else
		return dot(rgb, float3(0.0396819152, 0.458021790, 0.00609653955));
	#endif
}

float3 AdjustLitLuminance(float3 rgb, float targetLuminance)
{
	float currentLuminance;
	#if defined(UNITY_COLORSPACE_GAMMA)
		currentLuminance = dot(rgb, float3(0.22, 0.707, 0.071));
	#else
		currentLuminance = dot(rgb, float3(0.0396819152, 0.458021790, 0.00609653955));
	#endif

	float luminanceRatio = targetLuminance / currentLuminance;
	return rgb * luminanceRatio;
}

float3 ClampLuminance(float3 rgb, float minLuminance, float maxLuminance)
{
	float currentLuminance = dot(rgb, float3(0.299, 0.587, 0.114));
	float minRatio = (currentLuminance != 0) ? minLuminance / currentLuminance : 1.0;
	float maxRatio = (currentLuminance != 0) ? maxLuminance / currentLuminance : 1.0;
	float luminanceRatio = clamp(min(maxRatio, max(minRatio, 1.0)), 0.0, 1.0);
	return lerp(rgb, rgb * luminanceRatio, luminanceRatio < 1.0);
}

float3 MaxLuminance(float3 rgb, float maxLuminance)
{
	float currentLuminance = dot(rgb, float3(0.299, 0.587, 0.114));
	float luminanceRatio = (currentLuminance != 0) ? maxLuminance / max(currentLuminance, 0.00001) : 1.0;
	return lerp(rgb, rgb * luminanceRatio, currentLuminance > maxLuminance);
}

float OpenLitGray(float3 rgb)
{
	return dot(rgb, float3(1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0));
}

void OpenLitShadeSH9ToonDouble(float3 lightDirection, out float3 shMax, out float3 shMin)
{
	#if !defined(LIGHTMAP_ON)
		float3 N = lightDirection * 0.666666;
		float4 vB = N.xyzz * N.yzzx;
		// L0 L2
		float3 res = float3(PoiSHAr.w, PoiSHAg.w, PoiSHAb.w);
		res.r += dot(PoiSHBr, vB);
		res.g += dot(PoiSHBg, vB);
		res.b += dot(PoiSHBb, vB);
		res += PoiSHC.rgb * (N.x * N.x - N.y * N.y);
		// L1
		float3 l1;
		l1.r = dot(PoiSHAr.rgb, N);
		l1.g = dot(PoiSHAg.rgb, N);
		l1.b = dot(PoiSHAb.rgb, N);
		shMax = res + l1;
		shMin = res - l1;
		#if defined(UNITY_COLORSPACE_GAMMA)
			shMax = OpenLitLinearToSRGB(shMax);
			shMin = OpenLitLinearToSRGB(shMin);
		#endif
	#else
		shMax = 0.0;
		shMin = 0.0;
	#endif
}

float3 OpenLitComputeCustomLightDirection(float4 lightDirectionOverride)
{
	float3 customDir = length(lightDirectionOverride.xyz) * normalize(mul((float3x3)unity_ObjectToWorld, lightDirectionOverride.xyz));
	return lightDirectionOverride.w ? customDir : lightDirectionOverride.xyz; // .w isn't doc'd anywhere and is always 0 unless end user changes it

}

float3 OpenLitLightingDirectionForSH9(float3 mainDir)
{
	#if !defined(LIGHTMAP_ON)
		float3 sh9Dir = PoiSHAr.xyz * 0.333333 + PoiSHAg.xyz * 0.333333 + PoiSHAb.xyz * 0.333333;
		float3 sh9DirAbs = float3(sh9Dir.x, abs(sh9Dir.y), sh9Dir.z);
	#else
		float3 sh9Dir = 0;
		float3 sh9DirAbs = 0;
	#endif

	float3 lightDirectionForSH9 = sh9Dir + mainDir;
	lightDirectionForSH9 = dot(lightDirectionForSH9, lightDirectionForSH9) < 0.000001 ? 0 : normalize(lightDirectionForSH9);
	return lightDirectionForSH9;
}

float3 OpenLitLightingDirectionForSH9(Light light)
{
	float3 mainDir = light.direction * OpenLitLuminance(light.color);
	return OpenLitLightingDirectionForSH9(mainDir);
}

float3 OpenLitLightingDirection(float4 lightDirectionOverride, float3 mainDir)
{
	#if !defined(LIGHTMAP_ON) && UNITY_SHOULD_SAMPLE_SH
		float3 sh9Dir = PoiSHAr.xyz * 0.333333 + PoiSHAg.xyz * 0.333333 + PoiSHAb.xyz * 0.333333;
		float3 sh9DirAbs = float3(sh9Dir.x, abs(sh9Dir.y), sh9Dir.z);
	#else
		float3 sh9Dir = 0;
		float3 sh9DirAbs = 0;
	#endif
	float3 customDir = OpenLitComputeCustomLightDirection(lightDirectionOverride);

	return normalize(sh9DirAbs + mainDir + customDir);
}

float3 OpenLitLightingDirection(float4 lightDirectionOverride, Light light)
{
	float3 mainDir = light.direction.xyz * OpenLitLuminance(light.color);
	return OpenLitLightingDirection(lightDirectionOverride, mainDir);
}

float3 OpenLitLightingDirection(Light light)
{
	float4 customDir = float4(0.001, 0.002, 0.001, 0.0);
	return OpenLitLightingDirection(customDir, light);
}

inline float4 CalculateFrustumCorrection()
{
	float x1 = -UNITY_MATRIX_P._31 / (UNITY_MATRIX_P._11 * UNITY_MATRIX_P._34);
	float x2 = -UNITY_MATRIX_P._32 / (UNITY_MATRIX_P._22 * UNITY_MATRIX_P._34);
	return float4(x1, x2, 0, UNITY_MATRIX_P._33 / UNITY_MATRIX_P._34 + x1 * UNITY_MATRIX_P._13 + x2 * UNITY_MATRIX_P._23);
}

inline float CorrectedLinearEyeDepth(float z, float correctionFactor)
{
	return 1.f / (z / UNITY_MATRIX_P._34 + correctionFactor);
}

inline float GetFOVScale()
{
	return abs(UNITY_MATRIX_P._m11) * 0.5;
}

inline float GetAspectRatio()
{
	return _ScreenParams.y / _ScreenParams.x;
}

inline float WorldToScreenScale(float worldRadius, float distance)
{
	return (worldRadius / max(distance, 0.001)) * GetFOVScale();
}

inline float2 GetScreenAspectCorrection()
{
	return float2(_ScreenParams.y / _ScreenParams.x, 1.0);
}

float evalRamp4(float time, float4 ramp)
{
	return lerp(ramp.x, ramp.y, smoothstep(ramp.z, ramp.w, time));
}

// Silent's code
float2 sharpSample(float4 texelSize, float2 p)
{
	p = p * texelSize.zw;
	float2 c = max(0.0, fwidth(p));
	p = floor(p) + saturate(frac(p) / c);
	p = (p - 0.5) * texelSize.xy;
	return p;
}


void applyToGlobalMask(inout PoiMods poiMods, int index, int blendType, float val)
{
	// Compute blended values for all 16 masks
	float v0 = saturate(maskBlend(poiMods.globalMask[0], val, blendType));
	float v1 = saturate(maskBlend(poiMods.globalMask[1], val, blendType));
	float v2 = saturate(maskBlend(poiMods.globalMask[2], val, blendType));
	float v3 = saturate(maskBlend(poiMods.globalMask[3], val, blendType));
	float v4 = saturate(maskBlend(poiMods.globalMask[4], val, blendType));
	float v5 = saturate(maskBlend(poiMods.globalMask[5], val, blendType));
	float v6 = saturate(maskBlend(poiMods.globalMask[6], val, blendType));
	float v7 = saturate(maskBlend(poiMods.globalMask[7], val, blendType));
	float v8 = saturate(maskBlend(poiMods.globalMask[8], val, blendType));
	float v9 = saturate(maskBlend(poiMods.globalMask[9], val, blendType));
	float v10 = saturate(maskBlend(poiMods.globalMask[10], val, blendType));
	float v11 = saturate(maskBlend(poiMods.globalMask[11], val, blendType));
	float v12 = saturate(maskBlend(poiMods.globalMask[12], val, blendType));
	float v13 = saturate(maskBlend(poiMods.globalMask[13], val, blendType));
	float v14 = saturate(maskBlend(poiMods.globalMask[14], val, blendType));
	float v15 = saturate(maskBlend(poiMods.globalMask[15], val, blendType));
	
	// Branchless weights
	float i = index;
	float w0 = saturate(1 - abs(i - 0));
	float w1 = saturate(1 - abs(i - 1));
	float w2 = saturate(1 - abs(i - 2));
	float w3 = saturate(1 - abs(i - 3));
	float w4 = saturate(1 - abs(i - 4));
	float w5 = saturate(1 - abs(i - 5));
	float w6 = saturate(1 - abs(i - 6));
	float w7 = saturate(1 - abs(i - 7));
	float w8 = saturate(1 - abs(i - 8));
	float w9 = saturate(1 - abs(i - 9));
	float w10 = saturate(1 - abs(i - 10));
	float w11 = saturate(1 - abs(i - 11));
	float w12 = saturate(1 - abs(i - 12));
	float w13 = saturate(1 - abs(i - 13));
	float w14 = saturate(1 - abs(i - 14));
	float w15 = saturate(1 - abs(i - 15));
	
	// Branchless conditional writes
	poiMods.globalMask[0] = lerp(poiMods.globalMask[0], v0, w0);
	poiMods.globalMask[1] = lerp(poiMods.globalMask[1], v1, w1);
	poiMods.globalMask[2] = lerp(poiMods.globalMask[2], v2, w2);
	poiMods.globalMask[3] = lerp(poiMods.globalMask[3], v3, w3);
	poiMods.globalMask[4] = lerp(poiMods.globalMask[4], v4, w4);
	poiMods.globalMask[5] = lerp(poiMods.globalMask[5], v5, w5);
	poiMods.globalMask[6] = lerp(poiMods.globalMask[6], v6, w6);
	poiMods.globalMask[7] = lerp(poiMods.globalMask[7], v7, w7);
	poiMods.globalMask[8] = lerp(poiMods.globalMask[8], v8, w8);
	poiMods.globalMask[9] = lerp(poiMods.globalMask[9], v9, w9);
	poiMods.globalMask[10] = lerp(poiMods.globalMask[10], v10, w10);
	poiMods.globalMask[11] = lerp(poiMods.globalMask[11], v11, w11);
	poiMods.globalMask[12] = lerp(poiMods.globalMask[12], v12, w12);
	poiMods.globalMask[13] = lerp(poiMods.globalMask[13], v13, w13);
	poiMods.globalMask[14] = lerp(poiMods.globalMask[14], v14, w14);
	poiMods.globalMask[15] = lerp(poiMods.globalMask[15], v15, w15);
}

void assignValueToVectorFromIndex(inout float4 vec, int index, float value)
{
	float i = clamp(index, 0, 3);
	float w0 = saturate(1 - abs(i - 0));
	float w1 = saturate(1 - abs(i - 1));
	float w2 = saturate(1 - abs(i - 2));
	float w3 = saturate(1 - abs(i - 3));
	vec[0] = lerp(vec[0], value, w0);
	vec[1] = lerp(vec[1], value, w1);
	vec[2] = lerp(vec[2], value, w2);
	vec[3] = lerp(vec[3], value, w3);
}

// SNose
float3 mod289(float3 x)
{
	return x - floor(x * (1.0 / 289.0)) * 289.0;
}
float2 mod289(float2 x)
{
	return x - floor(x * (1.0 / 289.0)) * 289.0;
}
float3 permute(float3 x)
{
	return mod289(((x * 34.0) + 1.0) * x);
}

float snoise(float2 v)
{
	const float4 C = float4(0.211324865405187, // (3.0 - sqrt(3.0)) / 6.0
	0.366025403784439, // 0.5 * (sqrt(3.0) - 1.0)
	- 0.577350269189626, // - 1.0 + 2.0 * C.x
	0.024390243902439); // 1.0 / 41.0
	float2 i = floor(v + dot(v, C.yy));
	float2 x0 = v - i + dot(i, C.xx);
	float2 i1;
	i1 = (x0.x > x0.y) ? float2(1.0, 0.0) : float2(0.0, 1.0);
	float4 x12 = x0.xyxy + C.xxzz;
	x12.xy -= i1;
	i = mod289(i); // Avoid truncation effects in permutation
	float3 p = permute(permute(i.y + float3(0.0, i1.y, 1.0))
	+ i.x + float3(0.0, i1.x, 1.0));

	float3 m = max(0.5 - float3(dot(x0, x0), dot(x12.xy, x12.xy), dot(x12.zw, x12.zw)), 0.0);
	m = m * m ;
	m = m * m ;
	float3 x = 2.0 * frac(p * C.www) - 1.0;
	float3 h = abs(x) - 0.5;
	float3 ox = floor(x + 0.5);
	float3 a0 = x - ox;
	m *= 1.79284291400159 - 0.85373472095314 * (a0 * a0 + h * h);
	float3 g;
	g.x = a0.x * x0.x + h.x * x0.y;
	g.yz = a0.yz * x12.xz + h.yz * x12.yw;
	return 130.0 * dot(m, g);
}


float poiInvertToggle(in float value, in float toggle)
{
	return (toggle == 0 ? value : 1 - value);
}

float3 PoiBlendNormal(float3 dstNormal, float3 srcNormal)
{
	return float3(dstNormal.xy + srcNormal.xy, dstNormal.z * srcNormal.z);
}

float3 lilTransformDirOStoWS(float3 directionOS, bool doNormalize)
{
	if (doNormalize) return normalize(mul((float3x3)unity_ObjectToWorld, directionOS));
	else            return mul((float3x3)unity_ObjectToWorld, directionOS);
}

float2 poiGetWidthAndHeight(Texture2D tex)
{
	uint width, height;
	tex.GetDimensions(width, height);
	return float2(width, height);
}

float2 poiGetWidthAndHeight(Texture2DArray tex)
{
	uint width, height, element;
	tex.GetDimensions(width, height, element);
	return float2(width, height);
}

bool SceneHasReflections()
{
	bool hasReflection = false;
	float width, height;
	unity_SpecCube0.GetDimensions(width, height);
	hasReflection = !(width * height < 2);

	#if CLUSTER_HAS_REFLECTION_PROBES
		urp_ReflProbes_Atlas.GetDimensions(width, height);
		hasReflection = hasReflection | !(width * height < 2);
	#endif
	
	return hasReflection;
}

void applyUnityFog(inout float3 col, float fogCoord, float ignoreFog)
{
	if (ignoreFog > 0.5) return;
	float z = UNITY_Z_0_FAR_FROM_CLIPSPACE(fogCoord);

	half fogIntensity = 1.0;
	// Linear fog: unity_FogParams.z and .w differ
	if (unity_FogParams.z != unity_FogParams.w)
	{
		fogIntensity = saturate(z * unity_FogParams.z + unity_FogParams.w);
	}
	// Exp/Exp2 fog: treat as Exp2
	else if (unity_FogParams.x != 0.0)
	{
		half fogFactor = unity_FogParams.x * z;
		fogIntensity = saturate(exp2(-fogFactor * fogFactor));
	}

	float3 appliedFogColor = unity_FogColor.rgb;

	#if defined(POI_PASS_ADD)
		appliedFogColor = float3(0, 0, 0);
	#endif

	col.rgb = lerp(appliedFogColor, col.rgb, fogIntensity);
}

#T#PoiPassSpecificHelperFunctions

float2 vertexUV(in VertexOut o, int index)
{
	float i = index;
	float w0 = saturate(1 - abs(i - 0));
	float w1 = saturate(1 - abs(i - 1));
	float w2 = saturate(1 - abs(i - 2));
	float w3 = saturate(1 - abs(i - 3));
	return o.uv[0].xy * w0 + o.uv[0].zw * w1 + o.uv[1].xy * w2 + o.uv[1].zw * w3;
}

float2 vertexUV(in appdata v, int index)
{
	float i = index;
	float w0 = saturate(1 - abs(i - 0));
	float w1 = saturate(1 - abs(i - 1));
	float w2 = saturate(1 - abs(i - 2));
	float w3 = saturate(1 - abs(i - 3));
	return v.uv0.xy * w0 + v.uv1.xy * w1 + v.uv2.xy * w2 + v.uv3.xy * w3;
}

#ifdef POI_PASS_LILFUR
float2 vertexUV(in PoiGeoToFragLilFur o, int index)
{
	float i = index;
	float w0 = saturate(1 - abs(i - 0));
	float w1 = saturate(1 - abs(i - 1));
	float w2 = saturate(1 - abs(i - 2));
	float w3 = saturate(1 - abs(i - 3));
	return o.uv[0].xy * w0 + o.uv[0].zw * w1 + o.uv[1].xy * w2 + o.uv[1].zw * w3;
}

float2 vertexUV(in PoiVertToGeoLilFur o, int index)
{
	float i = index;
	float w0 = saturate(1 - abs(i - 0));
	float w1 = saturate(1 - abs(i - 1));
	float w2 = saturate(1 - abs(i - 2));
	float w3 = saturate(1 - abs(i - 3));
	return o.uv[0].xy * w0 + o.uv[0].zw * w1 + o.uv[1].xy * w2 + o.uv[1].zw * w3;
}
#endif

#ifdef POI_AUDIOLINK
inline int poiALBandPass(int bandIdx)
{
	bandIdx = clamp(bandIdx, 0, 3);
	return bandIdx == 0 ? ALPASS_AUDIOBASS : bandIdx == 1 ? ALPASS_AUDIOLOWMIDS : bandIdx == 2 ? ALPASS_AUDIOHIGHMIDS : ALPASS_AUDIOTREBLE;
}
#endif

float3 poiThemeColor(in PoiMods poiMods, in float3 srcColor, in float themeIndex)
{
	float3 outputColor = srcColor;
	if (themeIndex != 0)
	{
		themeIndex = max(themeIndex - 1, 0);
		
		if (themeIndex <= 3)
		{
			outputColor = poiMods.globalColorTheme[themeIndex].rgb;
		}
		else
		{
			#ifdef POI_AUDIOLINK
				if (poiMods.audioLinkAvailable)
				{
					outputColor = poiMods.globalColorTheme[themeIndex].rgb;
				}
			#endif
		}
	}
	return outputColor;
}

struct PoiSpriteSheetConfig
{
	float columns;
	float rows;
	float totalFrames;
	float frameSelection;
	float fps;
	float randomStart;
	float inset;
	float2 textureST;
};

float poiSpriteSheetFrameIndex(PoiSpriteSheetConfig config, float progress, float seed)
{
	float totalFrames = (config.totalFrames > 0) ? config.totalFrames : (config.columns * config.rows);
	float frameIndex = 0;
	
	if (config.frameSelection == 0)
		frameIndex = floor(seed * totalFrames);
	else if (config.frameSelection == 1)
		frameIndex = floor(progress * totalFrames);
	else if (config.frameSelection == 2)
	{
		float timeOffset = config.randomStart ? seed * totalFrames : 0;
		frameIndex = floor(fmod(POI_TIME.y * config.fps + timeOffset, totalFrames));
	}
	
	return clamp(frameIndex, 0, totalFrames - 1);
}

float poiSpriteSheetFrameIndex(float columns, float rows, float configTotalFrames, float frameSelection, float fps, float randomStart, float progress, float seed)
{
	float totalFrames = (configTotalFrames > 0) ? configTotalFrames : (columns * rows);
	float frameIndex = 0;
	
	if (frameSelection == 0)
		frameIndex = floor(seed * totalFrames);
	else if (frameSelection == 1)
		frameIndex = floor(progress * totalFrames);
	else if (frameSelection == 2)
	{
		float timeOffset = randomStart ? seed * totalFrames : 0;
		frameIndex = floor(fmod(POI_TIME.y * fps + timeOffset, totalFrames));
	}
	
	return clamp(frameIndex, 0, totalFrames - 1);
}

float2 poiSpriteSheetUV(float2 uv, float columns, float rows, float frameIndex, float inset, float2 textureST)
{
	float invCols = 1.0 / columns;
	float invRows = 1.0 / rows;
	float2 frameSize = float2(invCols, invRows);
	
	float column = fmod(frameIndex, columns);
	float row = floor(frameIndex * invCols);
	row = rows - 1 - row;
	
	float2 lookupUV = (uv * frameSize) + float2(column, row) * frameSize;
	if (inset > 0)
	{
		float2 texelInset = inset * frameSize / max(textureST, 1e-6);
		lookupUV = lerp(lookupUV, float2(column + 1, row + 1) * frameSize, texelInset);
	}
	
	return lookupUV;
}

float2 poiSpriteSheetUV(float2 uv, PoiSpriteSheetConfig config, float frameIndex)
{
	return poiSpriteSheetUV(uv, config.columns, config.rows, frameIndex, config.inset, config.textureST);
}