Shading Model
What is a shading model
A shading model is what controls the lighting & shading of your surface. By default, Two different shading models are provided, ShadingModelStandard
which is the default, and ShadingModelValveStandard
. Not only can you use one of these pre-defined shading models, but you can also create your own shading model to shade your surface the way you want.
Using built-in shading models
Invoking and using your shading model is done by calling the FinalizePixelMaterial
function, which is defined automatically when you include the common/pixel.hlsl
file within your pixel shader.
By default, if you do not provide a shading model to the FinalizePixelMaterial
call, it will use the ShadingModelStandard
. For example:
If you wish to use the ShadingModelValveStandard
, it will need to be passed in as the third argument to FinalizePixelMaterial
like so.
Creating a shading model
Structure
All shading models contain 4 basic methods which need to be implemented. Init
, which is called once the shading model is initialized for the first time. Direct
is called for every single direct light which is currently visible, Indirect
is called once to calculate indirect lighting & PostProcess
which allows tweaking of the shading after everything is done, for example adding fog. To begin implementing any of these methods, you will need to create a class & inherit from ShadingModel
. An empty shading model can be seen below:
Light Data
Type | Name | Description |
---|---|---|
float3 | Color | The color is an RGB value in the linear sRGB color space. |
float3 | LightDir | The normalized light vector, in world space (direction from the current fragment's position to the light). |
float | NdotL | The dot product of the shading normal (with normal mapping applied) and the light vector. This value is equal to the result of saturate(dot(getWorldSpaceNormal(), lightData.l)) . This value is always between 0.0 and 1.0. When the value is <= 0.0, the current fragment is not visible from the light and lighting computations can be skipped. |
float3 | PositionWs | The position of the light in world space. |
float | Attenuation | Attenuation of the light based on the distance from the current fragment to the light in world space. This value between 0.0 and 1.0 is computed differently for each type of light (it's always 1.0 for directional lights). |
float | Visibility | Visibility factor computed from shadow maps or other occlusion data specific to the light being evaluated. This value is between 0.0 and 1.0. |
Usage
Once the shading model is created, it can be used like any other shading model. It's first declared and then passed into FinalizePixelMaterial
like so: