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

<CustomShader version="4">
    <Parameters>
        <Parameter name="directionAndFrequency" target="directionAndFrequency"  type="float4" defaultValue = "0.06 0.0 0.0 1.2"/>
        <Parameter name="speed"                 target="speed"                  type="float4" defaultValue = "1.0 0.0 0.0 0.0"/>
    </Parameters>
    <Textures>
        <Texture name = "mMaskSampler"       group="force" defaultColorProfile = "linearRGB"   defaultFilename = "../shared/clouds_diffuse.png"/>
    </Textures>
    <VertexAttributes>
        <VertexAttribute name="uv1" group="uv1"/>
        <VertexAttribute name="color" group="color"/>
    </VertexAttributes>
    <Variations>
        <Variation name = "FORCE_SECONDUV" groups="base uv1">
<![CDATA[
    #define FORCE_SECONDUV
]]>
        </Variation>
        <Variation name = "FORCE_VERTEXCOLORS" groups="base color force">
<![CDATA[
    #define FORCE_VERTEXCOLORS
]]>
        </Variation>
        <Variation name = "WAVE_VERTEXCOLORS" groups="base color">
<![CDATA[
    #define WAVE_VERTEXCOLORS
]]>
        </Variation>
    </Variations>
    <LodLevel startDistance="0">
        <CodeInjections>
            <CodeInjection position="MATERIALINFO">
<![CDATA[
uniform float4 directionAndFrequency;
uniform float4 speed;
]]>
            </CodeInjection>
            <CodeInjection position = "SAMPLERS">
<![CDATA[
#if defined( FORCE_VERTEXCOLORS )
sampler2D mMaskSampler;
#endif
]]>
            </CodeInjection>
            <CodeInjection position = "FS_GLOBALS">
<![CDATA[
#if defined( FORCE_VERTEXCOLORS )
    float4 gVertexColor;
    float4 gMask;
#endif
]]>
            </CodeInjection>
            <CodeInjection position="GET_POSITION_VS">
<![CDATA[
    float4 mDirFreq = directionAndFrequency;
    float  mSpeed   = speed.x;
    float  mAtt = getDefaultTexCoords( In ).y;
    
    float3 mObjPos = float3(modelMatrix[0][3] + invViewMatrix[0][3],
                            modelMatrix[1][3] + invViewMatrix[1][3],
                            modelMatrix[2][3] + invViewMatrix[2][3]);
    float mObjPosVarSeed = mObjPos.x + mObjPos.y + mObjPos.z;
#if defined( FORCE_SECONDUV )
    mAtt = convertDefaultTexCoords( In, In.texCoords1.xy ).y;
#endif
#if defined( FORCE_VERTEXCOLORS )
    mAtt = In.color.r;
#endif
    float3 offset1;
    float3 offset2;
    float3 offset; 
    mDirFreq.xyz *= 0.25;
    mSpeed *= 0.25; 
    offset1 = mDirFreq.xyz * sin( mSpeed * cTime_s + In.position.x*In.position.y * mDirFreq.w + mObjPos ) * mAtt;
#if defined( WAVE_VERTEXCOLORS )
    offset1 = mDirFreq.xyz * sin( ( mSpeed * cTime_s + 10 * In.color.r ) * mDirFreq.w + mObjPos );
#endif
    mDirFreq.xyz *= 8.0;
    mSpeed *= 10.0;
    offset2 = mDirFreq.xyz * sin( mSpeed * cTime_s + In.position.x*In.position.y * mDirFreq.w + mObjPos ) * mAtt;
#if defined( WAVE_VERTEXCOLORS )
    offset2 = mDirFreq.xyz * sin( ( mSpeed * cTime_s + 10 * In.color.r ) * mDirFreq.w + mObjPos );
#endif
    offset = lerp( offset1, offset2, cShared0 );
    return In.position.xyz + offset;
]]>
            </CodeInjection>
            <CodeInjection position = "START_FS">
<![CDATA[
#if defined( FORCE_VERTEXCOLORS )
    globals.gVertexColor = In.vs.vertexColor.rgba;
    globals.gMask  = tex2D( mMaskSampler, 2 * In.vs.ALBEDOMAP_TEXCOORD ).rgba;
#endif
]]>
            </CodeInjection>
           <CodeInjection position="ALPHA_FS">
<![CDATA[
#if defined( FORCE_VERTEXCOLORS )
    float mAlpha = saturate(lerp( globals.gMask.g*globals.gVertexColor.a, 1.0 , globals.gVertexColor.a) );
    alpha *= mAlpha;
#endif
]]>
            </CodeInjection>
            <CodeInjection position="FINAL_POS_FS">
<![CDATA[
#if defined( DEBUG_SHOW_PARALLAX )
    oColor.rgb  = (float3)mAlpha;
#endif
]]>
            </CodeInjection>
        </CodeInjections>
    </LodLevel>
</CustomShader>
