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

<CustomShader version="1.0" classRequirement="">
    <LodLevel startDistance="0">
        <Parameters>
            <Parameter name="alphaBlendStartEnd" target="In.mat.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
]]>
            </CodeInjection>
            <CodeInjection position="GET_TEXCOORDS_VS">
<![CDATA[
// convert from short to 0-1 range
return In.texCoords[texCoordSet]/32767.0;
]]>
            </CodeInjection>
            <CodeInjection position="GET_NORMAL_VS">
<![CDATA[
#ifdef FOLIAGE_COLOR_OUTPUT
    // we use blendIndices to store normal, since normal attribute does not support UBYTE4 type
    return normalize(In.blendIndices.xyz/127.5 - 1.0);
#endif
]]>
            </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)
    //uniform float4 bumpSpeedScale;
#endif
uniform float4 alphaBlendStartEnd;
]]>
            </CodeInjection>
            <CodeInjection position="VS_OUTPUT">
<![CDATA[
float alphaScale;
#ifdef VERTEX_DIRLIGHT_LIGHTING
    float3 dirVertexLighting[NUM_DIRECTIONAL_LIGHTS];
#endif
]]>
            </CodeInjection>
            <CodeInjection position="VS_END">
<![CDATA[
//Out.alphaScale = 1 + saturate((length(viewDir)-In.mat.alphaBlendStartEnd.x)/(In.mat.alphaBlendStartEnd.y-In.mat.alphaBlendStartEnd.x))*In.mat.alphaBlendStartEnd.z;

Out.alphaScale = 1.0 - saturate((length(viewDir)-(In.mat.alphaBlendStartEnd.x))/(In.mat.alphaBlendStartEnd.y-In.mat.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(-In.lights.dirLightDirections[i].xyz, worldNormal.xyz));
        Out.dirVertexLighting[i].xyz = NdotL*In.lights.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>
