/***************************************************************************
 *                          Zanoza Modeler v3.x                            *
 *      This is unpublished propietary source code of Zanoza Software.     *
 *      The copyright notice does not evidence any actual or intended      *
 *                    publication of such source code.                     *
 *                                                                         *
 *                 Copyright (c) 2002-2012 Zanoza Software.                *
 *                                                                         *
 *                        All rights reserved.                             *
 ***************************************************************************
 *--------------------------------------------------------------------------
 * @author:   Oleg M.
 *--------------------------------------------------------------------------
 * @purpose:  ZModeler 3.x Generic Masked color shader
 *--------------------------------------------------------------------------
 * @histroy: 14.06.2015. Created
<[[
UserName = "Color"
Description = "Color map shader"
Version  = 1.9.0
Options  = SOLID SPECULAR TRANSITION EMISSIVECOLOR DUSTCOLOR DIRTCOLOR SCRATCHCOLOR BURNCOLOR DIFFUSECOLOR2 DIFFUSECOLOR3 DIFFUSECOLOR4
Samplers = MASK
ManualUV1 = v.vUV01.xy
ManualUV2 = v.vUV01.zw
ManualUV3 = v.vUV23.xy
ManualUV4 = v.vUV23.zw
EnvUV = v.vUVRefl.xy
WorldUV = v.vUVRefl.zw
AlphaRegister = 29

[MASK]
Variable    = g_Mask
Register    = 0
Mask = R G B A RGB
Config = 30

[DUSTCOLOR]
Register    = 31

[DIRTCOLOR]
Register    = 32

[SCRATCHCOLOR]
Register    = 33

[BURNCOLOR]
Register    = 34

[DIFFUSECOLOR2]
Register    = 35

[DIFFUSECOLOR3]
Register    = 36

[DIFFUSECOLOR4]
Register    = 37

[ALPHABLEND]

[ALPHATEST]

]]>
 */

#pragma pack_matrix( row_major ) 

struct VS_INPUT
{
    float4 vPosition        : POSITION;
    float4 vNormal          : NORMAL;
    float4 vTangent         : TANGENT;
    float4 vColorDif        : COLOR0;
    float4 vUV01            : TEXCOORD0;
    float4 vUV23            : TEXCOORD1;
    float4 vDeformPosition  : TEXCOORD5;
    float4 vDeformNormal    : COLOR2;
    uint4  vBones0          : BLENDINDICES0;
    uint4  vBones1          : BLENDINDICES1;
    float4 vWeights0        : BLENDWEIGHT0;
    float4 vWeights1        : BLENDWEIGHT1;
};

struct VS_OUTPUT
{
    float4 vPosition  : POSITION;   //transformed position
    //precomputed stuff:
    float4 vColorDif  : COLOR0;
    float4 vNormal    : TEXCOORD0;  //world-space normal;
    float4 vViewer    : TEXCOORD1;  //camera-space position;
    float4 vUV01      : TEXCOORD2;
    float4 vUV23      : TEXCOORD3;
    float4 vUVRefl    : TEXCOORD4;
};


#include "zmCommon.inl"
float4      g_alpha          : register(c29);  //[minlevel:testvalue, maxlevel, mapaffect, angular level]
float4      g_maskConfig     : register(c30);  //[mask amp, mask angular, diff tile.uv]
float4      g_dustColor      : register(c31);  //dust color r,g,b and level in .a
float4      g_dirtColor      : register(c32);  //dirt color r,g,b and level in .a
float4      g_scratchColor   : register(c33);  //scratch color r,g,b and level in .a
float4      g_burnColor      : register(c34);  //burn color r,g,b and level in .a
                                               //level of main diffuse is in g_colors[1].a;
float4      g_diffuse2Color  : register(c35);  //diffuse2 color r,g,b and level in .a
float4      g_diffuse3Color  : register(c36);  //diffuse3 color r,g,b and level in .a
float4      g_diffuse4Color  : register(c37);  //diffuse4 color r,g,b and level in .a

// textures:
sampler     g_Mask           : register(s[0]);

#ifdef VERTEX_SHADER
VS_OUTPUT mainVS(in VS_INPUT v)
{
    VS_OUTPUT output = (VS_OUTPUT)0;
    output.vNormal = v.vNormal;
    float4 vTangent = (float4)0;
    output.vPosition = transformInput(v, output.vNormal, vTangent, output.vViewer);
    output.vColorDif = any(v.vColorDif.rgb) ? v.vColorDif : 1;

    output.vUV01 = v.vUV01;
    output.vUV23 = v.vUV23;
    output.vUVRefl.xy = (mul(output.vNormal.xyz, (float3x3)g_mView).xy+1)/2;
    output.vUVRefl.zw = v.vPosition.xz;

    return output;
}
#endif //VERTEX_SHADER

#ifdef PIXEL_SHADER
//------------------------------------------------------------
//  PixelShader: generic color + specular highlight with maskmap;
//------------------------------------------------------------
float4 mainPS(in VS_OUTPUT v) : COLOR0
{
    float4 oColor;
    float4 CONST_TEXEL    = float4(1,1,1,1);
    float4 VCOLOR_TEXEL   = v.vColorDif;
    float4 PERVERTEXCOLOR = lerp(1, VCOLOR_TEXEL, g_deform.z);
    float4 MASK_TEXEL     = tex2D(MASK_SOURCE, MASK_UV_SOURCE*g_maskConfig.zw)*g_maskConfig.x*(MASK_INPUT_MASK);

    float3 light = -g_lightDir.xyz;
    float2 Temp = 1; 
#ifdef SPECULAR_ENABLED
    float3 normal = normalize(v.vNormal.xyz);
    float3 halfSpec = normalize(light + v.vViewer.xyz);
    Temp = float2(dot(normal, light), dot(normal, halfSpec));
    float4 LitCoef = lit(Temp.x, Temp.y, g_colors[2].a);
#else
    float3 normal = v.vNormal.xyz;
    float4 LitCoef = float4(1, dot(normal, light), 0, 0);
#endif//SPECULAR_ENABLED


    //
    // advanced color, and shine/opacity overload controllers:
    ADVANCED_COLOR_DIFFUSELEVEL(fAdvColorLevels)
    ADVANCED_COLOR_DIFFUSEVAR(fAdvancedDiffuseColor, fAdvColorLevels)
    ADVANCED_COLOR_EFFECTLEVELS(fEffectLevels)
    ADVANCED_COLOR_EFFECTVAR(fEffectColor, fEffectLevels)
    ADVANCED_COLOR_OPTIONS(advShineOpac, fEffectLevels, fAdvColorLevels)

    LitCoef.y = saturate(LitCoef.y + (g_ambientLight.r + g_ambientLight.g + g_ambientLight.b)/3);
    float3 fColorSpecular = g_colors[2].rgb*(LitCoef.z + LitCoef.z*(1+LitCoef.y)*saturate(advShineOpac.g));

    oColor.rgb = 
                // ambient: 
                (g_colors[0].rgb + 0.2*PERVERTEXCOLOR.rgb + 0.8*g_ambientLight.rgb)*g_ambientLight.rgb +
                // diffuse component:
                (fAdvancedDiffuseColor*PERVERTEXCOLOR.rgb*(1-fEffectColor.a) + fEffectColor.rgb*fEffectColor.a)*max((EMISSIVECOLOR_INPUT_MASK)*advShineOpac.r*4*g_colors[3].a, LitCoef.y) +
                // matted:
                advShineOpac.r*(
                  // specular component:
                  fColorSpecular +
                  // emissive component:
                  g_colors[3].rgb*(EMISSIVECOLOR_INPUT_MASK)*g_colors[3].a
                );


#ifdef ALPHABLEND_ENABLED
    Temp.x = abs(dot(v.vViewer.xyz, normal));
    Temp.y = ALPHABLEND_VERTEXCOLOR*g_alpha.y;
    if (g_alpha.w >= 0)
      Temp.x = lerp(1, Temp.x, g_alpha.w);
    else
      Temp.x = lerp(1, 1-Temp.x, abs(g_alpha.w));
    oColor.a = saturate(lerp(1, ALPHABLEND_INPUT, g_alpha.z)*ALPHABLEND_VERTEXCOLOR*Temp.x*g_alpha.y + 
                        length(fColorSpecular)*0.5 + advShineOpac.b);
#else
    oColor.a = g_alpha.y;
#endif//ALPHABLEND_ENABLED
#ifdef ALPHATEST_ENABLED
    clip(oColor.a - g_alpha.x);
#endif//ALPHATEST_ENABLED
    return oColor;
}
#endif //PIXEL_SHADER
