Its not a prefect piece of code (I realize that, so dont hate to hard). That being said is works and looks great in XNA, now I am trying to move this to MonoGame and its not going so well. Basically its drawing things right on the camera, and I have no idea why. Im sure it something simple, but im not the most shader knowledgeable. Please help!
also I did try Level_9_1 also and it did not fix it…
Here is my code:
// XNA 4.0 Shader Programming #4 - Normal Mapping
// Matrix
float4x4 World;
float4x4 View;
float4x4 Projection;
float4x4 xLightsWorldViewProjection;
// Light related
float4 AmbientColor;
float AmbientIntensity;
float2 UvChange;
float2 WaveSize;
float2 WaveChange;
float4 FilterColor;
float3 LightDirection;
float4 DiffuseColor;
float DiffuseIntensity;
float alpha;
float4 SpecularColor;
float3 EyePosition;
texture2D ColorMap;
sampler2D ColorMapSampler = sampler_state
{
Texture = <ColorMap>;
MinFilter = linear;
MagFilter = linear;
MipFilter = linear;
};
texture2D NormalMap;
sampler2D NormalMapSampler = sampler_state
{
Texture = <NormalMap>;
MinFilter = linear;
MagFilter = linear;
MipFilter = linear;
};
texture2D SpecularMap;
sampler2D SpecularMapSampler = sampler_state
{
Texture = <SpecularMap>;
MinFilter = linear;
MagFilter = linear;
MipFilter = linear;
};
float ShadowMapUniform;
texture2D ShadowMap;
sampler2D ShadowMapSampler = sampler_state
{
texture = <ShadowMap>;
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter= LINEAR;
AddressU = clamp;
AddressV = clamp;
};
// The input for the VertexShader
struct VertexShaderInput
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
float3 Normal : NORMAL0;
float3 Binormal : BINORMAL0;
float3 Tangent : TANGENT0;
};
// The output from the vertex shader, used for later processing
struct VertexShaderOutput
{
float4 Position : POSITION0;
float4 Pos2DAsSeenByLight : TEXCOORD0;
float2 TexCoord : TEXCOORD1;
float3 View : TEXCOORD2;
float3x3 WorldToTangentSpace : TEXCOORD3;
};
// The VertexShader.
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.TexCoord = input.TexCoord;
output.WorldToTangentSpace[0] = mul(normalize(input.Tangent), World);
output.WorldToTangentSpace[1] = mul(normalize(input.Binormal), World);
output.WorldToTangentSpace[2] = mul(normalize(input.Normal), World);
output.View = normalize(float4(EyePosition,1.0) - worldPosition);
output.Pos2DAsSeenByLight = mul(input.Position, xLightsWorldViewProjection);
return output;
}
float ShadowCalc(VertexShaderOutput input)
{
int num = 4;
float4 shadowColors[5];
float2 ProjectedTexCoords, ProjectedTexCoords2;
ProjectedTexCoords[0] = input.Pos2DAsSeenByLight.x/input.Pos2DAsSeenByLight.w/2.0f +0.5f;
ProjectedTexCoords[1] = -input.Pos2DAsSeenByLight.y/input.Pos2DAsSeenByLight.w/2.0f +0.5f;
float ShadowMapUniformX2 = ShadowMapUniform*2;
shadowColors[0] = tex2D(ShadowMapSampler, ProjectedTexCoords);
//ProjectedTexCoords += float2(ShadowMapUniformX2, ShadowMapUniformX2);
shadowColors[1] = tex2D(ShadowMapSampler, ProjectedTexCoords + float2(ShadowMapUniformX2, ShadowMapUniformX2));
shadowColors[2] = tex2D(ShadowMapSampler, ProjectedTexCoords + float2(-ShadowMapUniformX2, ShadowMapUniformX2));
shadowColors[3] = tex2D(ShadowMapSampler, ProjectedTexCoords + float2(ShadowMapUniformX2, -ShadowMapUniformX2));
//shadowColors[4] = tex2D(ShadowMapSampler, ProjectedTexCoords + float2(-ShadowMapUniformX2, -ShadowMapUniformX2));
float shadowAmount = 0;
float realDistance = input.Pos2DAsSeenByLight.z/input.Pos2DAsSeenByLight.w - 1.0f/750.0f;
float depthStoredInShadowMap;
//float originDepth = (shadowColors[0].r * 65536.0f + shadowColors[0].g * 256.0f)/(65536.0f);
for(int i=0; i<num; i++)
{
depthStoredInShadowMap = (shadowColors[i].r * 65536.0f + shadowColors[i].g * 256.0f)/(65536.0f);
if (realDistance > depthStoredInShadowMap)
{
shadowAmount += shadowColors[i].b;
}
}
shadowAmount = shadowAmount/(float)num;
return (1-(shadowAmount * .5f));
}
// The Pixel Shader
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float4 color = tex2D(ColorMapSampler, input.TexCoord );
float4 temp = color;
float3 normalMap = 2.0 *(tex2D(NormalMapSampler, input.TexCoord)) - 1.0;
normalMap = normalize(mul(normalMap, input.WorldToTangentSpace));
float4 normal = float4(normalMap,1.0);
float4 specMap = tex2D(SpecularMapSampler, input.TexCoord);
float4 diffuse = saturate(dot(-LightDirection,normal));
float4 reflect = normalize(2*diffuse*normal-float4(LightDirection,1.0));
float4 specular = pow(saturate(dot(reflect, input.View)), 20);
color = (color /* * DiffuseIntensity * DiffuseColor*/ * diffuse) * 1.4f + ( specMap * specular) * 1.4f;
color = color * FilterColor;
//////////////////// SHADOWS ///////////////////////////////////////////////////////////////////////
color *= ShadowCalc(input);
//////////////////// END SHADOWS //////////////////////////////////////////////////////////////////
color.a = temp.a * alpha;
return color;
}
// Our Techinique
technique Technique1
{
pass Pass1
{
VertexShader = compile vs_4_0 VertexShaderFunction();
PixelShader = compile ps_4_0 PixelShaderFunction();
}
}
Edit: I also realize it could but an issue with the VS, I just put ps only for simplicity…