// General functions // Expand a range-compressed vector float3 expand(float3 v) { return (v - 0.5) * 2; } /* Bump mapping vertex program In this program, we want to calculate the tangent space light vector on a per-vertex level which will get passed to the fragment program, or to the fixed function dot3 operation, to produce the per-pixel lighting effect. */ void main_vp(float4 position : POSITION, float3 normal : NORMAL, float2 uv : TEXCOORD0, float4 tangent : TANGENT, // outputs out float4 oPosition : POSITION, out float2 oUv : TEXCOORD0, out float3 oTSLightDir : TEXCOORD1, // parameters uniform float4 lightPosition, // object space uniform float4x4 worldViewProj) { // calculate output position oPosition = mul(worldViewProj, position); // pass the main uvs straight through unchanged oUv = uv; // calculate tangent space light vector // Get object space light direction // Non-normalised since we'll do that in the fragment program anyway float3 lightDir = lightPosition.xyz - (position * lightPosition.w); // Calculate the binormal (NB we assume both normal and tangent are // already normalised) // NB looks like nvidia cross params are BACKWARDS to what you'd expect // this equates to NxT, not TxN #if TANGENTS_HAVE_PARITY float3 binormal = cross(tangent.xyz, normal) * tangent.www; #else // fixed handedness float3 binormal = cross(tangent.xyz, normal); #endif // Form a rotation matrix out of the vectors float3x3 rotation = float3x3(tangent.xyz, binormal, normal); // Transform the light vector according to this matrix oTSLightDir = mul(rotation, lightDir); } /* Bump mapping vertex program for shadow receiving In this program, we want to calculate the tangent space light vector on a per-vertex level which will get passed to the fragment program, or to the fixed function dot3 operation, to produce the per-pixel lighting effect. */ void main_shadowreceiver_vp(float4 position : POSITION, float3 normal : NORMAL, float2 uv : TEXCOORD0, float3 tangent : TANGENT, // outputs out float4 oPosition : POSITION, out float4 uvproj : TEXCOORD0, out float2 oUv : TEXCOORD1, out float3 oTSLightDir : TEXCOORD2, // parameters uniform float4 lightPosition, // object space uniform float4x4 worldViewProj, uniform float4x4 worldMatrix, uniform float4x4 texViewProj) { // calculate output position oPosition = mul(worldViewProj, position); // pass the main uvs straight through unchanged oUv = uv; // calculate tangent space light vector // Get object space light direction // Non-normalised since we'll do that in the fragment program anyway float3 lightDir = lightPosition.xyz - (position * lightPosition.w); // Calculate the binormal (NB we assume both normal and tangent are // already normalised) // NB looks like nvidia cross params are BACKWARDS to what you'd expect // this equates to NxT, not TxN float3 binormal = cross(tangent, normal); // Form a rotation matrix out of the vectors float3x3 rotation = float3x3(tangent.xyz, binormal, normal); // Transform the light vector according to this matrix oTSLightDir = mul(rotation, lightDir); // Projection uvproj = mul(worldMatrix, position); uvproj = mul(texViewProj, uvproj); } void main_fp( float4 position : POSITION, float2 uv : TEXCOORD0, float3 TSlightDir : TEXCOORD1, out float4 colour : COLOR, uniform float4 lightDiffuse, uniform sampler2D normalMap : register(s0)) { // retrieve normalised light vector, expand from range-compressed float3 lightVec = normalize(TSlightDir).xyz; // get bump map vector, again expand from range-compressed float3 bumpVec = expand(tex2D(normalMap, uv).xyz); // Calculate dot product colour = lightDiffuse * dot(bumpVec, lightVec); } void main_shadowreceiver_fp( float4 position : POSITION, float4 uvproj : TEXCOORD0, float2 uv : TEXCOORD1, float3 TSlightDir : TEXCOORD2, out float4 colour : COLOR, uniform float4 lightDiffuse, uniform sampler2D shadowMap : register(s0), uniform sampler2D normalMap : register(s1)) { // retrieve normalised light vector, expand from range-compressed float3 lightVec = expand(normalize(TSlightDir).xyz); // get bump map vector, again expand from range-compressed float3 bumpVec = expand(tex2D(normalMap, uv).xyz); // get shadow value float3 shadow = tex2Dproj(shadowMap, uvproj).xyz; // Calculate dot product colour = float4(shadow * lightDiffuse * dot(bumpVec, lightVec), 1.0f); } /* Vertex program which includes specular component */ void specular_vp(float4 position : POSITION, float3 normal : NORMAL, float2 uv : TEXCOORD0, float3 tangent : TANGENT, // outputs out float4 oPosition : POSITION, out float2 oUv : TEXCOORD0, out float3 oTSLightDir : TEXCOORD1, out float3 oTSHalfAngle : TEXCOORD2, // parameters uniform float4 lightPosition, // object space uniform float3 eyePosition, // object space uniform float4x4 worldViewProj) { // calculate output position oPosition = mul(worldViewProj, position); // pass the main uvs straight through unchanged oUv = uv; // calculate tangent space light vector // Get object space light direction float3 lightDir = normalize(lightPosition.xyz - (position * lightPosition.w)); // Calculate the binormal (NB we assume both normal and tangent are // already normalised) // NB looks like nvidia cross params are BACKWARDS to what you'd expect // this equates to NxT, not TxN float3 binormal = cross(tangent, normal); // Form a rotation matrix out of the vectors float3x3 rotation = float3x3(tangent, binormal, normal); // Transform the light vector according to this matrix oTSLightDir = mul(rotation, lightDir); // Calculate half-angle in tangent space float3 eyeDir = normalize(eyePosition - position.xyz); float3 halfAngle = normalize(eyeDir + lightDir); oTSHalfAngle = mul(rotation, halfAngle); } /* Fragment program which supports specular component */ void specular_fp( float4 position : POSITION, float2 uv : TEXCOORD0, float3 TSlightDir : TEXCOORD1, float3 TShalfAngle: TEXCOORD2, out float4 colour : COLOR, uniform float4 lightDiffuse, uniform float4 lightSpecular, uniform sampler2D normalMap : register(s0)) { // retrieve normalised light vector float3 lightVec = normalize(TSlightDir); // retrieve half angle and normalise float3 halfAngle = normalize(TShalfAngle); // get bump map vector, again expand from range-compressed float3 bumpVec = expand(tex2D(normalMap, uv).xyz); // Pre-raise the specular exponent to the eight power float specFactor = pow(saturate(dot(bumpVec, halfAngle)), 4); // Calculate dot product for diffuse colour = (lightDiffuse * saturate(dot(bumpVec, lightVec))) + (lightSpecular * specFactor); }