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 IEffect{
    <<Interface>>
    +Apply()
    +GetVertexShaderBytecode()
}
class IEffectMatrices{
    <<Interface>>
    +SetWorld()
    +SetView()
    +SetProjection()
    +SetMatrices()
}
class IEffectLights{
    <<Interface>>
    +SetLightDirection()
    +EnableDefaultLighting()
}
class NPREffect{
    +SetMode()
    +SetAlpha()
    +SetDiffuseColor()
    +SetSpecularColor()
    +SetSpecularThreshold()
    +SetCelShaderBands()
    +SetGoochCoolColor()
    +SetGoochWarmColor()
    +SetRimLightingColor()
    +SetTextureEnabled()
    +SetVertexColorEnabled()
    +SetBiasedVertexNormals()
    +SetInstancingEnabled()
}
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 11 device.

std::unique_ptr<NPREffect> effect;
effect = std::make_unique<NPREffect>(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

NPREffect supports IEffect, IEffectMatrices, and IEffectLights.

Lighting is always enabled and MaxDirectionalLights is 1.

Fog settings are not supported.

Input layout

This effect requires SV_Position, NORMAL, COLOR if per-vertex colors are enabled, and TEXCOORD0 if texturing or matcap is enabled.

If instancing is enabled, NPREffect also requires these vertex elements:

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

Properties

  • SetMode: Sets which debug shader to use. Defaults to Mode_Cel:
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)
  • 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).

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

  • SetTextureEnabled: Enables texturing. Defaults to false. Modifying this setting requires recreating associated input layouts, and enabling it requires TEXCOORD0.

  • SetTexture: Associates a texture shader resource view with the effect. Can be set to nullptr to remove a reference. Can optionally include an alpha channel as well.

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

  • SetInstancingEnabled: Enables support for per-vertex instancing by adding a per-vertex XMFLOAT3X4 transform matrix.

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

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.

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

Remarks

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

SetLightingEnabled, SetPerPixelLighting, 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
  • Windows 8.1
  • Xbox One

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 12

DirectXMesh

DirectXTex

DirectXMath

Win2D

Tools

Test Suite

Model Viewer

Content Exporter

DxCapsViewer

Clone this wiki locally