uniform float textureRepeat = 6; uniform sampler2D ColorMapSampler : register(s0); uniform sampler2D DetailMapSampler : register(s1); uniform sampler2D BumpMapSampler : register(s2); uniform float4x4 WorldViewProj; uniform float4x4 World; uniform float4x4 WorldI; uniform float4 wLightPos; uniform float3 wCamPos; float3x3 TransfModelToTangent( in float3 Tangent, in float3 Binormal, in float3 Normal ) { float T2 = dot(Tangent, Tangent); float B2 = dot(Binormal, Binormal); float N2 = dot(Normal, Normal); float BT = dot(Binormal, Tangent); float det = B2 * T2 - BT * BT; return float3x3( (B2 * Tangent - BT * Binormal)/det, (T2 * Binormal - BT * Tangent)/det, Normal/N2 ); /* // simplified solution return float3x3(Tangent/T2, Binormal/B2, Normal/N2); */ } float4 Illumination(float3 Light, float3 Normal, float3 View, float2 TexCoord, float2 TexCoord2) { // Blinn lighting float3 Half = normalize(Light + View); float Diffuse = dot(Normal, Light); float Specular = dot(Normal, Half); float4 Lighting = lit(Diffuse, Specular, 16); float4 Color= tex2D(ColorMapSampler, TexCoord) * tex2D(DetailMapSampler, TexCoord2); return (Lighting.y + 0.1) * Color; } struct VS_INPUT { float4 Position : POSITION; // point in modeling space float2 TexCoord : TEXCOORD0; // texture coordinates float2 TexCoord2 : TEXCOORD1; // texture coordinates float3 Tangent : TEXCOORD2; // model space tangent vector float3 Normal : NORMAL; // model space triangle normal vector }; struct VS_OUTPUT { float4 hPosition : POSITION; // point in normalized device space before homogeneous division float2 TexCoord : TEXCOORD0; // texture coordinates float2 TexCoord2 : TEXCOORD6; // texture coordinates float3 mView : TEXCOORD1; // model space view vector float3 mLight : TEXCOORD2; // model space light vector float3 wTangent : TEXCOORD3; // model space tangent vector float3 wBinormal : TEXCOORD4; // model space binormal vector float3 wNormal : TEXCOORD5; // model space triangle normal vector }; //------------------------------------------------------------------------------------ // // Base vertex shader: vertex shader for all methods: calculates tangent-space // tLight, tView, hPosition vectors // //------------------------------------------------------------------------------------ VS_OUTPUT BaseVS(VS_INPUT IN) { VS_OUTPUT OUT; float3 wPos = mul(World, IN.Position); // world-space view vector OUT.mView = wCamPos - wPos; // world-space light vector OUT.mLight = wLightPos.xyz - wPos * wLightPos.w; OUT.wTangent = normalize(mul(float4(IN.Tangent, 1),WorldI)); OUT.wNormal = normalize(mul(float4(IN.Normal, 1),WorldI)); OUT.wBinormal = cross(OUT.wTangent, OUT.wNormal); // vertex position before homogenious division OUT.hPosition = mul(WorldViewProj, IN.Position); // tex coordinates passed to pixel shader OUT.TexCoord = textureRepeat * IN.TexCoord; OUT.TexCoord2 = IN.TexCoord2; return OUT; } float4 BumpPS(VS_OUTPUT IN) : COLOR { //return tex2D(BumpMapSampler,IN.TexCoord).a; IN.wTangent = normalize(IN.wTangent); IN.wBinormal = normalize(IN.wBinormal); IN.wNormal = normalize(IN.wNormal); // needs normalization because of linear interpolation float3 mLight = normalize( IN.mLight ); float3 mView = normalize( IN.mView ); // get model space normal vector float3x3 ModelToTangent = TransfModelToTangent(IN.wTangent, IN.wBinormal, IN.wNormal ); // get model space normal vector float3 tNormal = tex2D(BumpMapSampler, IN.TexCoord).rgb; // Normal vector should be transformed with the inverse transpose of TangentToModel // which is the transpose of ModelToTangent float3 mNormal = normalize( mul( tNormal, ModelToTangent ) ); // illumination calculation return Illumination(mLight, mNormal, mView, IN.TexCoord, IN.TexCoord2); }