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

<CustomShader version="4">
    <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"        group="tex1"/>
        <Texture name = "mTexture1Normal"   defaultColorProfile = "linearRGB"   group="tex1"/>
        <Texture name = "mTexture2Diffuse"  defaultColorProfile = "sRGB"        group="tex2"/>
        <Texture name = "mTexture2Normal"   defaultColorProfile = "linearRGB"   group="tex2"/>
        <Texture name = "mTexture3Diffuse"  defaultColorProfile = "sRGB"        group="tex3"/>
        <Texture name = "mTexture3Normal"   defaultColorProfile = "linearRGB"   group="tex3"/>
    </Textures>
    <VertexAttributes>
        <VertexAttribute name="color" group="vertexColor"/>
        <VertexAttribute name="uv1" group="uv1"/>
    </VertexAttributes>
    <Variations>
        <Variation name = "FORCE_GLOOSS_SECONDUV" groups="base uv1">
<![CDATA[
    #define FORCE_GLOOSS_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" groups="base tex1 vertexColor uv1">
<![CDATA[
    #define ADD_TEXTURES_BY_VERTEXCOLOR   // Enables 1 additional texture masked by vertexColor.r
    #define FORCE_GLOOSS_SECONDUV
]]>
        </Variation>
        <Variation name = "ADD_TEXTURES_BY_VERTEXCOLOR3" groups="base tex1 tex2 tex3 vertexColor uv1">
<![CDATA[
    #define ADD_TEXTURES_BY_VERTEXCOLOR3   // Enables 3 additional textures masked by vertexColor.rgb
    #define FORCE_GLOOSS_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
	float  gMyNewAlpha;
]]>
            </CodeInjection>
            <CodeInjection position="VS_OUTPUT">
<![CDATA[
#if defined( FORCE_GLOOSS_SECONDUV )
    float2  glossMapTexCoords : TEXCOORDn;
#endif
    float2 normalMapTexCoord2 : TEXCOORDn;
]]>
            </CodeInjection>
            <CodeInjection position="POST_SET_TEXCOORDS_VS">
<![CDATA[
#if defined( FORCE_GLOOSS_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;
	globals.gMyNewAlpha  = 1.0;
#if defined( ADD_TEXTURES_BY_VERTEXCOLOR ) || defined(ADD_TEXTURES_BY_VERTEXCOLOR3)
    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_GLOOSS_SECONDUV )
        mUV  = In.vs.glossMapTexCoords;
    #else
        mUV  = In.vs.GLOSSMAP_TEXCOORD;
    #endif
    // new jittering
    float2 mJittering = (float2)(0.001 * lerp(-1.0, 1.0, mNoise ));
    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.25, 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 cCosPower = 255;	
    float cosPower = max( cCosPower * globals.gGloss.r * mDritMaskInverted, 0.1 );
    roughness = sqrt(sqrt(2.0/(cosPower+2.0)));
    metalness = 0; //(globals.gGloss.r * mDritMaskInverted);
	bakedAO = globals.gAO;
#endif
]]>
            </CodeInjection>
            <CodeInjection position = "POST_DIFFUSE_COLOR_FS">
<![CDATA[
#if ( defined( ADD_TEXTURES_BY_VERTEXCOLOR ) || defined( ADD_TEXTURES_BY_VERTEXCOLOR3 ) )
	
	float4 diffuseColor4f = float4(diffuseColor.rgb, 1);
	
    #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);
        diffuseColor4f.MERGE_SWIZ = lerp( diffuseColor4f.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);

        diffuseColor4f.MERGE_SWIZ = lerp( diffuseColor4f.MERGE_SWIZ, mAlbedo1.MERGE_SWIZ, globals.gVertexColor.r );
        diffuseColor4f.MERGE_SWIZ = lerp( diffuseColor4f.MERGE_SWIZ, mAlbedo2.MERGE_SWIZ, globals.gVertexColor.g );
        diffuseColor4f.MERGE_SWIZ = lerp( diffuseColor4f.MERGE_SWIZ, mAlbedo3.MERGE_SWIZ, globals.gVertexColor.b );
    #endif
	
	diffuseColor.rgb = diffuseColor4f.rgb;
	globals.gMyNewAlpha = diffuseColor4f.a;
	
#endif

#if defined(ALBEDO_MAP)
    #if defined( COLOR_MASK )
        diffuseColor.rgb = lerp( diffuseColor.rgb, diffuseColor.rgb * colorScale.rgb, globals.gGloss.a );
    #endif
    #if defined( COLOR_MASK_REPLACE )
        diffuseColor.rgb = lerp( diffuseColor.rgb, colorScale.rgb, globals.gGloss.a );
    #endif
    float3 mDirtColor= float3( 0.1248, 0.0908, 0.0545 ); // HardCoded Dirt Color
    diffuseColor.rgb = lerp( diffuseColor.rgb, mDirtColor,  globals.gDirtMask );
    diffuseColor.rgb *= globals.gAO;
#endif
]]>
			</CodeInjection>
            <CodeInjection position = "ALPHA_FS">
<![CDATA[
#if defined(ALPHA_BLENDED) || defined(ALPHA_TESTED)
	alpha = globals.gMyNewAlpha;
#endif	
]]>
			</CodeInjection>
        </CodeInjections>
    </LodLevel>
</CustomShader>
