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

<CustomShader version="3">
    <Parameters>
        <Parameter name="VertxoffsetVertexdeformMotionUVscale" target="VertxoffsetVertexdeformMotionUVscale" type="float4" defaultValue = "-7.0 1.0 0.0 1.5" minValue = "-10.0 -1.0 0.0 0.1" maxValue = "10.0 1.0 10.0 10.0"/>
    </Parameters>
    <LodLevel startDistance = "0">
        <CodeInjections>
            <CodeInjection position="SAMPLERS">
<![CDATA[
    sampler2D Mask;
]]>
            </CodeInjection>
            <CodeInjection position="MATERIALINFO">
<![CDATA[
    uniform float4 VertxoffsetVertexdeformMotionUVscale;
    /*
        Vertxoffset  - scale vertexes into negative Y direction ( set by artist )
        Vertexdeform - dynamic parameter ( 0 .. 1 ), 0 - minimum hight, 1 - maximum hight ( set by script )
        Motion       - dynamic parameter, should be changed by time and speed of the vehicle ( set by script ) 
        UVscale      - scale of the source texture ( set by artist )    
    */
]]>
            </CodeInjection>
            <CodeInjection position="GET_POSITION_VS">
<![CDATA[
    // =================================   
    float mMotion = 0.25 * VertxoffsetVertexdeformMotionUVscale.z;
    float mSpeedPos1 = ( In.position.y + In.position.z ) + 1.0 * mMotion;
    float mSpeedPos2 = In.position.z - abs( In.position.y + In.position.x  ) + 2.0 * mMotion;
    float4 mSinCos; 
    sincos( 16.24 * mSpeedPos1 + 13.5, mSinCos.x, mSinCos.y ); 
    sincos(  6.50 * mSpeedPos2       , mSinCos.z, mSinCos.w );
    float2 texCoord0 = In.texCoords0.xy; 
    float3 offset1 = 1.00 * In.normal.xyz * (  mSinCos.x + mSinCos.y ) * texCoord0.x * texCoord0.x;
    float3 offset2 = 0.20 * In.normal.xyz * (  mSinCos.z + mSinCos.w ) * texCoord0.x ;
    float3 mDeformedPosition = offset1 + offset2;
    // =================================
    // FADEIN
    mDeformedPosition = lerp( VertxoffsetVertexdeformMotionUVscale.x * float3( 0, 1, 0 ) * texCoord0.x, mDeformedPosition, VertxoffsetVertexdeformMotionUVscale.y );
    // =================================
    return float4( In.position.xyz + mDeformedPosition, 1.0 );
]]>
            </CodeInjection>
            <CodeInjection position = "FS_GLOBALS">
<![CDATA[
    float2 gUV;
]]>
            </CodeInjection>
            <CodeInjection position = "START_FS">
<![CDATA[
    float mSpeed    = 0.25 * VertxoffsetVertexdeformMotionUVscale.z ;
    float mUVscale  = VertxoffsetVertexdeformMotionUVscale.w ;
    globals.gUV.xy  = mUVscale.x * In.vs.ALBEDOMAP_TEXCOORD + float2( mSpeed * mUVscale, 0 );
]]>
            </CodeInjection>
            <CodeInjection position = "GET_NORMAL_FS">
<![CDATA[
 #if defined( NORMAL_MAP )
    float3 mNormal  = tex2D( normalMap, globals.gUV.xy ).xyz * 2.0 - 1.0;
    return normalize( mNormal );
 #endif
]]>
            </CodeInjection>
            <CodeInjection position="POST_ALBEDO_FS">
<![CDATA[
#if defined( ALBEDO_MAP )
    albedo.rgb = tex2D( baseMap, globals.gUV.xy ).rgb;
#endif
]]>
            </CodeInjection>
        </CodeInjections>
    </LodLevel>
</CustomShader>
