Skip to content

NPREffect

Chuck Walbourn edited this page Jul 27, 2026 · 5 revisions
DirectXTK Effects

NPREffect implements non-photorealistic rendering techniques such as cel shading, Gooch shading, and MatCap (material capture) rendering. It provides an optional rim-lighting Fresnel effect for cel and Gooch shading modes.

NPR effect

classDiagram
class EffectFlags{
    <<enumeration>>
    Texture
    VertexColor
    BiasedVertexNormals
    Instancing
}
class IEffect{
    <<Interface>>
    +Apply()
}
class IEffectMatrices{
    <<Interface>>
    +SetWorld()
    +SetView()
    +SetProjection()
    +SetMatrices()
}
class IEffectLights{
    <<Interface>>
    +SetLightDirection()
    +EnableDefaultLighting()
}
class NPREffect{
    +SetAlpha()
    +SetDiffuseColor()
    +SetSpecularColor()
    +SetSpecularThreshold()
    +SetCelShaderBands()
    +SetGoochCoolColor()
    +SetGoochWarmColor()
    +SetRimLightingColor()
}
NPREffect--|> IEffect
NPREffect--|> IEffectMatrices
NPREffect--|> IEffectLights
Loading
class DirectX::NPREffect :
    public DirectX::IEffect,
    public DirectX::IEffectMatrices,
    public DirectX::IEffectLights

Header

#include <Effects.h>

Initialization

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

std::unique_ptr<NPREffect> effect;

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

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

effect = std::make_unique<NPREffect>(device, EffectFlags::None, pd);

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

The constructor takes an additional defaulted parameter which controls the specific shader to use:

Mode_Cel Cel shading (a.k.a. toon shading)
Mode_Gooch Gooch shading (warm/cool technical illustration)
Mode_MatCap Material capture rendering (lit-sphere texture)
NPREffect(ID3D12Device* device, uint32_t effectFlags,
    const EffectPipelineStateDescription& pipelineDescription,
    Mode nprMode = Mode_Cel);

Interfaces

NPREffect supports IEffect, IEffectMatrices, and IEffectLights.

Lighting is always enabled and MaxDirectionalLights is 1.

Input layout

This effect requires SV_Position, NORMAL, COLOR if per-vertex colors are enabled (EffectFlags::VertexColor), and TEXCOORD0 if texturing is enabled (EffectFlags::Texture) or the mode is set to Mode_MatCap.

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

Properties

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

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

  • SetSpecularThreshold: Sets the specular threshold and smoothing value for the effect. Both values must be in the 0 to 1 range. Note that this is a linear value between 0 and 1 and not a Phong power value.

  • DisableSpecular: Disables the specular lighting for the effect.

  • 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 texture and sampler descriptor with the effect. Must have used EffectFlags::Texture to enable texturing. Can optionally include an alpha channel as well.

Cel/toon shading

  • SetCelShaderBands: Sets the number of cel bands when rendering. Defaults to 4.

Gooch shading

  • SetGoochCoolColor: Sets the 'cool' color for Gooch shading and alpha weight.

  • SetGoochWarmColor: Sets the 'warm' color for Gooch shading and beta weight.

MatCap rendering

  • SetMatCap: Sets the material capture (matcap) texture to use when rendering in this mode. Uses the same texture coordinates as texturing (i.e. TEXCOORD0). It optionally takes a sampler, but it is the same sampler as the base texture (i.e. there's only one sampler used by this effect).

RimLighting

  • SetRimLightingColor: Sets the rim-lighting color. Defaults to black (0,0,0).

  • SetRimLightingPower: Sets the rim-lighting power. Defaults to 4.

  • SetRimLightingIntensity: Sets a multiplier for the rim-lighting effect in the range of 0 to 1. Defaults to 0.75.

  • SetRimLightingRange: Sets the rim-lighting range between 0 and 1. Defaults to [0.2, 0.6].

  • DisableRimLighting: Disables the rim-lighting effect.

Applies to both Cel/toon and Gooch shading, but not MatCap.

Exceptions

The ctor can throw std::bad_alloc or std::invalid_argument.

Apply can throw std::runtime_error for if there's no texture or sampler set and texturing/matcap is enabled.

The methods SetSpecularThreshold, SetCelShaderBands, SetRimLightingIntensity, SetRimLightingRange can throw std::invalid_argument,

The other property methods of this implementation do not throw C++ exceptions.

Remarks

The EffectFlags::Lighting and EffectsFlags::PerPixelLighting are always enabled for these effects, so use or absence of these flags is ignored.

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.

EffectFlags::Fog is not supported by this effect.

The cel and Gooch shading always uses 1 directional light, and material capture lighting all comes from the matcap texture.

SetAmbientLightColor, SetLightEnabled, SetLightDiffuseColor, and SetLightSpecularColor are not implemented by this effect.

Further reading

Unity Shaders|MatCap casestudy
MatCap Library on GitHub

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 2026
  • Visual Studio 2022 (17.12 or later)
  • clang/LLVM v12 - v21
  • MinGW 14.2, 15.2, 16.1
  • CMake 3.21

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