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

<CustomShader version="3">
    <Parameters>
        <Parameter name = "uvScale"       target = "uvScale"       type ="float4"   defaultValue = "30.0 30.0 2.0 2.0"/>
        <Parameter name = "colorScale"    target = "colorScale"    type = "float4"  defaultValue = "0.8 0.55 0.055 1.0"  minValue = "0.0 0.0 0.0 0.0"          maxValue = "1.0 1.0 1.0 1.0"/>
    </Parameters>
    <Textures>
        <Texture name = "mDirtNormal"       defaultColorProfile = "linearRGB"   defaultFilename = "../shared/dirt_normal.png"/>
        <Texture name = "mTexture1Diffuse"  defaultColorProfile = "sRGB"        />
        <Texture name = "mTexture1Normal"   defaultColorProfile = "linearRGB"   />
        <Texture name = "mTexture2Diffuse"  defaultColorProfile = "sRGB"        />
        <Texture name = "mTexture2Normal"   defaultColorProfile = "linearRGB"   />
        <Texture name = "mTexture3Diffuse"  defaultColorProfile = "sRGB"        />
        <Texture name = "mTexture3Normal"   defaultColorProfile = "linearRGB"   />
    </Textures>
    <Variations>
        <Variation name = "FORCE_GLOSS_SECONDUV">
<![CDATA[
    #define FORCE_GLOSS_SECONDUV       // Enables second UVset for gloss
]]>
        </Variation>
        <Variation name = "COLOR_MASK">
<![CDATA[
    #define COLOR_MASK      // Enables colorPainting by mask from glossMap.a
]]>
        </Variation>
        <Variation name = "COLOR_MASK_REPLACE">
<![CDATA[
    #define COLOR_MASK_REPLACE   // Enables colorPainting by mask from glossMap.a
]]>
        </Variation>
        <Variation name = "ADD_TEXTURES_BY_VERTEXCOLOR">
<![CDATA[
    #define ADD_TEXTURES_BY_VERTEXCOLOR   // Enables 1 additional texture masked by vertexColor.r
    #define FORCE_GLOSS_SECONDUV
]]>
        </Variation>
        <Variation name = "ADD_TEXTURES_BY_VERTEXCOLOR3">
<![CDATA[
    #define ADD_TEXTURES_BY_VERTEXCOLOR3   // Enables 3 additional textures masked by vertexColor.rgb
    #define FORCE_GLOSS_SECONDUV
]]>
        </Variation>
    </Variations>
    <LodLevel startDistance="0">
        <CodeInjections>
            <CodeInjection position="MATERIALINFO">
<![CDATA[
    uniform float4 uvScale;
#if defined( COLOR_MASK ) || defined( COLOR_MASK_REPLACE )
    uniform float4 colorScale;
#endif
]]>
            </CodeInjection>
            <CodeInjection position = "SAMPLERS">
<![CDATA[
sampler2D mDirtNormal;
#if defined( ADD_TEXTURES_BY_VERTEXCOLOR )
sampler2D mTexture1Diffuse;
sampler2D mTexture1Normal;
#endif
#if defined( ADD_TEXTURES_BY_VERTEXCOLOR3 )
sampler2D mTexture1Diffuse;
sampler2D mTexture1Normal;
sampler2D mTexture2Diffuse;
sampler2D mTexture2Normal;
sampler2D mTexture3Diffuse;
sampler2D mTexture3Normal;
#endif
]]>
            </CodeInjection>
            <CodeInjection position = "FS_GLOBALS">
<![CDATA[
    float4 gVertexColor;
    float4 gGloss;
    float  gDirtMask;
    float  gAO;
    float4 gDirtNormal; // alpha channel must be with noise
]]>
            </CodeInjection>
            <CodeInjection position="VS_OUTPUT">
<![CDATA[
#if defined( FORCE_GLOSS_SECONDUV )
    float2  glossMapTexCoords : TEXCOORDn;
#endif
    float2 normalMapTexCoord2 : TEXCOORDn;
]]>
            </CodeInjection>
            <CodeInjection position="POST_SET_TEXCOORDS_VS">
<![CDATA[
#if defined( FORCE_GLOSS_SECONDUV )
    Out.glossMapTexCoords = convertDefaultTexCoords( In, In.texCoords1.xy ); // Pass second UVSet to the Pixel Shader
#endif
    Out.normalMapTexCoord2 = uvScale.xy * convertDefaultTexCoords( In, In.texCoords0.xy );
]]>
            </CodeInjection>
            <CodeInjection position = "START_FS">
<![CDATA[
    globals.gGloss       = float4( 1.0,1.0,1.0,1.0 );
    globals.gDirtMask    = 0.0;
    globals.gAO          = 1.0;
    globals.gVertexColor = float4( 0.0, 0.0, 0.0, 1.0 );
    globals.gDirtNormal  = tex2D( mDirtNormal, In.vs.normalMapTexCoord2 ).rgba;
#if defined( VERTEX_COLOR )
    globals.gVertexColor     = In.vs.vertexColor.rgba;
#endif
 #if defined( GLOSS_MAP )
 /*
 glossMap.r - roughness
 glossMap.g - AO
 glossMap.b - dirt mask
 */
    float  mNoise        = globals.gDirtNormal.a;
    float2 mUV           = float2( 0.0,0.0 );
    #if defined( FORCE_GLOSS_SECONDUV )
        mUV  = In.vs.glossMapTexCoords;
    #else
        mUV  = In.vs.GLOSSMAP_TEXCOORD;
    #endif
    // new jittering
    float2 mJittering = (float2)(0.001 * ( 2.0 * mNoise - 1.0 ));
    globals.gGloss     = tex2D( glossMap, mUV + mJittering ).rgba;
    globals.gDirtMask  = globals.gGloss.b;
    // old jittering
    //globals.gDirtMask  = saturate( globals.gDirtMask * lerp(1.0, 6.0 * mNoise, globals.gDirtMask ) );
    globals.gAO        = globals.gGloss.g;
    globals.gAO        = lerp( 0.05, 1.0, globals.gAO);
#endif
]]>
            </CodeInjection>
            <CodeInjection position = "GET_UNNORMALIZED_TANGENT_SPACE_NORMAL_FS">
<![CDATA[
#if defined( NORMAL_MAP )
    float3 mNormal   = tex2D( normalMap, In.vs.NORMALMAP_TEXCOORD ).xyz - 0.5;

    #if defined( ADD_TEXTURES_BY_VERTEXCOLOR )
        float3 mNormal1 = tex2D( mTexture1Normal, In.vs.NORMALMAP_TEXCOORD ).xyz - 0.5;
        mNormal.rgb = lerp( mNormal.rgb, mNormal1.rgb, globals.gVertexColor.r+0.00001 ); // +0.00001 hack to fix bug in shader compiler
    #endif

    #if defined( ADD_TEXTURES_BY_VERTEXCOLOR3 )
        float3 mNormal1 = tex2D( mTexture1Normal, In.vs.NORMALMAP_TEXCOORD ).xyz - 0.5;
        float3 mNormal2 = tex2D( mTexture2Normal, In.vs.NORMALMAP_TEXCOORD ).xyz - 0.5;
        float3 mNormal3 = tex2D( mTexture3Normal, In.vs.NORMALMAP_TEXCOORD ).xyz - 0.5;

        mNormal.rgb = lerp( mNormal.rgb, mNormal1.rgb, globals.gVertexColor.r );
        mNormal.rgb = lerp( mNormal.rgb, mNormal2.rgb, globals.gVertexColor.g );
        mNormal.rgb = lerp( mNormal.rgb, mNormal3.rgb, globals.gVertexColor.b );
    #endif

    float3 mDirtNmap = globals.gDirtNormal.xyz - 0.5;
    mNormal.xy += mDirtNmap.xy*globals.gDirtMask;
    return mNormal;
 #endif
]]>
            </CodeInjection>
            <CodeInjection position = "POST_GLOSS_COLOR_FS">
<![CDATA[
#if defined(GLOSS_MAP)
    float mDritMaskInverted = ( 1 - globals.gDirtMask );
    
    float cosPower = max( cCosPower * globals.gGloss.r * mDritMaskInverted, 0.1 );
    roughness = sqrt(sqrt(2.0/(cosPower+2.0)));
    glossColor.xyz          = (float3) (globals.gGloss.r * mDritMaskInverted);
#endif
]]>
            </CodeInjection>
            <CodeInjection position = "POST_ALBEDO_FS">
<![CDATA[
#if ( defined( ADD_TEXTURES_BY_VERTEXCOLOR ) || defined( ADD_TEXTURES_BY_VERTEXCOLOR3 ) )
    #if defined( VERTEX_COLOR )
        #undef VERTEX_COLOR
    #endif
    #if defined(ALPHA_BLENDED) || defined(ALPHA_TESTED)
        #define MERGE_SWIZ rgba
    #else
        #define MERGE_SWIZ rgb
    #endif
    #if defined( ADD_TEXTURES_BY_VERTEXCOLOR )
        float4 mAlbedo1   = tex2D( mTexture1Diffuse, In.vs.ALBEDOMAP_TEXCOORD);
        albedo.MERGE_SWIZ = lerp( albedo.MERGE_SWIZ, mAlbedo1.MERGE_SWIZ, globals.gVertexColor.r + 0.00001 ); // +0.00001 hack to fix bug in shader compiler
    #endif
    #if defined( ADD_TEXTURES_BY_VERTEXCOLOR3 )
        float4 mAlbedo1   = tex2D( mTexture1Diffuse, In.vs.ALBEDOMAP_TEXCOORD);
        float4 mAlbedo2   = tex2D( mTexture2Diffuse, In.vs.ALBEDOMAP_TEXCOORD);
        float4 mAlbedo3   = tex2D( mTexture3Diffuse, In.vs.ALBEDOMAP_TEXCOORD);

        albedo.MERGE_SWIZ = lerp( albedo.MERGE_SWIZ, mAlbedo1.MERGE_SWIZ, globals.gVertexColor.r );
        albedo.MERGE_SWIZ = lerp( albedo.MERGE_SWIZ, mAlbedo2.MERGE_SWIZ, globals.gVertexColor.g );
        albedo.MERGE_SWIZ = lerp( albedo.MERGE_SWIZ, mAlbedo3.MERGE_SWIZ, globals.gVertexColor.b );
    #endif
#endif

#if defined(ALBEDO_MAP)
    #if defined( COLOR_MASK )
        albedo.rgb = lerp( albedo.rgb, albedo.rgb * colorScale.rgb, globals.gGloss.a );
    #endif
    #if defined( COLOR_MASK_REPLACE )
        albedo.rgb = lerp( albedo.rgb, colorScale.rgb, globals.gGloss.a );
    #endif
    float3 mDirtColor     = float3( 0.1248, 0.0908, 0.0545 ); // HardCoded Dirt Color
    albedo.rgb            = lerp( albedo.rgb, mDirtColor,  globals.gDirtMask );
    albedo.rgb  *= globals.gAO * 1.25;
#endif
]]>
            </CodeInjection>
        </CodeInjections>
    </LodLevel>
</CustomShader>
