Graphics Reference
In-Depth Information
// Ensure tangent is orthogonal to normal vector
// Gram-Schmidt orthogonalize
float3 T = normalize(tangent - normal *
dot(normal, tangent));
// Create the Bitangent
float3 bitangent = cross(normal, T) * tangent.w;
// Create TBN matrix to transform from tangent space
float3x3 TBN = float3x3(T, bitangent, normal);
return normalize(mul(normalSample, TBN));
}
14. Within the Diffuse, Blinn-Phong, and Phong pixel shaders, add a new input texture
for the normal map (using the second texture slot t1 ). Then, update the PSMain
function to adjust the normal prior to any lighting calculations.
Texture2D Texture0 : register(t0);
Texture2D NormalMap : register(t1);
SamplerState Sampler : register(s0);
float4 PSMain(PixelShaderInput pixel) : SV_Target
{
// Normalize our vectors as they are not
// guaranteed to be unit vectors after interpolation
float3 normal = normalize(pixel.WorldNormal);
float3 tangent = normalize(pixel.WorldTangent.xyz);
float3 toEye = normalize(CameraPosition -
pixel.WorldPosition);
float3 toLight = normalize(-Light.Direction);
// If there is a normal map, apply it
if (HasNormalMap)
normal = ApplyNormalMap(normal,
float4(tangent, pixel.WorldTangent.w),
NormalMap.Sample(Sampler,
pixel.TextureUV).rgb);
// Texture sample here (use white if no texture)
float4 sample = (float4)1.0f;
if (HasTexture)
sample = Texture0.Sample(Sampler, pixel.TextureUV);
...
}
 
Search WWH ::




Custom Search