Hey guys,
Working on a shader to render models with basic diffuse, normal, and specular maps. I have things up and running it but looks like my models are getting only specular highlights(i.e. everything is either fully lit, or super bright).
Here is the shader currently:
[SOURCE]
uniform extern float4x4 worldViewProj : WorldViewProjection;
uniform extern float4x4 worldInverse : WorldInverse;
uniform extern float3 cameraPosition;
uniform extern float3 lightPosition;
uniform extern texture colorMap;
uniform extern texture normalMap;
uniform extern texture specularMap;
uniform extern float specularPower;
struct inputVS
{
float3 pos : POSITION;
float3 tangent : TANGENT;
float3 binormal : BINORMAL;
float3 normal : NORMAL;
float2 tex : TEXCOORD0;
};
struct outputVS
{
float4 pos : POSITION;
float3 camera : TEXCOORD0;
float3 lightDir: TEXCOORD1;
float2 tex : TEXCOORD2;
};
sampler NormalMapSampler = sampler_state
{
Texture = <normalMap>;
MinFilter = ANISOTROPIC;
MaxAnisotropy = 8;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
sampler ColorMapSampler = sampler_state
{
Texture = <colorMap>;
MinFilter = ANISOTROPIC;
MaxAnisotropy = 8;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
sampler SpecularMapSampler = sampler_state
{
Texture = <specularMap>;
MinFilter = ANISOTROPIC;
MaxAnisotropy = 8;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
outputVS mainVS(inputVS INPUT)
{
// Zero out our structure.
outputVS outVS = (outputVS)0;
// Contruct TBN Matrix
float3x3 TBNMatrix;
TBNMatrix[0] = INPUT.tangent;
TBNMatrix[1] = INPUT.binormal;
TBNMatrix[2] = INPUT.normal;
// Tangent Space transform
float3x3 TangentSpace = transpose(TBNMatrix);
// Camera World Pos to local Camera position;
float3 localCameraPos = mul(float4(cameraPosition, 1.0f), worldInverse);
// Local camera vector
float3 tangentToCamera = localCameraPos - INPUT.pos;
// Local camera to tangent space
outVS.camera = mul(tangentToCamera, TangentSpace);
// Light direction from world space to local space
float3 lightDirection = mul(float4(lightPosition, 0.0f), worldInverse);
// Light Direction into tangent space
outVS.lightDir = mul(lightDirection, TangentSpace);
// Basic local pos to H space
outVS.pos = mul(float4(INPUT.pos, 1.0f), worldViewProj);
// Pass on the texture coord
outVS.tex = INPUT.tex;
// To the pixel shader!
return outVS;
}
float4 mainPS(outputVS OUTPUT) : COLOR
{
// Normalize our Camera and light vectors, just in case.
OUTPUT.camera = normalize(OUTPUT.camera);
OUTPUT.lightDir = normalize(OUTPUT.lightDir);
// Reverse the light direction
float3 realLightDir = -OUTPUT.lightDir;
// Sample the normal map
float3 normalTex = tex2D(NormalMapSampler, OUTPUT.tex);
// from 0 to 1 to -1 to 1
normalTex = 2.0f * normalTex - 1.0f;
normalTex = normalize(normalTex);
// Reflection ray for our specular lighting
float3 lightReflect = (-realLightDir, normalTex);
// Specular light value
float t = pow(max(dot(lightReflect, OUTPUT.camera), 0.0f), specularPower);
// Diffuse intensity
float s = max(dot(realLightDir, normalTex), 0.0f);
// No specular lighting if we shouldn't get light normally.
if(s <= 0.0f)
t = 0.0f;
// Final specular value. Modulated by a specular map.
float3 spec = (s * tex2D(SpecularMapSampler, OUTPUT.tex)).rgb;
// Diffuse texture sample.
float4 color = tex2D(ColorMapSampler, OUTPUT.tex);
// Spec + Diffuse
color.rgb = (color.rgb + spec);
// Return color
return color;
}
I think the problem is in these lines (as altering them has given me some results, hard black lighting edges, but nothing that would "fix" the issue).
float t = pow(max(dot(lightReflect, OUTPUT.camera), 0.0f), specularPower);
float s = max(dot(realLightDir, normalTex), 0.0f);
if(s <= 0.0f)
t = 0.0f;
float3 spec = (s * tex2D(SpecularMapSampler, OUTPUT.tex)).rgb;
float4 color = tex2D(ColorMapSampler, OUTPUT.tex);
color.rgb = (color.rgb + spec);
I've been coding for a while today so some fresh eyes might see what I'm not. :)