Graphics Reference
In-Depth Information
Performing the normal transformation to world space within the pixel
shader as we are doing within
ApplyNormalMap
is a suboptimal
solution with regards to performance in certain circumstances.
While it's useful to have the flexibility gained by using
HasNormalMap
and
HasTexture
when trying out different techniques, it is more
efficient to have multiple shaders that may or may not support normal
mapping or textures (as appropriate).
Last of all, we need to update our
MeshRenderer
class to update the
HasNormalMap
constant buffer property.
15. Add a new public property to the
MeshRenderer
class.
public bool EnableNormalMap { get; set; }
16. Initialize this property within the constructor.
this.EnableNormalMap = true;
17. We can now update the
MeshRenderer.DoRender
function so that when we are
applying the mesh's materials to the per material constant buffer, we are now also
updating the
PerMaterial.HasNormalMap
property. Here, we are assuming that
the normal map is assigned to the second texture of the material within the mesh file.
The changes are highlighted in the following code snippet:
protected override void DoRender() {
...
int texIndxOffset = mIndx * Common.Mesh.MaxTextures;
material.HasTexture = (uint)(textureViews[texIndxOffset] !=
null ? 1 : 0); // 0=false
material.HasNormalMap = (uint)(EnableNormalMap &&
textureViews[texIndxOffset+1] != null ? 1 : 0);
// Bind textures to the pixel shader
context.PixelShader.SetShaderResources(0,
textureViews.GetRange(texIndxOffset,
Common.Mesh.MaxTextures).ToArray());
...
// Update material buffer
context.UpdateSubresource(ref material, PerMaterialBuffer);
...
}