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 wLightDir; uniform float3 wCamPos; uniform float4 lightRange; uniform float lightPower; #define SpotLightFalloff 1.0 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 tileTexCoord, float2 detailTexCoord2) { // Blinn lighting float d = length(Light); Light = normalize(Light); float3 Half = normalize(Light + View); float4 Lighting = lit(dot(Normal, Light), dot(Normal, Half), 120); float4 Color= tex2D(ColorMapSampler, tileTexCoord) * tex2D(DetailMapSampler, detailTexCoord2); return (Lighting.y * Color + Lighting.z * 0.5) * (d < lightRange.x) / (lightRange.y + d * lightRange.z + d * d * lightRange.w) * lightPower; } struct VS_INPUT { float4 Position : POSITION; // point in modeling space float2 TexCoord : TEXCOORD0; // texture coordinates float2 TexCoord2 : TEXCOORD1; // texture coordinates float2 TexCoord3 : TEXCOORD2; // texture coordinates float3 Tangent : TEXCOORD3; // 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 float2 TexNormalCoord : TEXCOORD7; // 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, uniform float normalCoord) { VS_OUTPUT OUT; float3 wPos = mul(World, IN.Position).xyz; // 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)).rgb; OUT.wNormal = normalize(mul(float4(IN.Normal, 1),WorldI)).rgb; OUT.wBinormal = cross(OUT.wTangent, OUT.wNormal).rgb; // vertex position before homogenious division OUT.hPosition = mul(WorldViewProj, IN.Position); // tex coordinates passed to pixel shader OUT.TexCoord = IN.TexCoord; OUT.TexCoord2 = IN.TexCoord2; if(normalCoord == 0) OUT.TexNormalCoord = IN.TexCoord; else OUT.TexNormalCoord = IN.TexCoord2; return OUT; } float4 BumpPS(VS_OUTPUT IN) : COLOR { float SpotLightAngle = 120.0 / 180.0 * 3.14; //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 = 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.TexNormalCoord).rgb; if(length(tNormal == 0)) tNormal = float3(0,0,1); tNormal.xy = (tNormal.xy *2.0) - 1.0; // Normal vector should be transformed with the inverse transpose of TangentToModel // which is the transpose of ModelToTangent float3 mNormal = normalize( mul( tNormal, ModelToTangent ) ); //spot angle float spotFalloff = 1; if(wLightPos.w && dot(wLightDir, wLightDir) != 0) { //spotFalloff = pow(dot(normalize(-mLight), normalize(wLightDir)), 5); spotFalloff = (dot(normalize(-mLight), normalize(wLightDir)) - cos(SpotLightAngle / 2.0)) / (1.0 - cos(SpotLightAngle / 2.0)); spotFalloff = pow(saturate(spotFalloff),1); } // illumination calculation float4 illum = Illumination(mLight, mNormal/*IN.wNormal*/, mView, IN.TexCoord2, IN.TexCoord) * spotFalloff; return float4(illum.rgb, 0.7); }