#define LINEAR_CLAMP_MAP(x) \
		MipFilter[x] = LINEAR; \
		MagFilter[x] = LINEAR; \
		MinFilter[x] = LINEAR; \
		AddressU[x] = CLAMP; \
		AddressV[x] = CLAMP
		
#define LINEAR_WRAP_MAP(x) \
		MipFilter[x] = LINEAR; \
		MagFilter[x] = LINEAR; \
		MinFilter[x] = LINEAR; \
		AddressU[x] = WRAP; \
		AddressV[x] = WRAP

#define DIFF_MAP LINEAR_WRAP_MAP(0)
#define NORM_MAP LINEAR_CLAMP_MAP(1)
#define CHAN_MAP LINEAR_CLAMP_MAP(2)
#define APP_MAP  LINEAR_CLAMP_MAP(3)
	  	
#define DEPTH_ENABLE \
		ZEnable = TRUE; \
		ZWriteEnable = TRUE
		
#define DEPTH_DISABLE \
		ZEnable = FALSE; \
		ZWriteEnable = FALSE
		
#define ALPHA_BLEND_ENABLE \
		AlphaBlendEnable = TRUE; \
		SrcBlend = SRCALPHA; \
		DestBlend = INVSRCALPHA

#define ALPHA_MASK_ENABLE \
        AlphaBlendEnable = TRUE; \
        SrcBlend = DESTALPHA; \
        DestBlend = ZERO
        
#define ALPHA_BLEND_DISABLE \
		AlphaBlendEnable = FALSE; \
		SrcBlend = ONE; \
		DestBlend = ZERO
		
#define STENCIL_ENABLE  StencilEnable = TRUE
#define STENCIL_DISABLE StencilEnable = FALSE
#define CULL_DISABLE CullMode = NONE
#define CULL_ENABLE  CullMode = CW
#define CULL_REVERSE CullMode = CCW

sampler DiffMap : register(s0);
sampler NormMap : register(s1);
sampler ChanMap : register(s2);
sampler AppMap  : register(s3);
sampler ShadowMap : register(s4);

float4x4 o2pMat		: WORLDVIEWPROJECTION;	 // object to perspective matrix
float4x4 o2wMat		: WORLD;				 // object to world matrix
float4x4 viewMat    : VIEW;
float4x4 projMat    : PROJ;

#ifdef _OPENFIST_

float4x4 mat0		: MAT0;
float4x4 mat1		: MAT1;
float4   xyzw0		: XYZW0;
float4   xyzw1		: XYZW1;
float4   xyzw2		: XYZW2;
float4   xyzw3		: XYZW3;
float4	 wLitPos	: LPOS0;
float4	 wLitPos2	: LPOS1;
float3	 cLitClr    : LCLR0;
float3   cLitClr2   : LCLR1;
float    nLitInt    : LINT0;
float    nLitInt2   : LINT1;
float    nAmbInt    : AMBINT;
float3	 cAmbClr    : AMBCLR;
float4   bBoneTrans[96] : BONETRANS; 
float4 b2oBoneQuats[96] : BONEROTS;

#else // for XSI and other viewers
float4   bBoneTrans[96] < bool SasUiVisible = false; >;
float4 b2oBoneQuats[96] < bool SasUiVisible = false; >;
float4x4 mat0 < bool SasUiVisible = false; >;
float4x4 mat1 < bool SasUiVisible = false; >;
float4	 wLitPos2 < bool SasUiVisible = false; >;
float3   cLitClr2 < bool SasUiVisible = false; >;
float    nLitInt2 < bool SasUiVisible = false; >;
float4   xyzw1 < bool SasUiVisible = false; >;
float4   xyzw2 < bool SasUiVisible = false; >;
float4   xyzw3 < bool SasUiVisible = false; >;
float4	 wLitPos//	: LPOS0
<
    //string SasUiControl     = "Numeric";        // a.k.a. UIWidget
    //string SasUiLabel       = "Light Position"; // a.k.a. UIName
    //string SasUiDescription = "The position of light";
    // bool SasUiVisible = false; //XSI bug
    string SasBindAddress = "Sas.PointLight[0].Position";
>;// = { 100, 100, 100, 0 };

float3	 cLitClr
<
    //string SasUiControl     = "ColorPicker";
    //string SasUiLabel       = "Light Color"; // XSI bug
    //string SasUiDescription = "The color of the light";
    string SasBindAddress = "Sas.PointLight[0].Color"; //"Light01Color";//"Sas.Light[0].Color";
>;/// = {1.0f, 0.85f, 0.85f};

float nLitInt// : LINT
<
    string SasUiControl = "Numeric";
    string SasUiLabel   = "Light Intensity";
    float SasUiMax = 1.0f;
    float SasUiMin = 0.0f;
> = { 1.0f };

float3	 cAmbClr// : AMBCLR
<
    string SasUiControl     = "ColorPicker";
    string SasUiLabel       = "Ambient Light Color"; // XSI bug
    string SasUiDescription = "The color of the ambient light";
    //string SasBindAddress = "Sas.AmbientLight[0].Color"; XSI Bug - AmbientLight is slammed to 0.4,,,
> = {0.85f, 0.85f, 1.0f};

float nAmbInt// : AMBLINT
<
    string SasUiControl     = "Numeric";
    string SasUiLabel       = "Ambient Light Intensity";
    float SasUiMax = 1.0f;
    float SasUiMin = 0.0f;
> = { 0.3f };

float4   xyzw0		: XYZW0
<
    string SasUiControl = "Slider";
    string SasUiLabel   = "Fade Amount (X is for the UnlitFade technique)";
    float SasUiMax = 1.0f;
    float SasUiMin = 0.0f;
> = { 1.0f, 0, 0, 0 };
#endif


float4	 wCamPos	: CAMERAPOSITION;



//variable prefix 'n' = value/numbers (no space),
//				  'o' = object space,
//				  'w' = world space,
//				  'v' = view space, 
//				  't' = tangent space,
//				  'p' = perspective space (final- screen space)
//                'c' = color space [0...1]
//                'b' = bone space (each bone's origin)

/*-----------------------------------------------------------------------------------
||		Generic Lighting Equations
\*----------------------------------------------------------------------------------*/


float3 CalcLitClr(float3 surfaceClr, float3 litClr, float litInt)
{
    return surfaceClr * litClr.rgb * litInt;
}

float3 CalcLitClr(float3 surfaceClr, float3 litClr, float litInt, float3 toLit, float3 normal)
{
	return CalcLitClr(surfaceClr, litClr, litInt) * saturate(dot( normal, toLit ));
}

float3 CalcLitClr(float3 surfaceClr, float3 litClr, float litInt, float3 toLit, float3 normal, float3 halfAngle, float specularity, float glossiness = 0.5f)
{
	const int MaxGlossiness = 4096;
	
	float nSpecularIncidence = saturate(dot( normal, halfAngle ));
	float nSpecContribution	 = pow(nSpecularIncidence, pow((MaxGlossiness*glossiness), glossiness)); //normalizes visual glossiness, such that 0.5 glossiness appears about half as glossy as 1.0
	float3 specularLitClr = nSpecContribution * litInt * litClr * specularity;
	return CalcLitClr(surfaceClr, litClr, litInt, toLit, normal) + specularLitClr;
}

/*-----------------------------------------------------------------------------------
||		Generic Math Equations
\*----------------------------------------------------------------------------------*/

//the scalar is stored in the w
float4 mulQuat(float4 q2, float4 q1) // this function is taken from a math book that multiplies right to left, so by swapping q2 and q1 in the parameter list, this algorithm becomes left to right
{
	float s1 = q1.w;
	float s2 = q2.w;
	
	float3 v1 = q1.xyz;
	float3 v2 = q2.xyz;
	
	//mathematically written: s1*s2 - dot(v1,v2) + s1*v2 + s2*v1 + cross(v1, v2)
	return float4(s1*v2 + s2*v1 + cross(v1, v2), s1*s2 - dot(v1,v2));
}

// quaternion conjugate
float4 conjugate(float4 q) { return float4(-q.x, -q.y, -q.z, q.w); }
float4 rotateVecByQuat(float4 v, float4 q) { return mulQuat( mulQuat(conjugate(q), v), q ); }

// bone to object space transform (rotates vert around the bone center and than transforms it to a position [weighted for 4 bones])
float4 b2oTransform(float4 bPos, float4 nBlendIdxs, float4 nBlendWgts)
{
    float4 oPos = float4(0,0,0,0);
    int bone;
    bone = nBlendIdxs.x; oPos += (rotateVecByQuat(bPos, b2oBoneQuats[bone]) + bBoneTrans[bone]) * nBlendWgts.x;
    bone = nBlendIdxs.y; oPos += (rotateVecByQuat(bPos, b2oBoneQuats[bone]) + bBoneTrans[bone]) * nBlendWgts.y;
    bone = nBlendIdxs.w; oPos += (rotateVecByQuat(bPos, b2oBoneQuats[bone]) + bBoneTrans[bone]) * nBlendWgts.w;
    bone = nBlendIdxs.z; oPos += (rotateVecByQuat(bPos, b2oBoneQuats[bone]) + bBoneTrans[bone]) * nBlendWgts.z;
    oPos.w = 1;
    return oPos;
}

float4 sum2Rotations(float4 v, float4 nBlendIdxs, float4 nBlendWgts)
{
    float4 sum = float4(0,0,0,0);
    int bone;
    bone = nBlendIdxs.x; sum += rotateVecByQuat(v, b2oBoneQuats[bone]) * nBlendWgts.x;
    bone = nBlendIdxs.y; sum += rotateVecByQuat(v, b2oBoneQuats[bone]) * nBlendWgts.y;
    return sum;
}

float4 sum4Rotations(float4 v, float4 nBlendIdxs, float4 nBlendWgts)
{
    float4 sum = float4(0,0,0,0);
    int bone;
    bone = nBlendIdxs.x; sum += rotateVecByQuat(v, b2oBoneQuats[bone]) * nBlendWgts.x;
    bone = nBlendIdxs.y; sum += rotateVecByQuat(v, b2oBoneQuats[bone]) * nBlendWgts.y;
    bone = nBlendIdxs.z; sum += rotateVecByQuat(v, b2oBoneQuats[bone]) * nBlendWgts.z;
    bone = nBlendIdxs.w; sum += rotateVecByQuat(v, b2oBoneQuats[bone]) * nBlendWgts.w;
    return sum;
}

/*-----------------------------------------------------------------------------------
|| THIS IS A PROJECT SPECIFIC LIGHTING EQUATION
\*----------------------------------------------------------------------------------*/
float CalcIncidentLitIntensity(float3 normal, float3 toCam) { return clamp( pow( 1-dot( normal, toCam ),6 ), 0, 0.5 )*0.3f; }

#define PS_XSI_TEST cAmbClr = float4(0.75f, 0.8f, 0.85f, 1.0f); cLitClr = float4(1,1,1,0.3f);
#define VS_XSI_TEST wLitPos = float4(10,10,10,10);

/*-----------------------------------------------------------------------------------
||		Blinn w/ Incident Lighting (a.k.a. the 'Plushy' shader)
\*----------------------------------------------------------------------------------*/

struct BlinnPlushAnimIn
{
	float4 bPos		  : POSITION;
	float3 oNormal	  : NORMAL;
	float2 tTex		  : TEXCOORD0;
    float4 nBlendIdxs : BLENDINDICES0;
	float4 nBlendWgts : BLENDWEIGHT0;
};

struct BlinnPlushIn
{
	float4 oPos		: POSITION;
	float3 oNormal	: NORMAL;
	float2 tTex		: TEXCOORD0;
};

struct BlinnPlushOut
{
	float4 pPos		 : POSITION;
	float2 tTex		 : TEXCOORD0;
	float3 wToLit	 : TEXCOORD1;
	float3 wToCam	 : TEXCOORD2;
	float3 wNormal	 : TEXCOORD3;
};

BlinnPlushOut vsBlinnPlush4BoneBlendAnim(BlinnPlushAnimIn vIn)
{
    BlinnPlushOut vOut;

    float4 oPos = b2oTransform(vIn.bPos, vIn.nBlendIdxs, vIn.nBlendWgts);
    float4 wPos = mul(oPos, o2wMat);
      vOut.pPos = mul(oPos, o2pMat);

    vOut.wToCam	= wCamPos - wPos;
    vOut.wToLit = wLitPos - wPos;
    vOut.wNormal= mul(sum4Rotations(float4(vIn.oNormal,1), vIn.nBlendIdxs, vIn.nBlendWgts), (float3x3)o2wMat);

    vOut.tTex   = vIn.tTex;

    return vOut;
}

BlinnPlushOut vsBlinnPlush2BoneBlendAnim(BlinnPlushAnimIn vIn)
{
    BlinnPlushOut vOut;

    float4 oPos = b2oTransform(vIn.bPos, vIn.nBlendIdxs, vIn.nBlendWgts);
    float4 wPos = mul(oPos, o2wMat);
      vOut.pPos = mul(oPos, o2pMat);

    vOut.wToCam	= wCamPos - wPos;
    vOut.wToLit = wLitPos - wPos;
    vOut.wNormal= mul(sum2Rotations(float4(vIn.oNormal,1), vIn.nBlendIdxs, vIn.nBlendWgts), (float3x3)o2wMat);

    vOut.tTex   = vIn.tTex;

    return vOut;
}

BlinnPlushOut vsBlinnPlush(BlinnPlushIn vIn)
{
	BlinnPlushOut vOut;

    float4 wPos		= mul(vIn.oPos, o2wMat);
      vOut.pPos		= mul(vIn.oPos, o2pMat);
            
    vOut.wToCam	    = wCamPos - wPos;
    vOut.wToLit		= wLitPos - wPos;
    vOut.wNormal	= mul(vIn.oNormal, (float3x3)o2wMat);
    
    vOut.tTex		= vIn.tTex;

    return vOut;
}


float4 psBlinnPlush(BlinnPlushOut vIn): COLOR0
{
	float4 cDiffuseClr		 = tex2D(DiffMap, vIn.tTex);
    float3 wNormal = normalize(vIn.wNormal);
    float3 wToLit  = normalize(vIn.wToLit);
    float3 wToCam  = normalize(vIn.wToCam);
	return float4(CalcLitClr(cDiffuseClr.rgb, cLitClr, nLitInt, wToLit, wNormal)
                + CalcLitClr(cDiffuseClr.rgb, cAmbClr, nAmbInt)
                + CalcIncidentLitIntensity(wNormal, wToCam), cDiffuseClr.a); // transparency
}



/*-----------------------------------------------------------------------------------
||		BlinnSpec w/ Incident Lighting (a.k.a. the 'Plushy' shader w/ spec)
\*----------------------------------------------------------------------------------*/

struct BlinnSpecPlushIn
{
	float4 oPos		: POSITION;
	float3 oNormal	: NORMAL;
	float2 tTex		: TEXCOORD0;
};

struct BlinnSpecPlushOut
{
	float4 pPos		 : POSITION;
	float2 tTex		 : TEXCOORD0;
	float3 wToLit	 : TEXCOORD1;
	float3 wToCam	 : TEXCOORD2;
	float3 wNormal	 : TEXCOORD3;
	float3 wHalfAngle: TEXCOORD4;
};

BlinnSpecPlushOut vsBlinnSpecPlush(BlinnSpecPlushIn vIn)
{
	BlinnSpecPlushOut vOut;

    float4 wPos		= mul(vIn.oPos, o2wMat);
      vOut.pPos		= mul(vIn.oPos, o2pMat);
            
    vOut.wToCam	    = normalize(wCamPos - wPos);
    vOut.wToLit		= normalize(wLitPos - wPos);
    vOut.wNormal	= mul(vIn.oNormal, (float3x3)o2wMat);
    vOut.wHalfAngle	= vOut.wToLit + vOut.wToCam;
    
    vOut.tTex		= vIn.tTex;

    return vOut;
}


float4 psBlinnSpecPlush(BlinnSpecPlushOut vIn): COLOR0
{
	float4 cDiffuseClr = tex2D(DiffMap, vIn.tTex);
    float4 cChanMapClr = tex2D(ChanMap, vIn.tTex);
    float3 wNormal = normalize(vIn.wNormal);
    float3 wToLit  = normalize(vIn.wToLit);
    float3 wHalfAngle = normalize(vIn.wHalfAngle);
	return float4(CalcLitClr(cDiffuseClr.rgb, cLitClr, nLitInt, wToLit, wNormal, wHalfAngle, cChanMapClr.r) +
                  CalcLitClr(cDiffuseClr.rgb, cAmbClr, nAmbInt) +
                  CalcIncidentLitIntensity(vIn.wNormal, vIn.wToCam), cDiffuseClr.a);
}



// The output of the vertex shader for rendering shadows
struct PlanarStencilShadowsOut
{
	float4 pos : POSITION; // Position after vertex shader has executed
	float4 color : COLOR; // Color after vertex shader has executed
};

// This is the vertex shader responsible for drawing shadow
PlanarStencilShadowsOut vsPlanarStencilShadows(float4 pos : POSITION)
{
	PlanarStencilShadowsOut vOut = (PlanarStencilShadowsOut)0; // The output of the vertex shader
	
	vOut.pos = mul(pos, o2wMat);
	
	float4 p = { xyzw2[0], xyzw2[1], xyzw2[2], -xyzw2[3] };
	float4 l = wLitPos - vOut.pos;
	l.w = 0; // w == 1.0f for point light, 0.0f for directional light
	
	float d = dot(p,l);
	
	/*	RH Shadow Projection Matrix Onto A Plane
	
		p = normalize(Plane);
		l = Light;
		d = dot(P, L)

		d - p.x * l.x	-(p.x * l.y)	-(p.x * l.z)	-(p.x * l.w)
		-(p.y * l.x)	d - p.y * l.y	-(p.y * l.z)	-(p.y * l.w)
		-(p.z * l.x)	-(p.z * l.y)	d - p.z * l.z	-(p.z * l.w)
		-(p.w * l.x)	-(p.w * l.y)	-(p.w * l.z)	d - p.w * l.w
	*/	
	float4x4 shadowMat = {d - p.x * l.x,	-(p.x * l.y),	-(p.x * l.z),	-(p.x * l.w),
						  -(p.y * l.x),		d - p.y * l.y,	-(p.y * l.z),	-(p.y * l.w),
						  -(p.z * l.x),		-(p.z * l.y),	d - p.z * l.z,	-(p.z * l.w),
						  -(p.w * l.x),		-(p.w * l.y),	-(p.w * l.z),	d - p.w * l.w };
	
	// Determine alpha so that we 
	// don't draw stuff below the plane onto the plane
	float a = 0.2f;
	float3 v = (vOut.pos.xyz - float3(0,xyzw2.w,0));
	
	if(dot(xyzw2.xyz, v) < 0)
		a = 0;
		
	vOut.pos = mul(vOut.pos, shadowMat); // Multiply by shadow matrix
	vOut.pos = mul(vOut.pos, viewMat); // Multiply by view matrix
	vOut.pos = mul(vOut.pos, projMat); // Multiply by the projection matrix  
	vOut.color = float4(0.0, 0.0f, 0.0f, a); // Shadow color
	//vOut.color = float4(1, 1, 1, a); // Shadow color
		
	return vOut;
}

float4 psPlanarStencilShadows(PlanarStencilShadowsOut pIn): COLOR0
{
	return pIn.color;	
}

/*-----------------------------------------------------------------------------------
||		Shape shader w/ Incident Lighting (a.k.a. the 'Plushy' shader) and UI Lighting
\*----------------------------------------------------------------------------------*/

/*struct ShapeIn
{
	float4 oPos		: POSITION;
	float3 oNormal	: NORMAL;
	float2 tTex		: TEXCOORD0;
};

struct ShapeOut
{
	float4 pPos		 : POSITION;
	float2 tTex		 : TEXCOORD0;
	float3 wToLit	 : TEXCOORD1;
	float3 wNormal	 : TEXCOORD2;
    float4 wToLit2   : TEXCOORD3;
    float3 wToCam    : TEXCOORD4;
};

ShapeOut vsShape(ShapeIn vIn)
{
	ShapeOut vOut;

    float4 wPos		= mul(vIn.oPos, o2wMat);
      vOut.pPos		= mul(vIn.oPos, o2pMat);
            
    vOut.wToCam	    = normalize(wCamPos - wPos);
    vOut.wToLit		= normalize(wLitPos - wPos);
    vOut.wToLit2.xyz= wLitPos2 - wPos;
    vOut.wToLit2.w  = 1.0f/(length(vOut.wToLit2.xyz)/10.0f);
    vOut.wToLit2.xyz= normalize(vOut.wToLit2.xyz);
    vOut.wNormal	= mul(vIn.oNormal, (float3x3)o2wMat);
    
    vOut.tTex		= vIn.tTex;

    return vOut;
}

float4 psShape(ShapeOut vIn): COLOR0
{
	float4 cDiffuseClr = tex2D(DiffMap, vIn.tTex);
	cDiffuseClr = float4(lerp(cDiffuseClr.rgb, xyzw0.rgb, xyzw0.a), cDiffuseClr.a);
    float3 wNormal = normalize(vIn.wNormal);
    float3 wToLit  = normalize(vIn.wToLit);
    float3 wToLit2 = normalize(vIn.wToLit2.xyz);
	
	return float4(CalcLitClr(cDiffuseClr.rgb, cLitClr, nLitInt, wToLit, wNormal) +
                  CalcLitClr(cDiffuseClr.rgb, cLitClr2,nLitInt2,wToLit2, wNormal)*vIn.wToLit2.w +
                  CalcLitClr(cDiffuseClr.rgb, cAmbClr, nAmbInt) +
                  CalcIncidentLitIntensity(vIn.wNormal, vIn.wToCam), cDiffuseClr.a);
}

*/

// ================================================ SHAPE REWRITE ===============================

struct ShapeIn
{
	float4 oPos		: POSITION;
	float3 oNormal	: NORMAL;
	float2 tTex		: TEXCOORD0;
};

struct ShapeOut
{
	float4 pPos		 : POSITION;
	float2 tTex		 : TEXCOORD0;
	float3 wToLit	 : TEXCOORD1;
	float3 wNormal	 : TEXCOORD2;
    float3 wToCam    : TEXCOORD3;
};

ShapeOut vsShape(ShapeIn vIn)
{
	ShapeOut vOut;

    float4 wPos		= mul(vIn.oPos, o2wMat);
      vOut.pPos		= mul(vIn.oPos, o2pMat);
            
    vOut.wToCam	    = normalize(wCamPos - wPos);
    vOut.wToLit		= wLitPos - wPos;
    vOut.wNormal	= mul(vIn.oNormal, (float3x3)o2wMat);
    
    vOut.tTex		= vIn.tTex;

    return vOut;
}

float4 psShape(ShapeOut vIn): COLOR0
{
	float4 cDiffuseClr = tex2D(DiffMap, vIn.tTex);
	cDiffuseClr = float4(lerp(cDiffuseClr.rgb, xyzw0.rgb, xyzw0.a), cDiffuseClr.a);
    float3 wNormal = normalize(vIn.wNormal);
    float3 wToLit  = normalize(vIn.wToLit);
	
	return float4(CalcLitClr(cDiffuseClr.rgb, cLitClr, nLitInt, wToLit, wNormal) +
                  CalcLitClr(cDiffuseClr.rgb, cAmbClr, nAmbInt) +
                  CalcIncidentLitIntensity(vIn.wNormal, vIn.wToCam), cDiffuseClr.a);
}




/*-----------------------------------------------------------------------------------
||		SelectionRing
\*----------------------------------------------------------------------------------*/

struct SelRingData								
{														
	float4	pos		:	POSITION;						
	float2	uv0		:	TEXCOORD0;						
};																						

SelRingData vsSelRing(SelRingData vIn)								
{														
	SelRingData vOut;												
	vOut.pos = mul(vIn.pos, o2pMat);				
	vOut.uv0 = vIn.uv0;							
		return vOut;
};
														
float4 psSelRing(SelRingData pIn) : COLOR0					
{														
	return tex2D(DiffMap, pIn.uv0);										
};												




/*-----------------------------------------------------------------------------------
||		Day to Night (Lerps a three color gradient with another three colors)
\*----------------------------------------------------------------------------------*/

struct vsDayToNightIn
{
	float4 oPos : POSITION;
	float2 tTex : TEXCOORD0;
};

struct vsDayToNightOut
{
	float4 pPos : POSITION;
	float2 tTex : TEXCOORD0;
};

vsDayToNightOut DayToNightVS(vsDayToNightIn vIn)
{
	vsDayToNightOut vOut;

	vOut.pPos = mul(vIn.oPos, o2pMat);
    vOut.tTex = vIn.tTex;
		return vOut;
}

float4 DayToNightPS(vsDayToNightOut pIn) : COLOR0
{
	float h, t2m, m2b;
	float f = 0.0f;
	
	h = pIn.tTex.y; // Height
	
	t2m = clamp(h, 0, 0.5f);
	t2m *= 2.0f;
	
	m2b = clamp(h, 0.5f, 1.0f);
	m2b -= 0.5f;
	m2b *= 2.0f;
	
	if(h < 0.5f)
		f = 1.0f;
		
// Uncomment the line below and to use the other set of gradient colors
//#define MARTY_TEST
#ifdef MARTY_TEST
	float4 gradClr0 = { 1.0f, 0.5, 1.0f, 1.0f };
	float4 gradClr1 = { 1.0f, 0.5, 1.0f, 1.0f };
	float4 gradClr2 = { 1.0f, 0.5, 1.0f, 1.0f };
#else
	float4 gradClr0 = { mat0[0][0], mat0[0][1], mat0[0][2], 1.0f };
	float4 gradClr1 = { mat0[1][0], mat0[1][1], mat0[1][2], 1.0f };
	float4 gradClr2 = { mat0[2][0], mat0[2][1], mat0[2][2], 1.0f };
#endif

	float4 c0 = lerp(gradClr0, gradClr1, t2m);
	float4 c1 = lerp(gradClr1, gradClr2, m2b);
	
	return c0 * f + (c1 * (1.0f - f));
	
}



/*-----------------------------------------------------------------------------------
||		Static Unlit Shader
\*----------------------------------------------------------------------------------*/

struct UnlitIn								
{														
	float3	pos		:	POSITION;						
	float2	uv0		:	TEXCOORD0;						
};														

struct UnlitOut											
{														
	float4  pos		:	POSITION;						
	float2	uv0		:	TEXCOORD0;						
};														

UnlitOut vsUnlit(UnlitIn vIn)								
{														
	UnlitOut vOut;												
	vOut.pos = mul(float4(vIn.pos, 1), o2pMat);				
	vOut.uv0 = vIn.uv0;							
		return vOut;
};

UnlitOut vsUnlitUVScroll(UnlitIn vIn)
{
	UnlitOut vOut;
	vOut.pos = mul(float4(vIn.pos, 1), o2pMat);
	vOut.uv0 = vIn.uv0 + (float2)xyzw0;
	return vOut;
}
														
float4 psUnlit(UnlitOut pIn) : COLOR0					
{	
	return tex2D(DiffMap, pIn.uv0);										
};														


/*-----------------------------------------------------------------------------------
||		Project specific defines
\*----------------------------------------------------------------------------------*/

#define SHADOWS_ENABLE  STENCIL_ENABLE; StencilFunc = ALWAYS; StencilPass = ZERO
#define SHADOWS_PASS    StencilRef = 1; StencilFunc = EQUAL; StencilPass = INCRSAT
#define STENCIL_REPLACE StencilEnable = TRUE; StencilFunc = ALWAYS; StencilPass = REPLACE; StencilRef = 1
#define DEPTH_TEST      ZEnable = TRUE; ZWriteEnable = FALSE

/*-----------------------------------------------------------------------------------
||		Engine Version of the Techniques
\*----------------------------------------------------------------------------------*/

#ifdef _OPENFIST_

technique UnlitUVScroll				  { pass p0 { DIFF_MAP;								 DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsUnlitUVScroll();      pixelshader = compile ps_2_0 psUnlit();           } }
technique UnlitAlphaNoZ               { pass p0 { DIFF_MAP;                              DEPTH_DISABLE; ALPHA_BLEND_ENABLE;  STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsUnlit();                    pixelshader = compile ps_2_0 psUnlit();          } }
technique BlinnPlush                  {	pass p0	{ DIFF_MAP;                              DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsBlinnPlush();               pixelshader = compile ps_2_0 psBlinnPlush();     } }
technique BlinnPlush2BoneBlendAnim    { pass p0 { DIFF_MAP;                              DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsBlinnPlush2BoneBlendAnim(); pixelshader = compile ps_2_0 psBlinnPlush();     } }
technique BlinnPlush4BoneBlendAnim    { pass p0 { DIFF_MAP;                              DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsBlinnPlush4BoneBlendAnim(); pixelshader = compile ps_2_0 psBlinnPlush();     } }
technique BlinnSpecPlush              {	pass p0	{ DIFF_MAP;            CHAN_MAP;         DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsBlinnSpecPlush();           pixelshader = compile ps_2_0 psBlinnSpecPlush(); } }
technique DayToNight                  {	pass p0	{ DIFF_MAP;                              DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 DayToNightVS();               pixelshader = compile ps_2_0 DayToNightPS();     } }
technique Shape                       {	pass p0	{ DIFF_MAP;                              DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; SHADOWS_ENABLE;  CULL_ENABLE;  vertexshader = compile vs_2_0 vsShape();                    pixelshader = compile ps_2_0 psShape();          }
                                        pass p1	{                                        DEPTH_DISABLE;	ALPHA_BLEND_ENABLE;  SHADOWS_PASS;                  vertexshader = compile vs_2_0 vsPlanarStencilShadows();     pixelshader = compile ps_2_0 psPlanarStencilShadows(); } }
technique SelectionRing		          { pass p0 { DIFF_MAP;  		                     DEPTH_TEST;    ALPHA_BLEND_ENABLE;  STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsSelRing();                  pixelshader = compile ps_2_0 psSelRing();        } }
technique Platform                    {	pass p0	{ DIFF_MAP;            CHAN_MAP;         DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_REPLACE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsBlinnPlush();               pixelshader = compile ps_2_0 psBlinnPlush();     } }
technique PlatformDeco                {	pass p0	{ DIFF_MAP;            CHAN_MAP;         DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; SHADOWS_ENABLE;  CULL_ENABLE;  vertexshader = compile vs_2_0 vsBlinnPlush();               pixelshader = compile ps_2_0 psBlinnPlush();     }
                                        pass p1	{                                        DEPTH_DISABLE;	ALPHA_BLEND_ENABLE;  SHADOWS_PASS;                  vertexshader = compile vs_2_0 vsPlanarStencilShadows();     pixelshader = compile ps_2_0 psPlanarStencilShadows(); } }

/*-----------------------------------------------------------------------------------
||		XSI Version of the Techniques (no animation, shadows or glows)
\*----------------------------------------------------------------------------------*/

#else

technique UnlitUVScroll				  { pass p0 { DIFF_MAP;								 DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsUnlitUVScroll();      pixelshader = compile ps_2_0 psUnlit();           } }
technique UnlitAlphaNoZ               { pass p0 { DIFF_MAP;                              DEPTH_DISABLE; ALPHA_BLEND_ENABLE;  STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsUnlit();                    pixelshader = compile ps_2_0 psUnlit();          } }
technique BlinnPlush                  {	pass p0	{ DIFF_MAP;                              DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsBlinnPlush();               pixelshader = compile ps_2_0 psBlinnPlush();     } }
technique BlinnPlush2BoneBlendAnim    { pass p0 { DIFF_MAP;                              DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsBlinnPlush();               pixelshader = compile ps_2_0 psBlinnPlush();     } }
technique BlinnPlush4BoneBlendAnim    { pass p0 { DIFF_MAP;                              DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsBlinnPlush();               pixelshader = compile ps_2_0 psBlinnPlush();     } }
technique BlinnSpecPlush              {	pass p0	{ DIFF_MAP;            CHAN_MAP;         DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsBlinnSpecPlush();           pixelshader = compile ps_2_0 psBlinnSpecPlush(); } }
technique DayToNight                  {	pass p0	{ DIFF_MAP;                              DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 DayToNightVS();               pixelshader = compile ps_2_0 DayToNightPS();     } }
technique Shape                       {	pass p0	{ DIFF_MAP;                              DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsShape();                    pixelshader = compile ps_2_0 psShape();          } }
technique SelectionRing		          { pass p0 { DIFF_MAP;  		                     DEPTH_TEST;    ALPHA_BLEND_ENABLE;  STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsSelRing();                  pixelshader = compile ps_2_0 psSelRing();        } }
technique Platform                    {	pass p0	{ DIFF_MAP;            CHAN_MAP;         DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_REPLACE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsBlinnPlush();               pixelshader = compile ps_2_0 psBlinnPlush();     } }
technique PlatformDeco                {	pass p0	{ DIFF_MAP;            CHAN_MAP;         DEPTH_ENABLE;  ALPHA_BLEND_DISABLE; STENCIL_DISABLE; CULL_ENABLE;  vertexshader = compile vs_2_0 vsBlinnPlush();               pixelshader = compile ps_2_0 psBlinnPlush();     } }

#endif