• Unity Shader——Writing Surface Shaders(2)——Custom Lighting models in Surface Shaders


    Surface Shader中的自定义光照模型

      当你在编写 Surface Shaders 时,是在描述一个表面的属性(反射颜色、法线……),而且光的交互过程是由一个光照模型来计算的。内建的光照模型有Lambert(漫反射光照)和BlinnPhong(镜面光照)。

      有时候,你可能想要使用一个自定义的光照模型,这在Surface Shader中是可能的。光照模型其实就是一些满足某些约定的Cg/HLSL函数。Unity内建的光照模型Lambert和BlinnPhong定义在Lighting.cginc文件中。这个文件在:

    • Windows:{Unity安装目录}/Data/CGIncludes/Lighting.cginc
    • Mac:/Applications/Unity/Unity.app/Contents/CGIncludes/Lighting.cginc

    光照模型声明

      光照模型是一系列名字以Lighting开头的约定函数。它们能够声明在shader文件或者包含的文件中的任何地方。这些函数是:

    1. half4 Lighting<Name> (SurfaceOutput s, half3 lightDir, half atten);用于正向渲染路径中不依赖视线方向的光照模型。
    2. half4 Lighting<Name> (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten);用于正向渲染路径中依赖视线方向的光照模型。
    3. half4 Lighting<Name>_PrePass (SurfaceOutput s, half4 light); 用于延迟光照路径中。

      注意:你不需要声明所有的函数。光照模型要么使用视线方向,要么不使用。同样的,如果光照模型不工作在延迟光照中,就不要声明 _PrePass函数,而且所有使用它的shader只会编译到正向渲染中。

    解码光照贴图

      用于正向渲染和延迟光照的光照贴图数据的解码可以被自定义在类似光照函数的方式中。根据光照模型是否依赖视线方向,选择下面其中一种函数。要解码标准的Unity光照贴图纹理数据(传入到colortotalColor,indirectOnlyColor 和scale 参数中),请使用内建的DecodeLightmap函数。

      自定义解码单张光照贴图的函数是:

    1. half4 Lighting<Name>_SingleLightmap (SurfaceOutput s, fixed4 color);用于不依赖视线方向的光照模型(如漫反射)。
    2. half4 Lighting<Name>_SingleLightmap (SurfaceOutput s, fixed4 color, half3 viewDir); 用于依赖视线方向的光照模型。

      自定义解码两张光照贴图的函数是:

    1. half4 Lighting<Name>_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade); 用于不依赖视线方向的光照模型(如漫反射)。
    2. half4 Lighting<Name>_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade, half3 viewDir); 用于依赖视线方向的光照模型。

      自定义解码方向光照贴图的函数是:

    1. half4 Lighting<Name>_DirLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, bool surfFuncWritesNormal); 用于不依赖视线方向的光照模型(如漫反射)。
    2. half4 Lighting<Name>_DirLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, half3 viewDir, bool surfFuncWritesNormal, out half3 specColor); 用于依赖视线方向的光照模型。

    例子

    Surface Shader Lighting Examples

  • 相关阅读:
    NTDS活动目录数据库维护--碎片整理、移动数据库文件、日志文件
    管理活动目录数据库
    NTDS活动目录数据库维护--碎片整理、移动数据库文件、日志文件
    IIS上的反向代理
    java.lang.OutOfMemoryError: Metaspace 的解决
    WPF分页控件
    C#学习笔记之.Static关键字
    C#学习笔记之简说参数
    C#学习笔记之Winform登录注册
    C#学习笔记之XML工具类
  • 原文地址:https://www.cnblogs.com/dreamlofter/p/4504468.html
Copyright © 2020-2023  润新知