首页 技术 正文
技术 2022年11月9日
0 收藏 743 点赞 5,067 浏览 7589 个字

unity5的UnityShaderVariables.cginc比unity4大了1kb
这里装着unity shader 大部分内部参数,写这个方便以后自己查询
窥探Unity5渲染内部之解析UnityShaderVariables.cginc

Camera参数

uniform float4 _Time;

时间,x = t/20,y = t,z = t*2,w = t*3

uniform float4 _SinTime;

sin(时间), x = sin(t/8),y = sin(t/4),z = sin(t/2),w = sin(t)

uniform float4 _CosTime;

cos(时间), x = cos(t/8),y = cos(t/4),z = cos(t/2),w = cos(t)

uniform float4 unity_DeltaTime;

每帧时间, x = dt,y = 1/dt,z =  smoothdt,w = 1/smoothdt

uniform float3 _WorldSpaceCameraPos;
世界空间相机坐标

uniform float4 _ProjectionParams;
投影参数
x = 1,如果投影翻转则x = -1
y是camera近裁剪平面
z是camera远裁剪平面
w是1/远裁剪平面

uniform float4 _ScreenParams;
屏幕参数:x = 屏幕宽度,y = 屏幕高度,z =  1 + 1.0/屏幕宽度, w = 1 + 1.0/height屏幕高度(指像素数)

 uniform float4 _ZBufferParams;
用于线性化z buffer
x = 1-far/near
y = far/near
z = x/far
w = y/far
参照了http://www.humus.name/temp/Linearize%20depth.txt
关于z buffer
aras如是说:
 Currently
in D3D “depth” textures are single channel floating point texture, and
we output linear 0..1 depth over far plane range when rendering into it.
In
OpenGL, the “depth” texture is much like a depth buffer, i.e. it has
non-linear range. Usually depth buffer range in OpenGL is -1…1 range,
so in Humus’ text, OpenGL math would have to be used. However, OpenGL
depth textures seem to actually have 0..1 range, i.e. just like depth
buffer in D3D. It’s not explicitly written in the specification, but I
found that out by trial and error. So since it matches D3D’s depth
buffer range, the D3D math has to be used.
博主又查了一下计算机图形学第三版9-14如是说:

In OpenGL,depth values are normalized in range from 0 to 1.0 , so that the preceding initialization sets all depth-buffer values to the maximum value 1.0 by default.
Projection coordinates in OpenGL
are normalized in range from -1.0 to 1.0,and the depth values between
the near and far clipping planes are further normalized to the range
from 0 to 1.0.The value 0.0 corresponds to the near clipping plane (the
projection plane),and the value 1.0 corresponds to the far clipping
plane.

 

 uniform float4x4 unity_CameraProjection;

Projection matrices of the camera. Note that this might be different from projection matrix
that is set right now, e.g. while rendering shadows the matric8es below are still the projection
of original camera.
 相机的投影矩阵,这里的投影矩阵有很多个

uniform float4x4 unity_CameraInvProjection;

相机的投影矩阵的逆矩阵

光照方面

如果是directional light则光的方向为_WorldSpaceLightPos0,是half4类型的,
如果是point light则点光源的位置为_WorldSpaceLightPos0,是float4类型的,
因为directional light是方向不需要特别精准,只要half就够了,而点光源的位置因为需要精确的计算,是需要很准确的,所以为float类型,由此可见unity技术人员的优化和细致。

uniform float4 _LightPositionRange;
xyz为位置position,w为1/range


    float4 unity_4LightPosX0;
    float4 unity_4LightPosY0;
    float4 unity_4LightPosZ0;

世界空间四个光源的position的x,y,z坐标

 

half4 unity_4LightAtten0;
四个光源的衰减

half4 unity_LightColor[8];
点光源的颜色,前篇文章Unity5内部渲染的优化3:移除固定功能提到了,unity5可以使用8个点光源了。

float4 unity_LightPosition[8];
view space的点光源位置,其值为(position,1)。
如果为directional light 则其值为方向(-direction,0)


half4 unity_LightAtten[8];

x = cos(spotAngle/2) or -1 for non-spot
y = 1/cos(spotAngle/4) or 1 for non-spot
z = quadratic attenuation  二次方衰减
 w = range*range
8个光源的衰减

 float4 unity_SpotDirection[8];
view space 的spot light的方向,如果无spot light则其值为(0,0,1,0)

    half4 unity_SHAr;
    half4 unity_SHAg;
    half4 unity_SHAb;
    half4 unity_SHBr;
    half4 unity_SHBg;
    half4 unity_SHBb;
    half4 unity_SHC;

球谐光照参数

half3 unity_LightColor0, unity_LightColor1, unity_LightColor2, unity_LightColor3;
老旧unity的参数(5之前),获取四个光源的颜色。

阴影方面

    float4 unity_ShadowSplitSpheres[4];
    float4 unity_ShadowSplitSqRadii;
    float4 unity_LightShadowBias;
    float4 _LightSplitsNear;

    float4 _LightSplitsFar;

 

 float4x4 unity_World2Shadow[4];

position点从世界坐标转到阴影空间,通常用来计算阴影坐标Shadow coordinate

half4 _LightShadowData;
float4 unity_ShadowFadeCenterAndType;

#define _World2Shadow unity_World2Shadow[0]
#define _World2Shadow1 unity_World2Shadow[1]
#define _World2Shadow2 unity_World2Shadow[2]
#define _World2Shadow3 unity_World2Shadow[3]

同上

Camera绘制参数

    float4x4 glstate_matrix_mvp;
    float4x4 glstate_matrix_modelview0;
    float4x4 glstate_matrix_invtrans_modelview0;
    #define UNITY_MATRIX_MVP glstate_matrix_mvp

model物体空间 ->view视空间->projection投影空间转换矩阵

#define UNITY_MATRIX_MV glstate_matrix_modelview0

model物体空间 ->view视空间转换矩阵

#define UNITY_MATRIX_IT_MV glstate_matrix_invtrans_modelview0

model物体空间 ->view视空间矩阵的逆转置矩阵

uniform float4x4 _Object2World;

物体空间转世界空间

uniform float4x4 _World2Object;
世界空间转物体空间

uniform float4 unity_LODFade;
在unity5的lod可以选择fade mode,平滑改变lod等级。x 是 fade值,在[0,1]范围内。y是x量子化为16个级别,具体可以看官方文档
其中说明:
From
Unity 5, you can choose Fade Mode for each LOD level. The fading is
used to “blend” two neighboring LODs to achieve a smooth transition
effect. However Unity doesn’t provide a default built-in technique to
blend LOD geometries. You need to implement your own technique according
to your game type and asset production pipeline. Unity calculates a
“blend factor” from the object’s screen size and passes it to your
shader.
计算混合因子blend factor的两种方式之一
The blend factor is
accessed as the unity_LODFade.x uniform variable in your shader program.
Either keyword LOD_FADE_PERCENTAGE orLOD_FADE_CROSSFADE will be chosen
for objects rendered with LOD fading.
窥探Unity5渲染内部之解析UnityShaderVariables.cginc
 
unity5比4多了 fade mode,也就是让lod级数变化得更平滑,与上面的参数相关

关于lod group 在Component->Rendering->LOD Group中,需要你建立几个不同细节的网格,距离可调,具体:http://blog.csdn.net/mfc11/article/details/9146625
博主试了一下,效果拔群

窥探Unity5渲染内部之解析UnityShaderVariables.cginc

窥探Unity5渲染内部之解析UnityShaderVariables.cginc


float4x4 glstate_matrix_transpose_modelview0;
 #define UNITY_MATRIX_T_MV glstate_matrix_transpose_modelview0

UNITY_MATRIX_T_MV宏 本质为OpenGL state(glstate_matrix_transpose_modelview0) ,model物体空间 ->view视空间转换矩阵的逆转置矩阵

Camera每帧参数


    float4x4 glstate_matrix_projection;
    fixed4     glstate_lightmodel_ambient;
    #define UNITY_MATRIX_P glstate_matrix_projection

UNITY_MATRIX_P宏 本质为OpenGL state(glstate_matrix_projection) ,model物体空间 -> projection投影空间转换矩阵

 #define UNITY_LIGHTMODEL_AMBIENT (glstate_lightmodel_ambient * 2)
环境光颜色,为glstate_lightmodel_ambient * 2

    float4x4 unity_MatrixV;
    float4x4 unity_MatrixVP;
    #define UNITY_MATRIX_V unity_MatrixV

model物体空间->view视空间转换矩阵

 #define UNITY_MATRIX_VP unity_MatrixVP

view视空间 -> projection投影空间转换矩阵

    fixed4 unity_AmbientSky;
    fixed4 unity_AmbientEquator;
    fixed4 unity_AmbientGround;

在unity wiki 上的解释:
In
Unity, a uniform ambient light is specified by choosing Window >
Lighting > Scene from the main menu, setting Ambient Source to Color
and specifying the Ambient Color. In a Cg shader in Unity, this color is
then available as UNITY_LIGHTMODEL_AMBIENT, which is one of the
pre-defined uniforms mentioned in Section “Shading in World Space”. (If
you choose Gradient instead of Color then UNITY_LIGHTMODEL_AMBIENT and
unity_AmbientSky specify the Sky Color, while the Equator Color and the
Ground Color are specified by unity_AmbientEquator and
unity_AmbientGround.)

窥探Unity5渲染内部之解析UnityShaderVariables.cginc

窥探Unity5渲染内部之解析UnityShaderVariables.cginc

窥探Unity5渲染内部之解析UnityShaderVariables.cginc


上图所示unity的默认环境光是从skybox来的,可以设为gradient梯度,就分为Sky天空; Equator地平线;
Ground地;三种颜色梯度,上面的参数获取的就是这个颜色。再有一种环境光就是固定color,老版本的untiy就是固定color的环境光

雾参数

注意:只对unity的雾有效,自己加的post prossing 是无效的。

uniform fixed4 unity_FogColor;
雾颜色

uniform float4 unity_FogParams;
雾参数,x = 密度 / sqrt(ln(2)),对Exp2模式有效(fog面板参数可调模式)
y = 密度 / ln(2) ,对Exp模式有效
z = -1/(末端-始端), 对Linear模式有效
w =末端/(末端-始端), 对Linear模式有效

Lightmap

// Main lightmap
UNITY_DECLARE_TEX2D(unity_Lightmap);
// Dual or directional lightmap (always used with unity_Lightmap, so can share sampler)
UNITY_DECLARE_TEX2D_NOSAMPLER(unity_LightmapInd);

// Dynamic GI lightmap
UNITY_DECLARE_TEX2D(unity_DynamicLightmap);
UNITY_DECLARE_TEX2D_NOSAMPLER(unity_DynamicDirectionality);
UNITY_DECLARE_TEX2D_NOSAMPLER(unity_DynamicNormal);

float4 unity_LightmapST;
    float4 unity_DynamicLightmapST;

Reflection Probes

UNITY_DECLARE_TEXCUBE(unity_SpecCube0);
UNITY_DECLARE_TEXCUBE(unity_SpecCube1);

float4 unity_SpecCube0_BoxMax;
    float4 unity_SpecCube0_BoxMin;
    float4 unity_SpecCube0_ProbePosition;
    half4  unity_SpecCube0_HDR;

float4 unity_SpecCube1_BoxMax;
    float4 unity_SpecCube1_BoxMin;
    float4 unity_SpecCube1_ProbePosition;

half4  unity_SpecCube1_HDR;

两个reflection probes的参数

矩阵类参数

#define UNITY_MATRIX_TEXTURE0 float4x4(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1)
#define UNITY_MATRIX_TEXTURE1 float4x4(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1)
#define UNITY_MATRIX_TEXTURE2 float4x4(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1)

#define UNITY_MATRIX_TEXTURE3 float4x4(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1)

已经过时了但还可以使用
There
used to be fixed function-like texture matrices, defined as
UNITY_MATRIX_TEXTUREn. These are gone now; and are just defined to
identity.

另外博主明年上半年就要实习了,,,求工作+指点。。太感谢  O(∩_∩)O

之后还会写别的内部cginc,未完待续

              —-by wolf96

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,909
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,434
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,249
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,060
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,692
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,730