/***************************************************************************
 *                         Zanoza Modeler v3.3.x                           *
 *     This is unpublished proprietary source code of Zanoza Software.     *
 *      The copyright notice does not evidence any actual or intended      *
 *                    publication of such source code.                     *
 *                                                                         *
 *                 Copyright (c) 2002-2024 Zanoza Software.                *
 *                                                                         *
 *                        All rights reserved.                             *
 ***************************************************************************
 *--------------------------------------------------------------------------
 * @author:   Oleg M.
 *--------------------------------------------------------------------------
 * @purpose:  ZModeler 3.3.x Pure (no parameters) screen-space textured shader
 *--------------------------------------------------------------------------
 * @history: 12.05.2024. Created
 * @Description:
   ScreenTexDual.zsh - minimal vertex shading transform (screen space)
   to draw solid primitives with diffuse texture.
  
   Shader for default rendering (with no materials). Used to draw 
   general-purpose geometry with textures, mostly a texture rectange
   as background for other rendering (e.g. UVMapper background).
   This version of shader does not involve any light-specific 
   computations (attenuation on normals, back-facing fade etc.)
   and input vertices format does not need normals/tangents.
<[[
UserName = "On-screen solid with texture for dual render targets"
Version  = 2.0.1
Options  = PURE DUAL
Engine = 11
]]>
*/

#pragma pack_matrix( row_major ) 

struct VS_INPUT
{
    float4 vPosition        : SV_POSITION;
    float4 vColorDif        : COLOR0;
    float2 vUV              : TEXCOORD0;
};

struct VS_OUTPUT
{
    float4 vPosition : SV_POSITION;
    float4 vDiffuse  : COLOR;
    float2 vUV       : TEXCOORD0;
};

struct PS_OUTPUT
{
    float4  color0 : SV_TARGET0;
    float4  color1 : SV_TARGET1;
};

#include "zmCommonBuffers.inl"

texture2D   	g_Texture        : register(t0);
SamplerState	g_TextureSampler : register(s0);

#ifdef VERTEX_SHADER

VS_OUTPUT mainVS(in VS_INPUT v)
{
    VS_OUTPUT output;

    //transform to screen:
    output.vPosition = float4(mul(v.vPosition, (float4x3)g_mWorld), 1);
    output.vPosition = float4(mul(output.vPosition, (float4x3)g_mView),1);
    output.vPosition = mul(output.vPosition, g_mProj);

    //
    // diffuse:
    output.vDiffuse = v.vColorDif;
    output.vUV = v.vUV;

    return output;
}
#endif //VERTEX_SHADER

PS_OUTPUT mainPS(in VS_OUTPUT v) : SV_TARGET
{
  float4 TEXEL = g_Texture.SampleLevel(g_TextureSampler, v.vUV, 0);
  // add emissive and refine alpha
  // to avoid pure black color with 0 alpha in output.
  PS_OUTPUT sOut;
  sOut.color0 = lerp(1, g_colors[1], g_colorBlend.y) * 
                 lerp(1, v.vDiffuse*TEXEL, g_colorBlend.x);
  sOut.color0.a = g_colors[1].a*lerp(1, v.vDiffuse.a*lerp(1, TEXEL.a, g_alpha.z), g_colorBlend.a);
  sOut.color0 += float4(g_colors[3].rgb*g_colors[3].w, 0.004);
  sOut.color1 = float4(g_colors[2].rgb, g_colors[2].a);
  return out;
}

