#T#PoiLightingDataBasePassProps

[HideInInspector] m_start_PoiLightData ("Light Data--{button_help:{text:Tutorial,action:{type:URL,data:https://www.poiyomi.com/shading/light-data},hover:Documentation}}", Float) = 0
// Lighting Data
[sRGBWarning][ThryRGBAPacker(R, G, B, A, Linear, false)]_LightingAOMaps ("AO Maps (expand)--{reference_properties:[_LightingAOMapsPan, _LightingAOMapsUV,_LightDataAOStrengthR,_LightDataAOStrengthG,_LightDataAOStrengthB,_LightDataAOStrengthA, _LightDataAOGlobalMaskR]}", 2D) = "white" { }
[HideInInspector][Vector2]_LightingAOMapsPan ("Panning", Vector) = (0, 0, 0, 0)
[HideInInspector][ThryWideEnum(UV0, 0, UV1, 1, UV2, 2, UV3, 3, Panosphere, 4, World Pos, 5, Local Pos, 8, Polar UV, 6, Distorted UV, 7)] _LightingAOMapsUV ("UV", Int) = 0
[HideInInspector]_LightDataAOStrengthR ("R Strength", Range(0, 1)) = 1
[HideInInspector]_LightDataAOStrengthG ("G Strength", Range(0, 1)) = 0
[HideInInspector]_LightDataAOStrengthB ("B Strength", Range(0, 1)) = 0
[HideInInspector]_LightDataAOStrengthA ("A Strength", Range(0, 1)) = 0
[HideInInspector][ThryWideEnum(Off, 0, 1R, 1, 1G, 2, 1B, 3, 1A, 4, 2R, 5, 2G, 6, 2B, 7, 2A, 8, 3R, 9, 3G, 10, 3B, 11, 3A, 12, 4R, 13, 4G, 14, 4B, 15, 4A, 16)] _LightDataAOGlobalMaskR ("Global Mask--{reference_property:_LightDataAOGlobalMaskBlendTypeR}", Int) = 0
[HideInInspector][ThryWideEnum(Add, 7, Subtract, 1, Multiply, 2, Divide, 3, Min, 4, Max, 5, Average, 6, Replace, 0)] _LightDataAOGlobalMaskBlendTypeR ("Blending", Range(0, 1)) = 2
[sRGBWarning][ThryRGBAPacker(R, G, B, A, Linear, false)]_LightingDetailShadowMaps ("Shadow Map (expand)--{reference_properties:[_LightingDetailShadowMapsPan, _LightingDetailShadowMapsUV,_LightingDetailShadowStrengthR,_LightingDetailShadowStrengthG,_LightingDetailShadowStrengthB,_LightingDetailShadowStrengthA,_LightingAddDetailShadowStrengthR,_LightingAddDetailShadowStrengthG,_LightingAddDetailShadowStrengthB,_LightingAddDetailShadowStrengthA, _LightDataDetailShadowGlobalMaskR]}", 2D) = "white" { }
[HideInInspector][Vector2]_LightingDetailShadowMapsPan ("Panning", Vector) = (0, 0, 0, 0)
[HideInInspector][ThryWideEnum(UV0, 0, UV1, 1, UV2, 2, UV3, 3, Panosphere, 4, World Pos, 5, Local Pos, 8, Polar UV, 6, Distorted UV, 7)] _LightingDetailShadowMapsUV ("UV", Int) = 0
[HideInInspector]_LightingDetailShadowStrengthR ("R Strength", Range(0, 1)) = 1
[HideInInspector]_LightingDetailShadowStrengthG ("G Strength", Range(0, 1)) = 0
[HideInInspector]_LightingDetailShadowStrengthB ("B Strength", Range(0, 1)) = 0
[HideInInspector]_LightingDetailShadowStrengthA ("A Strength", Range(0, 1)) = 0
[HideInInspector]_LightingAddDetailShadowStrengthR ("Additive R Strength", Range(0, 1)) = 1
[HideInInspector]_LightingAddDetailShadowStrengthG ("Additive G Strength", Range(0, 1)) = 0
[HideInInspector]_LightingAddDetailShadowStrengthB ("Additive B Strength", Range(0, 1)) = 0
[HideInInspector]_LightingAddDetailShadowStrengthA ("Additive A Strength", Range(0, 1)) = 0
[HideInInspector][ThryWideEnum(Off, 0, 1R, 1, 1G, 2, 1B, 3, 1A, 4, 2R, 5, 2G, 6, 2B, 7, 2A, 8, 3R, 9, 3G, 10, 3B, 11, 3A, 12, 4R, 13, 4G, 14, 4B, 15, 4A, 16)] _LightDataDetailShadowGlobalMaskR ("Global Mask--{reference_property:_LightDataDetailShadowGlobalMaskBlendTypeR}", Int) = 0
[HideInInspector][ThryWideEnum(Add, 7, Subtract, 1, Multiply, 2, Divide, 3, Min, 4, Max, 5, Average, 6, Replace, 0)] _LightDataDetailShadowGlobalMaskBlendTypeR ("Blending", Range(0, 1)) = 2
[sRGBWarning][ThryRGBAPacker(R, G, B, A, Linear, false)]_LightingShadowMasks ("Shadow Masks (expand)--{reference_properties:[_LightingShadowMasksPan, _LightingShadowMasksUV,_LightingShadowMaskStrengthR,_LightingShadowMaskStrengthG,_LightingShadowMaskStrengthB,_LightingShadowMaskStrengthA, _LightDataShadowMaskGlobalMaskR]}", 2D) = "white" { }
[HideInInspector][Vector2]_LightingShadowMasksPan ("Panning", Vector) = (0, 0, 0, 0)
[HideInInspector][ThryWideEnum(UV0, 0, UV1, 1, UV2, 2, UV3, 3, Panosphere, 4, World Pos, 5, Local Pos, 8, Polar UV, 6, Distorted UV, 7)] _LightingShadowMasksUV ("UV", Int) = 0
[HideInInspector]_LightingShadowMaskStrengthR ("R Strength", Range(0, 1)) = 1
[HideInInspector]_LightingShadowMaskStrengthG ("G Strength", Range(0, 1)) = 0
[HideInInspector]_LightingShadowMaskStrengthB ("B Strength", Range(0, 1)) = 0
[HideInInspector]_LightingShadowMaskStrengthA ("A Strength", Range(0, 1)) = 0
[HideInInspector][ThryWideEnum(Off, 0, 1R, 1, 1G, 2, 1B, 3, 1A, 4, 2R, 5, 2G, 6, 2B, 7, 2A, 8, 3R, 9, 3G, 10, 3B, 11, 3A, 12, 4R, 13, 4G, 14, 4B, 15, 4A, 16)] _LightDataShadowMaskGlobalMaskR ("Global Mask--{reference_property:_LightDataShadowMaskGlobalMaskBlendTypeR}", Int) = 0
[HideInInspector][ThryWideEnum(Add, 7, Subtract, 1, Multiply, 2, Divide, 3, Min, 4, Max, 5, Average, 6, Replace, 0)] _LightDataShadowMaskGlobalMaskBlendTypeR ("Blending", Range(0, 1)) = 2
// Base Pass
[HideInInspector] s_start_LightDataBasePass ("Base Pass (Directional & Baked Lights)--{persistent_expand:true,default_expand:true}", Float) = 1
[Enum(Poi Custom, 0, Standard, 1, UTS2, 2, OpenLit(lil toon), 3)] _LightingColorMode ("Light Color Mode", Int) = 0
[Enum(Poi Custom, 0, Normalized NDotL, 1, Saturated NDotL, 2, Casted Shadows Only, 3, SDF, 4)] _LightingMapMode ("Light Map Mode", Int) = 0

[HideInInspector] s_start_LightDataSDF ("Signed Distance fields--{persistent_expand:true,default_expand:true, condition_showS:(_LightingMapMode==4)}", Float) = 1
[sRGBWarning][ThryRGBAPacker(R, G, Nothing, Nothing, Linear, false)]_LightDataSDFMap ("SDF Map (expand)--{reference_properties:[_LightDataSDFMapPan, _LightDataSDFMapUV]}", 2D) = "white" { }
[HideInInspector][Vector2]_LightDataSDFMapPan ("Panning", Vector) = (0, 0, 0, 0)
[HideInInspector][ThryWideEnum(UV0, 0, UV1, 1, UV2, 2, UV3, 3, Panosphere, 4, World Pos, 5, Local Pos, 8, Polar UV, 6, Distorted UV, 7)] _LightDataSDFMapUV ("UV", Int) = 0
_LightDataSDFMapLOD ("LOD", Range(0, 1)) = 0
_LightDataSDFBlendY ("Blend Y Direction", Range(0.001, 2)) = 1
[HideInInspector] s_end_LightDataSDF ("Signed Distance fields", Float) = 1

[Enum(Poi Custom, 0, Forced Local Direction, 1, Forced World Direction, 2, UTS2, 3, OpenLit(lil toon), 4, View Direction, 5)] _LightingDirectionMode ("Light Direction Mode", Int) = 0
[Vector3]_LightngForcedDirection ("Forced Direction--{condition_showS:(_LightingDirectionMode==1 || _LightingDirectionMode==2)}", Vector) = (0, 0, 0)
_LightingViewDirOffsetPitch ("View Dir Offset Pitch--{condition_showS:_LightingDirectionMode==5}", Range(-90, 90)) = 0
_LightingViewDirOffsetYaw ("View Dir Offset Yaw--{condition_showS:_LightingDirectionMode==5}", Range(-90, 90)) = 0
[ToggleUI]_LightingForceColorEnabled ("Force Light Color", Float) = 0
_LightingForcedColor ("Forced Color--{condition_showS:(_LightingForceColorEnabled==1), reference_property:_LightingForcedColorThemeIndex}", Color) = (1, 1, 1)
[HideInInspector][ThryWideEnum(Off, 0, Theme Color 0, 1, Theme Color 1, 2, Theme Color 2, 3, Theme Color 3, 4, ColorChord 0, 5, ColorChord 1, 6, ColorChord 2, 7, ColorChord 3, 8, AL Theme 0, 9, AL Theme 1, 10, AL Theme 2, 11, AL Theme 3, 12)] _LightingForcedColorThemeIndex ("", Int) = 0
_Unlit_Intensity ("Unlit_Intensity--{condition_showS:(_LightingColorMode==2)}", Range(0.001, 4)) = 1
[ToggleUI]_LightingCapEnabled ("Limit Brightness", Float) = 1
_LightingCap ("Max Brightness--{condition_showS:(_LightingCapEnabled==1)}", Range(0, 10)) = 1
_LightingMinLightBrightness ("Min Brightness", Range(0, 1)) = 0
_LightingIndirectUsesNormals ("Indirect Uses Normals--{condition_showS:(_LightingColorMode==0)}", Range(0, 1)) = 0
_LightingCastedShadows ("Receive Casted Shadows", Range(0, 1)) = 0
_LightingMonochromatic ("Grayscale Lighting", Range(0, 1)) = 0
[ToggleUI]_LightingVertexLightingEnabled ("Vertex lights (Non-Important)", Float) = 1
[ToggleUI]_LightingMirrorVertexLightingEnabled ("Mirror Vertex lights (Non-Important)", Float) = 1
[HideInInspector] s_end_LightDataBasePass ("Base Pass", Float) = 1
// Lighting Additive
[HideInInspector] s_start_LightDataAddPass ("Add Pass (Point & Spot lights)--{persistent_expand:true,default_expand:true}", Float) = 1
[ToggleUI]_LightingAdditiveEnable ("Pixel lights (Important)", Float) = 1
[ToggleUI]_DisableDirectionalInAdd ("Ignore Directional Lights--{condition_showS:(_LightingAdditiveEnable==1)}", Float) = 1
[ToggleUI]_LightingAdditiveLimited ("Limit Brightness", Float) = 1
_LightingAdditiveLimit ("Max Brightness--{condition_showS:(_LightingAdditiveLimited==1)}", Range(0, 10)) = 1
_LightingAdditiveCastedShadows ("Receive Casted Shadows", Range(0, 1)) = 1
_LightingAdditiveMonochromatic ("Grayscale Lighting", Range(0, 1)) = 0
_LightingAdditivePassthrough ("Point Light Passthrough--{condition_showS:(_LightingAdditiveEnable==1)}", Range(0, 1)) = .5
[HideInInspector] s_end_LightDataAddPass ("Add Pass", Float) = 1
// Lighting Data Debug
[HideInInspector] s_start_LightDataDebug ("Debug / Data Visualizations--{reference_property:_LightDataDebugEnabled,persistent_expand:true}", Float) = 0
[HideInInspector][DoNotAnimate][ThryToggleUI(false)]_LightDataDebugEnabled ("Debug", Float) = 0
[ThryWideEnum(Direct Color, 0, Indirect Color, 1, Light Map, 2, Attenuation, 3, N Dot L, 4, Half Dir, 5, Direction, 6, Add Color, 7, Add Attenuation, 8, Add Shadow, 9, Add N Dot L, 10)] _LightingDebugVisualize ("Visualize", Int) = 0
[HideInInspector] s_end_LightDataDebug ("Debug", Float) = 0
[HideInInspector] m_end_PoiLightData ("Light Data", Float) = 0


#T#PoiLightingDataKeywords


#T#PoiLightingDataVariables
#if defined(PROP_LIGHTINGAOMAPS) || !defined(OPTIMIZER_ENABLED)
	Texture2D _LightingAOMaps;
#endif
float4 _LightingAOMaps_ST;
float2 _LightingAOMapsPan;
float _LightingAOMapsUV;
float _LightDataAOStrengthR;
float _LightDataAOStrengthG;
float _LightDataAOStrengthB;
float _LightDataAOStrengthA;
float _LightDataAOGlobalMaskR;
float _LightDataAOGlobalMaskBlendTypeR;

#if defined(PROP_LIGHTINGDETAILSHADOWMAPS) || !defined(OPTIMIZER_ENABLED)
	Texture2D _LightingDetailShadowMaps;
#endif
float4 _LightingDetailShadowMaps_ST;
float2 _LightingDetailShadowMapsPan;
float _LightingDetailShadowMapsUV;
float _LightingDetailShadowStrengthR;
float _LightingDetailShadowStrengthG;
float _LightingDetailShadowStrengthB;
float _LightingDetailShadowStrengthA;
float _LightingAddDetailShadowStrengthR;
float _LightingAddDetailShadowStrengthG;
float _LightingAddDetailShadowStrengthB;
float _LightingAddDetailShadowStrengthA;
float _LightDataDetailShadowGlobalMaskR;
float _LightDataDetailShadowGlobalMaskBlendTypeR;

#if defined(PROP_LIGHTINGSHADOWMASKS) || !defined(OPTIMIZER_ENABLED)
	Texture2D _LightingShadowMasks;
#endif
float4 _LightingShadowMasks_ST;
float2 _LightingShadowMasksPan;
float _LightingShadowMasksUV;
float _LightingShadowMaskStrengthR;
float _LightingShadowMaskStrengthG;
float _LightingShadowMaskStrengthB;
float _LightingShadowMaskStrengthA;
float _LightDataShadowMaskGlobalMaskR;
float _LightDataShadowMaskGlobalMaskBlendTypeR;

// Lighting Data
float _Unlit_Intensity;
float _LightingColorMode;
float _LightingMapMode;

#if defined(PROP_LIGHTDATASDFMAP) || !defined(OPTIMIZER_ENABLED)
	Texture2D _LightDataSDFMap;
	float4 _LightDataSDFMap_ST;
	float2 _LightDataSDFMapPan;
	float _LightDataSDFMapUV;
	float _LightDataSDFMapLOD;
	float _LightDataSDFBlendY;
#endif

float _LightingDirectionMode;
float3 _LightngForcedDirection;
float _LightingViewDirOffsetPitch;
float _LightingViewDirOffsetYaw;
float _LightingIndirectUsesNormals;
float _LightingCapEnabled;
float _LightingCap;
float _LightingForceColorEnabled;
float3 _LightingForcedColor;
float _LightingForcedColorThemeIndex;
float _LightingCastedShadows;
float _LightingMonochromatic;
float _LightingMinLightBrightness;
// Additive Lighting Data
float _LightingAdditiveEnable;
float _LightingAdditiveLimited;
float _LightingAdditiveLimit;
float _LightingAdditiveCastedShadows;
float _LightingAdditiveMonochromatic;
float _LightingAdditivePassthrough;
float _DisableDirectionalInAdd;
float _LightingVertexLightingEnabled;
float _LightingMirrorVertexLightingEnabled;
// Lighting Data Debug
float _LightDataDebugEnabled;
float _LightingDebugVisualize;


#T#PoiLightingDataImplementation

poiLight.finalLightAdd = 0;

// Ambient Occlusion
#if defined(PROP_LIGHTINGAOMAPS) || !defined(OPTIMIZER_ENABLED)
	float4 AOMaps = POI2D_SAMPLER_PAN(_LightingAOMaps, _MainTex, poiUV(poiMesh.uv[_LightingAOMapsUV], _LightingAOMaps_ST), _LightingAOMapsPan);
	poiLight.occlusion = min(min(min(lerp(1, AOMaps.r, _LightDataAOStrengthR), lerp(1, AOMaps.g, _LightDataAOStrengthG)), lerp(1, AOMaps.b, _LightDataAOStrengthB)), lerp(1, AOMaps.a, _LightDataAOStrengthA));
#else
	poiLight.occlusion = 1;
#endif

if (_LightDataAOGlobalMaskR > 0)
{
	poiLight.occlusion = maskBlend(poiLight.occlusion, poiMods.globalMask[_LightDataAOGlobalMaskR - 1], _LightDataAOGlobalMaskBlendTypeR);
}

// Detail Shadows
#if defined(PROP_LIGHTINGDETAILSHADOWMAPS) || !defined(OPTIMIZER_ENABLED)
	float4 DetailShadows = POI2D_SAMPLER_PAN(_LightingDetailShadowMaps, _MainTex, poiUV(poiMesh.uv[_LightingDetailShadowMapsUV], _LightingDetailShadowMaps_ST), _LightingDetailShadowMapsPan);
	#ifndef POI_PASS_ADD
		poiLight.detailShadow = lerp(1, DetailShadows.r, _LightingDetailShadowStrengthR) * lerp(1, DetailShadows.g, _LightingDetailShadowStrengthG) * lerp(1, DetailShadows.b, _LightingDetailShadowStrengthB) * lerp(1, DetailShadows.a, _LightingDetailShadowStrengthA);
	#else
		poiLight.detailShadow = lerp(1, DetailShadows.r, _LightingAddDetailShadowStrengthR) * lerp(1, DetailShadows.g, _LightingAddDetailShadowStrengthG) * lerp(1, DetailShadows.b, _LightingAddDetailShadowStrengthB) * lerp(1, DetailShadows.a, _LightingAddDetailShadowStrengthA);
	#endif
#else
	poiLight.detailShadow = 1;
#endif

if (_LightDataDetailShadowGlobalMaskR > 0)
{
	poiLight.detailShadow = maskBlend(poiLight.detailShadow, poiMods.globalMask[_LightDataDetailShadowGlobalMaskR - 1], _LightDataDetailShadowGlobalMaskBlendTypeR);
}

// Shadow Masks
#if defined(PROP_LIGHTINGSHADOWMASKS) || !defined(OPTIMIZER_ENABLED)
	float4 ShadowMasks = POI2D_SAMPLER_PAN(_LightingShadowMasks, _MainTex, poiUV(poiMesh.uv[_LightingShadowMasksUV], _LightingShadowMasks_ST), _LightingShadowMasksPan);
	poiLight.shadowMask = lerp(1, ShadowMasks.r, _LightingShadowMaskStrengthR) * lerp(1, ShadowMasks.g, _LightingShadowMaskStrengthG) * lerp(1, ShadowMasks.b, _LightingShadowMaskStrengthB) * lerp(1, ShadowMasks.a, _LightingShadowMaskStrengthA);
#else
	poiLight.shadowMask = 1;
#endif
if (_LightDataShadowMaskGlobalMaskR > 0)
{
	poiLight.shadowMask = maskBlend(poiLight.shadowMask, poiMods.globalMask[_LightDataShadowMaskGlobalMaskR - 1], _LightDataShadowMaskGlobalMaskBlendTypeR);
}

#ifdef UNITY_PASS_FORWARDBASE

	bool lightExists = false;
	if (any(_LightColor0.rgb >= 0.002))
	{
		lightExists = true;
	}

	if (_LightingVertexLightingEnabled)
	{
		poiFragData.toggleVertexLights = 1;
	}
	if (IsInMirror() && _LightingMirrorVertexLightingEnabled == 0)
	{
		poiFragData.toggleVertexLights = 0;
	}

	if (_LightingVertexLightingEnabled)
	{
		#if defined(VERTEXLIGHT_ON)
			float4 toLightX = unity_4LightPosX0 - i.worldPos.x;
			float4 toLightY = unity_4LightPosY0 - i.worldPos.y;
			float4 toLightZ = unity_4LightPosZ0 - i.worldPos.z;
			float4 lengthSq = 0;
			lengthSq += toLightX * toLightX;
			lengthSq += toLightY * toLightY;
			lengthSq += toLightZ * toLightZ;
			
			float4 lightAttenSq = unity_4LightAtten0;
			float4 atten = 1.0 / (1.0 + lengthSq * lightAttenSq);
			float4 vLightWeight = saturate(1 - (lengthSq * lightAttenSq / 25));
			poiLight.vAttenuation = min(atten, vLightWeight * vLightWeight);
			
			poiLight.vDotNL = 0;
			poiLight.vDotNL += toLightX * poiMesh.normals[1].x;
			poiLight.vDotNL += toLightY * poiMesh.normals[1].y;
			poiLight.vDotNL += toLightZ * poiMesh.normals[1].z;
			
			float4 corr = rsqrt(lengthSq);
			poiLight.vertexVDotNL = max(0, poiLight.vDotNL * corr);

			poiLight.vertexVDotNL = 0;
			poiLight.vertexVDotNL += toLightX * poiMesh.normals[0].x;
			poiLight.vertexVDotNL += toLightY * poiMesh.normals[0].y;
			poiLight.vertexVDotNL += toLightZ * poiMesh.normals[0].z;
			
			poiLight.vertexVDotNL = max(0, poiLight.vDotNL * corr);

			poiLight.vSaturatedDotNL = saturate(poiLight.vDotNL);
			
			[unroll]
			for (int index = 0; index < 4; index++)
			{
				poiLight.vPosition[index] = float3(unity_4LightPosX0[index], unity_4LightPosY0[index], unity_4LightPosZ0[index]);
				
				float3 vertexToLightSource = poiLight.vPosition[index] - poiMesh.worldPos;
				poiLight.vDirection[index] = normalize(vertexToLightSource);
				poiLight.vColor[index] = _LightingAdditiveLimited ? MaxLuminance(unity_LightColor[index].rgb * poiLight.vAttenuation[index], _LightingAdditiveLimit) : unity_LightColor[index].rgb * poiLight.vAttenuation[index];
				poiLight.vColor[index] = lerp(poiLight.vColor[index], dot(poiLight.vColor[index], float3(0.299, 0.587, 0.114)), _LightingAdditiveMonochromatic);
				poiLight.vHalfDir[index] = Unity_SafeNormalize(poiLight.vDirection[index] + poiCam.viewDir);
				poiLight.vDotNL[index] = dot(poiMesh.normals[1], poiLight.vDirection[index]);
				poiLight.vCorrectedDotNL[index] = .5 * (poiLight.vDotNL[index] + 1);
				poiLight.vDotLH[index] = saturate(dot(poiLight.vDirection[index], poiLight.vHalfDir[index]));
				
				poiLight.vDotNH[index] = dot(poiMesh.normals[1], poiLight.vHalfDir[index]);
				poiLight.vertexVDotNH[index] = saturate(dot(poiMesh.normals[0], poiLight.vHalfDir[index]));
			}
		#endif
	}


	//UNITY_BRANCH
	if (_LightingColorMode == 0) // Poi Custom Light Color

	{
		float3 magic = max(BetterSH9(normalize(unity_SHAr + unity_SHAg + unity_SHAb)), 0);
		float3 normalLight = _LightColor0.rgb + BetterSH9(float4(0, 0, 0, 1));
		
		float magiLumi = calculateluminance(magic);
		float normaLumi = calculateluminance(normalLight);
		float maginormalumi = magiLumi + normaLumi;
		
		float magiratio = magiLumi / maginormalumi;
		float normaRatio = normaLumi / maginormalumi;
		
		float target = calculateluminance(magic * magiratio + normalLight * normaRatio);
		float3 properLightColor = magic + normalLight;
		float properLuminance = calculateluminance(magic + normalLight);
		poiLight.directColor = properLightColor * max(0.0001, (target / properLuminance));

		poiLight.indirectColor = BetterSH9(float4(lerp(0, poiMesh.normals[1], _LightingIndirectUsesNormals), 1));
	}

	//UNITY_BRANCH
	if (_LightingColorMode == 1) // More standard approach to light color

	{
		float3 indirectColor = BetterSH9(float4(poiMesh.normals[1], 1));
		if (lightExists)
		{
			poiLight.directColor = _LightColor0.rgb;
			poiLight.indirectColor = indirectColor;
		}
		else
		{
			poiLight.directColor = indirectColor * 0.6;
			poiLight.indirectColor = indirectColor * 0.5;
		}
	}

	if (_LightingColorMode == 2) // UTS style

	{
		poiLight.indirectColor = saturate(max(half3(0.05, 0.05, 0.05) * _Unlit_Intensity, max(ShadeSH9(half4(0.0, 0.0, 0.0, 1.0)), ShadeSH9(half4(0.0, -1.0, 0.0, 1.0)).rgb) * _Unlit_Intensity));
		poiLight.directColor = max(poiLight.indirectColor, _LightColor0.rgb);
	}

	if (_LightingColorMode == 3) // OpenLit

	{
		float3 lightDirectionForSH9 = OpenLitLightingDirectionForSH9();
		OpenLitShadeSH9ToonDouble(lightDirectionForSH9, poiLight.directColor, poiLight.indirectColor);
		poiLight.directColor += _LightColor0.rgb;
		// OpenLit does a few other things by default like clamp direct colour
		// see https://github.com/lilxyzw/OpenLit/blob/main/Assets/OpenLit/core.hlsl#L174

	}

	float lightMapMode = _LightingMapMode;
	//UNITY_BRANCH
	if (_LightingDirectionMode == 0)
	{
		poiLight.direction = calculateluminance(_LightColor0.rgb) * _WorldSpaceLightPos0.xyz + 0.2 * unity_SHAr.xyz + 0.7 * unity_SHAg.xyz + 0.1 * unity_SHAb.xyz;
	}
	if (_LightingDirectionMode == 1 || _LightingDirectionMode == 2)
	{
		//UNITY_BRANCH
		if (_LightingDirectionMode == 1)
		{
			poiLight.direction = mul(unity_ObjectToWorld, _LightngForcedDirection).xyz;;
		}
		//UNITY_BRANCH
		if (_LightingDirectionMode == 2)
		{
			poiLight.direction = _LightngForcedDirection;
		}
		if (lightMapMode == 0)
		{
			lightMapMode == 1;
		}
	}

	if (_LightingDirectionMode == 3) // UTS

	{
		float3 defaultLightDirection = normalize(UNITY_MATRIX_V[2].xyz + UNITY_MATRIX_V[1].xyz);
		float3 lightDirection = normalize(lerp(defaultLightDirection, _WorldSpaceLightPos0.xyz, any(_WorldSpaceLightPos0.xyz)));
		poiLight.direction = lightDirection;
	}
	if (_LightingDirectionMode == 4) // OpenLit

	{
		poiLight.direction = OpenLitLightingDirection(); // float4 customDir = 0; // Do we want to give users to alter this (OpenLit always does!)?

	}

	if (_LightingDirectionMode == 5) // View Direction

	{
		float3 upViewDir = normalize(UNITY_MATRIX_V[1].xyz);
		float3 rightViewDir = normalize(UNITY_MATRIX_V[0].xyz);
		float yawOffset_Rads = radians(!IsInMirror() ? - _LightingViewDirOffsetYaw : _LightingViewDirOffsetYaw);
		float3 rotatedViewYaw = normalize(RotateAroundAxis(rightViewDir, upViewDir, yawOffset_Rads));
		float3 rotatedViewCameraMeshOffset = RotateAroundAxis((getCameraPosition() - (poiMesh.worldPos)), upViewDir, yawOffset_Rads);
		float pitchOffset_Rads = radians(!IsInMirror() ? _LightingViewDirOffsetPitch : - _LightingViewDirOffsetPitch);
		float3 rotatedViewPitch = RotateAroundAxis(rotatedViewCameraMeshOffset, rotatedViewYaw, pitchOffset_Rads);
		poiLight.direction = normalize(rotatedViewPitch);
	}

	if (!any(poiLight.direction))
	{
		poiLight.direction = float3(.4, 1, .4);
	}

	poiLight.direction = normalize(poiLight.direction);
	poiLight.attenuationStrength = _LightingCastedShadows;
	poiLight.attenuation = 1;
	if (!all(_LightColor0.rgb == 0.0))
	{
		UNITY_LIGHT_ATTENUATION(attenuation, i, poiMesh.worldPos)
		poiLight.attenuation *= attenuation;
	}

	#if defined(HANDLE_SHADOWS_BLENDING_IN_GI)
		half bakedAtten = UnitySampleBakedOcclusion(poiMesh.lightmapUV.xy, poiMesh.worldPos);
		float zDist = dot(_WorldSpaceCameraPos - poiMesh.worldPos, UNITY_MATRIX_V[2].xyz);
		float fadeDist = UnityComputeShadowFadeDistance(poiMesh.worldPos, zDist);
		poiLight.attenuation = UnityMixRealtimeAndBakedShadows(poiLight.attenuation, bakedAtten, UnityComputeShadowFade(fadeDist));
	#endif

	#K#BASE_AFTER_ATTENUATION

	if (!any(poiLight.directColor) && !any(poiLight.indirectColor) && lightMapMode == 0)
	{
		lightMapMode = 1;
		if (_LightingDirectionMode == 0)
		{
			poiLight.direction = normalize(float3(.4, 1, .4));
		}
	}

	poiLight.halfDir = normalize(poiLight.direction + poiCam.viewDir);
	poiLight.vertexNDotL = dot(poiMesh.normals[0], poiLight.direction);
	poiLight.nDotL = dot(poiMesh.normals[1], poiLight.direction);
	poiLight.nDotLSaturated = saturate(poiLight.nDotL);
	poiLight.nDotLNormalized = (poiLight.nDotL + 1) * 0.5;
	poiLight.nDotV = abs(dot(poiMesh.normals[1], poiCam.viewDir));
	poiLight.vertexNDotV = abs(dot(poiMesh.normals[0], poiCam.viewDir));
	poiLight.nDotH = dot(poiMesh.normals[1], poiLight.halfDir);
	poiLight.vertexNDotH = max(0.00001, dot(poiMesh.normals[0], poiLight.halfDir));
	poiLight.lDotv = dot(poiLight.direction, poiCam.viewDir);
	poiLight.lDotH = max(0.00001, dot(poiLight.direction, poiLight.halfDir));
	

	// Poi special light map
	if (lightMapMode == 0)
	{
		float3 ShadeSH9Plus = GetSHLength();
		float3 ShadeSH9Minus = float3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w) + float3(unity_SHBr.z, unity_SHBg.z, unity_SHBb.z) / 3.0;

		float3 greyScaleVector = float3(.33333, .33333, .33333);
		float bw_lightColor = dot(poiLight.directColor, greyScaleVector);
		float bw_directLighting = (((poiLight.nDotL * 0.5 + 0.5) * bw_lightColor * lerp(1, poiLight.attenuation, poiLight.attenuationStrength)) + dot(ShadeSH9(float4(poiMesh.normals[1], 1)), greyScaleVector));
		float bw_directLightingNoAtten = (((poiLight.nDotL * 0.5 + 0.5) * bw_lightColor) + dot(ShadeSH9(float4(poiMesh.normals[1], 1)), greyScaleVector));
		float bw_bottomIndirectLighting = dot(ShadeSH9Minus, greyScaleVector);
		float bw_topIndirectLighting = dot(ShadeSH9Plus, greyScaleVector);
		float lightDifference = ((bw_topIndirectLighting + bw_lightColor) - bw_bottomIndirectLighting);

		poiLight.lightMap = smoothstep(0, lightDifference, bw_directLighting - bw_bottomIndirectLighting);
		poiLight.lightMapNoAttenuation = smoothstep(0, lightDifference, bw_directLightingNoAtten - bw_bottomIndirectLighting);
	}
	// Normalized nDotL
	if (lightMapMode == 1)
	{
		poiLight.lightMapNoAttenuation = poiLight.nDotLNormalized;
		poiLight.lightMap = poiLight.nDotLNormalized * lerp(1, poiLight.attenuation, poiLight.attenuationStrength);
	}
	// Saturated nDotL
	if (lightMapMode == 2)
	{
		poiLight.lightMapNoAttenuation = poiLight.nDotLSaturated;
		poiLight.lightMap = poiLight.nDotLSaturated * lerp(1, poiLight.attenuation, poiLight.attenuationStrength);
	}
	if (lightMapMode == 3)
	{
		poiLight.lightMapNoAttenuation = 1;
		poiLight.lightMap = lerp(1, poiLight.attenuation, poiLight.attenuationStrength);
	}
	if (lightMapMode == 4)
	{
		#if defined(PROP_LIGHTDATASDFMAP) || !defined(OPTIMIZER_ENABLED)
			float2 lightDataSDFMap = 1;
			if (_LightDataSDFMapLOD > 0)
			{
				float sdfLod = pow(_LightDataSDFMapLOD, 4.0);
				lightDataSDFMap = POI2D_SAMPLER_PANGRAD(_LightDataSDFMap, _linear_repeat, poiUV(poiMesh.uv[_LightDataSDFMapUV], _LightDataSDFMap_ST), _LightDataSDFMapPan, max(poiMesh.dx, sdfLod), max(poiMesh.dy, sdfLod)).rg;
			}
			else
			{
				lightDataSDFMap = POI2D_SAMPLER_PAN(_LightDataSDFMap, _linear_repeat, poiUV(poiMesh.uv[_LightDataSDFMapUV], _LightDataSDFMap_ST), _LightDataSDFMapPan).rg;
			}
			
			poiLight.lightMapNoAttenuation = poiLight.nDotLSaturated;
			float3 faceR = mul((float3x3)unity_ObjectToWorld, float3(-1.0, 0.0, 0.0));
			float LdotR = dot(poiLight.direction.xz, faceR.xz);
			float sdf = LdotR < 0 ? lightDataSDFMap.g : lightDataSDFMap.r;

			float3 faceF = mul((float3x3)unity_ObjectToWorld, float3(0.0, 0.0, 1.0)).xyz;
			faceF.y *= _LightDataSDFBlendY;
			faceF = dot(faceF, faceF) == 0 ? 0 : normalize(faceF);
			float3 faceL = poiLight.direction;
			faceL.y *= _LightDataSDFBlendY;
			faceL = dot(faceL, faceL) == 0 ? 0 : normalize(faceL);

			float lnSDF = dot(faceL, faceF);
			poiLight.lightMapNoAttenuation = saturate(lnSDF * 0.5 + sdf * 0.5 + 0.25);
			poiLight.lightMap = saturate(lnSDF * 0.5 + sdf * 0.5 + 0.25) * lerp(1, poiLight.attenuation, poiLight.attenuationStrength);
		#else
			poiLight.lightMapNoAttenuation = poiLight.nDotLNormalized;
			poiLight.lightMap = poiLight.nDotLNormalized * lerp(1, poiLight.attenuation, poiLight.attenuationStrength);
		#endif
	}
	poiLight.lightMapNoAttenuation *= poiLight.detailShadow;
	poiLight.lightMap *= poiLight.detailShadow;

	poiLight.directColor = max(poiLight.directColor, 0.0001);
	poiLight.indirectColor = max(poiLight.indirectColor, 0.0001);

	if (_LightingColorMode == 3)
	{
		// OpenLit
		poiLight.directColor = max(poiLight.directColor, _LightingMinLightBrightness);
	}
	else
	{
		poiLight.directColor = max(poiLight.directColor, poiLight.directColor * min(10000, (_LightingMinLightBrightness * rcp(calculateluminance(poiLight.directColor)))));
		poiLight.indirectColor = max(poiLight.indirectColor, poiLight.indirectColor * min(10000, (_LightingMinLightBrightness * rcp(calculateluminance(poiLight.indirectColor)))));
	}

	poiLight.directColor = lerp(poiLight.directColor, dot(poiLight.directColor, float3(0.299, 0.587, 0.114)), _LightingMonochromatic);
	poiLight.indirectColor = lerp(poiLight.indirectColor, dot(poiLight.indirectColor, float3(0.299, 0.587, 0.114)), _LightingMonochromatic);

	if (_LightingCapEnabled)
	{
		poiLight.directColor = min(poiLight.directColor, _LightingCap);
		poiLight.indirectColor = min(poiLight.indirectColor, _LightingCap);
	}

	if (_LightingForceColorEnabled)
	{
		poiLight.directColor = poiThemeColor(poiMods, _LightingForcedColor, _LightingForcedColorThemeIndex);
	}

	#K#BASE_LIGHTDATA_MOD
	
#endif

#ifdef POI_PASS_ADD
	if (!_LightingAdditiveEnable)
	{
		return float4(mainTexture.rgb * .0001, 1);
	}

	#if defined(DIRECTIONAL)
		if (_DisableDirectionalInAdd)
		{
			return float4(mainTexture.rgb * .0001, 1);
		}
	#endif

	poiLight.direction = normalize(_WorldSpaceLightPos0.xyz - i.worldPos.xyz * _WorldSpaceLightPos0.w);
	#if defined(POINT) || defined(SPOT)
		#ifdef POINT
			unityShadowCoord3 lightCoord = mul(unity_WorldToLight, unityShadowCoord4(poiMesh.worldPos, 1)).xyz;
			poiLight.attenuation = tex2D(_LightTexture0, dot(lightCoord, lightCoord).rr).r;
		#endif

		#ifdef SPOT
			unityShadowCoord4 lightCoord = mul(unity_WorldToLight, unityShadowCoord4(poiMesh.worldPos, 1));
			poiLight.attenuation = (lightCoord.z > 0) * UnitySpotCookie(lightCoord) * UnitySpotAttenuate(lightCoord.xyz);
		#endif
	#else
		UNITY_LIGHT_ATTENUATION(attenuation, i, poiMesh.worldPos)
		poiLight.attenuation = attenuation;
	#endif
	poiLight.additiveShadow = UNITY_SHADOW_ATTENUATION(i, poiMesh.worldPos);
	poiLight.attenuationStrength = _LightingAdditiveCastedShadows;
	poiLight.directColor = _LightingAdditiveLimited ? MaxLuminance(_LightColor0.rgb * poiLight.attenuation, _LightingAdditiveLimit) : _LightColor0.rgb * poiLight.attenuation;

	#if defined(POINT_COOKIE) || defined(DIRECTIONAL_COOKIE)
		poiLight.indirectColor = 0;
	#else
		poiLight.indirectColor = lerp(0, poiLight.directColor, _LightingAdditivePassthrough);
		poiLight.indirectColor = _LightingAdditiveLimited ? MaxLuminance(poiLight.indirectColor, _LightingAdditiveLimit) : poiLight.indirectColor;
	#endif

	
	
	poiLight.directColor = lerp(poiLight.directColor, dot(poiLight.directColor, float3(0.299, 0.587, 0.114)), _LightingAdditiveMonochromatic);
	poiLight.indirectColor = lerp(poiLight.indirectColor, dot(poiLight.indirectColor, float3(0.299, 0.587, 0.114)), _LightingAdditiveMonochromatic);

	poiLight.halfDir = normalize(poiLight.direction + poiCam.viewDir);
	poiLight.nDotL = dot(poiMesh.normals[1], poiLight.direction);
	poiLight.nDotLSaturated = saturate(poiLight.nDotL);
	poiLight.nDotLNormalized = (poiLight.nDotL + 1) * 0.5;
	poiLight.nDotV = abs(dot(poiMesh.normals[1], poiCam.viewDir));
	poiLight.nDotH = dot(poiMesh.normals[1], poiLight.halfDir);
	poiLight.lDotv = dot(poiLight.direction, poiCam.viewDir);
	poiLight.lDotH = dot(poiLight.direction, poiLight.halfDir);
	poiLight.vertexNDotL = dot(poiMesh.normals[0], poiLight.direction);
	poiLight.vertexNDotV = abs(dot(poiMesh.normals[0], poiCam.viewDir));
	poiLight.vertexNDotH = max(0.00001, dot(poiMesh.normals[0], poiLight.halfDir));
	
	// Normalized nDotL
	if (_LightingMapMode == 0 || _LightingMapMode == 1 || _LightingMapMode == 2)
	{
		poiLight.lightMap = poiLight.nDotLNormalized;
	}
	if (_LightingMapMode == 3)
	{
		poiLight.lightMap = 1;
	}
	poiLight.lightMap *= poiLight.detailShadow;
	poiLight.lightMapNoAttenuation = poiLight.lightMap;
	poiLight.lightMap *= lerp(1, poiLight.additiveShadow, poiLight.attenuationStrength);
#endif

//ifex _LightDataDebugEnabled==0
if (_LightDataDebugEnabled)
{
	#ifdef UNITY_PASS_FORWARDBASE
		//UNITY_BRANCH
		if (_LightingDebugVisualize <= 6)
		{
			switch(_LightingDebugVisualize)
			{
				case 0: // Direct Light Color
				return float4(poiLight.directColor + mainTexture.rgb * .0001, 1);
				break;
				case 1: // Indirect Light Color
				return float4(poiLight.indirectColor + mainTexture.rgb * .0001, 1);
				break;
				case 2: // Light Map
				return float4(poiLight.lightMap + mainTexture.rgb * .0001, 1);
				break;
				case 3: // Attenuation
				return float4(poiLight.attenuation + mainTexture.rgb * .0001, 1);
				break;
				case 4: // N Dot L
				return float4(poiLight.nDotLNormalized, poiLight.nDotLNormalized, poiLight.nDotLNormalized, 1) + mainTexture * .0001;
				break;
				case 5:
					return float4(poiLight.halfDir, 1) + mainTexture * .0001;
				break;
				case 6:
					return float4(poiLight.direction, 1) + mainTexture * .0001;
				break;
			}
		}
		else
		{
			return POI_SAFE_RGB1;
		}
	#endif
	#ifdef POI_PASS_ADD
		//UNITY_BRANCH
		if (_LightingDebugVisualize < 6)
		{
			return POI_SAFE_RGB1;
		}
		else
		{
			switch(_LightingDebugVisualize)
			{
				case 7:
					return float4(poiLight.directColor * poiLight.attenuation + mainTexture.rgb * .0001, 1);
				break;
				case 8:
					return float4(poiLight.attenuation + mainTexture.rgb * .0001, 1);
				break;
				case 9:
					return float4(poiLight.additiveShadow + mainTexture.rgb * .0001, 1);
				break;
				case 10:
					return float4(poiLight.nDotLNormalized + mainTexture.rgb * .0001, 1);
				break;
				case 11:
					return float4(poiLight.halfDir, 1) + mainTexture * .0001;
				break;
			}
		}
	#endif
}
//endex