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

<CustomShader version="4">
    <Parameters>
        <Parameter name="RDT"                     target="RDT"                      type="float4" group="base"                  defaultValue="1.0 1.0 0.0 0.0"       minValue = "0.0 0.0 0.0 0.0"          maxValue = "1.0 1.0 50.0 50.0"/>
        <Parameter name="dirtColor"               target="dirtColor"                type="float4" group="base"                  defaultValue="0.20 0.14 0.08 0.0"    minValue = "0.0 0.0 0.0 0.0"          maxValue = "1.0 1.0 1.0 1.0"/>
	</Parameters>
    <Textures>
    </Textures>
    <Variations>
    </Variations>
    <LodLevel startDistance = "0">
        <CodeInjections>
            <CodeInjection position = "MATERIALINFO">
<![CDATA[
/*
    RDT.x  - Scratches Amount
    RDT.y  - Dirt Amount
    RDT.zw - NOT USED
*/
    uniform float4 RDT;
    uniform float4 dirtColor;
]]>
            </CodeInjection>
            <CodeInjection position = "FS_GLOBALS">
<![CDATA[
    float  gScratchesMask;
    float  gAO;
    float  gDirt;
]]>
            </CodeInjection>
            <CodeInjection position="VS_OUTPUT">
<![CDATA[
    float2 texCoordsOrig : TEXCOORDn;
    float2 texCoordsX    : TEXCOORDn;
    float2 texCoordsY    : TEXCOORDn;
    float2 texCoordsZ    : TEXCOORDn;
    float3 localNormal   : TEXCOORDn;
]]>
            </CodeInjection>
            <CodeInjection position="POST_SET_TEXCOORDS_VS">
<![CDATA[
    float3 localPosition = In.position.xyz;
    float mUVScale = 1.0;
    Out.texCoordsOrig = getDefaultTexCoords(In);
    Out.texCoordsX = mUVScale * localPosition.yz;
    Out.texCoordsY = mUVScale * localPosition.xz;
    Out.texCoordsZ = mUVScale * localPosition.xy;
    Out.localNormal = normalize(In.normal.xyz);
]]>
            </CodeInjection>
            <CodeInjection position = "START_FS">
<![CDATA[
    float4 mGloss  = float4(0.0,0.0,0.0,0.0);;
    globals.gAO     = 0.0;
    globals.gScratchesMask = 0.0;
    globals.gDirt          = 0.0;
#if defined( GLOSS_MAP )
    mGloss = tex2D( glossMap, In.vs.GLOSSMAP_TEXCOORD).rgba;
    globals.gScratchesMask = mGloss.r;
    globals.gAO = mGloss.g;
    globals.gDirt = mGloss.b;
#endif
    globals.gScratchesMask = smoothstep(1-RDT.x, 1-RDT.x+0.05, globals.gScratchesMask);
    float mDirt = smoothstep(1-RDT.y, 1-RDT.y+0.5, globals.gDirt);
    float mDust = globals.gDirt*RDT.y;
    globals.gDirt = saturate(mDust + mDirt);
    // Detail textures

]]>
            </CodeInjection>
            <CodeInjection position = "POST_DIFFUSE_COLOR_FS">
<![CDATA[
#if defined( SPECULAR ) && defined( GLOSS_MAP )
#endif
]]>
            </CodeInjection>
            <CodeInjection position="ALPHA_FS">
<![CDATA[
    alpha = lerp(alpha,1.0,globals.gDirt);
	alpha = lerp(alpha,0.0,globals.gScratchesMask);
]]>
            </CodeInjection>
        </CodeInjections>
    </LodLevel>
</CustomShader>
