"MADFINGER/Environment/Unlit (Supports Lightmap)"是 ShadowGun 示例中最简单的 shader 了,如下:

// Unlit shader. Simplest possible textured shader.
// - SUPPORTS lightmap
// - no lighting
// - no per-material color Shader "MADFINGER/Environment/Unlit (Supports Lightmap)" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Color ("HACK: temporary to fix lightmap bouncing light (will be fixed in RC1)", Color) = (,,,)
} SubShader {
Tags { "RenderType"="Opaque" }
LOD // Non-lightmapped
Pass {
Tags { "LightMode" = "Vertex" }
Lighting Off
SetTexture [_MainTex] { combine texture }
} // Lightmapped, encoded as dLDR
Pass {
Tags { "LightMode" = "VertexLM" } Lighting Off
BindChannels {
Bind "Vertex", vertex
Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
Bind "texcoord", texcoord1 // main uses 1st uv
} SetTexture [unity_Lightmap] {
matrix [unity_LightmapMatrix]
combine texture
}
SetTexture [_MainTex] {
combine texture * previous DOUBLE, texture * primary
}
} // Lightmapped, encoded as RGBM
Pass {
Tags { "LightMode" = "VertexLMRGBM" } Lighting Off
BindChannels {
Bind "Vertex", vertex
Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
Bind "texcoord", texcoord1 // main uses 1st uv
} SetTexture [unity_Lightmap] {
matrix [unity_LightmapMatrix]
combine texture * texture alpha DOUBLE
}
SetTexture [_MainTex] {
combine texture * previous QUAD, texture * primary
}
}
}
}

  在 Unity 中每一个 Pass 都会使得对象渲染一次,一般来讲 VertexLit 的 shader 只有一个 Pass,里面都是些简单状态设置和纹理混合,相当于 dx 的 .fx 文件。那么以上的 shader 中出现了3个不同的 Pass,是渲染3遍吗?

  答案是否,请看官方的另一个说明:Unity's Rendering Pipeline, 文章最后一段写到(重点看红色粗体部分):

Vertex Lit Rendering path

Since vertex lighting is most often used on platforms that do not support programmable shaders, Unity can't create multiple shader permutations internally to handle lightmapped vs. non-lightmapped cases. So to handle lightmapped and non-lightmapped objects, multiple passes have to be written explicitly.

  • Vertex pass is used for non-lightmapped objects. All lights are rendered at once, using a fixed function OpenGL/Direct3D lighting model (Blinn-Phong)
  • VertexLMRGBM pass is used for lightmapped objects, when lightmaps are RGBM encoded (this happens on most desktops and consoles). No realtime lighting is applied; pass is expected to combine textures with a lightmap.
  • VertexLMM pass is used for lightmapped objects, when lightmaps are double-LDR encoded (this happens on mobiles and old desktops). No realtime lighting is applied; pass is expected to combine textures with a lightmap.

  Unity 在使用 Vertex lit 模式时无法在内部自动分别处理使用了光照图的对象和未使用的,所以需要作者自己显式的针对 Vertex, VertexLMRGBM, VertexLMM 这三个 LightMode 的 PassTag 分别写一个 Pass,以便适应没有光照图,以及使用了光照图但编码不同的情况。

  在 UnityCG.cginc 中的 DecodeLightmap 里包含了上述的两种光照图解码:

// Decodes lightmaps:
// - doubleLDR encoded on GLES
// - RGBM encoded with range [0;8] on other platforms using surface shaders
inline fixed3 DecodeLightmap( fixed4 color )
{
#if defined(SHADER_API_GLES) && defined(SHADER_API_MOBILE)
return 2.0 * color.rgb;
#else
// potentially faster to do the scalar multiplication
// in parenthesis for scalar GPUs
return (8.0 * color.a) * color.rgb;
#endif
}

  参照这个代码就能知道第一个 shader 中后两个 Pass 的算法和这个函数是对应的。

 

Unity 的“Vertex Lit Rendering path“中 shader Pass 的注意事项的更多相关文章

  1. Unity Lighting - Choosing a Rendering Path 选择渲染路径(三)

      Choosing a Rendering Path 选择渲染路径 Unity supports a number of rendering techniques, or ‘paths’. An i ...

  2. shader 3 rendering path

    渲染通道, rendering path. vertexlit, forward 和 Deferred lighting 旧有的非统一架构下: 分为顶点着色引擎和像素渲染通道 渲染通道是GPU负责给图 ...

  3. Thinking in Unity3D:渲染管线中的Rendering Path

      关于<Thinking in Unity3D> 笔者在研究和使用Unity3D的过程中,获得了一些Unity3D方面的信息,同时也感叹Unity3D设计之精妙.不得不说,笔者最近几年的 ...

  4. Unity加载模块深度解析(Shader)

    作者:张鑫链接:https://zhuanlan.zhihu.com/p/21949663来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 接上一篇 加载模块深度解析(二 ...

  5. Unity shader学习之Forward Rendering Path

    Forward rendering path shader如下: // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObje ...

  6. 【原】实时渲染中常用的几种Rendering Path

    [原]实时渲染中常用的几种Rendering Path 本文转载请注明出处 —— polobymulberry-博客园 本文为我的图形学大作业的论文部分,介绍了一些Rendering Path,比较简 ...

  7. 渲染路径-实时渲染中常用的几种Rendering Path

    http://www.cnblogs.com/polobymulberry/p/5126892.html?utm_source=tuicool&utm_medium=referral 回到顶部 ...

  8. Unity Shaders Vertex & Fragment Shader入门

    http://blog.csdn.net/candycat1992/article/details/40212735 三个月以前,在一篇讲卡通风格的Shader的最后,我们说到在Surface Sha ...

  9. Unity 3d中Shader是什么,可以吃吗?

    众所周知,Unity3d是一款跨平台非常广的游戏引擎,上手容易,界面友好,集成功能众多,是目前开发手游的主流引擎.本人有幸使用Unity 3d进行开发已一年多时间,已领略了这歀引擎的强大之处. 编写s ...

随机推荐

  1. 读取Properties配置文件

    一,Android中 在Android中读取配置文件,可以使用System.getProperties()方法读取: 1,在res资源目录下,新建一个文件夹 raw,然后在其下创建一个.propert ...

  2. rac中 kull session会话脚本

    方法:ALTER SYSTEM KILL SESSION '80, 6, @2';  --<= 80 sid,6 serial#,@2 inst_id kill session 脚本如下:sel ...

  3. FTPClient文件下载

    一.FTPClient下载文件所需要的jar包: org.apache.commons.net  [commons-net-3.4.jar] 二.FTPClient的连接和关闭 //FTPClient ...

  4. sqlserver2008安装出现跨语言

    我在安装sqlserver2008的时候出现了一个问题,安装到一半的时候出现 跨语言安装失败 ,我细细的查了下问题,我装的安装语言绝对没有错的吧,然后我后退几步又是同样的错误,最后我把镜像重新加载到虚 ...

  5. [Twisted] 事件驱动模型

    在事件驱动编程中,多个任务交替执行,并且在单一线程控制下进行.当执行I/O或者其他耗时操作时,回调函数会被注册到事件循环. 当I/O完成时,执行回调.回调函数描述了在事件完成之后,如何处理事件.事件循 ...

  6. C# 之【获取网页响应码200】

    做了个监控网站的C#项目,判断网站是否运行正常,采用的就是获取HTTP头返回的状态码. 比如状态码为200的就是网站正常,403是Forbidden,404是网页未找到这样. 代码如下: 首先是使用库 ...

  7. Python:列表

    #!/usr/bin/python3 #列表 是可变的,可修改的 listDemo = ["one","two","three"," ...

  8. linear model for classification

    不同error function比较

  9. 第一天的CI笔记

    1 CI不区分大小写2. http://xxx.com/index/[控制器名称]/[控制器里面方法的确名称]/[传入方法的参数 ]/ 3. 控制器及控制器类名称与文件名称一致, 继承 CI_Cont ...

  10. js中的forin

    前言 自己在平时没事干练练手,发现的以前一直以为是错的.幸亏今天知道了,要不说起来自己还不知道呢. 过程 遍历前置页面上的textbox,给他们赋值(js). 一开始自己用forin遍历的. 如论如何 ...