#include "shadergen:/autogenConditioners.h"
#include "shaders/common/torque.hlsl"
uniform sampler2D ColorSampler: register(s0);
uniform sampler2D Mask1Sampler: register(s1);
uniform sampler2D Mask2Sampler: register(s2);
uniform sampler2D NormalSampler: register(s3);
uniform sampler1D GradientMapSampler: register(s4);
/* data passed from vertex shader to pixel shader */
struct PS_INPUT {
float4 HPosition : POSITION;
float2 UV : TEXCOORD0;
float3 LightVec : TEXCOORD1;
float3 WorldNormal : TEXCOORD2;
float3 WorldTangent : TEXCOORD3;
float3 WorldBinormal : TEXCOORD4;
float3 WorldView : TEXCOORD5;
};
struct PS_OUTPUT
{
float4 result : COLOR0;
};
PS_OUTPUT main(PS_INPUT IN){
PS_OUTPUT OUT = (PS_OUTPUT)0;
float3 Ln = normalize(IN.LightVec); //light direction
float3 Nn = normalize(IN.WorldNormal);
float3 Tn = normalize(IN.WorldTangent);
//half lambert
float ldn = pow(((dot(Ln,Nn))/2)+0.5f,2);
ldn = max(ldn,0.0);
////////////////// Diffuse////////////////////
float3 diffuseColor = tex2D(ColorSampler,IN.UV).rgb;
////////////////// Normal ///////////////////////
float3 Bn = normalize(IN.WorldBinormal);
float3 bump = 10 * (tex2D(NormalSampler,IN.UV).rgb - float3(0.5,0.5,0.5));
Nn = Nn + bump.x*Tn + bump.y*Bn;
Nn = normalize(Nn);
//---------------------------------------------------------
////////////////// Mask 1 //////////////////////
////////////////// G - Specular Exponent (sharpness of light) ////////////////////////// NEED TO FIX THIS SO IT USES THE MAP TO DECIDE THE SHARPNESS
float3 specularExponent = tex2D(Mask1Sampler,IN.UV).rgb;
specularExponent.rb = specularExponent.g;
////////////////// R - Specular intensity ///////////////////////
float3 reflectionVector = 2*dot(Nn, Ln)*Nn - Ln;
float3 specularIntensity = tex2D(Mask1Sampler,IN.UV).rgb;
specularIntensity.gb = specularIntensity.r;
specularIntensity = pow(saturate (dot(reflectionVector, IN.WorldView)),1/(specularExponent*specularIntensity));
////////////////// B - specular colour mask(where the mask is, tells if the specular is tinted) ////////////////////
float3 specularColourMask = tex2D(Mask1Sampler,IN.UV).rgb;
specularColourMask.rg = specularColourMask.b;
specularIntensity *= specularColourMask * diffuseColor;
//--------------------------------------------------------------------
////////////////// Mask 2 //////////////////////
////////////////// R - Diffuse/fresnel mask colour warp (like gradient map layer in PS) //////////////////////////
float3 fresnelVector = 1-dot(Nn, IN.WorldView);
float3 fresnelMask = tex2D(Mask2Sampler,IN.UV).rgb;
fresnelMask.gb = fresnelMask.r;
float3 fresnelColour;
float3 InColor = tex2D(ColorSampler, IN.UV).xyz;
fresnelColour.r = tex1D(GradientMapSampler, InColor.r).r;
fresnelColour.g = tex1D(GradientMapSampler, InColor.g).g;
fresnelColour.b = tex1D(GradientMapSampler, InColor.b).b;
fresnelColour *= pow(saturate(fresnelVector),2)*fresnelMask;
////////////////// G - Rim light intensity ///////////////////////
float3 rimLightVector = 1-dot(Nn, IN.WorldView);
float3 rimLightIntensity = tex2D(Mask2Sampler,IN.UV).rgb;
rimLightIntensity.rb = rimLightIntensity.g;
rimLightIntensity *= pow(saturate(rimLightVector),8)* float3(0.8,0.8,0.8)/*RimColour*/;
////////////////// B - self-illumination mask(illuminated areas will be colour of the diffuse) ////////////////////
float3 selfIllumination = tex2D(Mask2Sampler,IN.UV).rgb;
selfIllumination.rg = selfIllumination.b;
///////////////// A - diffuse remapping ///////////////////////////////////////////////
//add variation to the texture by simply inputting a 1x256 pixel gradient
/*
float3 InColor = tex2D(ColorSampler, IN.UV).xyz;
diffuseColor.r = tex1D(GradientMapSampler, InColor.r).r;
diffuseColor.g = tex1D(GradientMapSampler, InColor.g).g;
diffuseColor.b = tex1D(GradientMapSampler, InColor.b).b;
*/
//--------------------------------------------------------------------
//result
float3 result = (diffuseColor * (ldn * Lamp0Color + AmbiColor)) + fresnelColour + specularIntensity + rimLightIntensity;
// return as float4
return OUT;
}