窥探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 渲染引擎 渲染 ...
随机推荐
- mybatis中几种typeHandler的定义使用
1.存储到数据库, 将LONG数组转换成字符串;从数据库获取数据, 将字符串转为LONG数组 package com.winturn.utils.handler; import java.sql.Ca ...
- 浅谈Mamcached集成web项目
1.资源文件配置 config.properties 添加 #memcached服务器地址 memchchedIP=192.168.1.8 2.编写工具类 MemUtils package cn.co ...
- 用Markdown优雅的渲染我们的网页
认识 Markdown Markdown 是一种用来写作的轻量级「标记语言」,它用简洁的语法代替排版,而不像一般我们用的字处理软件 Word 或 Pages 有大量的排版.字体设置.它使我们专心于码字 ...
- sql脚本的格式
创建表前先判断是否存在 IF OBJECT_ID(N'TableDataDictionary') IS NULL 存储过程头:--=================================== ...
- Asp.Net MVC Ajax
将ASP.NET MVC中的form提交改为ajax提交 在ASP.NET MVC视图中通过 @using (Html.BeginForm()) 产生的是form表单提交代码,可以用javascrip ...
- C#当中的多线程_线程基础
前言 最近工作不是很忙,想把买了很久了的<C#多线程编程实战>看完,所以索性把每一章的重点记录一下,方便以后回忆. 第1章 线程基础 1.创建一个线程 using System; usin ...
- Android - NullPointerException
Android中的空指针,感觉出现率,比普通的java出现的概率多得多,今天我无意中发现的NullPointerException,还是想记录下来. 我们知道, 我们的控件一般定义在layout.xm ...
- 类库探源——System.Environment
Environment 类: 提供有关当前环境和平台的信息以及操作它们的方法.此类不能被继承. 命名空间: System 程序集: mscorlib.dll 继承关系: 常用属性(字段)和方法: ...
- 数据库学习(整理)----6--Oracle如何快速备份和多次备份数表数据
1.说明: 这里假设一种应用场景! 假设,银行系统中有大量的数据需要及时备份,如何才能快速高效呢! 条件需求: (1).不能设置同步锁(设置的会影响银行正常业务进行!使得银行系统处于维护状态,这是不 ...
- 简单的背包问题(入门)HDU2602 HDU2546 HDU1864
动态规划,我一直都不熟悉,因为体量不够,所以今天开始努力地学习学习. 当然背包从01开始,先选择了一个简单的经典的背包HDU2602. Many years ago , in Teddy's home ...