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

<CustomShader version="3">
    <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"/>
    </Parameters>
    <Textures>
        <Texture name="fadeMask" defaultColorProfile = "linearRGB" defaultFilename = "fx_mask01_diffuse.png"/>
        <Texture name="Mask"     defaultColorProfile = "linearRGB" defaultFilename = "fx_clouds01_diffuse.png"/>
    </Textures>
    <LodLevel startDistance = "0" alphaForceMode = "use">
        <CodeInjections>
            <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;
]]>
            </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, In.texCoords1.xy );
    // =================================
]]>
            </CodeInjection>
            <CodeInjection position="GET_POSITION_VS">
<![CDATA[
#if defined( VERTEX_COLOR )
    float4 mVertexColor = In.color.rgba; 
    /*
    In.vs.vertexColor.r - sin frequency offset + sin amplitude ( as usual like uv gradient )
    In.vs.vertexColor.g - sin amplitude
    In.vs.vertexColor.b - mask for 2nd UVset ( seam removal code )
    In.vs.vertexColor.a - transparency 
    */
#endif
    // =================================    
    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 );
    // =================================
    return float4( In.position.xyz + mDeformedPosition, 1.0 );
]]>
            </CodeInjection>
            <CodeInjection position = "FS_GLOBALS">
<![CDATA[
    float4 gUV1;
    float4 gUV2;
    float4 gVertexColor; 
    float  gMask;
]]>
            </CodeInjection>
            <CodeInjection position = "START_FS">
<![CDATA[
    globals.gVertexColor = float4( 1.0,1.0,1.0,1.0 );
#if defined( VERTEX_COLOR )
    globals.gVertexColor = In.vs.vertexColor.rgba; 
    #undef VERTEX_COLOR
#endif
    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_NORMAL_FS">
<![CDATA[
 #if defined( NORMAL_MAP )
    float3 mNormalA  = tex2D( normalMap, globals.gUV1.rg ).xyz * 2.0 - 1.0;
    float3 mNormalB  = tex2D( normalMap, globals.gUV2.ba ).xyz * 2.0 - 1.0;
    float3 mResultNormal = lerp( mNormalA, mNormalB, globals.gVertexColor.b );
    return normalize( mResultNormal );
 #endif
]]>
            </CodeInjection>
            <CodeInjection position="POST_ALBEDO_FS">
<![CDATA[
#if defined( ALBEDO_MAP )
    float4 mResultColor = albedo.rgba;
    
    float4 mA = 1.0 * tex2D( baseMap, globals.gUV1.rg ).rgba;
    float4 mB = 0.9 * tex2D( baseMap, globals.gUV1.ba ).rgba;
    mResultColor = lerp( mA, mB, globals.gMask );
    
    albedo.rgb = mResultColor.rgb;
    albedo.rgb *= 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 * mResultColor.a * ColorAlpha.a;
    // =================================
    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 );
    oColor.a = mAlphaValue;
]]>
            </CodeInjection>
            <CodeInjection position = "NUM_TEXCOORDS">
<![CDATA[
#if GPU_PROFILE < GPU_PROFILE_MEDIUM
    #if defined( REFLECTION_MAP_CUBE )
        #undef REFLECTION_MAP_CUBE
    #endif
    #if defined( NORMAL_MAP )
        #undef NORMAL_MAP
    #endif
    #if defined( SPECULAR )
        #undef SPECULAR
    #endif
#endif
]]>
            </CodeInjection>
        </CodeInjections>
    </LodLevel>
</CustomShader>
