the custom lighting model in Surface shader
When you're writing surface Shaders, you're describing the properties of a surface (reflection color, normals ...). ), and the interaction of light is calculated by a light model. The built-in lighting models have Lambert (diffuse illumination) and blinnphong (specular illumination).
Sometimes, you might want to use a custom lighting model, which is possible in surface shader. The illumination model is actually some CG/HLSL function that satisfies certain conventions. The unity built-in lighting model Lambert and Blinnphong are defined in the Lighting.cginc file. This file is in:
- Windows:{unity installation directory}/data/cgincludes/lighting.cginc
- Mac:/applications/unity/unity.app/contents/cgincludes/lighting.cginc
Lighting Model Declaration
A lighting model is a series of convention functions whose names begin with lighting. They can be declared anywhere in the shader file or in the contained file. These functions are:
- Half4 lighting<name> (surfaceoutput s, Half3 lightdir, half atten); a light model that is not dependent on the line of sight direction in the forward rendering path.
- Half4 lighting<name> (surfaceoutput s, half3 Lightdir, Half3 viewdir, half atten); a light model that relies on the line of sight direction in the forward rendering path.
half4 Lighting<Name>_PrePass (SurfaceOutput s, half4 light);
Used to delay the light path.
Note: You do not need to declare all the functions. The lighting model either uses the line of sight or is not used. Similarly, if the light model does not work in delayed lighting, do not declare_PrePass函数,而且所有使用它的shader只会编译到正向渲染中。
Decode Light Map
The decoding of the illumination map data for forward rendering and delayed illumination can be customized in a manner similar to the lighting function. Select one of the following functions, depending on whether the lighting model relies on line of sight direction. To decode the standard unity light map texture data (passed color
in, totalColor
, indirectOnlyColor
and scale
parameters), use the built-in decodelightmap function.
The functions that customize the decoding of a single light map are:
- Half4 Lighting<name>_singlelightmap (surfaceoutput s, fixed4 color), for light models (such as diffuse) that do not rely on line of sight direction.
half4 Lighting<Name>_SingleLightmap (SurfaceOutput s, fixed4 color, half3 viewDir);
A light model that relies on line of sight direction.
The functions for customizing the decoding of two illumination maps are:
half4 Lighting<Name>_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade);
Used for light models that do not rely on line of sight direction, such as diffuse reflection.
half4 Lighting<Name>_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade, half3 viewDir);
A light model that relies on line of sight direction.
The functions for customizing the decoding direction illumination map are:
- Half4 Lighting<name>_dirlightmap (surfaceoutput s, fixed4 color, fixed4 scale, bool surffuncwritesnormal); Used for light models that do not rely on line of sight direction, such as diffuse reflection.
half4 Lighting<Name>_DirLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, half3 viewDir, bool surfFuncWritesNormal, out half3 specColor);
A light model that relies on line of sight direction.
Example
Surface Shader Lighting Examples
Unity shader--writing surface Shaders (2)--custom Lighting models in surface Shaders