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

<CustomShader version="4">
    <Parameters>
        <Parameter name="fadeProgress" target="fadeProgress" type="float4" 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"/>
        <Parameter name="UVScaleSpeed" target="UVScaleSpeed" type="float4" defaultValue = "5 -2.5 0.0 0.0" minValue = "-10.0 -10.0 -10.0 -10.0" maxValue = "10.0 10.0 10.0 10.0"/>
        <Parameter name="ColorAlpha" target="ColorAlpha" 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 5.0"/>
        <Parameter name="vSpeedFrequencyAmplitudeFadescale" target="vSpeedFrequencyAmplitudeFadescale" type="float4" defaultValue = "10 5 0.25 0" minValue = "0.0 0.0 -25.0 -1.0" maxValue = "100.0 100.0 25.0 1.0"/>
        <Parameter name="alphaBlendStartEnd"    target="alphaBlendStartEnd"     type="float4" defaultValue="75 80 -0.0005 0.1"/>
    </Parameters>
    <Textures>
        <Texture name="fadeMask" defaultColorProfile = "linearRGB" defaultFilename = "../particleSystems/fx_mask01_diffuse.png" defaultWrap="false"/>
        <Texture name="Mask"     defaultColorProfile = "linearRGB" defaultFilename = "../particleSystems/fx_clouds01_diffuse.png"/>
    </Textures>
    <VertexAttributes>
        <VertexAttribute name="uv1" group="base"/>
        <VertexAttribute name="color" group="base"/>
    </VertexAttributes>
    <Variations>
        <Variation name = "CUSTOM_ALPHA_TEST">
<![CDATA[
    #define CUSTOM_ALPHA_TEST
]]>
        </Variation>
    </Variations>
    <LodLevel startDistance = "0" alphaForceMode = "use">
        <CodeInjections>
            <CodeInjection position="SHARED_START">
<![CDATA[
#if defined( CUSTOM_ALPHA_TEST )
    // We do our own alpha testing
    #undef ALPHA_TESTED
#endif   
]]>
            </CodeInjection>
            <CodeInjection position="SAMPLERS">
<![CDATA[
    sampler2D fadeMask;
    sampler2D Mask;
]]>
            </CodeInjection>
            <CodeInjection position="MATERIALINFO">
<![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
    */
    uniform float4 fadeProgress;
    uniform float4 UVScaleSpeed;
    uniform float4 ColorAlpha;
    /*
    vSpeedFrequencyAmplitudeFadescale.x - sin speed
    vSpeedFrequencyAmplitudeFadescale.y - sin frequency
    vSpeedFrequencyAmplitudeFadescale.z - sin amplitude
    vSpeedFrequencyAmplitudeFadescale.w - scale ( by normals ) while fading
    */
    uniform float4 vSpeedFrequencyAmplitudeFadescale;
#if defined( CUSTOM_ALPHA_TEST )
    uniform float4 alphaBlendStartEnd;
#endif
]]>
            </CodeInjection>
            <CodeInjection position="VS_OUTPUT">
<![CDATA[
#if defined( CUSTOM_ALPHA_TEST )
    float  alphaScale : TEXCOORDn;
#endif
float2 defaultTexCoords2 : TEXCOORDn;
]]>
            </CodeInjection>
            <CodeInjection position="POST_SET_TEXCOORDS_VS">
<![CDATA[
    // Pass second UVSet to the Pixel Shader
    Out.defaultTexCoords2 = convertDefaultTexCoords( In, In.texCoords1.xy );
    // =================================
]]>
            </CodeInjection>
            <CodeInjection position="GET_POSITION_VS">
<![CDATA[
    // =================================
    float mSpeed = vSpeedFrequencyAmplitudeFadescale.x * cTime_s;
    float mFrequency = vSpeedFrequencyAmplitudeFadescale.y;
    // Do not use Invert !! ( 1 - x ) strange behavior
    float mAmplitude = vSpeedFrequencyAmplitudeFadescale.z * In.color.g * In.color.r;
    float offset1 = ( 0.5 * sin( mSpeed - In.color.r * mFrequency ) + 0.5 ) * mAmplitude;
    float3 mDeformedPosition = offset1 * In.normal.xyz;
    // =================================
    // FADEIN
    mDeformedPosition = lerp( vSpeedFrequencyAmplitudeFadescale.w * In.normal.xyz, mDeformedPosition, 0.5 * ( fadeProgress.x ) + 0.5 );
    // FADEOUT
    mDeformedPosition = lerp( vSpeedFrequencyAmplitudeFadescale.w * In.normal.xyz, mDeformedPosition, 0.5 * ( fadeProgress.y ) + 0.5 );
    // =================================
    float3 mResultPosition = In.position.xyz + mDeformedPosition;
#if defined(SKINNING)
    return skinPoint(mResultPosition.xyz, In) - float3(invViewMatrix[0][3], invViewMatrix[1][3], invViewMatrix[2][3]);
#else
    return mResultPosition.xyz;
#endif
]]>
            </CodeInjection>
            <CodeInjection position="VS_END">
<![CDATA[
#if defined( CUSTOM_ALPHA_TEST )
    Out.alphaScale = saturate((length(viewDir)-(alphaBlendStartEnd.x))/(alphaBlendStartEnd.x-alphaBlendStartEnd.y));
#endif
]]>
            </CodeInjection>
            <CodeInjection position = "FS_GLOBALS">
<![CDATA[
    float4 gUV1;
    float4 gUV2;
    float4 gVertexColor;
    float  gMask;
]]>
            </CodeInjection>
            <CodeInjection position = "START_FS">
<![CDATA[
    globals.gVertexColor = In.vs.vertexColor.rgba;
    float mSpeed   = UVScaleSpeed.y * cTime_s;
    float mUVscale = UVScaleSpeed.x ;
    globals.gUV1.rg   = 1.00 * mUVscale * In.vs.ALBEDOMAP_TEXCOORD + float2( mSpeed * mUVscale, 0.00 );
    globals.gUV1.ba   = 1.33 * mUVscale * In.vs.ALBEDOMAP_TEXCOORD + float2( mSpeed * mUVscale, 0.15 );
    globals.gUV2.rg   = 1.00 * mUVscale * In.vs.defaultTexCoords2 + float2( mSpeed * mUVscale, 0.00 );
    globals.gUV2.ba   = 1.33 * mUVscale * In.vs.defaultTexCoords2 + float2( mSpeed * mUVscale, 0.15 );
    globals.gMask    = tex2D( Mask, ( 1 / mUVscale ) * globals.gUV1.rg ).r;
]]>
            </CodeInjection>
            <CodeInjection position = "GET_UNNORMALIZED_TANGENT_SPACE_NORMAL_FS">
<![CDATA[
 #if defined( NORMAL_MAP )
    float3 mNormalA  = tex2D( normalMap, globals.gUV1.rg ).xyz;
    float3 mNormalB  = tex2D( normalMap, globals.gUV2.ba ).xyz;
    float3 mResultNormal = lerp( mNormalA, mNormalB, globals.gVertexColor.b ) - 0.5;
    return mResultNormal;
 #endif
]]>
            </CodeInjection>
            <CodeInjection position="POST_DIFFUSE_COLOR_FS">
<![CDATA[
#if defined( ALBEDO_MAP )
    float4 mA = 1.0 * tex2D( baseMap, globals.gUV1.rg ).rgba;
    float4 mB = 0.9 * tex2D( baseMap, globals.gUV1.ba ).rgba;
    float4 mResultColor = lerp( mA, mB, globals.gMask );

    diffuseColor = mResultColor.rgb;
    diffuseColor *= ColorAlpha.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[
    float2 mUV1 = In.vs.ALBEDOMAP_TEXCOORD;
    float2 mUV2 = In.vs.defaultTexCoords2;
    float mEnd1 = tex2D( Mask, 0.5 * ( mUV1 + float2( -0.8, 0.05 ) * cTime_s  ) ).b * tex2D( Mask, 0.76 * ( mUV1 + float2( -1.2, 0.02 )  * cTime_s  ) ).b ;
    float mEnd2 = tex2D( Mask, 0.5 * ( mUV2 + float2( -0.8, 0.05 ) * cTime_s  ) ).b * tex2D( Mask, 0.76 * ( mUV2 + float2( -1.2, 0.02 )  * cTime_s  ) ).b ;
    float mEnd3 = lerp( mEnd1, mEnd2, globals.gVertexColor.b );
    float mEndAlpha = 1 - mUV1.x * mEnd3 * mEnd3  + 0.25 * ( 1 - mUV1.x );
    mEndAlpha *= mEndAlpha;
    mEndAlpha *= globals.gVertexColor.a * ColorAlpha.a;
#if defined( ALBEDO_MAP )
    mEndAlpha *= mResultColor.a;
#endif
    // =================================
    float mFadeIN  = 1.0;
    float mFadeOUT = 1.0;
    // -1.0 ==>  1.0
    mFadeIN   = tex2D( fadeMask, float2( fadeProgress.x, 0 ) + ( 1 - mUV1 ) ).r;
    //  1.0 ==> -1.0
    mFadeOUT  = tex2D( fadeMask, float2( fadeProgress.y, 0 ) + mUV1  ).r;
    // =================================
    float mAlphaValue =  mEndAlpha * mFadeIN * mFadeOUT;
    mEndAlpha = clamp( mEndAlpha, 0.0, 1.0 );
    alpha = mAlphaValue;
]]>
            </CodeInjection>
            <CodeInjection position="FINAL_POS_FS">
<![CDATA[
#if defined( CUSTOM_ALPHA_TEST )
    float mBlend = In.vs.alphaScale * lerp( alphaBlendStartEnd.z, alphaBlendStartEnd.w, alpha );
    clip( mBlend );
    #if defined( DEBUG_SHOW_PARALLAX )
        oColor.rgb  = (float3)mBlend;
    #endif    
#endif
]]>
            </CodeInjection>
            <CodeInjection position = "NUM_TEXCOORDS">
<![CDATA[
#if GPU_PROFILE < GPU_PROFILE_MEDIUM
    #undef ENV_MAP_SPECULAR
    #undef NORMAL_MAP
    #undef SPECULAR
#endif
]]>
            </CodeInjection>
        </CodeInjections>
    </LodLevel>
</CustomShader>
