预定义shader预处理宏:
  Target platform:

  SHADER_API_OPENGL - desktop OpenGL
  SHADER_API_D3D9 - Direct3D
  SHADER_API_XBOX360 - Xbox
  SHADER_API_PS3 - PlayStation
  SHADER_API_D3D11 - desktop Direct3D
  SHADER_API_GLES - OpenGL ES 2.0 (desktop or mobile), use presence of SHADER_API_MOBILE to determine.
  SHADER_API_FLASH - Flash Stage3D
  SHADER_API_D3D11_9X - Direct3D target for Windows RT

  Surface shader pass indicators:

  UNITY_PASS_FORWARDBASE - 前向渲染的base pass(主方向光、lightmaps、SH)
  UNITY_PASS_FORWARDADD - 前向渲染的add pass(没盏灯一个pass)
  UNITY_PASS_PREPASSBASE - 延迟渲染base pass(renders normals & specular exponent).
  UNITY_PASS_PREPASSFINAL - 延迟渲染final pass (applies lighting & textures).
  UNITY_PASS_SHADOWCASTER - 阴影投射渲染pass.
  UNITY_PASS_SHADOWCOLLECTOR - 阴影手机pass for directional light shadows.

内置全局状态变量:

  内置的矩阵:

UNITY_MATRIX_MVP                                   float4x4    Current model * view * projection matrix.
UNITY_MATRIX_MV float4x4 Current model * view matrix.
UNITY_MATRIX_V float4x4 Current view matrix.
UNITY_MATRIX_P                      float4x4 Current projection matrix.
UNITY_MATRIX_VP                    float4x4 Current view * projection matrix.
UNITY_MATRIX_T_MV float4x4 Transpose of model * view matrix.
UNITY_MATRIX_IT_MV                  float4x4 Inverse transpose of model * view matrix.
UNITY_MATRIX_TEXTURE0 to UNITY_MATRIX_TEXTURE3 float4x4 Texture transformation matrices.
_Object2World float4x4 Current model matrix.
_World2Object float4x4 Inverse of current world matrix.
_WorldSpaceCameraPos float3 World space position of the camera.
unity_Scale float4 xyz components unused; w contains scale for uniformly scaled objects.

  内置的向量:

  UNITY_LIGHTMODEL_AMBIENT: Current ambient color.

  光照相关的:

_ModelLightColor                                   float4    Material’s Main * Light color
_SpecularLightColor float4 Material’s Specular * Light color
_ObjectSpaceLightPos float4 Light’s position in object space. w component is for directional lights, for other lights
_Light2World float4x4 Light to World space matrix
_World2Light float4x4 World to Light space matrix
_Object2Light float4x4 Object to Light space matrix

  变量:

_Time                                              float4    Time (t/, t, t*, t*), use to animate things inside the shaders.
_SinTime float4 Sine of time: (t/, t/, t/, t).
_CosTime float4 Cosine of time: (t/, t/, t/, t).
unity_DeltaTime float4 Delta time: (dt, /dt, smoothDt, /smoothDt).
_ProjectionParams float4 x is 1.0 (or –1.0 if currently rendering with a flipped projection matrix), y is the camera’s near plane, z is the camera’s far plane and w is /FarPlane.
_ScreenParams float4 x is the current render target width in pixels, y is the current render target height in pixels, z is 1.0 + 1.0/width and w is 1.0 + 1.0/height.

  内置光照参数:
  对于不同的Rendering Path和Pass Tag,可以使用的光照参数是不一样的。(当然所有参数你都可以在shader中使用,但并不能保证其一定会存储有效的值)。
  Forward rendering(ForwardBase和PorwardAdd标记的pass)支持的变量:

LightColor0 (declared in Lighting.cginc) | fixed4 |Light color. | |WorldSpaceLightPos0  float4    Directional lights: (world space direction, ). Other lights: (world space position, ).
_LightMatrix0 (declared in AutoLight.cginc)                           float4x4   World-to-light matrix. Used to sample cookie & attenuation textures.
unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0                    float4    (ForwardBase pass only) world space positions of first four non-important point lights.
unity_4LightAtten0                                          float4    (ForwardBase pass only) attenuation factors of first four non-important point lights.
unity_LightColor                                           half4[]   (ForwardBase pass only) colors of of first four non-important point lights.

  说明:

  (1)_LightColor0在ForwardBass里面表示:主光,一定是Pixel(Important)、Directional光源,没有的话则改值不可用;

  (2)_LightColor0在ForwardAdd里面表示:当前执行的ForwardAdd的pixel光;

  _WorldSpaceLightPos0的意义和_LightColor类似,也是在Base里表示主光,Add里表示像素光。

  (3)unity_LightColor是距离物体最近的4个point光,也就是说同一个场景中每个物体的这4个光不一样,注意这4个point光的选择非常有意思;

    (a)当shader中没有ForwardAdd Pass的时候,unity_LightColor记录了4盏vertex point光源,无视pixel光;

    (b)当shader中包含ForwardAdd Pass的时候,unity_LightColor记录了4盏point光源,pixel比vertex优先。

  (4)如果场景中有多个pixel光,则对每一个物体,所有的非主光的pixel光都会执行一遍ForwardAdd Pass,按照强度大小、平行光优先的顺序来执行;

  (5)不要在ForwardAdd Pass里面使用unity4LightPosX/Y/Z等数据,因为它们就是给ForwardBase Pass使用的,而且如果其中有pixel光,它们本身就会对ForwardAdd执行一遍;

  (6)可以使用Shade4PointLights函数在ForwardBase中计算Vertex光源的照明。

  Deferred shading:

LightColor | float4 | Light color. | |LightMatrix0                        float4x4  World-to-light matrix. Used to sample cookie & attenuation textures.

  Vertex-lit rendering(Vertex标记的pass):
  顶点光照可以使用最多8盏光源,这些光源按照亮度排序,按顺序存储在一下这些数据结构中,如果光源数量少于8,那么多出来的都将黑色:

unity_LightColor            half4[]    Light colors.
unity_LightPosition          float4[]    View-space light positions. (-direction,) for directional lights; (position,) for point/spot lights.
unity_LightAtten            half4[]    Light attenuation factors. x is cos(spotAngle/) or – for non-spot lights; y is /cos(spotAngle/) or for non-spot lights; z is quadratic attenuation; w is squared light range.
unity_SpotDirection          float4[]    View-space spot light positions; (,,,) for non-spot lights.

  说明:

  (1)_LightColor0表示unity_LightColor[0]

  (2)unity_LightPosition是MV空间中的位置

  (3)逐顶点光照函数float4 ShadeVertexLights(float4 vertex, float3 normal)

  注意:

  (1)_LightColor0变量不代表某个具体含义,它在不同的Render Path和Pass里面是有不同的意义的

  (2)ShaderLab里面的内置变量是不会及时清理的,这是Unity处于性能方面的考虑,因为在CPU和GPU之间频繁传递数据是很耗资源的

  Fog和Ambient环境光信息:

UNITY_LIGHTMODEL_AMBIENT      fixed4    Ambient lighting color (Equator ambient in three-color lighting case).
unity_AmbientSky           fixed4    Sky ambient lighting color in three-color lighting case.
unity_AmbientGround         fixed4    Ground ambient lighting color in three-color lighting case.
unity_FogColor            fixed4    Fog color.
unity_FogParams            float4    Parameters for fog calculation: (density / sqrt(ln()), density / ln(), –/(end-start), end/(end-start)). x is useful for Exp2 fog mode, y for Exp mode, z and w for Linear mode.

使用multi_compile编译Shader的多个版本:

  有时候我们希望保留一个shader的大多数实现,而只是做一些细节的修改,此时我们就可以通过该方式来生成shader的多个版本,然后在代码中控制使用哪个版本。
  命令:
  #pragma multi_compile FANCY_STUFF_OFF FANCY_STUFF_ON
  控制:
  Material.EnableKeyword(keyword: string)/DisableKeyword(keyword: string)
  Shader.EnableKeyword(keyword: string)/DisableKeyword(keyword: string)
  在shader中根据定义好的关键字处理具体显示方案:

  float4 frag(vertOut i):COLOR
  {
    float4 c = float4(, , , );
    #ifdef MY_multi_1
      c = float4(, , , );
    #endif
    #ifdef MY_multi_2
      c = float4(, , , );
    #endif
    return c;
}

  注意不要在shader中大量使用此命令,因为关键字的个数是有上限的,而多条multi_complie会交叉匹配产生很多种结果。

GLSL shader编程
  在Shader中,除了Cg/HLSL以外,GLSL(OpenGL Shading Languate)也可以直接使用。
  一般不建议使用GLSL,除非你确定你的目标平台只有Max OS X或兼容OpenGL ES 2.0的移动设备。
  Unity默认都会将Cg/HLSL交叉编译出优化过得GLSL,以此来支持多平台。在desktop平台下可以通过打开#pragma glsl选项来支持。

平台特性相关的渲染差异:
  Unity封装了大多数的平台问题,但有些时候还是需要自己处理一下的。
  纹理坐标
  Direct3D里面是左上角为原点;
  OpenGL和OpenGL ES是左下角为原点。
  多数时候都ok,除了在Render To Texture时。但是在RTT时,Unity对于D3D平台会自动将渲染的贴图上下翻转一下,以此隐藏了各平台的差异。
  D3D平台下,有一种情况不需要Unity自动翻转渲染贴图,那就是使用Image Effects和Anti-Aliasing时,因为直接渲染到Screen了,
  需要手动处理的情况:但同时使用screen texture和RenderTexture时,有可能会得到不一致的坐标朝向,此时就需要在shader里面通过代码处理了:

// On D3D when AA is used, the main texture and scene depth texture
// will come out in different vertical orientations.
// So flip sampling of the texture when that is the case (main texture
// texel size will have negative Y). #if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < )
uv.y = -uv.y; #endif

  AlphaTest:
  在OpenGL ES 2.0和Direct3D 11中,没有alpha testing的固定函数。所以在编写programmable shader时,建议在pixel shader中使用CG/HLSL clip()函数来替代。
  D3D 11 shader编译器比较挑剔:
  D3D 9和OpenGL使用NVIDIA的Cg来编译shader,但D3D 11使用微软的HLSL来编译,此时就会产生一些差异化。
  OpenGL ES 2.0只支持部分GLSL原生的东西,所以Unity实现了一些内置的参数,让其以OpenGL的方式工作,但是也有一些参数是没有的。

_WorldSpaceLightPos0:
  在UnityCG.cginc里,提供了获取世界空间下的光照方向的函数

    // Computes world space light direction
inline float3 WorldSpaceLightDir( in float4 v )
{
float3 worldPos = mul(_Object2World, v).xyz;
#ifndef USING_LIGHT_MULTI_COMPILE
return _WorldSpaceLightPos0.xyz - worldPos * _WorldSpaceLightPos0.w;
#else
#ifndef USING_DIRECTIONAL_LIGHT
return _WorldSpaceLightPos0.xyz - worldPos;
#else
return _WorldSpaceLightPos0.xyz;
#endif
#endif
}

其中,由于平行光的方向不随顶点位置发生变化,因此直接使用_WorldSpaceLightPos0.xyz即可,此时里面存储的其实就是平行光的方向,而非位置。同时,
_WorldSpaceLightPos0.w可以表明该光源的类型,如果为0表示是平行光,为1表示是点光源或者聚光灯光源。

TANGENT_SPACE_ROTATION:
  创建一个正切空间的旋转矩阵,TANGENT_SPACE_ROTATION由下面两行组成
  等同于如下两行代码:

float3 binormal = cross( v.normal, v.tangent.xyz ) * v.tangent.w;
float3x3 rotation = float3x3( v.tangent.xyz, binormal, v.normal );

CSDN博客:http://blog.csdn.net/candycat1992/article/details/47284289

Shader预处理宏、内置状态变量、多版本编译等的更多相关文章

  1. Shader程序中内置的状态变量

    经常在着色器程序需要访问一些全局状态,像当前的 model view projection 矩阵,当前环境的颜色诸如此类. 内置的矩阵 UNITY_MATRIX_MVP:当前模型 视窗 投影矩阵 UN ...

  2. 【Unity Shaders】使用CgInclude让你的Shader模块化——Unity内置的CgInclude文件

    本系列主要參考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同一时候会加上一点个人理解或拓展. 这里是本书全部的插图. 这里是本书所需的代码 ...

  3. 新浪云(SAE)使用没有内置的django版本

    SAE自带的django目前到1.5版本,如果要使用更高的版本,则需要把django包同代码一起上传. 以1.7.3为例 先从SAE svn签出代码,默认1是根目录 1.  1目录下创建文件夹 sit ...

  4. appium常见问题04_查看andriod内置浏览器webview版本

    方法一:手机上设置中查看 设置-->应用程序管理-->全部-->Android System WebView 方法二:adb指令查看(前提,已安装android sdk环境) 1,w ...

  5. Elasticsearch7.3使用内置的JDK12

    汇总:采用最简单的办法,就是在elasticsearch文件开头添加上这一行export JAVA_HOME=/home/vdb1/elastic_cluster/elasticsearch-7.3. ...

  6. Unity内置的shader include files

    Unity内置的shader include files:这些文件都以.cninc结尾, HLSLSupport.cginc:自动包含,一些跨平台编译相关的宏和定义. UnityShaderVaria ...

  7. Unity 内置Shader变量、辅助函数等

    一:标准库里的常用.cginc文件 HLSLSupport.cginc - (automatically included) Helper macros and definitions for cro ...

  8. CESIUM内置shader变量和函数[转]

    cesium中内置了一些常量.变量和函数,在vs和fs中可直接使用. 内置uniform 内置uniform主要置于AutomaticUniforms类里面,该类私有未开放文档. czm_backgr ...

  9. Unity内置shader 下载

    Unity内置shader  4.3.1 版本的  其他版本可以自己修改名称 下载地址 http://download.unity3d.com/download_unity/builtin_shade ...

随机推荐

  1. If you really want to compile without asm, configure with --disable-asm.

    真是一个奇葩问题,对我来说是的,完全不知道是什么意思,但是他就是出现了. 找到了一个相关问题http://trac.ffmpeg.org/wiki/How%20to%20quickly%20compi ...

  2. 设置UIButton或者UILabel显示文字的行数

    需要在UIButton的titleLabel或者UILabel的text 字符串里面添加换行符号 “\n”,并且设置 UILabel的numberOfLines属性 栗子:行数要和“\n”的个数对应, ...

  3. Dojo Data Store——统一数据访问接口

    原文地址:http://www.infoq.com/cn/articles/wq-dojo-data-store 无论在传统的桌面应用还是在主流的互联网应用中,数据始终占据着软件应用中的核心地位.当下 ...

  4. 利用HtmlAgilityPack库进行HTML数据抓取

    主要介绍基于XPATH的文本分析方式的实现,代码如下: using System; using System.Collections.Generic; using System.Linq; using ...

  5. 微信公共平台开发-(.net实现)2--ACCESSTOKEN值获得

    成功的走出第一步后,我们紧接着趁热打铁开始下一步: 成为了开发者之后微信平台会给您AppId和AppSecret,在订阅号中是没有的,所以因该申请一下服务号, 若没有请注意上一篇http://www. ...

  6. mono for android学习过程系列教程(6)

    接着上一讲,今天讲的是Button,CheckBox这二个安卓元素, 我们来看第一个Button这个控件,类似winform和webform里面一样,它也是 存在有触发事件的,我们新建初始化项目直接就 ...

  7. 使用Nginx实现负载均衡

    使用Nginx实现负载均衡 一.nginx简介 nginx是一个高性能的HTTP服务器和反向代理服务器.它起初是俄罗斯人Igor Sysoev开发的,至今支撑者俄罗斯的很多大型的网站. 二.nginx ...

  8. 排序 via F#

    冒泡排序: let rec bsort_asc s = let rec _bsort_asc = function |x1::x2::xs when x1 > x2 -> match _b ...

  9. 《JAVA与模式》之抽象工厂模式

    场景问题 举个生活中常见的例子——组装电脑,我们在组装电脑的时候,通常需要选择一系列的配件,比如CPU.硬盘.内存.主板.电源.机箱等.为讨论使用简单点,只考虑选择CPU和主板的问题. 事实上,在选择 ...

  10. 新手的第一个phonegap Android应用

    对PhoneGap开发感兴趣的请加入群 PhoneGap App开发 348192525   手机成为现在软件应用必不可少的一种设备,然而手机平台的不统一造成我们需要为不同手机重写代码,这对一般应用来 ...