Shader内置函数(方便自己看)
一、內(nèi)置包含文件
Unity中有類似于C++的包含文件.cginc,在編寫Shader時我們可以使用#include指令把這些文件包含進來
這樣我們就可以使用Unity為我們提供的一些非常好用的函數(shù)、宏和變量。
例如:#include"UnityCG.cginc"
包含文件的位置:根目錄\Editor\Data\CGIncludes
知識點1:以下是Unity中常用包含文件:
文件名 描述
1、UnityCG.cginc 包含最常用的幫助函數(shù)、宏和結構體
2、UnityShaderVariables.cginc 在編譯Shader時,會被自動包含進來,包含了許多內(nèi)置的全局變量,如UNITY_MATRIX_MVP
3、Ligghting.cginc 包含了各種內(nèi)置光照模型,如果編寫SurfaceShader的話,會被自動包含進來
4、HLSLSurport.cginc 在編譯Shader時,會被自動包含進來,聲明了很多跨平臺編譯的宏和定義
Unity5.2引入了許多新的重要的包含文件,如UnityStandardBRDF.cginc等。這些文件用于實現(xiàn)基于物理的渲染
知識點2:UnityShader中常用的結構體
名稱 描述 包含的變量
appdata_base 用于頂點著色器輸入 頂點位置、頂點法線、第一組紋理坐標
appdata_tan 用于頂點著色器輸入 頂點位置、頂點切線、頂點法線、第一組紋理坐標
appdata_full 用于頂點著色器輸入 頂點位置、頂點切線、頂點法線、四組(或更多)紋理坐標
appdata_img 用于頂點著色器輸入 頂點位置、第一組紋理坐標
v2f_img 用于頂點著色器輸出 裁剪空間中的位置、紋理坐標
知識點3:UnityShader中常用的幫助函數(shù)
函數(shù)名 描 ? ?述
float3 WorldSpaceViewDir(float4 v) ? 輸入一個模型頂點坐標,得到世界空間中從該點到攝像機的觀察方向
float3 ObjSpaceViewDir(float4 v) ? 輸入一個模型頂點坐標,得到模型空間中從該點到攝像機的觀察方向
float3 WorldSpaceLightDir(float4 v) ??輸入一個模型頂點坐標,得到世界空間中從該點到光源的光照方向(方向沒有歸一化,且只可用于前向渲染)
float3 ObjSpaceLightDir(float4 v) ?輸入一個模型頂點坐標,得到模型空間中從該點到光源的光照方向(方向沒有歸一化,且只可用于前向渲染)
float3 UnityObjectToWorldNormal(float3 norm) 將法線從模型空間轉(zhuǎn)換到世界空間
float3 UnityObjectToWorldDir(in float3 dir) ??把方向矢量從模型空間轉(zhuǎn)換到世界空間
float3 UnityWorldToObjectDir(float3 dir) ? 把方向矢量從世界空間轉(zhuǎn)換到模型空間
知識點4:UnityShader中內(nèi)置變量
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Unity內(nèi)置變換矩陣
? ? ? ? ? ?變量名 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 描 ? ? ? ? 述
UNITY_MATRIX_MVP ? ??當前模型*觀察*投影矩陣,用于將模型頂點/方向矢量從模型空間轉(zhuǎn)換到裁剪空間
UNITY_MATRIX_MV ??當前模型*觀察矩陣,用于將模型頂點/方向矢量從模型空間轉(zhuǎn)換到觀察空間
UNITY_MATRIX_V ? 當前觀察矩陣,用于將頂點/方向矢量從世界空間變換到觀察空間
UNITY_MATRIX_P ? 當前投影矩陣,用于將頂點/方向矢量從觀察空間變換到裁剪空間
UNITY_MATRIX_VP 當前觀察*投影矩陣,用于將頂點/方向矢量從世界空間變換到裁剪空間
UNITY_MATRIX_T_MV ? UNITY_MATRIX_MV轉(zhuǎn)置矩陣
UNITY_MATRIX_IT_MV UNITY_MATRIX_MV逆轉(zhuǎn)置矩陣,可將法線矢量從模型空間轉(zhuǎn)換到觀察空間
_Object2World 當前模型的矩陣,用于將模型頂點/方向矢量從模型空間轉(zhuǎn)換到世界空間
_World2Object ? ? ? ? _Object2World逆矩陣,用于將模型頂點/方向矢量從世界空間轉(zhuǎn)換到模型空間
另外:Unity還提供了能夠訪問時間、光照、霧效和環(huán)境光等目的的變量。這些內(nèi)置變量大多UnityShaderVariables.cginc中,
跟光照有關的還定義在Lighting.cginc 和AutoLight.cginc中。
知識點5:
1、uint CreateShader(enum type) : 創(chuàng)建空的shader object;?
??type: VERTEX_SHADER,?
2、void ShaderSource(uint shader, sizeicount, const **string, const int *length):加載shader源碼進shader object;可能多個字符串?
3、void CompileShader(uint shader):編譯shader object;?
??shader object有狀態(tài) 表示編譯結果?
4、void DeleteShader( uint shader ):刪除 shader object;?
5、void ShaderBinary( sizei count, const uint *shaders,?
enum binaryformat, const void *binary, sizei length ): 加載預編譯過的shader 二進制串;?
6、uint CreateProgram( void ):創(chuàng)建空的program object, programe object組織多個shader object,成為executable;?
7、void AttachShader( uint program, uint shader ):關聯(lián)shader object和program object;?
8、void DetachShader( uint program, uint shader ):解除關聯(lián);?
9、void LinkProgram( uint program ):program object準備執(zhí)行,其關聯(lián)的shader object必須編譯正確且符合限制條件;?
10、void UseProgram( uint program ):執(zhí)行program object;?
11、void ProgramParameteri( uint program, enum pname,?
int value ): 設置program object的參數(shù);?
12、void DeleteProgram( uint program ):刪除program object;?
13、shader 變量的qualifier:?
???默認:無修飾符,普通變量讀寫, 與外界無連接;?
???const:常量 const vec3 zAxis = vec3(0.0, 0.0, 1.0);?
???attribute: 申明傳給vertex shader的變量;只讀;不能為array或struct;attribute vec4 position;?
???uniform: 表明整個圖元處理中值相同;只讀; uniform vec4 lightPos;?
???varying: 被差值;讀寫; varying vec3 normal;?
???in, out, inout;?
shader變量的精度:?
???highp, mediump, lowp?
shader內(nèi)置變量:?
???gl_Position: 用于vertex shader, 寫頂點位置;被圖元收集、裁剪等固定操作功能所使用;?
????????????????其內(nèi)部聲明是:highp vec4 gl_Position;?
???gl_PointSize: 用于vertex shader, 寫光柵化后的點大小,像素個數(shù);?
????????????????其內(nèi)部聲明是:mediump float gl_Position;?
???gl_FragColor: 用于Fragment shader,寫fragment color;被后續(xù)的固定管線使用;?
?????????????????mediump vec4 gl_FragColor;?
???gl_FragData: 用于Fragment shader,是個數(shù)組,寫gl_FragData[n] 為data n;被后續(xù)的固定管線使用;?
?????????????????mediump vec4 gl_FragData[gl_MaxDrawBuffers];?
???gl_FragColor和gl_FragData是互斥的,不會同時寫入;?
???gl_FragCoord: 用于Fragment shader,只讀, Fragment相對于窗口的坐標位置 x,y,z,1/w; 這個是固定管線圖元差值后產(chǎn)生的;z 是深度值; mediump vec4 gl_FragCoord;?
???gl_FrontFacing: 用于判斷 fragment是否屬于 front-facing primitive;只讀;?
???????????????????bool gl_FrontFacing; ??
???gl_PointCoord: 僅用于 point primitive; mediump vec2 gl_PointCoord;?
shader內(nèi)置常量:?
???const mediump int gl_MaxVertexAttribs = 8;?
???const mediump int gl_MaxVertexUniformVectors = 128;?
???const mediump int gl_MaxVaryingVectors = 8;?
???const mediump int gl_MaxVertexTextureImageUnits = 0;?
???const mediump int gl_MaxCombinedTextureImageUnits = 8;?
???const mediump int gl_MaxTextureImageUnits = 8;?
???const mediump int gl_MaxFragmentUnitformVectors = 16;?
???const mediump int gl_MaxDrawBuffers = 1;?
shader內(nèi)置數(shù)學函數(shù):?
???一般默認都用弧度;?
???radians(degree) : 角度變弧度;?
???degrees(radian) : 弧度變角度;?
???sin(angle), cos(angle), tan(angle)?
???asin(x): arc sine, 返回弧度 [-PI/2, PI/2];?
???acos(x): arc cosine,返回弧度 [0, PI];?
???atan(y, x): arc tangent, 返回弧度 [-PI, PI];?
???atan(y/x): arc tangent, 返回弧度 [-PI/2, PI/2];?
??
???pow(x, y): x的y次方;?
???exp(x): 指數(shù), log(x):?
???exp2(x): 2的x次方, log2(x):?
???sqrt(x): x的根號; inversesqrt(x): x根號的倒數(shù)?
??
???abs(x): 絕對值?
???sign(x): 符號, 1, 0 或 -1?
????{sign(x)或者Sign(x)叫做符號函數(shù),在數(shù)學和計算機運算中,其功能是取某個數(shù)的符號(正或負):?
????當x>0,sign(x)=1;?
????當x=0,sign(x)=0;?
????當x<0, sign(x)=-1;}????floor(x): 底部取整?
???ceil(x): 頂部取整?
???fract(x): 取小數(shù)部分?
???mod(x, y): 取模, x - y*floor(x/y)?
???min(x, y): 取最小值?
???max(x, y): 取最大值?
???clamp(x, min, max): ?min(max(x, min), max);?
???mix(x, y, a): x, y的線性混疊, x(1-a) + y*a;?
???step(edge, x): 如 x?
???smoothstep(edge0, edge1, x): threshod ?smooth transition時使用。 edge0<=edge0時為0.0, x>=edge1時為1.0?
??
???length(x): 向量長度?
???distance(p0, p1): 兩點距離, length(p0-p1);?
???dot(x, y): 點積,各分量分別相乘 后 相加?
???cross(x, y): 差積,x[1]*y[2]-y[1]*x[2], x[2]*y[0] - y[2]*x[0], x[0]*y[1] - y[0]*x[1]?
???normalize(x): 歸一化, length(x)=1;?
???faceforward(N, I, Nref): 如 dot(Nref, I)< 0則N, 否則 -N?
???reflect(I, N): I的反射方向, I -2*dot(N, I)*N, N必須先歸一化?
???refract(I, N, eta): 折射,k=1.0-eta*eta*(1.0 - dot(N, I) * dot(N, I)); 如k<0.0 則0.0,否則 eta*I - (eta*dot(N, I)+sqrt(k))*N?
??
???matrixCompMult(matX, matY): 矩陣相乘, 每個分量 自行相乘, 即 r[j] = x[j]*y[j];?
??????????????????????????????矩陣線性相乘,直接用 *?
????
???lessThan(vecX, vecY): 向量 每個分量比較 x < y?
???lessThanEqual(vecX, vecY): 向量 每個分量比較 x<=y?
???greaterThan(vecX, vecY): 向量 每個分量比較 x>y?
???greaterThanEqual(vecX, vecY): 向量 每個分量比較 x>=y?
???equal(vecX, vecY): 向量 每個分量比較 x==y?
???notEqual(vecX, vexY): 向量 每個分量比較 x!=y?
???any(bvecX): 只要有一個分量是true, 則true?
???all(bvecX): 所有分量是true, 則true?
???not(bvecX): 所有分量取反?
??
???texture2D(sampler2D, coord): texture lookup?
???texture2D(sampler2D, coord, bias): LOD bias, mip-mapped texture?
???texture2DProj(sampler2D, coord):?
???texture2DProj(sampler2D, coord, bias):?
???texture2DLod(sampler2D, coord, lod):?
???texture2DProjLod(sampler2D, coord, lod):?
???textureCube(samplerCube, coord):?
???textureCube(samplerCube, coord, bias):?
???textureCubeLod(samplerCube, coord, lod):
?
Intrinsic Functions (DirectX HLSL)
The following table lists the intrinsic functions available in HLSL. Each function has a brief description, and a link to a reference page that has more detail about the input argument and return type.
| abs | abs(x) | Absolute value (per component). |
| acos | acos(x) | Returns the arccosine of each component of x. |
| all | all(x) | Test if all components of x are nonzero. |
| any | any(x) | Test if any component of x is nonzero. |
| asfloat | asfloat(x) | Convert the input type to a float. |
| asin | asin(x) | Returns the arcsine of each component of x. |
| asint | asint(x) | Convert the input type to an integer. |
| asuint | asuint(x) | Convert the input type to an unsigned integer. |
| atan | atan(x) | Returns the arctangent of x. |
| atan2 | atan2(y, x) | Returns the arctangent of of two values (x,y). |
| ceil | ceil(x) | Returns the smallest integer which is greater than or equal to x. |
| clamp | clamp(x, min, max) | Clamps x to the range [min, max]. |
| clip | clip(x) | Discards the current pixel, if any component of x is less than zero. |
| cos | cos(x) | Returns the cosine of x. |
| cosh | cosh(x) | Returns the hyperbolic cosine of x. |
| cross | cross(x, y) | Returns the cross product of two 3D vectors. |
| D3DCOLORtoUBYTE4 | D3DCOLORtoUBYTE4(x) | Swizzles and scales components of the 4D vector x to compensate for the lack of UBYTE4 support in some hardware. |
| ddx | ddx(x) | Returns the partial derivative of x with respect to the screen-space x-coordinate. |
| ddy | ddy(x) | Returns the partial derivative of x with respect to the screen-space y-coordinate. |
| degrees | degrees(x) | Converts x from radians to degrees. |
| determinant | determinant(m) | Returns the determinant of the square matrix m. |
| distance | distance(x, y) | Returns the distance between two points. |
| dot | dot(x, y) | Returns the dot product of two vectors. |
| exp | exp(x) | Returns the base-e exponent. |
| exp2 | exp2(x) | Base 2 exponent (per component). |
| faceforward | faceforward(n, i, ng) | Returns -n * sign(?(i, ng)). |
| floor | floor(x) | Returns the greatest integer which is less than or equal to x. |
| fmod | fmod(x, y) | Returns the floating point remainder of x/y. |
| frac | frac(x) | Returns the fractional part of x. |
| frexp | frexp(x, exp) | Returns the mantissa and exponent of x. |
| fwidth | fwidth(x) | Returns abs(ddx(x)) + abs(ddy(x)) |
| GetRenderTargetSampleCount | GetRenderTargetSampleCount() | Returns the number of render-target samples. |
| GetRenderTargetSamplePosition | GetRenderTargetSamplePosition(x) | Returns a sample position (x,y) for a given sample index. |
| isfinite | isfinite(x) | Returns true if x is finite, false otherwise. |
| isinf | isinf(x) | Returns true if x is +INF or -INF, false otherwise. |
| isnan | isnan(x) | Returns true if x is NAN or QNAN, false otherwise. |
| ldexp | ldexp(x, exp) | Returns x * 2exp |
| length | length(v) | Returns the length of the vector v. |
| lerp | lerp(x, y, s) | Returns x + s(y - x). |
| lit | lit(n ? l, n ? h, m) | Returns a lighting vector (ambient, diffuse, specular, 1) |
| log | log(x) | Returns the base-e logarithm of x. |
| log10 | log10(x) | Returns the base-10 logarithm of x. |
| log2 | log2(x) | Returns the base-2 logarithm of x. |
| max | max(x, y) | Selects the greater of x and y. |
| min | min(x, y) | Selects the lesser of x and y. |
| modf | modf(x, out ip) | Splits the value x into fractional and integer parts. |
| mul | mul(x, y) | Performs matrix multiplication using x and y. |
| noise | noise(x) | Generates a random value using the Perlin-noise algorithm. |
| normalize | normalize(x) | Returns a normalized vector. |
| pow | pow(x, y) | Returns xy. |
| radians | radians(x) | Converts x from degrees to radians. |
| reflect | reflect(i, n) | Returns a reflection vector. |
| refract | refract(i, n, R) | Returns the refraction vector. |
| round | round(x) | Rounds x to the nearest integer |
| rsqrt | rsqrt(x) | Returns 1 / sqrt(x) |
| saturate | saturate(x) | Clamps x to the range [0, 1] |
| sign | sign(x) | Computes the sign of x. |
| sin | sin(x) | Returns the sine of x |
| sincos | sincos(x, out s, out c) | Returns the sine and cosine of x. |
| sinh | sinh(x) | Returns the hyperbolic sine of x |
| smoothstep | smoothstep(min, max, x) | Returns a smooth Hermite interpolation between 0 and 1. |
| sqrt | sqrt(x) | Square root (per component) |
| step | step(a, x) | Returns (x >= a) ? 1 : 0 |
| tan | tan(x) | Returns the tangent of x |
| tanh | tanh(x) | Returns the hyperbolic tangent of x |
| tex1D | tex1D(s, t) | 1D texture lookup. |
| tex1Dbias | tex1Dbias(s, t) | 1D texture lookup with bias. |
| tex1Dgrad | tex1Dgrad(s, t, ddx, ddy) | 1D texture lookup with a gradient. |
| tex1Dlod | tex1Dlod(s, t) | 1D texture lookup with LOD. |
| tex1Dproj | tex1Dproj(s, t) | 1D texture lookup with projective divide. |
| tex2D | tex2D(s, t) | 2D texture lookup. |
| tex2Dbias | tex2Dbias(s, t) | 2D texture lookup with bias. |
| tex2Dgrad | tex2Dgrad(s, t, ddx, ddy) | 2D texture lookup with a gradient. |
| tex2Dlod | tex2Dlod(s, t) | 2D texture lookup with LOD. |
| tex2Dproj | tex2Dproj(s, t) | 2D texture lookup with projective divide. |
| tex3D | tex3D(s, t) | 3D texture lookup. |
| tex3Dbias | tex3Dbias(s, t) | 3D texture lookup with bias. |
| tex3Dgrad | tex3Dgrad(s, t, ddx, ddy) | 3D texture lookup with a gradient. |
| tex3Dlod | tex3Dlod(s, t) | 3D texture lookup with LOD. |
| tex3Dproj | tex3Dproj(s, t) | 3D texture lookup with projective divide. |
| texCUBE | texCUBE(s, t) | Cube texture lookup. |
| texCUBEbias | texCUBEbias(s, t) | Cube texture lookup with bias. |
| texCUBEgrad | texCUBEgrad(s, t, ddx, ddy) | Cube texture lookup with a gradient. |
| texCUBElod | tex3Dlod(s, t) | Cube texture lookup with LOD. |
| texCUBEproj | texCUBEproj(s, t) | Cube texture lookup with projective divide. |
| transpose | transpose(m) | Returns the transpose of the matrix m. |
| trunc | trunc(x) | Truncates floating-point value(s) to integer value(s) |
?
表 3-1 HLSL內(nèi)置函數(shù)
| 函數(shù)名?????????? ?用法 |
abs???????????????????????? 計算輸入值的絕對值。
acos??????????????????????? 返回輸入值反余弦值。
all???????????????????????????測試非0值。
any???????????????????????? 測試輸入值中的任何非零值。
asin???????????????????????? 返回輸入值的反正弦值。
atan??????????????????????? 返回輸入值的反正切值。
atan2?????????????????????? 返回y/x的反正切值。
ceil???????????????????????? 返回大于或等于輸入值的最小整數(shù)。
clamp????????????????????? 把輸入值限制在[min, max]范圍內(nèi)。
clip???????????????????????? 如果輸入向量中的任何元素小于0,則丟棄當前像素。
cos???????????????????????? 返回輸入值的余弦。
cosh?????????????????????? 返回輸入值的雙曲余弦。
cross????????????????????? 返回兩個3D向量的叉積。
ddx???????????????????????? 返回關于屏幕坐標x軸的偏導數(shù)。
ddy???????????????????????? 返回關于屏幕坐標y軸的偏導數(shù)。
degrees?????????????????? 弧度到角度的轉(zhuǎn)換
determinant????????????? 返回輸入矩陣的值。
distance?????????????????? 返回兩個輸入點間的距離。
dot????????????????????????? 返回兩個向量的點積。
exp???????????????????????? 返回以e為底數(shù),輸入值為指數(shù)的指數(shù)函數(shù)值。
exp2?????????????????????? 返回以2為底數(shù),輸入值為指數(shù)的指數(shù)函數(shù)值。
faceforward???????????? 檢測多邊形是否位于正面。
floor?????????????????????? 返回小于等于x的最大整數(shù)。
fmod?????????????????????? 返回a / b的浮點余數(shù)。
frac??????????????????????? 返回輸入值的小數(shù)部分。
frexp?????????????????????? 返回輸入值的尾數(shù)和指數(shù)
fwidth???????????????????? 返回 abs ( ddx (x) + abs ( ddy(x))。
isfinite???????????????????? 如果輸入值為有限值則返回true,否則返回false。
isinf??????????????????????? 如何輸入值為無限的則返回true。
isnan?????????????????????? 如果輸入值為NAN或QNAN則返回true。
ldexp?????????????????????? frexp的逆運算,返回 x * 2 ^ exp。
len / lenth??????????????? 返回輸入向量的長度。
lerp???????????????????????? 對輸入值進行插值計算。
lit??????????????????????????? 返回光照向量(環(huán)境光,漫反射光,鏡面高光,1)。
log????????????????????????? 返回以e為底的對數(shù)。
log10????????????????????? 返回以10為底的對數(shù)。
log2??????????????????????? 返回以2為底的對數(shù)。
max??????????????????????? 返回兩個輸入值中較大的一個。
min???????????????????????? 返回兩個輸入值中較小的一個。
modf?????????????????????? 把輸入值分解為整數(shù)和小數(shù)部分。
mul???????????????????????? 返回輸入矩陣相乘的積。
normalize???????????????? 返回規(guī)范化的向量,定義為 x / length(x)。
pow??????????????????????? 返回輸入值的指定次冪。
radians??????????????????? 角度到弧度的轉(zhuǎn)換。
reflect???????????????????? 返回入射光線i對表面法線n的反射光線。
refract???????????????????? 返回在入射光線i,表面法線n,折射率為eta下的折射光線v。
round????????????????????? 返回最接近于輸入值的整數(shù)。
rsqrt?????????????????????? 返回輸入值平方根的倒數(shù)。
saturate?????????????????? 把輸入值限制到[0, 1]之間。
sign??????????????????????? 計算輸入值的符號。
sin????????????????????????? 計算輸入值的正弦值。
sincos???????????????????? 返回輸入值的正弦和余弦值。
sinh??????????????????????? 返回x的雙曲正弦。
smoothstep????????????? 返回一個在輸入值之間平穩(wěn)變化的插值。
sqrt???????????????????????? 返回輸入值的平方根。
step??????????????????????? 返回(x >= a)? 1 : 0。
tan????????????????????????? 返回輸入值的正切值。
fanh??????????????????????? 返回輸入值的雙曲線切線。
transpose???????????????? 返回輸入矩陣的轉(zhuǎn)置。
tex1D*??????????????????? 1D紋理查詢。
tex2D*??????????????????? 2D紋理查詢。
tex3D*??????????????????? 3D紋理查詢。
texCUBE*??????????????? 立方紋理查詢。
總結
以上是生活随笔為你收集整理的Shader内置函数(方便自己看)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java虚拟机学习笔记
- 下一篇: Docker实现odoo部署