窺探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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM