Graphics Reference
In-Depth Information
10. Next, we need to update the
Vertex
structure within
Vertex.cs
to include the
Tangent
property. The constructors will also need to be updated to reflect this. The
complete
Vertex
structure members are shown here with the changes highlighted:
public Vector3 Position;
public Vector3 Normal;
public Color Color;
public Vector2 UV;
public Common.Mesh.SkinningVertex Skin;
public Vector4 Tangent;
11. Now, we tell the input assembler stage what our updated input layout for the vertex
structure looks like. This change is highlighted in the next code snippet, and is made
within
D3DApp.CreateDeviceDependentResources
.
// Layout from VertexShader input signature
vertexLayout = ToDispose(new InputLayout(device, ShaderSignature.
GetInputSignature(vertexShaderBytecode),
new[] {
// "SV_Position" = vertex coordinate in object space
new InputElement("SV_Position",0, Format.R32G32B32_Float,
0, 0),
...
// "TANGENT" = tangent vector, from loaded Mesh
new InputElement("TANGENT", 0,
Format.R32G32B32A32_Float,68,0),
}));
12. Lastly, we need to use the
Tangent
property of the mesh in
MeshRenderer.
CreateDeviceDependentResources
when creating the vertex buffer.
for (var i = 0; i < vb.Length; i++) {
...
// Create vertex
vertices[i] = new Vertex(vb[i].Position, vb[i].Normal,
vb[i].Color, vb[i].UV, skin, vb[i].Tangent);
}
With the tangent data now available to the pipeline, we can update each pixel shader
to perform the normal mapping.
13. First, we add the new function
ApplyNormalMap
within
Shaders\Common.hlsl
.
This will allow us to adjust the normal direction using a normal map sample.
float3 ApplyNormalMap(float3 normal, float4 tangent,
float3 normalSample) {
// Remap normalSample to the range (-1,1)
normalSample = (2.0 * normalSample) - 1.0;