float4x4 worldViewProj : WorldViewProjection;
float4x4 viewProj : ViewProjection;
float4x4 world : World;
float4x4 WorldView : WorldView;

#define BONEMODE_4x3 1


#include "..\generic.fx"
#include "..\mrt.fx"
#include "..\GameLights.fx"

float Timer : TIMER_0;

float CloakRatio : CloakRatio;

texture ScreenTexture : ScreenTexture;
sampler ScreenTextureSampler = sampler_state 
{
    texture = <ScreenTexture>;
    AddressU  = CLAMP;
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};


float3 campos : CAMERAPOS;

struct vertexInputBoned {
    float4 position				: POSITION;
    float3 normal				: NORMAL;
    float2 texCoordDiffuse		: TEXCOORD0;
    float4 boneids              : BLENDINDICES;
    float4 boneweights          : BLENDWEIGHT;
    float4 tangent              : TANGENT;
};

struct vertexInputUnboned {
    float4 position				: POSITION;
    float3 normal				: NORMAL;
    float2 texCoordDiffuse		: TEXCOORD0;
    float4 tangent              : TANGENT;
};

struct vertexOutput {
    float4 hPosition		: POSITION;
    float2 texCoordDiffuse	: TEXCOORD0;
    float3 posWorld         : TEXCOORD1;
    float3 normWorld        : TEXCOORD2;
    float4 tanWorld         : TEXCOORD3;
    float4 screenPos        : TEXCOORD4;
    float3 normView		    : TEXCOORD5;
};

vertexOutput VS_Shared(float4 pos, float3 normObj, float3 norm, float3 tang, float tangw, float2 uv)
{
    vertexOutput OUT;

    OUT.normWorld = norm;
    OUT.posWorld = pos;
    OUT.tanWorld = float4(tang, tangw);
    OUT.texCoordDiffuse = uv;
    OUT.hPosition = mul( pos , viewProj);
    OUT.screenPos = mul( pos , viewProj);
    OUT.normView = mul( normObj, WorldView );
    return OUT;
}

vertexOutput VS_TransformBoned(vertexInputBoned IN) 
{
    float4 pos;
    float3 norm;
    float3 tang;
    float weight4;
    weight4 = 1 - dot(IN.boneweights.xyz, float3(1,1,1));
    float4 weights = float4(IN.boneweights.xyz, weight4);
    
    float4 boneids = VS_ScaleBoneIDsFromD3DCOLOR(IN.boneids);
    VS_TransformBonesPNT_4(IN.position, pos, IN.normal, norm, IN.tangent, tang, boneids, weights, world);
    
    return VS_Shared(pos, IN.normal, norm, tang, IN.tangent.w, IN.texCoordDiffuse);
}

vertexOutput VS_TransformUnboned(vertexInputUnboned IN) 
{
    float4 pos;
    float3 norm;
    float3 tang;
    
    pos = mul(IN.position, world);
    norm = mul(IN.normal, world);
    tang = mul(IN.tangent.xyz, world);
    
    return VS_Shared(pos, IN.normal, norm, tang, IN.tangent.w, IN.texCoordDiffuse);
}

PS_OUTPUT PS_Textured( vertexOutput IN)
{
	float4 outColor;
	
    outColor.xyz = (IN.normView.xyz + 1) / 2;
    
    //outColor.xyz *= 1.0 + sin( Timer * 0.7f ) * 0.3f;    
    //CloakRatio = 0.5f;
    outColor.z = CloakRatio; // variable cloak ratio
    
    outColor.a = 0.0f;  // tag as 0 alpha in color channel
    
    PS_OUTPUT Out;
    SetPSOutput( Out, outColor, float4( campos - IN.posWorld.xyz, 1) );
    return Out;
}


//-----------------------------------
technique boned
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_TransformBoned();
		PixelShader  = compile ps_2_0 PS_Textured();
	 }
}

technique unboned
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_TransformUnboned();
		PixelShader  = compile ps_2_0 PS_Textured();
    }
}
