// Sight PS

cbuffer _Globals : register(b0)
{
  float4 g_vParams : packoffset(c0);
  float4 g_aUVShiftData[64] : packoffset(c1);
  float4 bones[192] : packoffset(c65);
  float4 difColor : packoffset(c257);
  float4x4 g_mViewProj : packoffset(c258);
  float4 g_vCamPos : packoffset(c262);
  float MAX_DISTANCE_SCALE : packoffset(c263) = {135};
}

SamplerState mySampler_s : register(s0);
Texture2D<float4> mySampler : register(t0);


// 3Dmigoto declarations
#define cmp -
Texture1D<float4> IniParams : register(t120);
// Texture2D<float4> StereoParams : register(t125);


void main( 
  float4 v0 : SV_Position0,
  float4 v1 : TEXCOORD0,
  float4 v2 : TEXCOORD2,
  float3 v3 : TEXCOORD1,
  out float4 o0 : SV_Target0)

{
  float4 r0,r1;
  uint4 bitmask, uiDest;
  float4 fDest;

float flagOnOff = IniParams.Load(0).y;   
float z = IniParams.Load(0).z; // type of HUD ID : 1=Ru, 2=Ger, 3=US, 4=It
float scale = IniParams.Load(int2(z, 0)).x;
float threshold = IniParams.Load(int2(z, 0)).y;
float zoomFactor = IniParams.Load(0).w; 
float2 tCoord;
float fix;
float3 tabFix[6] = {
    {1.0,0.0,0.1},  // not used
	{0.9,0.0,0.0}, 	//Ru color replace
	{1.0,0.15,0.0}, //Ge color replace
	{0.2,0.2,0.1}, //Us color replace
	{0.2,0.2,0.1}, //It color replace
	{0.6,0.2,0.0}  //Spit color replace
	};

// if (flagOnOff == 0) {
switch(flagOnOff) {
case 0:
  r0.x = dot(v3.xyz, v3.xyz);
  r0.x = rsqrt(r0.x);
  r0.xyz = v3.xyz * r0.xxx;
  r0.w = dot(v2.xyz, v2.xyz);
  r0.w = rsqrt(r0.w);
  r1.xyz = v2.xyz * r0.www;
  r0.x = dot(r0.xyz, r1.xyz);
  r0.x = max(0, -r0.x);
  r0.x = -0.996999979 + r0.x;
  r0.x = saturate(333 * r0.x);
  r0.x = log2(r0.x);
  r0.x = 1.5 * r0.x;
  r0.x = exp2(r0.x);
  r0.x = r0.x * 4 + 1;
  r1.xyzw = mySampler.Sample(mySampler_s, v1.xy).xyzw;
  r0.xyz = r1.xyz * r0.xxx;
  r0.w = -r1.w * 85.3333359 + 1;
  r0.w = saturate(10 * r0.w);
  r0.xyz = difColor.xyz * r0.xyz;
  o0.xyz = r0.xyz * r0.www;
  o0.w = 0;
  break;
// } else {
case 1:
case 2:
	fix = 0.0; 	
	r0.w=1.0;
	tCoord.x = v1.x*scale;
	tCoord.y = v1.y*scale;
	
	//fix for RU and US sight
	if (z == 1 || z == 3 ) {
		//fix to avoid texture repeated
		if (v1.x > 1/scale) r0.w=0.0;
		if (v1.y < 1-1/scale+0.001) r0.w=0.0;
		r1.xyzw = mySampler.Sample(mySampler_s, tCoord.xy).xyzw;
	}

	//fix for Ge and It sight
	if (z ==2 || z == 4 ) {
		//fix for pair scale
		fix=0;
		if (scale % 2 == 0 ) fix = 0.5;
		tCoord.x +=fix;
		tCoord.y +=fix;

		//fix to avoid texture repeated
		 if (v1.x > 0.5+1/(2*scale)) r0.w=0.0;
		 if (v1.x < 0.5-1/(2*scale)) r0.w=0.0;
		 if (v1.y > 0.5+1/(2*scale)) r0.w=0.0;
		 if (v1.y < 0.5-1/(2*scale)) r0.w=0.0;
		
		// if (tCoord.y < scale-1+0.1) r0.w=0.0;
		r1.xyzw = mySampler.Sample(mySampler_s, tCoord.xy).xyzw;
	}

	//fix Spit sight
	if (z == 5 ) {
		r1.xyzw = mySampler.Sample(mySampler_s, tCoord.xy).xyzw;
	}
	//strenghened sight color
	if (r1.x > threshold ) r1.xyz=tabFix[z].xyz;
	o0.xyz = r1.xyz * r0.www;
	o0.w = 0;
	break;
case 3:
	o0.xyzw=float4(0,0,0,0);
	break;
}


  // if (g_vCamPos.x != 0) o0.xyz+=float3(1,0,0); //oeil D
  // if (g_vCamPos.x == 0) o0.xyz+=float3(0,1,0); // oeil G
// if (v1.y > 0) o0.xyz+=float3(v1.y,0,0);
// if (v1.y < 0) o0.xyz+=float3(0,-v1.y,0);
  
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
//   using 3Dmigoto v1.2.50 on Mon Apr 17 18:09:31 2017
//
//
// Buffer Definitions: 
//
// cbuffer $Globals
// {
//
//   float4 g_vParams;                  // Offset:    0 Size:    16 [unused]
//   float4 g_aUVShiftData[64];         // Offset:   16 Size:  1024 [unused]
//   float4 bones[192];                 // Offset: 1040 Size:  3072 [unused]
//   float4 difColor;                   // Offset: 4112 Size:    16
//   float4x4 g_mViewProj;              // Offset: 4128 Size:    64 [unused]
//   float4 g_vCamPos;                  // Offset: 4192 Size:    16 [unused]
//   float MAX_DISTANCE_SCALE;          // Offset: 4208 Size:     4 [unused]
//      = 0x43070000 
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// mySampler                         sampler      NA          NA    0        1
// mySampler                         texture  float4          2d    0        1
// $Globals                          cbuffer      NA          NA    0        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Position              0   xyzw        0      POS   float       
// TEXCOORD                 0   xy          1     NONE   float   xy  
// TEXCOORD                 2   xyz         2     NONE   float   xyz 
// TEXCOORD                 1   xyz         3     NONE   float   xyz 
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Target                0   xyzw        0   TARGET   float   xyzw
//
ps_4_0
dcl_constantbuffer cb0[258], immediateIndexed
dcl_sampler s0, mode_default
dcl_resource_texture2d (float,float,float,float) t0
dcl_input_ps linear v1.xy
dcl_input_ps linear v2.xyz
dcl_input_ps linear v3.xyz
dcl_output o0.xyzw
dcl_temps 2
dp3 r0.x, v3.xyzx, v3.xyzx
rsq r0.x, r0.x
mul r0.xyz, r0.xxxx, v3.xyzx
dp3 r0.w, v2.xyzx, v2.xyzx
rsq r0.w, r0.w
mul r1.xyz, r0.wwww, v2.xyzx
dp3 r0.x, r0.xyzx, r1.xyzx
max r0.x, -r0.x, l(0.000000)
add r0.x, r0.x, l(-0.997000)
mul_sat r0.x, r0.x, l(333.000000)
log r0.x, r0.x
mul r0.x, r0.x, l(1.500000)
exp r0.x, r0.x
mad r0.x, r0.x, l(4.000000), l(1.000000)
sample r1.xyzw, v1.xyxx, t0.xyzw, s0
mul r0.xyz, r0.xxxx, r1.xyzx
mad r0.w, -r1.w, l(85.333336), l(1.000000)
mul_sat r0.w, r0.w, l(10.000000)
mul r0.xyz, r0.xyzx, cb0[257].xyzx
mul o0.xyz, r0.wwww, r0.xyzx
mov o0.w, l(0)
ret 
// Approximately 22 instruction slots used

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
