Unity Shader——Writing Surface Shaders(2)——Custom Lighting models in Surface Shaders
2015-05-15 13:19
513 查看
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相关文章推荐
- Custom Lighting models in Surface Shaders
- Unity Shader——Writing Surface Shaders
- Unity Shader——Writing Surface Shaders(3)——Surface Shader Lighting Examples
- Unity Shader——Writing Surface Shaders(1)——Surface Shader Examples
- Unity Shader——Writing Surface Shaders(0)
- Unity Shaders and Effects Cookbook (7-2) Surface Shader 中实现 顶点动画
- Unity shader 官网文档全方位学习(一)————Surface Shaders基础及Examples详尽解析
- Unity Shaders and Effects Cookbook (1-1) 基本的表面着色器 (Surface Shader)
- Unity Shaders and Effects Cookbook (7-1) 在Surface Shader 中 访问 顶点颜色
- Unity自定义协程——CustomYieldInstruction
- Change Queue Not Work In Unity Shader
- Unity Surface Shader输入结构体的定义
- unity, unlit surface shader (texColor only surface shader)
- Unity笔记 Surface Shader
- unity, 替换shader渲染(Rendering with Replaced Shaders)
- Writing Surface Shaders
- UnityShader——基于CustomRenderTexture的战争迷雾
- Surface Shaders 内置灯光模型(Build in lighting models)Lighting.cginc 和如何自定义灯光类型
- unity, 替换shader渲染(Rendering with Replaced Shaders)
- unity surface shader植物