Unity编写Shader内置各种矩阵和方法介绍

返回目录

大家好,我是阿赵。
这里记录一下Unity编写Shader内置各种矩阵和方法

一、Unity内置转换矩阵

1、MVP类矩阵

UNITY_MATRIX_MVP:Current model * view * projection matrix.
UNITY_MATRIX_MV:Current model * view matrix.
UNITY_MATRIX_V:Current view matrix.
UNITY_MATRIX_P:Current projection matrix.
UNITY_MATRIX_VP:Current view * projection matrix.
其中:
M:model(模型)
V:view(试图)
P:projection(投影)
所以上面那堆矩阵,其实就是这三种矩阵的排列组合。
注意:
mul(UNITY_MATRIX_MVP,v.pos)等价于UnityObjectToClipPos(v.pos),unity会自动替换

模型空间和世界空间转换
unity_ObjectToWorld:Current model matrix.
unity_WorldToObject:Inverse of current world matrix.
上面的矩阵里面没有包含单独的M矩阵,其实M矩阵就是unity_ObjectToWorld,只是格式不一样了,unity_WorldToObject是它的逆矩阵。

2、Transpos矩阵

Transpos:转置
由于法线和切线不是单纯的点坐标,而是有方向的向量。如果模型在非等比例缩放时,方向向量会发生变化,所以不能用普通的MV矩阵转换,要用T(transpos)矩阵
UNITY_MATRIX_T_MV
注意这个矩阵需要右乘,可以将法线向量从模型空间转换到观察空间:
float3 viewNormal = mul(v.normal,(float3x3)UNITY_MATRIX_T_MV);
逆矩阵:
UNITY_MATRIX_IT_MV

二、摄像机和屏幕

1、_WorldSpaceCameraPos

类型:float3
说明:World space position of the camera.摄像机的世界空间坐标

2、_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 1/FarPlane.
投影参数
x:1(如果当前渲染有一个翻转的投影矩阵是,这个值就变成-1)
y:摄像机的近端裁剪面
z:摄像机的远端裁剪面
w:1/远端裁剪面

3、_ScreenParams

类型:float4
说明:x is the width of the camera’s target texture in pixels
, y is the height of the camera’s target texture in pixels, z is 1.0 + 1.0/width and w is 1.0 + 1.0/height.
屏幕参数
假如摄像机设置了目标贴图(target texture,是指在摄像机上面挂了渲染输出的RenderTexture)
x:目标贴图的宽度(width)
y:目标贴图的高度(height)
z:1.0 + 1.0/width
w:1.0 + 1.0/height

4、_ZBufferParams

类型:float4
说明:Used to linearize Z buffer values. x is (1-far/near), y is (far/near), z is (x/far) and w is (y/far).
用来线性化Z缓存的值
x: (1-far/near)
y:(far/near)
z:(x/far)
w: (y/far)

5、unity_OrthoParams

类型:float4
说明:x is orthographic camera’s width, y is orthographic camera’s height, z is unused and w is 1.0 when camera is orthographic, 0.0 when perspective.
正交摄像机参数
x:正交摄像机的宽度
y:正交摄像机的高度
z:没用用
w:如果摄像机是正交,值为1,如果摄像机是透视,值为0

6、unity_CameraProjection

类型:float4x4
说明:Camera’s projection matrix.
摄像机的投影矩阵

7、unity_CameraInvProjection

类型:float4x4
说明:Inverse of camera’s projection matrix.
摄像机投影矩阵的逆矩阵

8、unity_CameraWorldClipPlanes[6]

类型:float4
说明:Camera frustum plane world space equations, in this order: left, right, bottom, top, near, far.
摄像机视锥六个面的世界空间方程,按顺序是:左、右、下、上、近、远

三、时间

1、_Time

类型:float4
说明:Time since level load (t/20, t, t2, t3), use to animate things inside the shaders.
从场景加载完之后经过的时间,一般用来在shader里面做动画
x:t/20
y:t
z:t2
w:t
3

2、_SinTime

类型:float4
说明:Sine of time: (t/8, t/4, t/2, t).
时间的正弦函数值
x:t/8
y:t/4
z:t/2
w:t

3、_CosTime

类型:float4
说明:Cosine of time: (t/8, t/4, t/2, t).
时间的余弦函数值
x:t/8
y:t/4
z:t/2
w:t

4、unity_DeltaTime

类型:float4
说明:Delta time: (dt, 1/dt, smoothDt, 1/smoothDt).
时间增量
dt 表示从上一帧到当前帧时间,以秒为单位
smoothDt 表示一个平稳的deltaTime,根据前N帧的时间加权平均的值。
x:dt
y:1/dt
z:smoothDt
w:1/smoothDt

四、灯光

Light parameters are passed to shaders in different ways depending on which Rendering Path
is used, and which LightMode Pass Tag is used in the shader.
光参数以不同的方式传递给着色器,这取决于使用的渲染路径,以及在着色器中使用的光模式传递标签。

1、Forward rendering (ForwardBase and ForwardAdd pass types):

前向渲染

1._LightColor0

类型:fixed4
说明:在UnityLightingCommon.cginc声明(旧版本是在Lighting.cginc声明),灯光的颜色

2._WorldSpaceLightPos0

类型:float4
说明:Directional lights: (world space direction, 0). Other lights: (world space position, 1).
当场景里面的是直射光时,这个是float4(灯光在世界空间的方向,0)
当场景里面是其他类型灯光时,这个是float4(灯光的世界空间位置坐标,1)

3.unity_WorldToLight

类型:float4x4
说明:World-to-light matrix. Used to sample cookie & attenuation textures.
在AutoLight.cginc声明,World-to-light矩阵。用于对cookie和衰减纹理进行取样

4.unity_4LightPosX0

unity_4LightPosY0
unity_4LightPosZ0
类型:float4
说明:(ForwardBase pass only) world space positions of first four non-important point lights.
只有在ForwardBase pass才会有,前4个不重要的点光源的世界空间位置

5.unity_4LightAtten0

类型:float4
说明:(ForwardBase pass only) attenuation factors of first four non-important point lights.
只有在ForwardBase pass才会有,前4个不重要的点光源的衰减因子

6.unity_LightColor

类型:half4[4](注意是数组)
说明:(ForwardBase pass only) colors of of first four non-important point lights.
只有在ForwardBase pass才会有,前4个不重要的点光源的灯光颜色

7.unity_WorldToShadow

类型:float4x4[4](注意是矩阵的数组)
说明:World-to-shadow matrices. One matrix for spot lights, up to four for directional light cascades.
World-to-shadow的矩阵数组,如果是聚光灯则只有一个矩阵,如果是直射光,则有4个矩阵

2、Deferred shading and deferred lighting, used in the lighting pass shader (all declared in UnityDeferredLibrary.cginc)

延迟着色和延迟光照,所有都是在UnityDeferredLibrary.cginc里面声明

1._LightColor

类型:float4
说明:灯光颜色

2.unity_WorldToLight

类型:float4x4
说明:World-to-light matrix. Used to sample cookie & attenuation textures.
World-to-light矩阵。用于对cookie和衰减纹理进行取样

3.unity_WorldToShadow

类型:float4x4[4](注意是矩阵的数组)
说明:World-to-shadow matrices. One matrix for spot lights, up to four for directional light cascades.
World-to-shadow的矩阵数组,如果是聚光灯则只有一个矩阵,如果是直射光,则有4个矩阵

3、Vertex-lit rendering (Vertex pass type)

顶点光照渲染

1.unity_LightColor

类型:half4[8]
说明:灯光颜色.

2.unity_LightPosition

类型:float4[8]
说明:View-space light positions. (-direction,0) for directional lights; (position,1) for point/spot lights.
观察空间的灯光位置
如果是直射光,是 float4(-direction,0)
如果是点光源或者聚光灯,则是float4(position,1)

3.unity_LightAtten

类型:half4[8]
说明:Light attenuation factors. x is cos(spotAngle/2) or –1 for non-spot lights; y is 1/cos(spotAngle/4) or 1 for non-spot lights; z is quadratic attenuation; w is squared light range.
灯光的衰减因子
x:如果是聚光灯cos(spotAngle/2),如果不是聚光灯是-1
y:如果是聚光灯1/cos(spotAngle/4),如果不是聚光灯是
z:二次衰减
w:灯光范围的平方

4.unity_SpotDirection

类型:float4[8]
说明:View-space spot light positions; (0,0,1,0) for non-spot lights.
观察空间的聚光灯位置,如果不是聚光灯,则值是 (0,0,1,0)

五、Lightmaps

1、unity_Lightmap

类型:Texture2D
说明:Contains lightmap information.
使用的Lightmap贴图,包含Lightmap信息

2、unity_LightmapST

类型:float4[8]
说明:Scales and translates the UV information to the correct range to sample the lightmap texture.
采样Lightmap的UV信息

六、雾和全局光

1.unity_AmbientSky

类型:fixed4
说明:Sky ambient lighting color in gradient ambient lighting case.
当环境光选择gradient时的天空环境光颜色
在这里插入图片描述

2.unity_AmbientEquator

类型:fixed4
说明:Equator ambient lighting color in gradient ambient lighting case.
当环境光选择gradient时,赤道环境光的颜色

3.unity_AmbientGround

类型:fixed4
说明:Ground ambient lighting color in gradient ambient lighting case.
当环境光选择gradient时,地面的灯光颜色

4.UNITY_LIGHTMODEL_AMBIENT

类型:fixed4
说明:Ambient lighting color (sky color in gradient ambient case). Legacy variable.
环境光颜色,如果是选择gradient时则是天空的颜色,这是旧版本遗留的变量

5.unity_FogColor

类型:fixed4
说明:雾的颜色

6.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.
雾的参数
x:(density / sqrt(ln(2)),在Exp2模式有用
y:density / ln(2),在Exp模式有用
z: –1/(end-start),在线性模式有用
w:end/(end-start)),在线性模式有用

七、Various

1、unity_LODFade

类型:float4
说明:Level-of-detail fade when using LODGroup. x is fade (0…1), y is fade quantized to 16 levels, z and w unused.
当使用LOD组时的细节等级过渡
x:0到1过渡
y:16个等级之间的过渡
z和w没有使用

2、_TextureSampleAdd

类型:float4
说明:Set automatically by Unity for UI only based on whether the texture being used is in Alpha8 format (the value is set to (1,1,1,0)) or not (the value is set to (0,0,0,0)).
只有Unity的UI会自动设置,
如果使用Alpha8格式,值会设置成 (1,1,1,0)
其他情况值会设置成 (0,0,0,0)

八、UnityCG.cginc

最后说一下,一般我们在写Unity的shader时都会把UnityCG.cginc包含。
这个库里面包含了很多好用的内容,可以自己打开看看,这里只举几个例子:

1、UNITY_PI

说明:圆周率π

2、颜色空间转换

inline bool IsGammaSpace()
说明:判断是否Gamma空间

inline half3 GammaToLinearSpace (half3 sRGB)
inline half3 LinearToGammaSpace (half3 linRGB)
说明:Gamma空间和线性空间颜色之间的转换

3、各种空间转换方法

封装好的各种空间转换方法,不需要考虑矩阵是左乘还是右乘,使用方便

1.坐标的空间转换的方法:

UnityWorldToClipPos
UnityViewToClipPos
UnityObjectToViewPos
UnityWorldToViewPos

2.向量的空间转换方法:

UnityObjectToWorldDir
UnityWorldToObjectDir
UnityObjectToWorldNormal

3.灯光方向

UnityWorldSpaceLightDir
ObjSpaceLightDir

4.观察空间

UnityWorldSpaceViewDir
ObjSpaceViewDir

4、各种贴图格式的转换

用得比较多的比如:
UnpackNormal
UnpackNormalWithScale
DecodeLightmap
DecodeHDR

猜你喜欢

转载自blog.csdn.net/liweizhao/article/details/130171576