#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)
[ThryWideEnum(Replace, 0, Multiply, 1, Add, 2)]_CubemapBlendType ("Blending", Float) = 0
_CubeMapBlendAmount ("Blend Alpha", Range(0, 1)) = 1

_CubeMapIntensity ("Brightness", Range(0, 5)) = 1
_CubeMapEmissionStrength ("Emission Strength", Range(0, 20)) = 0
_CubeMapSmoothness ("Smoothness", Range(0, 1)) = 1
[Enum(Vertex, 0, Pixel, 1)] _CubeMapNormal ("Normal to use", Int) = 1


[HideInInspector] s_start_CubeMapMasking ("Masking--{persistent_expand:true,default_expand:true}", Float) = 1
[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, 5, Local Pos, 8, 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
_CubeMapLightMask ("Hide in Shadow", Range(0, 1)) = 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(Add, 7, Subtract, 1, Multiply, 2, Divide, 3, Min, 4, Max, 5, Average, 6, Replace, 0)]_CubeMapMaskGlobalMaskBlendType ("Blending", Range(0, 1)) = 2
[HideInInspector] s_end_CubeMapMasking ("Masking", Float) = 0

[HideInInspector] s_start_CubeMapColorAdjust ("Color Adjust--{reference_property:_CubeMapHueShiftEnabled,persistent_expand:true,default_expand:false}", Float) = 0
[HideInInspector][ToggleUI]_CubeMapHueShiftEnabled ("Enabled", Float) = 0
[ThryWideEnum(OKLab, 0, HSV, 1)] _CubeMapHueShiftColorSpace ("Color Space", Int) = 0
[ThryWideEnum(Hue Select, 0, Hue Shift, 1)] _CubeMapHueSelectOrShift ("Select or Shift", Int) = 1
_CubeMapHueShiftSpeed ("Hue Shift Speed", Float) = 0
_CubeMapHueShift ("Hue Shift", Range(0, 1)) = 0
_CubeMapBrightness ("Brightness", Range(-1, 1)) = 0
_CubeMapContrast ("Contrast", Range(-1, 10)) = 1
_CubeMapSaturation ("Saturation", Range(-1, 10)) = 1
[HideInInspector] s_end_CubeMapColorAdjust ("", Float) = 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 _CubemapBlendType;
    float _CubeMapBlendAmount;
    float _CubeMapEnable;
    float _CubeMapLightMask;
    float _CubeMapEmissionStrength;
    float _CubeMapNormal;
    float _CubeMapHueShiftEnabled;
    float _CubeMapHueShiftSpeed;
    float _CubeMapHueShift;
    float _CubeMapHueShiftColorSpace;
    float _CubeMapHueSelectOrShift;
    float _CubeMapSaturation;
    float _CubeMapBrightness;
    float _CubeMapContrast;
    float _CubeMapSmoothness;
#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 = texCUBElod(_CubeMap, float4(CubeMapUV, (1 - _CubeMapSmoothness) * (1 - _CubeMapSmoothness) * 8));
            

            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 = maskBlend(CubeMapMask, poiMods.globalMask[_CubeMapMaskGlobalMask - 1], _CubeMapMaskGlobalMaskBlendType);
        }
        
        if (_CubeMapMaskInvert)
        {
            CubeMapMask = 1 - CubeMapMask;
        }
        
        //UNITY_BRANCH
        if (_CubeMapHueShiftEnabled)
        {
            cubeMap.rgb = hueShift(cubeMap.rgb, _CubeMapHueShift + _Time.x * _CubeMapHueShiftSpeed, _CubeMapHueShiftColorSpace, _CubeMapHueSelectOrShift);
            cubeMap = PoiColorBCS(cubeMap, _CubeMapBrightness, _CubeMapContrast, _CubeMapSaturation);
            //cubeMap.rgb = ModifyViaHSV(cubeMap.rgb, _CubeMapHueShift + _Time.x * _CubeMapHueShiftSpeed, _CubeMapSaturation, _CubeMapValue);

        }
        CubeMapMask = min(CubeMapMask, lerp(1, poiLight.rampedLightMap, _CubeMapLightMask));
        float cubeMapAlpha = CubeMapMask * cubeMap.a * _CubeMapBlendAmount;
        poiFragData.baseColor.rgb = lerp(poiFragData.baseColor.rgb, cubeMap.rgb, cubeMapAlpha * (_CubemapBlendType == 0));
        poiFragData.baseColor.rgb *= lerp(1, cubeMap.rgb, cubeMapAlpha * (_CubemapBlendType == 1));
        poiFragData.baseColor.rgb += cubeMap.rgb * cubeMapAlpha * (_CubemapBlendType == 2);
        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

