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

<CustomShader version="2" classRequirement="">
    <LodLevel startDistance="0">
        <Parameters>
            <Parameter name="alphaBlendStartEnd" target="alphaBlendStartEnd" type="float3" defaultValue="70 75 0 0" />
        </Parameters>
        <CodeInjections>
            <CodeInjection position="NUM_TEXCOORDS">
<![CDATA[
#if defined(SPECULAR)
#undef SPECULAR
#endif
#if (defined(AMBIENT) || defined(DIFFUSE) || defined(SPECULAR))
#define FOLIAGE_COLOR_OUTPUT
#endif
]]>
            </CodeInjection>
            <CodeInjection position="CONFIG_DEFINES">
<![CDATA[
#if defined(_NO_TANGENTSPACE_LIGHT) && (NUM_DIRECTIONAL_LIGHTS > 0) && defined(DIFFUSE)
#define VERTEX_DIRLIGHT_LIGHTING
#endif
#if GPU_PROFILE <= GPU_PROFILE_HIGH
    #define NUM_SHADOW_MAP_SAMPLES 1
#endif
]]>
            </CodeInjection>
            <CodeInjection position="GET_NORMAL_VS">
<![CDATA[
#ifdef FOLIAGE_COLOR_OUTPUT
    // convert from ubyte to [-1,1] range
    return In.normal.xyz/127.5 - 1.0;
#endif
]]>
            </CodeInjection>
			<CodeInjection position="GET_TEXCOORDS_VS">
<![CDATA[
return In.texCoords[texCoordSet].xy;
]]>
			</CodeInjection>
            <CodeInjection position="NEED_FS_NORMAL">
<![CDATA[
//#if defined(_NEED_FS_NORMAL)
//#undef _NEED_FS_NORMAL
//#endif
]]>
            </CodeInjection>
            <CodeInjection position="MATERIALINFO">
<![CDATA[
#if defined(FOLIAGE_COLOR_OUTPUT)
    //float4 bumpSpeedScale;
#endif
float4 alphaBlendStartEnd;
]]>
            </CodeInjection>
            <CodeInjection position="GET_POSITION_VS">
<![CDATA[
float frequency = 0.3;
float3 direction = float3(0,0,0)*0.1;
float offset = sin(time.y + In.position.x*In.position.z * frequency)*In.normal.w/255.0;
return float4(In.position.xyz + direction * offset, 1.0);
]]>
            </CodeInjection>
            <CodeInjection position="VS_OUTPUT">
<![CDATA[
float alphaScale : VARYINGN;
#ifdef VERTEX_DIRLIGHT_LIGHTING
    float3 dirVertexLighting[NUM_DIRECTIONAL_LIGHTS] : VARYINGN_I(NUM_DIRECTIONAL_LIGHTS);
#endif
]]>
            </CodeInjection>
            <CodeInjection position="VS_END">
<![CDATA[
//Out.alphaScale = 1 + saturate((length(viewDir)-alphaBlendStartEnd.x)/(alphaBlendStartEnd.y-alphaBlendStartEnd.x))*alphaBlendStartEnd.z;

Out.alphaScale = 1.0 - saturate((length(viewDir)-(alphaBlendStartEnd.x))/(alphaBlendStartEnd.y-alphaBlendStartEnd.x));
//Out.alphaScale = 1.0 - saturate((length(viewDir)-60)/(80-60));

#ifdef VERTEX_DIRLIGHT_LIGHTING
    for (int i=0; i < NUM_DIRECTIONAL_LIGHTS; i++) {
        float NdotL = saturate(dot(-dirLightDirections[i].xyz, worldNormal.xyz));
        Out.dirVertexLighting[i].xyz = NdotL*diffuseColors[i].xyz;
    }
#endif
]]>
            </CodeInjection>
            <CodeInjection position="POST_ALBEDO_FS">
<![CDATA[
albedo.w *= In.vs.alphaScale;
]]>
            </CodeInjection>
            <CodeInjection position="DIFFUSE_LIGHTING_DIR_FS">
<![CDATA[
#ifdef VERTEX_DIRLIGHT_LIGHTING
return In.vs.dirVertexLighting[dirLightI].xyz*atten;
#endif
]]>
            </CodeInjection>
        </CodeInjections>
    </LodLevel>
</CustomShader>
