Skip to content

NormalMapEffect

Chuck Walbourn edited this page Jan 18, 2019 · 36 revisions

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

See also Effects

Header

#include <Effects.h>

Initialization

Construction requires a Direct3D 11 device.

std::unique_ptr<NormalMapEffect> effect;
effect = std::make_unique<NormalMapEffect>(device);

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

Interfaces

NormalMapEffect supports IEffect, IEffectMatrices, IEffectLights, and IEffectFog

Input layout

This effect requires SV_Position, NORMAL, and TEXCOORD0. If per-vertex colors are enabled, it also requires COLOR.

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).

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

  • SetVertexColorEnabled: Enables per-vertex color. Defaults to false. Modifying this setting requires recreating associated input layouts, and enabling it requires COLOR.

  • SetTexture: Associates a texture shader resource view with the effect for the diffuse/albedo texture. Can be set to nullptr to remove a reference.

  • SetNormalTexture: Associates a texture shader resource view with the effect for the normal map texture. Can be set to nullptr to remove a reference.

  • SetSpecularTexture: Associates a texture shader resource view with the effect for the specular texture. Can be set to nullptr.

  • SetBiasedVertexNormals: Enables support for compressed vertex normals which require *2 - 1 biasing at runtime such as DXGI_FORMAT_R10G10B10A2_UNORM.

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 textures 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) an 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.

Remarks

This effect always performs per-pixel lighting. Calls to SetLightingEnabled(false); will generate a C++ exception, and calls to SetPerPixelLighting are ignored.

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

Feature Level Notes

This effect uses Shader Model 4.0 so requires Direct3D hardware feature level 10.0 or greater. Note this means you can also count on DXGI_FORMAT_BC5_UNORM texture compression support.

Direct3D feature levels

Further reading

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

For Use

  • Universal Windows Platform apps
  • Windows desktop apps
  • Windows 11
  • Windows 10
  • Windows 8.1
  • Xbox One

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 12

DirectXMesh

DirectXTex

DirectXMath

Win2D

Tools

Test Suite

Model Viewer

Content Exporter

DxCapsViewer

Clone this wiki locally