﻿<?xml version="1.0" encoding="utf-8"?>

<CustomShader version="5">
    <Parameters>
        <Parameter name="index"        target="index"        type="float"  group="base" defaultValue="0"              minValue = "0.0"             maxValue = "100.0"/>
        <Parameter name="spacing"      target="spacing"      type="float2" group="base" defaultValue="0 0"            minValue = "-1.0 -1.0"       maxValue = "1.0 1.0"/>
        <Parameter name="lightControl" target="lightControl" type="float"  group="base" defaultValue="0.0"            minValue = "-1.0"            maxValue = "100.0"/>
        <Parameter name="colorScale"   target="colorScale"   type="float3" group="base" defaultValue="0.0 0.0 0.0 1"  minValue = "0.0 0.0 0.0 0.0" maxValue = "1.0 1.0 1.0 1.0"/>
        <Parameter name="alphaErosion" target="alphaErosion" type="float"  group="base" defaultValue="0.0"            minValue = "-1.0"            maxValue = "1.0"/>
    </Parameters>
    <UvUsages/>
    <Textures>
        <Texture name = "alphaMap" group="base" defaultColorProfile = "linearRGB" defaultFilename = "$data/shared/default_alpha.png"/>
    </Textures>
    <LodLevel startDistance="0" alphaForceMode="use">
        <CodeInjections>
            <CodeInjection position="OBJECT_PARAMETERS">
<![CDATA[
    float  index;
    float2 spacing;
    float lightControl;
    float4 colorScale;
    float alphaErosion;
]]>
            </CodeInjection>
            <CodeInjection position="POST_SET_TEXCOORDS_VS">
<![CDATA[
#if defined(ALBEDO_MAP) || defined(EMISSIVE_MAP)
    float2 uv = getDefaultTexCoords(In, object);

    // don't care about the defined uv coords, use edge vertices to calculate uv
    // this is required to migrate old number meshes on vehicles without changing the uvs
    uv.x = sign(In.position.x) / 2 + 0.5;
    uv.y = sign(In.position.y) / 2 + 0.5;

    // strech font to full size of the mesh
    uv.x += -sign(uv.x - 0.5) * object.spacing.x;
    uv.y += -sign(uv.y - 0.5) * object.spacing.y;

    uv.x /= 37.0;
    uv.x += floor(fmod(object.index,36.99)) / 36.99;
    uv.y *= 0.5;
    uv.y += 0.5 + 0.5*floor(object.index/37.0);
    Out.defaultTexCoords = uv;
#endif
]]>
            </CodeInjection>
            <CodeInjection position = "SAMPLERS">
<![CDATA[
    sampler2D alphaMap;
]]>
            </CodeInjection>
            <CodeInjection position = "FS_GLOBALS">
<![CDATA[
    float gAlpha;
]]>
            </CodeInjection>
            <CodeInjection position = "POST_DIFFUSE_COLOR_FS">
<![CDATA[
#if defined(ALBEDO_MAP)
    diffuseColor *= object.colorScale.rgb;
#endif
]]>
            </CodeInjection>
            <CodeInjection position = "START_FS">
<![CDATA[
{
    globals.gAlpha = saturate(tex2D(alphaMap, In.vs.defaultTexCoords).r - object.alphaErosion);

#if defined(ALPHA_BLENDED)
    const float width = 0.02;
    globals.gAlpha = smoothstep(0.5- width, 0.5+width, globals.gAlpha);

#if 0
    // Supersampling: 4 extra points
    float dscale = 0.354; // half of 1/sqrt2
    float2 duv = dscale * fwidth(In.vs.defaultTexCoords);
    float4 box = float4(In.vs.defaultTexCoords-duv, In.vs.defaultTexCoords+duv);

    float supersampleSum = smoothstep(0.5-width, 0.5+width, tex2D(alphaMap, box.xy).r)
                         + smoothstep(0.5-width, 0.5+width, tex2D(alphaMap, box.zw).r)
                         + smoothstep(0.5-width, 0.5+width, tex2D(alphaMap, box.xw).r)
                         + smoothstep(0.5-width, 0.5+width, tex2D(alphaMap, box.zy).r);

    // Weighted average, with 4 extra points having 0.5 weight each, so divide by 1 + 0.5*4 = 3
    globals.gAlpha = (globals.gAlpha + 0.5 * supersampleSum) / 3.0;
#endif

#endif
}
]]>
            </CodeInjection>
            <CodeInjection position="EMISSIVE_FS">
<![CDATA[
#if defined(EMISSIVE_MAP)
    emissiveColor.rgb *= object.colorScale.rgb;
    emissiveColor *= object.colorScale.w * object.lightControl;
#else
    emissiveColor.xyz = object.colorScale.rgb;
    emissiveColor *= globals.gAlpha * object.colorScale.w * object.lightControl;
#endif
]]>
            </CodeInjection>
            <CodeInjection position="ALPHA_FS">
<![CDATA[
    alpha = globals.gAlpha * object.colorScale.w;
#if defined(ALPHA_BLENDED)
    reflectingLightingScale = alpha;
#endif
]]>
            </CodeInjection>
        </CodeInjections>
    </LodLevel>
</CustomShader>
