Unity Shader內建函式
內建shader輔助函式定義在UnityCG.cginc檔案中
頂點轉換函式:
Function: |
Description: |
float4 UnityObjectToClipPos(float3 pos) |
Transforms a point from object space to the camera’s clip space in homogeneous coordinates. This is the equivalent of mul(UNITY_MATRIX_MVP, float4(pos, 1.0)), and should be used in its place. homogeneous coordinates:齊次座標 等價於:mul(UNITY_MATRIX_MVP, float4(pos, 1.0)), |
float3 UnityObjectToViewPos(float3 pos) |
Transforms a point from object space to view space. This is the equivalent of mul(UNITY_MATRIX_MV, float4(pos, 1.0)).xyz, and should be used in its place. 等價於:mul(UNITY_MATRIX_MV, float4(pos, 1.0)). |
Generic helper functions(定義在UnityCG.cginc檔案中)
Function: |
Description: |
float3 WorldSpaceViewDir (float4 v) |
Returns world space direction (not normalized) from given object space vertex position towards the camera .(引數是object space下的頂點座標,取得world space下指向攝像機的方向,即視角方向) |
float3 ObjSpaceViewDir (float4 v) |
Returns object space direction (not normalized) from given object space vertex position towards the camera. (同上,不過取到的視角方向是在object space上的) |
float2 ParallaxOffset (half h, half height, half3 viewDir) |
calculates UV offset for parallax normal mapping. 為視差法線貼圖計算UV偏移 |
fixed Luminance (fixed3 c) |
Converts color to luminance (grayscale). 將顏色轉換為亮度(灰度) |
fixed3 DecodeLightmap (fixed4 color) |
Decodes color from Unity lightmap (RGBM or dLDR depending on platform). 從烘焙貼圖解碼,烘焙貼圖生成的是EXR格式的HDR貼圖,根據不同平臺返回RGBM或dLDR |
float4 EncodeFloatRGBA (float v) |
Encodes [0..1) range float into RGBA color, for storage in low precision render target. 把float編碼到RGBA8 |
Decodes RGBA color into a float.上面方法的反編 |
|
float2 EncodeFloatRG (float v) |
Encodes [0..1) range float into a float2. 編碼 [0.0,1.0)float--> float2 |
Decodes a previously-encoded RG float. float2解碼--> [0.0,1.0) |
|
float2 EncodeViewNormalStereo (float3 n) |
Encodes view space normal into two numbers in 0..1 range. 視空間法線(float3)編碼到float2 |
float3 DecodeViewNormalStereo (float4 enc4) |
Decodes view space normal from enc4.xy. 上面方法的解碼,只使用引數的xy值 |
Forwardrendering helper functions in UnityCG.cginc
These functions are only useful when using forward rendering(ForwardBase or ForwardAdd pass types).
僅用於前向渲染
Function: |
Description: |
|
Computes world space direction (not normalized) to light, given object space vertex position. 引數是object space下的頂點座標,取得world space下指向光源的方向 |
|
Computes object space direction (not normalized) to light, given object space vertex position. 引數是object space下的頂點座標,取得object space下指向光源的方向 |
|
Computes illumination from four point lights, with light data tightly packed into vectors. Forward rendering uses this to compute per-vertex lighting. 正向渲染中,最多有4個點光源會以逐頂點渲染的方式被計算。 |
Vertex-lithelper functions in UnityCG.cginc
These functionsare only useful when using per-vertex lit shaders (“Vertex” pass type).
僅用於per-vertex lit shaders
Function: |
Description: |
|
Computes illumination from four per-vertex lights and ambient, given object space position & normal. 引數為頂點跟法線,根據四個逐頂點光源跟環境光計算光照 |
mul(UNITY_MATRIX_MVP,v)跟ComputeScreenPos的區別
一個是model position->projection position 投影座標 一個是projection position->screen position...螢幕座標
投影座標系->螢幕座標系這是最簡單的。2D座標變換。也不多說。
使用例子:
o.position = mul(UNITY_MATRIX_MVP, v.vertex);
o.proj0 = ComputeScreenPos(o.position);
COMPUTE_EYEDEPTH(o.proj0.z);
有關深度的一些方法整理:
UNITY_TRANSFER_DEPTH(o):計算eye space的深度值,並寫入變數o(float2)。
當需要渲染到一張深度貼圖時,在vertex shader中使用該函式。
UNITY_OUTPUT_DEPTH(i): returns eye space depth from i (which must be a float2). Use it in a fragment programwhen rendering into a depth texture.
示例,製作深度貼圖用:
Shader"Render Depth" {
SubShader{
Tags { "RenderType"="Opaque"}
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include"UnityCG.cginc"
structv2f {
float4 pos : SV_POSITION;
float2 depth : TEXCOORD0;
}
v2fvert (appdata_base v) {
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
UNITY_TRANSFER_DEPTH(o.depth);
return o;
}
half4frag(v2f i) : SV_Target {
UNITY_OUTPUT_DEPTH(i.depth);
}
ENDCG
}
}
}
COMPUTE_EYEDEPTH(o):computes eye spacedepth of the vertex and outputs it in o. Use it in a vertexprogram when not rendering into adepth texture
實現程式碼#define COMPUTE_EYEDEPTH(o) o =-mul( UNITY_MATRIX_MV, v.vertex ).z設定o為當前頂點視空間的z值,在vertex 函式中使用
DECODE_EYEDEPTH(i):given high precision value from depth texture i, returns corresponding eye space depth. This macro just returnsi*FarPlane on Direct3D. On platforms withnative depth textures it linearizes and expands the value to match camera’s range.
返回深度紋理值在眼空間的深度。i為從深度紋理中取樣得到的值
z-buffer值不是線性的。
opengl 中是一個 0-1的值。0近1遠,
directx中是一個-1到1 的值
Linear01Depth引數是0-1的深度值,可得最後計算結果為z/f;就是說,是將[0, f]對映到[0,1]空間,若要對映[n, f]到[0, 1]則是(z-n)/(f-n)。
LinearEyeDepth中引數是0-1的深度值,可得最後計算結果為z。很明顯,LinearEyeDepth是將經過透視投影變換的深度值還原了。DECODE_EYEDEPTH跟該函式實現一樣