您的位置:首页 > 移动开发 > Unity3D

Custom Lighting models in Surface Shaders

2015-11-18 18:29 405 查看


Custom Lighting models in Surface Shaders 自定义表皮着色器光照模型

When writing Surface Shaders, you’re describing properties of a surface (albedo color, normal, …) and the lighting
interaction is computed by a Lighting Model. Built-in lighting models are Lambert (diffuse
lighting) and BlinnPhong (specular lighting).

当你在编写表面着色器的时候,你是描述一个表面的属性(反射率颜色,法线,…)和通过计算光照模式来计算灯光的相互作用。内置的光照模式是 Lambert (漫反射光diffuse lighting) 和 BlinnPhong (镜面反射光(高光)specular lighting)。

Sometimes you might want to use a custom lighting model, and it is possible to do that in Surface Shaders. Lighting model is nothing more than a couple of Cg/HLSL functions that match some conventions. The built-in 
Lambert
 and 
BlinnPhong
 models
are defined in 
Lighting.cginc
 file inside Unity ({unity install path}/Data/CGIncludes/Lighting.cginc on Windows, /Applications/Unity/Unity.app/Contents/CGIncludes/Lighting.cginc on
Mac).

有时你也许会想使用自定义光照模式( custom lighting model)。这在表面着色器(surface shader)中是可以做到的。光照模式(lighting model)无外乎是几个Cg/HLSL函数的组合。内置的 Lambert 和 BlinnPhong定义在 Lighting.cginc文件中。(这个文件的位置在 windows系统下:{unity安装路径}/Data
/CGIncludes /Lighting.cginc,Mac系统下: /Applications/Unity/Unity.app/Contents/CGIncludes/Lighting.cginc)


Lighting Model declaration 光照模式声明

Lighting model is a couple of regular functions with names starting with 
Lighting
. They can be declared anywhere in your shader file or one of included files. The functions are:

光照模式是一个以Lighting开头与名字组合在一起的合乎规范的函数。你可以在你的着色器文件(shader file)或导入文件(included files)中的任何一个地方声明它。这个函数是:
half4 LightingName (SurfaceOutput s, half3 lightDir, half atten); This is used in forward rendering path for light models that are not view direction dependent (e.g. diffuse). 
half4 LightingName (SurfaceOutput s, half3 lightDir, half atten);这是在正向渲染路径(forward rendering path)中使用的光照模式。它不是取决于视图方向的(view direction)。例如:漫反射(diffuse)。

half4 LightingName (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten); This is used in forward rendering path for light models that are view direction dependent. 
half4 LightingName (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten);这是在正向渲染路径(forward rendering path)中使用的光照模式。它取决于视图的方向(view direction)。
half4 LightingName_PrePass (SurfaceOutput s, half4 light); This is used in deferred lighting path. 
half4 LightingName_PrePass (SurfaceOutput s, half4 light); 这是在延时光照路径(deferred lighting path)中使用的。

Note that you don’t need to declare all functions. A lighting model either uses view direction or it does not. Similarly, if the lighting model will not work in deferred lighting, you just do not declare 
_PrePass
 function,
and all shaders that use it will compile to forward rendering only.

请注意:你不要声明所有的函数。一个光照模式(lighting model)要么使用视图方向(view direction)要么不使用。同样的,如果光照模式(lighting model)在延时光照(deferred lighting)中不工作,你只要不声明成 _PrePass(第三个)函数就好了。那么所有的着色器在使用它的时候只会在正向渲染(forward
rendering)中编译。


Decoding Lightmaps 解码光照贴图

Decoding lightmap data can be customized in a similar fashion as the lighting function for forward and deferred lighting. Use one of the functions below depending on whether your light model is view direction dependent or not. To decode standard Unity lightmap
texture data (passed in 
color
totalColor
indirectOnlyColor
 and 
scale
 arguments) use built-in DecodeLightmapfunction.

解码lightmap数据可以被定制以类似的方式作为forward和deferred照明的照明功能。下面的函数使用一个取决于你是否依赖光照模型的视图方向。使用内置Decode
Lightmap 函数(通过color、totalColor、 indirectOnlyColor和scale参数)解码标准的Unity Lightmap纹理数据。

Custom lightmap decoding functions handle forward and deferred lighting rendering paths automatically. However you must be aware that in deferred case 
Lighting<Name>_PrePass
 function will be called after lightmap
decoding and 
light
 argument will contain sum of realtime lighting and lightmaps. If necessary you can distinguish forward and deferred paths by using built-in UNITY_PASS_PREPASSFINAL macro.

自定义Lightmap解码函数会自动处理forward 和 deferred 照明渲染路径。但是你必须意识到,在deffered情况下Lighting<Name>_PrePass函数将在Lightmap解码后调用,参数将包含实时照明以及lightmaps。如果有必要你可以使用内置UNITY_PASS_PREPASSFINAL宏来区分forward和defferrd路径。

Functions to customize decoding of Single lightmaps are:

自定义解码 Single Lightmaps的函数:

half4 Lighting<Name>_SingleLightmap (SurfaceOutput s, fixed4 color);
 

This is used for light models that are not view direction dependent (e.g. diffuse).

非视图方向依赖的光照模型。(e.g. diffuse)

half4 Lighting<Name>_SingleLightmap (SurfaceOutput s, fixed4 color, half3 viewDir);
 

This is used for light models that are view direction dependent.

视图方向以来的光照模型。

Functions to customize decoding of Dual lightmaps are:

自定义解码 [b]Dual [/b]Lightmaps的函数:

half4 Lighting<Name>_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade);
 

This is used for light models that are not view direction dependent (e.g. diffuse).

half4 Lighting<Name>_DualLightmap (SurfaceOutput
s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade);
 这是在正向渲染路径(forward rendering path)中使用的光照模式。它不是取决于视图方向的(view direction)。例如:漫反射(diffuse)。

half4 Lighting<Name>_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade, half3 viewDir);
 

This is used for light models that are view direction dependent.

half4 Lighting<Name>_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4
indirectOnlyColor, half indirectFade, half3 viewDir);
 这是在正向渲染路径(forward rendering path)中使用的光照模式。它取决于视图的方向(view direction)。

Functions to customize decoding of Directional lightmaps are:

自定义解码 [b]Directional [/b]Lightmaps的函数:

half4 Lighting<Name>_DirLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, bool surfFuncWritesNormal);
 

This is used for light models that are not view direction dependent (e.g. diffuse).

half4 Lighting<Name>_DirLightmap
(SurfaceOutput s, fixed4 color, fixed4 scale, bool surfFuncWritesNormal);
 这是在正向渲染路径(forward rendering path)中使用的光照模式。它不是取决于视图方向的(view direction)。例如:漫反射(diffuse)。

half4 Lighting<Name>_DirLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, half3 viewDir, bool surfFuncWritesNormal, out half3 specColor);
 

This is used for light models that are view direction dependent.

half4 Lighting<Name>_DirLightmap (SurfaceOutput
s, fixed4 color, fixed4 scale, half3 viewDir, bool surfFuncWritesNormal, out half3 specColor);
 这是在正向渲染路径(forward rendering path)中使用的光照模式。它取决于视图的方向(view direction)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息