#T#PoiCubeMapProperties
//ifex _CubeMapEnabled==0
[HideInInspector] m_start_CubeMap ("CubeMap--{reference_property:_CubeMapEnabled,button_help:{text:Tutorial,action:{type:URL,data:https://www.poiyomi.com/shading/cubemap},hover:Documentation}}", Float) = 0
[HideInInspector][ThryToggle(_CUBEMAP)]_CubeMapEnabled ("Enable CubeMap", Float) = 0
[ThryWideEnum(Skybox, 0, Reflection, 1, World Normal Direction, 2, Local Normal Direction, 3)] _CubeMapUVMode ("UV Mode", Int) = 1
_CubeMapWorldNormalsStrength ("Normals Strength--{condition_showS:(_CubeMapUVMode==2)}", Range(0,1)) = 1
_CubeMapColor ("Color--{reference_property:_CubeMapColorThemeIndex}", Color) = (1, 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)] _CubeMapColorThemeIndex ("", Int) = 0
[NoScaleOffset]_CubeMap ("CubeMap--{reference_properties:[_CubeMapRotation, _CubeMapRotationPan]}", Cube) = "" { }
[HideInInspector][Vector3]_CubeMapRotation ("Rotation in Degrees", Vector) = (0, 0, 0, 0)
[HideInInspector][Vector3]_CubeMapRotationPan ("Panning in Degrees", Vector) = (0, 0, 0, 0)
[sRGBWarning]_CubeMapMask ("Mask--{reference_properties:[_CubeMapMaskPan, _CubeMapMaskUV, _CubeMapMaskChannel, _CubeMapMaskInvert]}", 2D) = "white" { }
[HideInInspector][Vector2]_CubeMapMaskPan ("Panning", Vector) = (0, 0, 0, 0)
[HideInInspector][ThryWideEnum(UV0, 0, UV1, 1, UV2, 2, UV3, 3, Panosphere, 4, World Pos XZ, 5, Polar UV, 6, Distorted UV, 7)] _CubeMapMaskUV ("UV", Int) = 0
[HideInInspector][Enum(R, 0, G, 1, B, 2, A, 3)]_CubeMapMaskChannel ("Channel", Float) = 0
[HideInInspector][ToggleUI]_CubeMapMaskInvert ("Invert", Float) = 0
[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)] _CubeMapMaskGlobalMask ("Global Mask--{reference_property:_CubeMapMaskGlobalMaskBlendType}", Int) = 0
[HideInInspector][ThryWideEnum(Replace, 0, Darken, 1, Multiply, 2, Color Burn, 3, Linear Burn, 4, Lighten, 5, Screen, 6, Color Dodge, 7, Linear Dodge(Add), 8, Overlay, 9, Soft Lighten, 10, Hard Light, 11, Vivid Light, 12, Linear Light, 13, Pin Light, 14, Hard Mix, 15, Difference, 16, Exclusion, 17, Subtract, 18, Divide, 19)]_CubeMapMaskGlobalMaskBlendType ("Blending", Range(0, 1)) = 2
_CubeMapEmissionStrength ("Emission Strength", Range(0, 20)) = 0
_CubeMapIntensity ("Color Strength", Range(0, 5)) = 1
_CubeMapLightMask ("Hide in Shadow", Range(0, 1)) = 0
_CubeMapReplace ("Replace With CubeMap", Range(0, 1)) = 1
_CubeMapMultiply ("Multiply CubeMap", Range(0, 1)) = 0
_CubeMapAdd ("Add CubeMap", Range(0, 1)) = 0
[Enum(Vertex, 0, Pixel, 1)] _CubeMapNormal ("Normal to use", Int) = 1

[Space(10)]
[ThryHeaderLabel(Hue Shift, 13)]
[Space(4)]
[ToggleUI]_CubeMapHueShiftEnabled ("Enabled", Float) = 0
_CubeMapHueShiftSpeed ("Shift Speed--{condition_showS:(_CubeMapHueShiftEnabled==1)}", Float) = 0
_CubeMapHueShift ("Hue Shift--{condition_showS:(_CubeMapHueShiftEnabled==1)}", Range(0, 1)) = 0
[HideInInspector] m_end_CubeMap ("CubeMap", Float) = 0
//endex

#T#PoiCubeMap_Keyword
//ifex _CubeMapEnabled==0
#pragma shader_feature_local _CUBEMAP
//endex

#T#PoiCubeMapVariables
//ifex _CubeMapEnabled==0
#ifdef _CUBEMAP
	#if defined(PROP_CUBEMAP) || !defined(OPTIMIZER_ENABLED)
		samplerCUBE _CubeMap;
		float3 _CubeMapRotation;
		float3 _CubeMapRotationPan;
	#endif
	#if defined(PROP_CUBEMAPMASK) || !defined(OPTIMIZER_ENABLED)
		Texture2D _CubeMapMask;
		float4 _CubeMapMask_ST;
		float2 _CubeMapMaskPan;
		float _CubeMapMaskUV;
		float _CubeMapMaskChannel;
	#endif
	float _CubeMapUVMode;
	float _CubeMapWorldNormalsStrength;
	float _CubeMapMaskInvert;
	float _CubeMapMaskGlobalMask;
	float _CubeMapMaskGlobalMaskBlendType;
	float4 _CubeMapColor;
	float _CubeMapColorThemeIndex;
	float _CubeMapIntensity;
	float _CubeMapReplace;
	float _CubeMapMultiply;
	float _CubeMapAdd;
	float _CubeMapEnable;
	float _CubeMapLightMask;
	float _CubeMapEmissionStrength;
	float _CubeMapNormal;
	float _CubeMapHueShiftEnabled;
	float _CubeMapHueShiftSpeed;
	float _CubeMapHueShift;
#endif
//endex

#T#PoiCubeMapFunctions
//ifex _CubeMapEnabled==0
#ifdef _CUBEMAP
	#if defined(PROP_CUBEMAP) || !defined(OPTIMIZER_ENABLED)
		// From Unity's MIT'd Skybox-Cubed.shader
		float3 RotateAroundYInDegrees(float3 dir, float degrees)
		{
			float alpha = degrees * UNITY_PI / 180.0;
			float sina, cosa;
			sincos(alpha, sina, cosa);
			float2x2 m = float2x2(cosa, -sina, sina, cosa);
			return float3(mul(m, dir.xz), dir.y).xzy;
		}
	#endif
	void applyCubemap(inout PoiFragData poiFragData, in PoiCam poiCam, in PoiMesh poiMesh, in PoiLight poiLight, in PoiMods poiMods)
	{
		float3 CubeMapUV = 0;

		switch(_CubeMapUVMode)
		{
			case 0: // Skybox
				CubeMapUV = -poiCam.viewDir;
				break;
			case 1: // Reflection
				CubeMapUV = poiCam.reflectionDir;
				break;
			case 2: // World Normal Direction
				CubeMapUV = lerp(poiMesh.normals[0], poiMesh.normals[1], _CubeMapWorldNormalsStrength);
				break;
			case 3: // Local Normal Direction
				CubeMapUV = poiMesh.objNormal;
				break;
		}

		#if defined(PROP_CUBEMAP) || !defined(OPTIMIZER_ENABLED)
			if (any(_CubeMapRotation.xyz) || any(_CubeMapRotationPan.xyz))
			{
				// Do funny swizzle so we don't have to make a new function for every direction
				CubeMapUV = RotateAroundYInDegrees(CubeMapUV.yxz, _CubeMapRotation.x + (_CubeMapRotationPan.x * _Time.y)).yxz;
				CubeMapUV = RotateAroundYInDegrees(CubeMapUV.xyz, _CubeMapRotation.y + (_CubeMapRotationPan.y * _Time.y)).xyz;
				CubeMapUV = RotateAroundYInDegrees(CubeMapUV.xzy, _CubeMapRotation.z + (_CubeMapRotationPan.z * _Time.y)).xzy;
			}
			float4 cubeMap = texCUBE(_CubeMap, CubeMapUV);
			cubeMap.rgb *= poiThemeColor(poiMods, _CubeMapColor, _CubeMapColorThemeIndex);
		#else
			float4 cubeMap = float4(0.21763764082, 0.21763764082, 0.21763764082, .5) * float4(poiThemeColor(poiMods, _CubeMapColor, _CubeMapColorThemeIndex), 1);
		#endif

		cubeMap.rgb *= _CubeMapIntensity;
		#if defined(PROP_CUBEMAPMASK) || !defined(OPTIMIZER_ENABLED)
			float CubeMapMask = POI2D_SAMPLER_PAN(_CubeMapMask, _MainTex, poiUV(poiMesh.uv[_CubeMapMaskUV], _CubeMapMask_ST), _CubeMapMaskPan)[_CubeMapMaskChannel];
		#else
			float CubeMapMask = 1;
		#endif

		if (_CubeMapMaskGlobalMask > 0)
		{
			CubeMapMask = customBlend(CubeMapMask, poiMods.globalMask[_CubeMapMaskGlobalMask-1], _CubeMapMaskGlobalMaskBlendType);
		}
		
		if (_CubeMapMaskInvert)
		{
			CubeMapMask = 1 - CubeMapMask;
		}
		
		//UNITY_BRANCH
		if (_CubeMapHueShiftEnabled)
		{
			cubeMap.rgb = hueShift(cubeMap.rgb, _CubeMapHueShift + _Time.x * _CubeMapHueShiftSpeed);
		}
		CubeMapMask = min(CubeMapMask, lerp(1, poiLight.rampedLightMap, _CubeMapLightMask));
		poiFragData.baseColor.rgb = lerp(poiFragData.baseColor.rgb, cubeMap.rgb, _CubeMapReplace * CubeMapMask * cubeMap.a);
		poiFragData.baseColor.rgb *= lerp(1, cubeMap.rgb, _CubeMapMultiply * CubeMapMask * cubeMap.a);
		poiFragData.baseColor.rgb += cubeMap.rgb * _CubeMapAdd * CubeMapMask * cubeMap.a;
		poiFragData.emission += cubeMap.rgb * _CubeMapEmissionStrength * CubeMapMask * cubeMap.a;
	}
#endif
//endex

#T#PoiCubeMapFunctionCalls
//ifex _CubeMapEnabled==0
#ifdef _CUBEMAP
	applyCubemap(poiFragData, poiCam, poiMesh, poiLight, poiMods);
#endif
//endex

