日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/6/17 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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文件或者包含的文件中的任何地方。這些函數是:

  • half4 Lighting<Name> (SurfaceOutput s, half3 lightDir, half atten);用于正向渲染路徑中不依賴視線方向的光照模型。
  • half4 Lighting<Name> (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten);用于正向渲染路徑中依賴視線方向的光照模型。
  • half4 Lighting<Name>_PrePass (SurfaceOutput s, half4 light);?用于延遲光照路徑中。
  •   注意:你不需要聲明所有的函數。光照模型要么使用視線方向,要么不使用。同樣的,如果光照模型不工作在延遲光照中,就不要聲明?_PrePass函數,而且所有使用它的shader只會編譯到正向渲染中。

    解碼光照貼圖

      用于正向渲染和延遲光照的光照貼圖數據的解碼可以被自定義在類似光照函數的方式中。根據光照模型是否依賴視線方向,選擇下面其中一種函數。要解碼標準的Unity光照貼圖紋理數據(傳入到color,?totalColor,indirectOnlyColor?和scale?參數中),請使用內建的DecodeLightmap函數。

      自定義解碼單張光照貼圖的函數是:

  • half4 Lighting<Name>_SingleLightmap (SurfaceOutput s, fixed4 color);用于不依賴視線方向的光照模型(如漫反射)。
  • half4 Lighting<Name>_SingleLightmap (SurfaceOutput s, fixed4 color, half3 viewDir);?用于依賴視線方向的光照模型。
  •   自定義解碼兩張光照貼圖的函數是:

  • half4 Lighting<Name>_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade);?用于不依賴視線方向的光照模型(如漫反射)。
  • half4 Lighting<Name>_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade, half3 viewDir);?用于依賴視線方向的光照模型。
  •   自定義解碼方向光照貼圖的函數是:

  • half4 Lighting<Name>_DirLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, bool surfFuncWritesNormal);?用于不依賴視線方向的光照模型(如漫反射)。
  • half4 Lighting<Name>_DirLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, half3 viewDir, bool surfFuncWritesNormal, out half3 specColor);?用于依賴視線方向的光照模型。
  • 例子

    Surface Shader Lighting Examples

    轉載于:https://www.cnblogs.com/dreamlofter/p/4504468.html

    總結

    以上是生活随笔為你收集整理的Unity Shader——Writing Surface Shaders(2)——Custom Lighting models in Surface Shaders的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。