#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
[TextureKeyword][sRGBWarning][ThryRGBAPacker(R, G, B, A, Linear, false)]_LightingAOMaps ("AO Maps (expand)--{reference_properties:[_LightingAOMapsPan, _LightingAOMapsUV,_LightDataAOStrengthR,_LightDataAOStrengthG,_LightDataAOStrengthB,_LightDataAOStrengthA, _LightDataAOGlobalMaskR]}", 2D) = "white" { }
[ThryHideInInspector][Vector2]_LightingAOMapsPan ("Panning", Vector) = (0, 0, 0, 0)
[ThryHideInInspector][ThryWideEnum(UV0, 0, UV1, 1, UV2, 2, UV3, 3, Panosphere, 4, World Pos, 5, Local Pos, 8, Polar UV, 6, Distorted UV, 7, Matcap, 9)] _LightingAOMapsUV ("UV", Int) = 0
[ThryHideInInspector]_LightDataAOStrengthR ("R Strength", Range(0, 1)) = 1
[ThryHideInInspector]_LightDataAOStrengthG ("G Strength", Range(0, 1)) = 0
[ThryHideInInspector]_LightDataAOStrengthB ("B Strength", Range(0, 1)) = 0
[ThryHideInInspector]_LightDataAOStrengthA ("A Strength", Range(0, 1)) = 0
[ThryHideInInspector][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
[ThryHideInInspector][ThryWideEnum(Add, 7, Subtract, 1, Multiply, 2, Divide, 3, Min, 4, Max, 5, Average, 6, Replace, 0)] _LightDataAOGlobalMaskBlendTypeR ("Blending", Range(0, 1)) = 2
[TextureKeyword][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" { }
[ThryHideInInspector][Vector2]_LightingDetailShadowMapsPan ("Panning", Vector) = (0, 0, 0, 0)
[ThryHideInInspector][ThryWideEnum(UV0, 0, UV1, 1, UV2, 2, UV3, 3, Panosphere, 4, World Pos, 5, Local Pos, 8, Polar UV, 6, Distorted UV, 7, Matcap, 9)] _LightingDetailShadowMapsUV ("UV", Int) = 0
[ThryHideInInspector]_LightingDetailShadowStrengthR ("R Strength", Range(0, 1)) = 1
[ThryHideInInspector]_LightingDetailShadowStrengthG ("G Strength", Range(0, 1)) = 0
[ThryHideInInspector]_LightingDetailShadowStrengthB ("B Strength", Range(0, 1)) = 0
[ThryHideInInspector]_LightingDetailShadowStrengthA ("A Strength", Range(0, 1)) = 0
[ThryHideInInspector]_LightingAddDetailShadowStrengthR ("Additive R Strength", Range(0, 1)) = 1
[ThryHideInInspector]_LightingAddDetailShadowStrengthG ("Additive G Strength", Range(0, 1)) = 0
[ThryHideInInspector]_LightingAddDetailShadowStrengthB ("Additive B Strength", Range(0, 1)) = 0
[ThryHideInInspector]_LightingAddDetailShadowStrengthA ("Additive A Strength", Range(0, 1)) = 0
[ThryHideInInspector][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
[ThryHideInInspector][ThryWideEnum(Add, 7, Subtract, 1, Multiply, 2, Divide, 3, Min, 4, Max, 5, Average, 6, Replace, 0)] _LightDataDetailShadowGlobalMaskBlendTypeR ("Blending", Range(0, 1)) = 2
[TextureKeyword][sRGBWarning][ThryRGBAPacker(R, G, B, A, Linear, false)]_LightingShadowMasks ("Shadow Masks (expand)--{reference_properties:[_LightingShadowMasksPan, _LightingShadowMasksUV,_LightingShadowMaskStrengthR,_LightingShadowMaskStrengthG,_LightingShadowMaskStrengthB,_LightingShadowMaskStrengthA, _LightDataShadowMaskGlobalMaskR]}", 2D) = "white" { }
[ThryHideInInspector][Vector2]_LightingShadowMasksPan ("Panning", Vector) = (0, 0, 0, 0)
[ThryHideInInspector][ThryWideEnum(UV0, 0, UV1, 1, UV2, 2, UV3, 3, Panosphere, 4, World Pos, 5, Local Pos, 8, Polar UV, 6, Distorted UV, 7, Matcap, 9)] _LightingShadowMasksUV ("UV", Int) = 0
[ThryHideInInspector]_LightingShadowMaskStrengthR ("R Strength", Range(0, 1)) = 1
[ThryHideInInspector]_LightingShadowMaskStrengthG ("G Strength", Range(0, 1)) = 0
[ThryHideInInspector]_LightingShadowMaskStrengthB ("B Strength", Range(0, 1)) = 0
[ThryHideInInspector]_LightingShadowMaskStrengthA ("A Strength", Range(0, 1)) = 0
[ThryHideInInspector][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
[ThryHideInInspector][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
[TextureKeyword][sRGBWarning][ThryRGBAPacker(R, G, Nothing, Nothing, Linear, false)]_LightDataSDFMap ("SDF Map (expand)--{reference_properties:[_LightDataSDFMapPan, _LightDataSDFMapUV]}", 2D) = "white" { }
[ThryHideInInspector][Vector2]_LightDataSDFMapPan ("Panning", Vector) = (0, 0, 0, 0)
[ThryHideInInspector][ThryWideEnum(UV0, 0, UV1, 1, UV2, 2, UV3, 3, Panosphere, 4, World Pos, 5, Local Pos, 8, Polar UV, 6, Distorted UV, 7, Matcap, 9)] _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)
[ThryHideInInspector][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
[DoNotLock][ToggleUI]_LightingCapEnabled ("Limit Brightness", Float) = 1
[DoNotLock]_LightingCap ("Max Brightness--{condition_showS:(_LightingCapEnabled==1)}", Range(0, 10)) = 1
[DoNotLock]_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
[DoNotLock]_LightingMonochromatic ("Grayscale Lighting", Range(0, 1)) = 0
[ToggleUI]_LightingVertexLightingEnabled ("Vertex lights (Non-Important)", Float) = 1
[ToggleUI]_LightingMirrorVertexLightingEnabled ("Mirror Vertex lights (Non-Important)", Float) = 1
[ToggleUI]_LightingEnableLightVolumes ("Light Volumes", Float) = 1
_LightingLightVolumesNormalBias ("Normal Bias Sampling--{condition_showS:(_LightingEnableLightVolumes==1)}", Range(0, 1)) = 0
[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
#pragma shader_feature_local PROP_LIGHTINGAOMAPS
#pragma shader_feature_local PROP_LIGHTINGDETAILSHADOWMAPS
#pragma shader_feature_local PROP_LIGHTINGSHADOWMASKS
#pragma shader_feature_local PROP_LIGHTDATASDFMAP

#T#PoiLightingDataVariablesExposed
float4 _LightingAOMaps_ST;
float2 _LightingAOMapsPan;
float _LightingAOMapsUV;
float _LightDataAOStrengthR;
float _LightDataAOStrengthG;
float _LightDataAOStrengthB;
float _LightDataAOStrengthA;
float _LightDataAOGlobalMaskR;
float _LightDataAOGlobalMaskBlendTypeR;
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;
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)
	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;
float _LightingEnableLightVolumes;
float _LightingLightVolumesNormalBias;
// Lighting Data Debug
float _LightDataDebugEnabled;
float _LightingDebugVisualize;

uint _UdonForceSceneLighting;

#T#PoiLightingDataVariables
#if defined(PROP_LIGHTINGAOMAPS)
	Texture2D _LightingAOMaps;
#endif
#if defined(PROP_LIGHTINGDETAILSHADOWMAPS)
	Texture2D _LightingDetailShadowMaps;
#endif
#if defined(PROP_LIGHTINGSHADOWMASKS)
	Texture2D _LightingShadowMasks;
#endif
#if defined(PROP_LIGHTDATASDFMAP)
	Texture2D _LightDataSDFMap;
#endif

#T#PoiLightingDataImplementation

if (_UdonForceSceneLighting)
{
	poiLight.lightingMinLightBrightness = 0;
	poiLight.lightingCapEnabled = 0;
	poiLight.lightingMonochromatic = 0;
}
else
{
	poiLight.lightingMinLightBrightness = _LightingMinLightBrightness;
	poiLight.lightingCapEnabled = _LightingCapEnabled;
	poiLight.lightingMonochromatic = _LightingMonochromatic;
}

#if defined(POI_PASS_BASE) || defined(POI_PASS_OUTLINE)
	float3 L0 = float3(0, 0, 0);
	float3 L1r = float3(0, 0, 0);
	float3 L1g = float3(0, 0, 0);
	float3 L1b = float3(0, 0, 0);
	#if POI_PIPE == POI_BIRP
		//ifex _LightingEnableLightVolumes==0  && isNotAnimated(_LightingEnableLightVolumes)
		if (_UdonLightVolumeEnabled && _LightingEnableLightVolumes)
		{
			LightVolumeSH(poiMesh.worldPos, L0, L1r, L1g, L1b, poiMesh.normals[1] * _LightingLightVolumesNormalBias);
			PoiSHAr = float4(L1r, L0.r);
			PoiSHAg = float4(L1g, L0.g);
			PoiSHAb = float4(L1b, L0.b);
			PoiSHBr = 0;
			PoiSHBg = 0;
			PoiSHBb = 0;
			PoiSHC = 0;
		}
		//endex
	#endif
	#if POI_PIPE == POI_URP
		//ifex _LightingEnableLightVolumes==0  && isNotAnimated(_LightingEnableLightVolumes)
		#ifdef PROBE_VOLUMES_L1
		if (_LightingEnableLightVolumes)
		{
			APVResources apvRes = FillAPVResources();
			float3 posWS = GetAbsolutePositionWS(poiMesh.worldPos + poiMesh.normals[1] * _LightingLightVolumesNormalBias);
			posWS = AddNoiseToSamplingPosition(posWS, poiCam.clipPos.xy, poiCam.viewDir);
			APVSample apvSample = SampleAPV(posWS, poiMesh.normals[1], GetMeshRenderingLayer(), poiCam.viewDir);
			if (apvSample.status != APV_SAMPLE_STATUS_INVALID)
			{
				apvSample.Decode();
				PoiSHAr = float4(apvSample.L1_R, apvSample.L0.r);
				PoiSHAg = float4(apvSample.L1_G, apvSample.L0.g);
				PoiSHAb = float4(apvSample.L1_B, apvSample.L0.b);
				#ifdef PROBE_VOLUMES_L2
					PoiSHBr = float4(apvSample.L2_R, apvSample.L0.r);
					PoiSHBg = float4(apvSample.L2_G, apvSample.L0.r);
					PoiSHBb = float4(apvSample.L2_B, apvSample.L0.r);
					PoiSHC = float4(apvSample.L2_C, 0);
				#else
					PoiSHBr = 0;
					PoiSHBg = 0;
					PoiSHBb = 0;
					PoiSHC = 0;
				#endif
			}
		}
		#endif
		//endex
	#endif
#endif

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

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

#ifndef POI_PASS_ADD
	poiLight.detailShadow = poiDetailShadowMain;
#else
	poiLight.detailShadow = poiDetailShadowAdditional;
#endif

// Shadow Masks
#if defined(PROP_LIGHTINGSHADOWMASKS)
	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.r, poiMods.globalMask[_LightDataShadowMaskGlobalMaskR - 1], _LightDataShadowMaskGlobalMaskBlendTypeR);
}

#if POI_PIPE == POI_URP && !defined(POI_PASS_LILFUR)
	poiLight.shadowMask = poiLight.shadowMask * PoiCalculateShadowMask(i.lightmapUV.xy);
#endif

// Ambient Occlusion
#if defined(PROP_LIGHTINGAOMAPS)
	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);
}

Light mainLight;
PoiInitStruct(Light, mainLight);

#if POI_PIPE == POI_BIRP
	mainLight.direction = _WorldSpaceLightPos0.xyz;
	mainLight.color = _LightColor0.rgb;
#elif POI_PIPE == POI_URP
	mainLight = GetMainLight(TransformWorldToShadowCoord(poiMesh.worldPos), poiMesh.worldPos, poiLight.shadowMask);
	BRDFData brdfData;
	InitializeBRDFData(1, 0.0, 0.0, 0.0, poiFragData.alpha, brdfData);
	BRDFData brdfDataClearCoat;
	InitializeBRDFDataClearCoat(0.0, 0.5, brdfData, brdfDataClearCoat);
	poiLight.brdfData = brdfData;
	poiLight.brdfDataClearCoat = brdfDataClearCoat;
#endif

// Main light without loop
#if POI_PIPE == POI_BIRP && defined(POI_PASS_ADD)
	poiLight.lightType = (_WorldSpaceLightPos0.w == 0.0) ? POI_DIRECTIONAL : POI_POINT_SPOT;
#else
	poiLight.lightType = POI_DIRECTIONAL;
#endif
poiLight.unityLight = mainLight;

float4 debugLight = PoiLightDataSetup(poiLight, poiFragData, poiMesh, poiCam, poiMods, i, mainTexture);

#if defined(POI_PASS_BASE) || defined(POI_PASS_ADD)
	if (_LightDataDebugEnabled) return debugLight;
#endif

#T#PoiLightDataFunction

float4 PoiLightDataSetupAdd(
	inout PoiLight poiLight,
	inout PoiFragData poiFragData,
	in PoiMesh poiMesh,
	in PoiCam poiCam,
	in PoiMods poiMods,
	#ifdef POI_PASS_LILFUR
		in PoiGeoToFragLilFur i,
	#else
		in VertexOut i,
	#endif
	in float4 mainTexture)
{
	poiLight.finalLightAdd = 0;

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

	#if defined(DIRECTIONAL)
		if (_DisableDirectionalInAdd)
		{
			return float4(mainTexture.rgb * .0001, 1);
		}
	#endif
	#if POI_PIPE == POI_BIRP
		poiLight.direction = normalize(_WorldSpaceLightPos0.xyz - poiMesh.worldPos.xyz * _WorldSpaceLightPos0.w);
	#elif POI_PIPE == POI_URP
		poiLight.direction = poiLight.unityLight.direction.xyz;
	#endif

	#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
		#if POI_PIPE == POI_BIRP
			#ifndef POI_PASS_LILFUR
				UNITY_LIGHT_ATTENUATION(attenuation, i, poiMesh.worldPos)
				poiLight.attenuation = attenuation;
			#else
				poiLight.attenuation = 1;
			#endif
		#elif POI_PIPE == POI_URP
			poiLight.attenuation = poiLight.unityLight.distanceAttenuation * poiLight.unityLight.shadowAttenuation;
		#endif
	#endif

	#if POI_PIPE == POI_BIRP && !defined(POI_PASS_LILFUR)
		poiLight.additiveShadow = UNITY_SHADOW_ATTENUATION(i, poiMesh.worldPos);
	#elif POI_PIPE == POI_URP
		poiLight.additiveShadow = poiLight.unityLight.shadowAttenuation;
	#endif
	poiLight.attenuationStrength = _LightingAdditiveCastedShadows;
	poiLight.directColor = _LightingAdditiveLimited ? MaxLuminance(poiLight.unityLight.color * poiLight.attenuation, _LightingAdditiveLimit) : poiLight.unityLight.color * 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);

	float4 debugColor = 0;

	//ifex _LightDataDebugEnabled==0
	if (_LightDataDebugEnabled)
	{
		switch(_LightingDebugVisualize)
		{
			case 7:
			{
				debugColor = float4(poiLight.directColor * poiLight.attenuation + mainTexture.rgb * .0001, 1);
				break;
			}
			case 8:
			{
				debugColor = float4(poiLight.attenuation + mainTexture.rgb * .0001, 1);
				break;
			}
			case 9:
			{
				debugColor = float4(poiLight.additiveShadow + mainTexture.rgb * .0001, 1);
				break;
			}
			case 10:
			{
				debugColor = float4(poiLight.nDotLNormalized + mainTexture.rgb * .0001, 1);
				break;
			}
			case 11:
			{
				debugColor = float4(poiLight.halfDir, 1) + mainTexture * .0001;
				break;
			}
		}
	}
	//endex

	return debugColor;
}

float4 PoiLightDataSetup(
	inout PoiLight poiLight,
	inout PoiFragData poiFragData,
	in PoiMesh poiMesh,
	in PoiCam poiCam,
	in PoiMods poiMods,
	#ifdef POI_PASS_LILFUR
		in PoiGeoToFragLilFur i,
	#else
		in VertexOut i,
	#endif
	in float4 mainTexture)
{
	bool lightExists = false;

	if (any(poiLight.unityLight.color.rgb >= 0.002))
	{
		lightExists = true;
	}

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

	#if POI_PIPE == POI_BIRP
		if (_LightingVertexLightingEnabled)
		{
			#if defined(VERTEXLIGHT_ON)
				float4 toLightX = unity_4LightPosX0 - poiMesh.worldPos.x;
				float4 toLightY = unity_4LightPosY0 - poiMesh.worldPos.y;
				float4 toLightZ = unity_4LightPosZ0 - poiMesh.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
		}
	#endif

	//UNITY_BRANCH
	if (_LightingColorMode == 0) // Poi Custom Light Color
	{
		float3 magic = max(BetterSH9(normalize(PoiSHAr + PoiSHAg + PoiSHAb)), 0);
		float3 normalLight = poiLight.unityLight.color.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));

		{
			float3 indirectSampleNormal = lerp(0, poiMesh.normals[1], _LightingIndirectUsesNormals);
			#ifdef POI_BENTNORMALMAP
				if (poiMesh.hasBentNormal > 0.5 && _BentNormalIndirectDiffuse > 0.5)
				{
					indirectSampleNormal = lerp(indirectSampleNormal, poiMesh.bentNormal, _LightingIndirectUsesNormals);
				}
			#endif
			poiLight.indirectColor = BetterSH9(float4(indirectSampleNormal, 1));
		}
	}

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

	{
		float3 indirectNormal = poiMesh.normals[1];
		#ifdef POI_BENTNORMALMAP
			if (poiMesh.hasBentNormal > 0.5 && _BentNormalIndirectDiffuse > 0.5)
			{
				indirectNormal = poiMesh.bentNormal;
			}
		#endif
		float3 indirectColor = BetterSH9(float4(indirectNormal, 1));
		if (lightExists)
		{
			poiLight.directColor = poiLight.unityLight.color.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(PoiShadeSH9(half4(0.0, 0.0, 0.0, 1.0)), PoiShadeSH9(half4(0.0, -1.0, 0.0, 1.0)).rgb) * _Unlit_Intensity));
		poiLight.directColor = max(poiLight.indirectColor, poiLight.unityLight.color.rgb);
	}

	if (_LightingColorMode == 3) // OpenLit

	{
		float3 lightDirectionForSH9 = OpenLitLightingDirectionForSH9(poiLight.unityLight);
		OpenLitShadeSH9ToonDouble(lightDirectionForSH9, poiLight.directColor, poiLight.indirectColor);
		poiLight.directColor += poiLight.unityLight.color.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(poiLight.unityLight.color.rgb) * poiLight.unityLight.direction.xyz + (PoiSHAr.xyz + PoiSHAg.xyz + PoiSHAb.xyz) / 3.0;
	}
	if (_LightingDirectionMode == 1 || _LightingDirectionMode == 2)
	{
		//UNITY_BRANCH
		if (_LightingDirectionMode == 1)
		{
			poiLight.direction = mul((float3x3)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, poiLight.unityLight.direction.xyz, any(poiLight.unityLight.direction.xyz)));
		poiLight.direction = lightDirection;
	}
	if (_LightingDirectionMode == 4) // OpenLit

	{
		poiLight.direction = OpenLitLightingDirection(poiLight.unityLight); // 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(poiLight.unityLight.color.rgb == 0.0))
	{
		#if POI_PIPE == POI_BIRP
			#ifndef POI_PASS_LILFUR
				UNITY_LIGHT_ATTENUATION(attenuation, i, poiMesh.worldPos)
				poiLight.attenuation *= attenuation;
			#endif
		#elif POI_PIPE == POI_URP
			poiLight.attenuation *= poiLight.unityLight.distanceAttenuation * poiLight.unityLight.shadowAttenuation;
		#endif
	}

	#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.nDotVCentered = abs(dot(poiMesh.normals[1], normalize(getCameraPosition() - poiMesh.worldPos.xyz)));
	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(PoiSHAr.w, PoiSHAg.w, PoiSHAb.w) + float3(PoiSHBr.z, PoiSHBg.z, PoiSHBb.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(PoiShadeSH9(float4(poiMesh.normals[1], 1)), greyScaleVector));
		float bw_directLightingNoAtten = (((poiLight.nDotL * 0.5 + 0.5) * bw_lightColor) + dot(PoiShadeSH9(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)
			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, poiLight.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)))));
		
		// Luminance pushes min brightness red
		poiLight.directColor = max(poiLight.directColor, poiLight.lightingMinLightBrightness);
		poiLight.indirectColor = max(poiLight.indirectColor, poiLight.lightingMinLightBrightness);
	}

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

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

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

	#K#BASE_LIGHTDATA_MOD

	float4 debugColor = 0;

	#ifdef POI_PASS_ADD
		debugColor = PoiLightDataSetupAdd(poiLight, poiFragData, poiMesh, poiCam, poiMods, i, mainTexture);
	#endif

	//ifex _LightDataDebugEnabled==0
	if (_LightDataDebugEnabled)
	{
		#ifdef POI_PASS_BASE
			switch(_LightingDebugVisualize)
			{
				case 0: // Direct Light Color
				{
					debugColor = float4(poiLight.directColor + mainTexture.rgb * .0001, 1);
					break;
				}
				case 1: // Indirect Light Color
				{
					debugColor = float4(poiLight.indirectColor + mainTexture.rgb * .0001, 1);
					break;
				}
				case 2: // Light Map
				{
					debugColor = float4(poiLight.lightMap + mainTexture.rgb * .0001, 1);
					break;
				}
				case 3: // Attenuation
				{
					debugColor = float4(poiLight.attenuation + mainTexture.rgb * .0001, 1);
					break;
				}
				case 4: // N Dot L
				{
					debugColor = float4(poiLight.nDotLNormalized, poiLight.nDotLNormalized, poiLight.nDotLNormalized, 1) + mainTexture * .0001;
					break;
				}
				case 5:
				{
					debugColor = float4(poiLight.halfDir, 1) + mainTexture * .0001;
					break;
				}
				case 6:
				{
					debugColor = float4(poiLight.direction, 1) + mainTexture * .0001;
					break;
				}
			}
		#endif
	}
	//endex
	return debugColor;
}