I have made a small level with 3D world studio, and when I load it everything looks fine, but when I apply a normal mapping shader to it the textures seem to be scrolled randomly. Has anybody found a way prevent this? I have attached a screen shot to show you what I mean.
here is the shader
//--------------------------------
// NormalMapping
//--------------------------------
// By Evolved
// http://www.vector3r.com/
// Additions for DBO Objects
// By Darrel Cusey
// Modified By GnomesGames
//--------------------------------
//-----------------
// un-tweaks
//-----------------
matrix WorldVP:WorldViewProjection;
matrix World:World;
matrix ViewInv:ViewInverse;
//-----------------
// tweaks
//-----------------
float4 Ambient = {0.66f, 0.66f, 0.66f, 1.0f};
float4 LightPosition_1 = {0.0f, -500.0f, 0.0f, 1.0f};
float4 LightColor_1 = {0.5f, 0.5f, 0.5f, 1.0f};
float LightRange_1 = 100.0f;
float U = 32.0f;
float V = 32.0f;
//-----------------
// Textures
//-----------------
//LightMap Specific
texture LightTX <string Name="<default>";>;
sampler2D Lighting = sampler_state
{
texture = <LightTX>;
minfilter = linear;
magfilter = linear;
mipfilter = linear;
addressU = wrap;
addressV = wrap;
};
//LightMap Specific
texture BaseTX <string Name="<default>";>;
sampler2D Base = sampler_state
{
texture = <BaseTX>;
minfilter = linear;
magfilter = linear;
mipfilter = linear;
addressU = wrap;
addressV = wrap;
};
texture NormalTX <string Name="<default>";>;
sampler2D Normal = sampler_state
{
texture = <NormalTX>;
minfilter = linear;
magfilter = linear;
mipfilter = linear;
addressU = wrap;
addressV = wrap;
};
texture NormalizerTX <string Name = "<default>";>;
samplerCUBE Normalizer = sampler_state
{
texture = <NormalizerTX>;
addressU = clamp;
addressV = clamp;
magfilter = linear;
minfilter = point;
mipfilter = none;
};
//-----------------
// structs
//-----------------
struct input
{
float4 Pos:POSITION;
float2 UV:TEXCOORD;
float3 Normal:NORMAL;
float3 Tangent:TANGENT;
float3 Binormal:BINORMAL;
};
struct output
{
float4 OPos:POSITION;
float2 LM:TEXCOORD0;
float2 Tex:TEXCOORD1;
float3 LightVec:TEXCOORD2;
float3 Attenuation:TEXCOORD3;
float3 ViewVec:TEXCOORD4;
};
//-----------------
// vertex shader
//-----------------
output VS(input IN)
{
output OUT;
OUT.OPos=mul(IN.Pos,WorldVP);
OUT.Tex=IN.UV*float2(U,V);
OUT.LM=IN.UV*float2(1.0,1.0);
float3 WNor=mul(IN.Normal,World);
WNor=normalize(WNor);
float3 Wtan=mul(IN.Tangent,World);
Wtan=normalize(Wtan);
float3 Wbin=mul(IN.Binormal,World);
Wbin=normalize(Wbin);
float3 WPos=mul(IN.Pos,World);
float3x3 TBN={-Wtan,Wbin,WNor};
TBN=transpose(TBN);
float3 LightPos=LightPosition_1-WPos;
float3 ViewPos=ViewInv[3].xyz-WPos;
OUT.LightVec=mul(LightPos,TBN);
OUT.Attenuation=-LightPos/LightRange_1;
OUT.ViewVec=mul(ViewPos,TBN);
return OUT;
}
//-----------------
// pixel shader
//-----------------
float4 PS(output IN) : COLOR
{
float4 Texture=tex2D(Base,IN.Tex);
float4 LightMap=(tex2D(Lighting,IN.LM)*2);
float3 NormalMap=tex2D(Normal,IN.Tex)*2-1;
float3 LightV=texCUBE(Normalizer,IN.LightVec)*2-1;
float3 View=texCUBE(Normalizer,IN.ViewVec)*2-1;
float Normal=saturate(dot(NormalMap,LightV));
float Specular=saturate(dot(reflect(-View,NormalMap),LightV));
LightMap=LightMap*LightMap;
float4 DiffuseLight=1-saturate(dot(IN.Attenuation,IN.Attenuation));
float4 Light=DiffuseLight*LightColor_1;
return Texture*(((Normal+Specular)*Light+LightMap)+(Ambient*LightMap));
}
//-----------------
// techniques
//-----------------
technique Light1
{
pass p1
{
vertexShader = compile vs_2_0 VS();
pixelShader = compile ps_2_0 PS();
}
}
If need be, I can upload an example which displays the problem.
Any insight as to why the textures are shifted when the shader is applied would be greatly appreciated.