//------- Constants --------float4x4 xView;float4x4 xProjection;float4x4 xWorld;float4x4 TangentMatrix;float3 xLightDirection;float xAmbient;bool xEnableLighting;float4 lightColor = float4(1,1,1,1);float3 ambientColor = float3(1,1,1);//------- Texture Samplers --------Texture xTexture1;sampler TextureSampler1 = sampler_state { texture = <xTexture1>; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = wrap; AddressV = wrap;};Texture xNormal1;sampler NormalSampler1 = sampler_state { texture = <xNormal1>; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = wrap; AddressV = wrap;};//------- Technique: Textured --------struct TexVertexToPixel{ float4 Position : POSITION0; float4 Color : COLOR0; float LightingFactor: TEXCOORD0; float2 TextureCoords: TEXCOORD1; float3 vView : TEXCOORD2; float3 UP : POSITION1; float3 RIGHT : POSITION2; float3 NORMAL : NORMAL0;};struct TexPixelToFrame{ float4 Color : COLOR0;};TexVertexToPixel TexturedVS( float4 inPos : POSITION0, float3 inNormal: NORMAL0, float4 inUp : POSITION1, float4 inRight : POSITION2 ,float2 inTexCoords: TEXCOORD0){ TexVertexToPixel Output = (TexVertexToPixel)0; float4x4 preViewProjection = mul (xView, xProjection); float4x4 preWorldViewProjection = mul (xWorld, preViewProjection); Output.Position = mul(inPos, preWorldViewProjection); Output.TextureCoords = inTexCoords; //enable for better light... float3 Normal = normalize(inNormal); Output.LightingFactor = 1; if (xEnableLighting) Output.LightingFactor = saturate(dot(Normal, xLightDirection)); Output.Color = float4(1.0f, 0.0f, 0.0f, 1.0f); Output.vView = -xView[3].xyz - Output.Position; Matrix world2; world2[0].x=xworld[0].x; world2[0].y=xworld[0].y; world2[0].z=xworld[0].z; world2[1].x=xworld[1].x; world2[1].y=xworld[1].y; world2[1].z=xworld[1].z; world2[2].x=xworld[2].x; world2[2].y=xworld[2].y; world2[2].z=xworld[2].z; world2[0].w=0; world2[1].w=0; world2[2].w=0; world2[3].x=0; world2[3].y=0; world2[3].z=0; world2[3].w=1; Output.UP = mul(inUp, world2); Output.RIGHT = mul(inRight, world2); Output.NORMAL = mul(inNormal, world2); return Output; }TexPixelToFrame GDPS(TexVertexToPixel PSIn) { TexPixelToFrame Output = (TexPixelToFrame)0; float3 right=normalize(PSIn.RIGHT); float3 up=normalize(PSIn.UP); float3 front=normalize(PSIn.NORMAL); //float4x4 TangentMatrix; TangentMatrix[0].x=right.x; TangentMatrix[0].y=right.y; TangentMatrix[0].z=right.z; TangentMatrix[0].w=0; TangentMatrix[1].x=up.x; TangentMatrix[1].y=up.y; TangentMatrix[1].z=up.z; TangentMatrix[1].w=0; TangentMatrix[2].x=front.x; TangentMatrix[2].y=front.y; TangentMatrix[2].z=front.z; TangentMatrix[2].w=0; TangentMatrix[3].x=0; TangentMatrix[3].y=0; TangentMatrix[3].z=0; TangentMatrix[3].w=1; float3 lightdir_in_tangent_space = mul(xLightDirection, TangentMatrix); float3 nor=tex2D(NormalSampler1, float2(PSIn.TextureCoords)).rgb*2-1; float pixel_light=dot(lightdir_in_tangent_space, -nor); Output.Color.rgb = tex2D(TextureSampler1, float2(PSIn.TextureCoords)).rgb; //Output.Color *= float4(pixel_light.r,pixel_light.g,pixel_light.b, 1); Output.Color *= pixel_light; //Output.Color.rgb *= saturate(PSIn.LightingFactor); Output.Color.a = 1; return Output;}technique Textured{ pass Pass0 { VertexShader = compile vs_3_0 TexturedVS(); PixelShader = compile ps_3_0 GDPS(); }}
then if that gives you bad results, play with the cross products before you think this doesnt work!