zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    批处理集锦——(5)使用dir查找文件
    批处理集锦——(4)2>nul和1>nul是什么意思?
    python3循环遍历嵌套字典替换指定值
    selenium对浏览器自动截图
    linux 安装mysql8以及远程连接步骤(图文并茂)
    Allure 自动化测试报告使用详解
    allure安装教程以及遇到的坑
    pytest接口自动化快速设置接口全局host
    pytest报错警告处理一:DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
    python3.x中 pytest之fixture
  • 原文地址:https://www.cnblogs.com/dreamlofter/p/4504468.html
Copyright © 2011-2022 走看看