Unity shader--writing surface Shaders (2)--custom Lighting models in surface Shaders

Source: Internet
Author: User

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:

    1. 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.
    2. 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.
    3. 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:

    1. Half4 Lighting<name>_singlelightmap (surfaceoutput s, fixed4 color), for light models (such as diffuse) that do not rely on line of sight direction.
    2. 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:

    1. 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.
    2. 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:

    1. 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.
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.