内置变量都在UnityShaderVariables.cginc文件中声明

变换矩阵

All these matrices arefloat4x4 type.

Name

Value

UNITY_MATRIX_MVP

Current model * view * projection matrix.

从object space转到 projection space

UNITY_MATRIX_MV

Current model * view matrix.

从object space转到camera space

UNITY_MATRIX_V

Current view matrix.

从world space转到camera space

UNITY_MATRIX_P

Current projection matrix.

从camera space转到projection space

UNITY_MATRIX_VP

Current view * projection matrix.

从world space转到projection space

UNITY_MATRIX_T_MV

Transpose of model * view matrix.

UNITY_MATRIX_MV 的转置矩阵

UNITY_MATRIX_IT_MV

Inverse transpose of model * view matrix.

UNITY_MATRIX_MV的逆转置矩阵,法线的矩阵转换需要用变换矩阵的逆转置矩阵.

_Object2World

Current model matrix.

从object space转到world space

_World2Object

Inverse of current world matrix.

_Object2World的逆矩阵,从world space 到object space

Camera and screen

These variables will correspond to the Camerathat is rendering. For example during shadowmap rendering,they will still refer to the Camera component values, and not the “virtual camera”that is used for the shadowmap projection.

Name

Type

Value

_WorldSpaceCameraPos

float3

World space position of the camera.

相机的世界坐标

_ProjectionParams

float4

Xis 1.0 (or –1.0 if currently rendering with aflipped projection matrix),yis the camera’s near plane,zis the camera’s far plane andwis 1/FarPlane.

x 值是1.0或-1.0(当为反转的投影矩阵时)

y 近截面,z远截面,w 1/远截面

_ScreenParams

float4

xis the camera’s render target width in pixels,yis the camera’s render target height in pixels,zis 1.0 + 1.0/width andwis 1.0 + 1.0/height.

屏幕尺寸x宽,y高,z 1+1.0/width,w 1+1.0/height

_ZBufferParams

float4

Used to linearize Z buffer values.xis (1-far/near),yis (far/near),zis (x/far) andwis (y/far).

用于线性化zbuffer

unity_OrthoParams

float4

xis orthographic camera’s width,yis orthographic camera’s height,zis unused andwis 1.0 when camera is orthographic, 0.0 when perspective.

正交摄像机的相关参数,w值1时是正交投影,0时是透视投影

unity_CameraProjection

float4x4

Camera’s projection matrix.

摄像机的投影矩阵

unity_CameraInvProjection

float4x4

Inverse of camera’s projection matrix.

摄像机的投影矩阵的逆矩阵

unity_CameraWorldClipPlanes[6]

float4

Camera frustum plane world space equations, in this order: left, right, bottom, top, near, far.

视锥体的左右下上远近

 

Time

Name

Type

Value

_Time

float4

Time since level load (t/20, t, t*2, t*3), use to animate things inside the shaders.

_SinTime

float4

Sine of time: (t/8, t/4, t/2, t).

_CosTime

float4

Cosine of time: (t/8, t/4, t/2, t).

unity_DeltaTime

float4

Delta time: (dt, 1/dt, smoothDt, 1/smoothDt).

帧间隔时间

 

Lighting

Light parameters are passed to shaders in differentways depending on which Rendering Pathis used, and which LightModePass Tag is used in the shader.

Forward rendering (ForwardBase andForwardAdd passtypes):

forward渲染的光照变量

Name

Type

Value

_LightColor0(declared in Lighting.cginc)

fixed4

Light color.

光线颜色

_WorldSpaceLightPos0

float4

Directional lights: (world space direction, 0). Other lights: (world space position, 1).

World space 光照位置,平行光的w值为0,其他光源的w值为1

_LightMatrix0(declared in AutoLight.cginc)

float4x4

World-to-light matrix. Used to sample cookie & attenuation textures.

World space转到light space矩阵

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[4]

(ForwardBase pass only)colors of of first four non-important point lights.

在正向渲染中,影响物体的最亮的几个光源使用逐像素光照模式。接下来,最多有4个点光源会以逐顶点渲染的方式被计算。其他光源将以球面调和(Spherical Harmonics)的方式进行计算,球面调和技术计算很快但只能得到近似值。

Deferred shading and deferred lighting, usedin the lighting pass shader (all declared in UnityDeferredLibrary.cginc):

延迟光照相关变量

Name

Type

Value

_LightColor

float4

Light color.光照颜色

_LightMatrix0

float4x4

World-to-light matrix. Used to sample cookie & attenuation textures.

World space转到light space矩阵

Spherical harmonics coefficients (used by ambientand light probes) are set up forForwardBase,PrePassFinalandDeferredpasstypes. They contain 3rd order SH to be evaluated by world space normal (seeShadeSH9fromUnityCG.cginc).The variables are all half4 type,unity_SHArandsimilar names.

Vertex-lit rendering (Vertex pass type):

Up to 8 lights are set up for aVertexpass type; always sorted starting from the brightest one. So if you wantto render objects affected by two lights at once, you can just take first two entriesin the arrays. If there are less lights affecting the object than 8, the rest willhave their color set to black.

顶点光照相关变量

Name

Type

Value

unity_LightColor

half4[8]

Light colors. 光照颜色

unity_LightPosition

float4[8]

View-space light positions. (-direction,0) for directional lights; (position,1) for point/spot lights.

View space光照位置,平行光的w值为0,其他光源的w值为1

unity_LightAtten

half4[8]

Light attenuation factors.xis cos(spotAngle/2) or –1 for non-spot lights;yis 1/cos(spotAngle/4) or 1 for non-spot lights;zis quadratic attenuation;wis squared light range.

光照衰减值

unity_SpotDirection

float4[8]

View-space spot light positions; (0,0,1,0) for non-spot lights.

View space聚光灯位置

 

Fog and Ambient

Window > Lighting> Scene 里设置的相应的颜色

Name

Type

Value

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(2)), density / ln(2), –1/(end-start), end/(end-start)).x is useful for Exp2 fog mode, y for Exp mode, z and w for Linear mode.

Various

Name

Type

Value

unity_LODFade

float4

Level-of-detail fade when using LODGroupx is fade (0..1), y is fade quantized to 16 levels,z and w unused.

Built-in shader helper functions

其他一些杂七杂八的变量

#pragmatarget 3.0  定义Shader模型为Shader Model 3.0,

Image Effect取到depthTexture相关的步骤

GetComponent<Camera>().depthTextureMode=DepthTextureMode.Depth//设置摄像机模式

Shader中声明  sampler2D _CameraDepthTexture;//深度纹理的声明

取样float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture,uv) , //0-1越远越大

生成一个深度+法线纹理

GetComponent<Camera>().depthTextureMode=DepthTextureMode.DepthNormals;

Shader中声明

sampler2D _CameraDepthTexture;//深度纹理的声明

sampler2D _CameraNormalsTexture;//法线纹理的声明

用tex2d方法进行法线取样

或者声明

sampler2D _CameraDepthNormalsTexture.//

DecodeDepthNormal(tex2D(_CameraDepthNormalsTexture,i.screen.xy), depthValue, normalValues);取得法线跟深度值

DecodeDepthNormalto decode depth and normalfrom the encoded pixel value. Returned depth is in 0.1 range.

_CameraDepthTexturealways refers to the camera’sprimary depth texture. By contrast, you can use_LastCameraDepthTextureto refer to the last depthtexture rendered by any camera.This couldbe useful for example if you render a half-resolution depth texture in script usinga secondary camera and want to make it available to a post-process shader.

_LastCameraDepthTexture取得最后渲染的深度纹理

Texturesize  纹理尺寸获得

{TextureName}_TexelSize- a float4 property contains texture size information:

xcontains1.0/width

ycontains1.0/height

zcontainswidth

wcontainsheight

例子

uniform float4 _MainTex_TexelSize分别对应Vector4(1 / width, 1 / height, width, height),

 

Texturetiling & offset 纹理偏移

Materialsoften have Tiling and Offset fields for their texture properties.This information is passed into shaders in a float4 {TextureName}_STproperty:

xcontainsX tiling value

ycontainsY tiling value

zcontainsX offset value

wcontainsY offset value

例:

o.uv =TRANSFORM_TEX(v.texcoord,_MainTex);

相当于

o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;

o.uv = v.texcoord.xy相当于Tiling为(1,1)Offset为(0,0)

shader内置变量的更多相关文章

  1. unity shader 内置变量

    官网Manual:http://docs.unity3d.com/Manual/SL-UnityShaderVariables.html unity提供大量的内置变量,来供我们使用,主要包括一些 :矩 ...

  2. 学习笔记:GLSL Core Tutorial – Vertex Shader(内置变量说明)

    1.每个Vertex Shader都有用户定义的输入属性,例如:位置,法线向量和纹理坐标等.Vertex Shaders也接收一致变量(uniform variables). uniform vari ...

  3. Maven系列三Maven内置变量

    Maven内置变量说明: ${basedir} 项目根目录(即pom.xml文件所在目录) ${project.build.directory} 构建目录,缺省为target目录 ${project. ...

  4. Maven的内置变量

    Maven内置变量说明: ${basedir} 项目根目录(即pom.xml文件所在目录) ${project.build.directory} 构建目录,缺省为target目录 ${project. ...

  5. nginx的那些内置变量

    nginx在配置文件nginx.conf中可以使用很多内置变量,配置如下: location /info { add_header 'Content-Type' 'text/html'; echo & ...

  6. Maven内置变量说明

    Maven内置变量说明: ${basedir} 项目根目录 ${project.build.directory} 构建目录,缺省为target ${project.build.outputDirect ...

  7. 学习笔记——Maven 内置变量

    Maven内置变量说明: ${basedir} 项目根目录(即pom.xml文件所在目录) ${project.build.directory} 构建目录,缺省为target目录 ${project. ...

  8. makefile 分析 -- 内置变量及自动变量

    makefile 分析1  -p 选项,可以打印出make过程中的数据库, 下面研究一下内置的变量和规则. -n 选项, 只运行,不执行, -d 选项,相当于--debug=a,  b(basic), ...

  9. Perl中的特殊内置变量详解

    #!/usr/bin/perl -w @array = qw(a b c d); foreach (@array) { print $_," "; } 例子的作用就是定义一个数组并 ...

随机推荐

  1. OA项目Ioc DI(二)

    依赖注入:属性和构造函数的注入 一个简单的Demo: IUserInfoDal接口: public interface IUserInfoDal { void Show(); string Name ...

  2. html meta标签使用总结(转)

    之前学习前端中,对meta标签的了解仅仅只是这一句. <meta charset="UTF-8"> 但是打开任意的网站,其head标签内都有一列的meta标签.比如我博 ...

  3. Redis(二)CentOS7安装Redis4.0.10与集群搭建

    一 Redis单机安装 1 Redis下载安装 1.1 检查依赖环境(Redis是C语言开发,编译依赖gcc环境) [root@node21 redis-]$ gcc -v -bash: gcc: c ...

  4. asp.net MVC发布iis无法加载css,js和图片

    今天真够郁闷的,遇到了在本地能运行的项目到了iis服务器那里就不行了,无法加载css,js和图片,这里说清楚一下先,关于asp.net 的MVC中这样的情况其实不少,但是之前遇到的是在visual s ...

  5. 8-1 binpacking uva1149(贪心)

    题意:给定N个物品的重量Li  背包的容量M 同时要求每个背包最多装两个物品 求至少要多少个背包才能装下所有物品 简单贪心  注意输出: #include<bits/stdc++.h> u ...

  6. QString::arg()//用字符串变量参数依次替代字符串中最小数值

    QString i = "iTest";           // current file's number QString total = "totalTest&qu ...

  7. 分布式系统的烦恼------《Designing Data-Intensive Applications》读书笔记11

    使用分布式系统与在单机系统中处理问题有很大的区别,分布式系统带来了更大的处理能力和存储容量之后,也带来了很多新的"烦恼".在这一篇之中,我们将看看分布式系统带给我们新的挑战. 1. ...

  8. Python 递归删除非空目录(包括子目录以及文件)

    Python的OS模块自带rmdir和removedirs函数用于删除目录,但是两者都不能删除非空目录,以下代码定义了一个函数 remove_dir 用于删除非空目录. #作者官网 http://ww ...

  9. java中的dao模式

    java中Dao模式   什么是DAO   1.Data Access Object(数据存取对象) 2.位于业务逻辑和持久化数据之间 3.实现对持久化数据的访问 DAO模式的作用 1隔离业务逻辑代码 ...

  10. Linux-数据库4

    存储引擎 什么是存储引擎? mysql中建的库是文件夹,建的表是文件.文件有不同的类型,数据库中的表也有不同的类型,表的类型不同,会对应mysql不同的存取机制,表类型又称为存储引擎. 存储引擎说白了 ...