<?xml version="1.0" encoding="utf-8"?>

<CustomShader version="3">
    <Parameters>
        <Parameter name = "UVScaleSpeed" target = "UVScaleSpeed" type = "float4"  defaultValue = "1.0 2.0 0.005 -0.001" minValue = "-10.0 -10.0 -10.0 -10.0"  maxValue = "10.0 10.0 10.0 10.0"/>
        <Parameter name = "colorScale"   target = "colorScale"   type = "float4"  defaultValue = "1.0 1.0 1.0 1.0"      minValue = "0.0 0.0 0.0 0.0"          maxValue = "1.0 1.0 1.0 1.0"/>
    </Parameters>
    <Textures>
        <Texture name = "mistNmap" defaultColorProfile = "linearRGB" defaultFilename = "mist_diffuse.png"/>
    </Textures>
    <Variations>
        <Variation name = "TOP_DOWN">
<![CDATA[
    #define TOP_DOWN
]]>
        </Variation>
    </Variations>
    <LodLevel startDistance="0" alphaForceMode = "use">
        <CodeInjections>
            <CodeInjection position = "SAMPLERS">
<![CDATA[
sampler2D mistNmap;
]]>
            </CodeInjection>
            <CodeInjection position="MATERIALINFO">
<![CDATA[
    uniform float4 UVScaleSpeed;
    uniform float4 colorScale;
]]>
            </CodeInjection>
            <CodeInjection position="VS_OUTPUT">
<![CDATA[
#if defined( TOP_DOWN )
float2 albedoMapTexCoords2 : TEXCOORDn;
#endif
]]>
            </CodeInjection>
            <CodeInjection position="POST_SET_TEXCOORDS_VS">
<![CDATA[
#if defined( TOP_DOWN )
    // Pass second UVSet to the Pixel Shader
    Out.albedoMapTexCoords2 = convertDefaultTexCoords( In, In.texCoords1.xy );
#endif
]]>
            </CodeInjection>
            <CodeInjection position="POST_GLOSS_COLOR_FS">
<![CDATA[
// Disable specular color for alphablended billboards
#if defined(SPECULAR) && defined(ALPHA_BLENDED)
    #undef SPECULAR
#endif
]]>
            </CodeInjection>
            <CodeInjection position="EMISSIVE_FS">
<![CDATA[
#if defined( EMISSIVE_MAP )
    emissiveColor.rgb *= colorScale.rgb;
#endif
]]>
            </CodeInjection> 
            <CodeInjection position="ALPHA_FS">
<![CDATA[
    float  mMist = 1.0;
    float  mPixelDepth = 1.0;
    float2 mSpeed   = UVScaleSpeed.zw * cTime_s;
    float2 mUVscale = UVScaleSpeed.xy ;
    float4 mA = tex2D( mistNmap, mUVscale * ( 1.00 * In.vs.ALBEDOMAP_TEXCOORD + mSpeed ) ).rgba;
    // ------------------------------------------------
    mMist = mA.g * mA.b + mA.r;
    mMist = clamp( mMist, 0.0 , 1.0 );
    // ------------------------------------------------
    float mFacing       = max(dot(viewDir, normal),0 );
    float mGradient     = In.vs.ALBEDOMAP_TEXCOORD.y;
    float mFinalAlpha   = 1.0;
    
    #if defined( TOP_DOWN )
        float mSphere   = 1 - length( In.vs.ALBEDOMAP_TEXCOORD.xy - float2(0.5, 0.5)) * 1.41421356237;
        mSphere         = pow( mSphere, 4 );
        mFinalAlpha     = mSphere;
        // --------------------------------------------
        mGradient       = In.vs.albedoMapTexCoords2.y;
    #endif
    mFacing     = pow( mFacing,   5);
    mGradient   = pow( mGradient, 5);
    mPixelDepth = 1 - 0.01*In.vs.vertToEyeVectorProjDistance;
    // ------------------------------------------------
    mFinalAlpha *= mPixelDepth * mMist * mFacing * mGradient * colorScale.a;
    // ------------------------------------------------
    alpha = mFinalAlpha;
]]>
            </CodeInjection>
            <CodeInjection position="GET_FOG_COLOR_FS">
<![CDATA[
#if defined(ALPHA_BLENDED) && FOG_MODE != 0
    return oColor.xyz*In.vs.fogFactor; // In additive blending mode, we need to use a black fog color
#endif
]]>
            </CodeInjection>
            <CodeInjection position="FINAL_POS_FS">
<![CDATA[
#if defined(ALPHA_BLENDED)
    oColor.a = 0.0; // additive blending
    #if defined( DEBUG_SHOW_PARALLAX )
        oColor.rgb = mFinalAlpha;
        oColor.a = 1.0f;
    #endif
#endif
]]>
            </CodeInjection>
        </CodeInjections>
    </LodLevel>
</CustomShader>