Skip to content

NormalMapEffect

Chuck Walbourn edited this page Oct 10, 2021 · 35 revisions

NormalMapEffect effect extends BasicEffect to support normal-mapping and an optional specular map. It supports texture mapping, vertex coloring, directional per-pixel lighting, fog, and GPU instancing.

SkinnedNormalMapEffect extends NormalMapEffect to support vertex skinning. The skinned effect does not support per-vertex colors or GPU instancing.

See also Effects

Related tutorials: Using advanced shaders, Multistream rendering and instancing

Header

#include <Effects.h>

Initialization

Construction requires a Direct3D 12 device, optional effect flags, and state description:

std::unique_ptr<NormalMapEffect> effect;

RenderTargetState rtState(m_deviceResources->GetBackBufferFormat(),
    m_deviceResources->GetDepthBufferFormat());

EffectPipelineStateDescription pd(
    &InputLayout,
    CommonStates::Opaque,
    CommonStates::DepthDefault,
    CommonStates::CullCounterClockwise,
    rtState);

effect = std::make_unique<NormalMapEffect>(device, EffectFlags::Specular, pd);

In addition to the usual effect flags, this effect also supports EffectFlags::Specular.

For exception safety, it is recommended you make use of the C++ RAII pattern and use a std::unique_ptr or std::shared_ptr

Older versions of the library used , bool specularMap = true); as the final parameter to the ctor. This is now replaced with an EffectFlags. To get the equivalent of the old ctor defaults, use EffectFlags::Specular.

Interfaces

NormalMapEffect supports IEffect, IEffectMatrices, IEffectLights, and IEffectFog. EffectFlags::Fog is required to enable fogging.

SkinnedNormalMapEffect also supports IEffectSkinning

Input layout

This effect requires SV_Position, NORMAL, and TEXCOORD0. If per-vertex colors are enabled (EffectFlags::VertexColor), it also requires COLOR.

If instancing is enabled (EffectFlags::Instancing), this effect also requires these vertex elements:

"InstMatrix",  0, DXGI_FORMAT_R32G32B32A32_FLOAT
"InstMatrix",  1, DXGI_FORMAT_R32G32B32A32_FLOAT
"InstMatrix",  2, DXGI_FORMAT_R32G32B32A32_FLOAT

If skinning is used, the vertex layout requires BLENDINDICES and BLENDWEIGHT.

Properties

  • SetDiffuseColor: Sets the diffuse color of the effect. Defaults to white (1,1,1). Alpha channel (.w component) is ignored.

  • SetEmissiveColor: Sets the emissive color of the effect. Defaults to black (0,0,0).

  • SetSpecularColor: Sets the specular color of the effect. Defaults to white (1,1,1).

  • SetSpecularPower: Sets the specular power of the effect. Defaults to 16. Settings power to 0 can cause strange rendering artifacts.

  • DisableSpecular: Disables the specular lighting for the effect. Sets the color to black (0,0,0) and power to 1.

  • SetAlpha: Sets the alpha (transparency) of the effect. Defaults to 1 (fully opaque). This value is also used for binning opaque vs. transparent geometry.

  • SetColorAndAlpha: Sets the diffuse color of the effect and the alpha (transparency).

  • SetTexture: Associates a diffuse/albedo texture and sampler descriptor with the effect. Can optionally include an alpha channel as well.

  • SetNormalTexture: Associates a normal map texture for the effect. Uses the same sampler as SetTexture.

  • SetSpecularTexture: Associates a specular texture with the effect. Uses the same sampler as SetTexture. Requires EffectFlags::Specular be set in the constructor, otherwise it is ignored.

Normal maps

The normal-mapping textures used by this effect are tangent-space normal maps as opposed to object-space normal maps.

The normal map textures used by this effect are assumed to be _UNORM formats and therefore encoded using the 'x2 bias' model, i.e. the normal component's value range of -1 to +1 have been converted to 0 to +1. This is a simple encoding that is commonly used for normal texture maps since most file image formats don't support negative numbers. The conversion back to signed values is handled internally in the shader code.

The red (x) and green (y) channels are loaded from the texture, but blue (z) and alpha (w) channels are unused. The shader reconstructs the z component of the normal using z = sqrt( 1 - dot( xy, xy ) ). This has two positive benefits:

  1. You can make use the DXGI_FORMAT_BC5_UNORM (i.e. 3Dc) for normal map texture compression without any shader or configuration changes.
  2. Due to quantization effects, the actual unit length of the normal can drift, so this reconstruction can improve quality by effectively renormalizing the value.

Finally, the green (y) channel is used "as is" in the shader code. Some viewing and materials conventions can require the green channel to be inverted to properly 'push in' or 'push out'. This issue should be handled by modifying the texture content either offline or at load time.

Remarks

This effect implements the classic diffuse Lambertian shading with Phong specular highlights lighting model with the addition of per-pixel normals and lighting. Optionally the specular highlights can be implemented as a specular map.

The EffectFlags::Texture, EffectFlags::Lighting and EffectsFlags::PerPixelLighting are always enabled for this effect, so use or absence of these flag is ignored for this effect.

The EffectFlags::BiasedVertexNormals is supported by this effect. This flag should be used if the vertex data contains normals encoded as biased data such as DXGI_FORMAT_R10G10B10A2_UNORM.

This effect always performs texturing, so if 'untextured' diffuse rendering is desired you must provide a 1x1 texture with white (1,1,1,1).

This effect always requires a normal texture, so if 'untextured, smooth' rendering is desired using this effect (i.e. when implementing instancing which is not supported by BasicEffect), you must provide a 1x1 normal texture with (0.5, 0.5, 1, 1).

Diffuse map Normal map Specular map

Further reading

Christian Schüler, "Normal Mapping without Precomputed Tangents", ShaderX 5, Chapter 2.6, pp. 131 – 140 and this blog post

J.M.P. van Waveren and Ignacio Castaño, "Real-Time Normal Map DXT Compression", id Software, February 2008 PDF (note: BC5 is "tangent-space 3Dc")

For Use

  • Universal Windows Platform apps
  • Windows desktop apps
  • Windows 11
  • Windows 10
  • Xbox One
  • Xbox Series X|S

Architecture

  • x86
  • x64
  • ARM64

For Development

  • Visual Studio 2022
  • Visual Studio 2019 (16.11)
  • clang/LLVM v12 - v18
  • MinGW 12.2, 13.2
  • CMake 3.20

Related Projects

DirectX Tool Kit for DirectX 11

DirectXMesh

DirectXTex

DirectXMath

Tools

Test Suite

Model Viewer

Content Exporter

DxCapsViewer

See also

DirectX Landing Page

Clone this wiki locally