窥探Unity5渲染内部之解析UnityShaderVariables.cginc
unity5的UnityShaderVariables.cginc比unity4大了1kb
这里装着unity shader 大部分内部参数,写这个方便以后自己查询
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比4多了 fade mode,也就是让lod级数变化得更平滑,与上面的参数相关
关于lod group 在Component->Rendering->LOD Group中,需要你建立几个不同细节的网格,距离可调,具体:http://blog.csdn.net/mfc11/article/details/9146625
博主试了一下,效果拔群
,
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.)
如
上图所示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
窥探Unity5渲染内部之解析UnityShaderVariables.cginc的更多相关文章
- react渲染原理深度解析
https://mp.weixin.qq.com/s/aM-SkTsQrgruuf5wy3xVmQ 原文件地址 [第1392期]React从渲染原理到性能优化(二)-- 更新渲染 黄琼 前端早读课 ...
- Vue源码学习(零):内部原理解析
本篇文章是在阅读<剖析 Vue.js 内部运行机制>小册子后总结所得,想要了解详细内容,请参考原文:https://juejin.im/book/5a36661851882538e2259 ...
- JSON转Model内部实现解析
一.思路: 1.通过模型类型获得所有的属性和其类型 2.对获得的json进行处理.类型处理 3.考虑字典键值和模型属性名不一致的情况 4.添加code用于归档 5.补充JSON转字典.字典转JSON. ...
- new操作符的内部运行解析
在加上new操作符,我们就能完成传统面向对象的class + new的方式创建对象,在Javascript中,我们将这类方式成为Pseudoclassical. 基于上面的例子,我们执行如下代码 ...
- Java反射学习总结四(动态代理使用实例和内部原理解析)
通过上一篇文章介绍的静态代理Java反射学习总结三(静态代理)中,大家可以发现在静态代理中每一个代理类只能为一个接口服务,这样一来必然会产生过多的代理,而且对于每个实例,如果需要添加不同代理就要去添加 ...
- 微信小程序--后台交互/wx.request({})方法/渲染页面方法 解析
小程序的后台获取数据方式get/post具体函数格式如下:wx.request({}) data: { logs:[] }, onLoad:function(){ this.getdata(); } ...
- 浏览器是怎样工作的:渲染引擎,HTML解析
渲染引擎 渲染引擎的职责是……渲染,也就是把请求的内容显示到浏览器屏幕上. 默认情况下渲染引擎可以显示HTML,XML文档以及图片. 通过插件(浏览器扩展)它可以显示其它类型文档.比如使用PDF vi ...
- 渲染引擎,HTML解析
这是how browser to work 的翻译 转自:携程设计委员会 渲染引擎 渲染引擎的职责是……渲染,也就是把请求的内容显示到浏览器屏幕上. 默认情况下渲染引擎可以显示HTML,XML文档以及 ...
- 浏览器是怎样工作的:渲染引擎,HTML解析(连载二)
转载自:http://ued.ctrip.com/blog/how-browsers-work-rendering-engine-html-parsing-series-ii.html 渲染引擎 渲染 ...
随机推荐
- (转)PHP获取今天、昨天、明天的日期
<?php echo "今天:".date("Y-m-d")."<br>"; echo "昨天:".d ...
- python查看网站的RTT
import requests time=0.0 jpserver=['jp1.herejump.com','jp1.herejump.com','jp1.herejump.com'] usserve ...
- java - String 浅谈
/** * String s1 = "a"; * 编译器会先检查常量池中是否已经有"a": * 如果没有,则在常量池先创建,后引用. * 如果有,则直接引用; ...
- 查看Safari和钥匙串中的密码
Safari Safari的同步书签功能很棒,还可以看到其他设备没关掉的网页.为了省时间,一些经常进的网站,比如博客,邮箱等,我都会选择让Safari保存密码,还使用iCloud同步!因为一直很放心苹 ...
- 三种C#.net生成静态页面的方法
ASP.NET生成静态页面方法主要有三种 第一种方法:向服务器的动态页面发送请求,获取页面的html代码.这种方法缺点显而易见:速度慢.另外如果请求的动态页面有验证控件的话,返回的html页面却无 ...
- [转] 属性选择器.mark
CSS 2 引入了属性选择器. 属性选择器可以根据元素的属性及属性值来选择元素. 简单属性选择 如果希望选择有某个属性的元素,而不论属性值是什么,可以使用简单属性选择器. 例子 1 如果您希望把包含标 ...
- js 的对象--如何定义一个对象
通过var object={} 对象字面量,可以叫对象直接量来自定义一个对象 对象自面量是一个表达式,这个表达式的每次运算都创建并初始化一个新对象.每次计算对象字面量的时候,也都会计算他的每个属性值 ...
- php接口开发--复制缩减Codeigniter的车轮
接口需求: 输出json 单一入口 安全 http://segmentfault.com/q/1010000000143852基于token验证?session? 缓存 session cookie ...
- 在 Sublime Text 3 中运行 PHP
参考http://segmentfault.com/blog/tony/1190000000395951 把php添加到环境变量 1.我的电脑->属性->高级系统设置->高级-> ...
- C语言中float,double类型,在内存中的结构(存储方式)
C语言中float,double类型,在内存中的结构(存储方式)从存储结构和算法上来讲,double和float是一样的,不一样的地方仅仅是float是32位的,double是64位的,所以doubl ...