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

<CustomShader version="5">
    <Parameters>
        <Parameter name="fillTypeId"   target="fillTypeId"   type="float"  group="base" defaultValue="0"/>
        <Parameter name="fadeProgress" target="fadeProgress" type="float4" group="base" defaultValue="1.0 1.0 0.0 0.0" minValue = "-1.0 -1.0 0.0 0.0" maxValue = "1.0 1.0 1.0 1.0"/>
    </Parameters>
    <UvUsages/>
    <Textures>
        <Texture name="baseMap"   group="base" type="2dArray"/>
        <Texture name="cloudMask" group="base" defaultColorProfile = "linearRGB" defaultFilename = "$data/effects/unload/clouds_diffuse.png"/>
    </Textures>
    <VertexAttributes>
        <VertexAttribute name="uv1" group="base"/>
    </VertexAttributes>
    <LodLevel startDistance = "0" alphaForceMode = "use">
        <CodeInjections>
            <CodeInjection position="SHARED_START">
<![CDATA[
#define ALBEDO_MAP_TYPE sampler2DArray
]]>
            </CodeInjection>
            <CodeInjection position="SAMPLERS">
<![CDATA[
    sampler2D cloudMask;
]]>
            </CodeInjection>
            <CodeInjection position="OBJECT_PARAMETERS">
<![CDATA[
    /*
    fadeProgress.x - FadeIN:    start = -1.0 end = 1.0
    fadeProgress.y - FadeOUT:   start = 1.0 end = -1.0

    Fully Visible       fadeProgress.x = 1.0        "1.0 1.0 0.0 0.0"
                        fadeProgress.y = 1.0
    Fully Invisible     fadeProgress.x = -1.0       "-1.0 -1.0 0.0 0.0"
                        fadeProgress.y = -1.0
    */
    float4 fadeProgress;
    float  fillTypeId;
]]>
            </CodeInjection>
            <CodeInjection position="LIB_FUNCTION_VS">
<![CDATA[
// Custom vertex functions
float3 getCustomPositionVS( VS_INPUT    In,
                      ObjectParameters& object,
                                float   mSpeed ){
    // =================================
    float mFrequency = 60.0;
    // Do not use Invert !! ( 1 - x ) strange behavior
    float texCoords1 = convertDefaultTexCoords(In, object, In.texCoords1.xy);
    float mAmplitude = 2.5 * In.texCoords1.y;
    float offset1 = ( 0.5 * sin( mSpeed - ( In.texCoords1.x + In.texCoords1.y ) * mFrequency ) + 0.5 ) * mAmplitude;
    float3 mDeformedPosition = offset1 * In.normal.xyz;
    return In.position.xyz + mDeformedPosition;
}
]]>
            </CodeInjection>
            <CodeInjection position="VS_OUTPUT">
<![CDATA[
float2 defaultTexCoords2 : TEXCOORDn;
]]>
            </CodeInjection>
            <CodeInjection position="POST_SET_TEXCOORDS_VS">
<![CDATA[
    // Pass second UVSet to the Pixel Shader
    Out.defaultTexCoords2 = convertDefaultTexCoords(In, object, In.texCoords1.xy);
    // =================================
]]>
            </CodeInjection>
            <CodeInjection position="GET_POSITION_VS">
<![CDATA[
    float mSpeed = 3.0 * cTime_s;
    return getCustomPositionVS(In, object, mSpeed);
]]>
            </CodeInjection>
            <CodeInjection position="GET_PREV_POSITION_VS">
<![CDATA[
    float mSpeed = 3.0 * cPrevTime_s;
    return getCustomPositionVS(In, object, mSpeed);
]]>
            </CodeInjection>
            <CodeInjection position="POST_GET_WORLD_POSE_VS">
<![CDATA[
    prevWorldPosition = mul(object.prevModelMatrix, float4(prevPosition, 1));
]]>
            </CodeInjection>
            <CodeInjection position="LIB_FUNCTION_FS">
<![CDATA[
float getDepthFade(FS_INPUT In, FS_GLOBALS globals, ObjectParameters& object, float fadeDistance)
{
    float screenDepth = In.vs.screenPosZ / In.vs.screenPosW;
    float screenDepthLinear = convertDepthToEyeZ(screenDepth);
    float sceneDepthLinear = getLinearSceneDepth(In, globals,object);
    return saturate((sceneDepthLinear - screenDepthLinear)/fadeDistance);
}
]]>
            </CodeInjection>
            <CodeInjection position = "FS_GLOBALS">
<![CDATA[
    float4 gUV;
    float  gAlpha;
]]>
            </CodeInjection>
            <CodeInjection position = "START_FS">
<![CDATA[
    float2 mSpeed   = float2(0.1,-0.3) * cTime_s;
    float  mUVscale = 0.75;
    globals.gUV.xy    =(        mUVscale * ALBEDO_MAP_TEXCOORDS ) + mSpeed * mUVscale;
    globals.gUV.zw    =( 1.33 * mUVscale * ALBEDO_MAP_TEXCOORDS ) + mSpeed * mUVscale;
    float2 mA = tex2D( cloudMask, globals.gUV.xy ).ba;
    float2 mB = tex2D( cloudMask, globals.gUV.zw ).ba;
    globals.gAlpha = mA.x * mB.x * mA.y * mB.y;
]]>
            </CodeInjection>
            <CodeInjection position="POST_DIFFUSE_COLOR_FS">
<![CDATA[
#if defined( ALBEDO_MAP )
    float  numMips        = (float)tex2DGetNumMipLevels(baseMap);
    float4 blurredDiffuse = tex2Dlod( baseMap, float3(globals.gUV.xy,object.fillTypeId), numMips-1 ).rgba;
    diffuseColor          = blurredDiffuse.rgb;
#endif
]]>
            </CodeInjection>
            <CodeInjection position="POST_GLOSS_COLOR_FS">
<![CDATA[
#if defined( SPECULAR ) && defined( ALPHA_BLENDED )
    #undef SPECULAR // Disable specular color
#endif
]]>
            </CodeInjection>
            <CodeInjection position="ALPHA_FS">
<![CDATA[
#if defined( ALPHA_BLENDED )
    float mAlphaFresnel = 1.0;
    float mAlphaGrad = 1.0;
    float2 mUV = In.vs.defaultTexCoords2;
    // =================================
    // -1.0 ==>  1.0
    float mFadeIN   = 0.5 * object.fadeProgress.x + 0.5;
    //  1.0 ==> -1.0
    float mFadeOUT  = 0.5 * object.fadeProgress.y + 0.5;
    // =================================
    float4 mUVmega = float4( mUV.x, mUV.y, 1 - mUV.x, 1 - mUV.y );
    mUVmega = 1 - mUVmega;
    mUVmega *= mUVmega * mUVmega;
    mUVmega = 1 - mUVmega;
    mAlphaGrad = mUVmega.x * mUVmega.z * mUVmega.y * mUVmega.w;
    mAlphaFresnel = pow( 1 - facing, 4 );
    alpha = saturate( 5* mAlphaFresnel * mAlphaGrad * globals.gAlpha * mFadeIN * mFadeOUT);
#endif
#if GPU_PROFILE >= GPU_PROFILE_HIGH
    alpha *= getDepthFade(In, globals, object,0.5);
#endif
]]>
            </CodeInjection>
            <CodeInjection position="POST_GET_LIGHTING_FS">
<![CDATA[
    float NdotLNegative = dot(normal, lightDir);
    if (NdotLNegative < 0) {
        NdotLNegative = -NdotLNegative*0.5;
    }
    NdotLNegative = min(NdotLNegative, 1);
    NdotL = saturate(dot(normal, lightDir));
    lighting = ((atten*NdotLNegative) * diffuseShading + (atten*NdotL)* specularShading )* lightColor;
]]>
            </CodeInjection>
        </CodeInjections>
    </LodLevel>
</CustomShader>
