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 lightinginteraction 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
Lambertand
BlinnPhongmodels
are defined in
Lighting.cgincfile 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
_PrePassfunction,
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 lightmaptexture data (passed in
color,
totalColor,
indirectOnlyColorand
scalearguments) 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>_PrePassfunction will be called after lightmap
decoding and
lightargument 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)。
相关文章推荐
- unity实现多点触控代码
- 在Unity中实现动画的正反播放代码
- unity实现摄像头跟随
- 用python做GPU计算(1)——安装以及配置
- Unity3D上路_01-2D太空射击游戏
- Unity3D上路_02-第一视角射击游戏
- Unity3D上路_03-塔防游戏
- Unity3D上路_04-基础资源介绍
- Unity3D上路_05-网络相关
- [硬件资讯]32nm Atom性能首曝:GPU性能三倍于今
- [软件资讯]Unity已支持将3D游戏导出成Flash
- Unity 武器拖尾效果
- vtkGPUVolumeRayCastMapper (Examples)
- GPU通用计算调研报告
- 关于XenServer 6.0 GPU Passthrough使用注意事项
- 第一個 CUDA 程式
- GPU 的硬體架構
- 定义在GPU上的变量
- 详解GPU的常见参数及其对显卡的重要性
- unity常见问题之20题