[Unity3D self-study record] Unity Shader common function list

CG  standard function library 
   Similar to  C's standard function library, Cg provides a set of built-in standard functions. These functions are used to perform mathematical general calculations or general algorithms (texture mapping, etc.). For example, if you need to obtain the reflected light direction vector of the incident light, you can use the reflect function in the standard function library, you can use the refract function to obtain the refracted light direction vector, and you can use the mul function when doing matrix multiplication.
   Some functions directly correspond to GPU instructions, so the execution efficiency is very high. Most of the standard functions are overloaded to support arrays and vectors of different lengths as input parameters.



     Cg  standard functions will be continuously optimized with the development of future  GPU  hardware, so programs written based on the standard function library can be used on future  GPU  hardware.  The Cg  standard function library is mainly divided into five parts:  
    mathematical functions ( Mathematical Functions )
      geometric functions (Geometric Functions) ,       texture mapping functions (Texture Map Functions), partial       derivative functions (Derivative Functions), debugging functions (Debugging       Function ) 
 
 


and mathematical functions (Mathematical Functions )
    . operation function. These functions are all overloaded to support scalar data and vectors of different lengths as input parameters.

 Function function abs(x) returns the absolute value of the input parameter acos(x)  arc cotangent function, the input parameter range is [-1,1], returns the angle value all(x)   in the interval [0,π]                                                                                                    

                                                                   
                                                                 
                                                                   Returns true if none of the input arguments are 0; otherwise returns flase. The && operation
any(x) returns true as long as one of the input parameters is not 0. ||Operate asin(x)  arc sine function, the input parameter range is [−1,1], and the return angle value range is [−π/2 , π/2 ] atan(x) arc tangent  function   , the return angle value range is⎡−π/2 ,π/ atan2(y,x)   calculates the arc tangent value of y/x. In fact, it has exactly the same function as the atan(x) function, at least the input parameters are different. atan(x) = atan2(x, float(1)) ceil(x)  rounds up the input argument. For example:ceil(float(1.3)) whose return value is 2.0 clamp(x,a,b) returns aif the value of a; if                                                                    
                                                              
                                                              
                                                         
                                                               
                                                        If x  is greater than  b , return  b ; otherwise, return  x
cos(x) returns  x  in radians                                                                  的余弦值。返回值范围为 [ 1,1 ]
osh(x)                                                                  双曲余弦( hyperbolic cosine )函数,计算  的双曲余弦值
cross(A,B)                                                            返回两个三元向量的叉积 (cross product) 。注意,输入参数必须是三元向量
degrees(x)                                                           输入参数为弧度值 (radians) ,函数将其转换为角度值 (degrees)
determinant(m)                                                  计算矩阵的行列式因子
dot(A,B)                                                             返回  和  的点积 (dot product) 。参数  和  可以是标量,也可以是向量(输入参数方面,点

                               
                                                                  积和叉积函数有很大不同)
exp(x)                                                                      计算e的值,e= 2.71828182845904523536
exp2(x)                                                                    计算2的值
floor(x)                                                          对输入参数向下取整。例如floor(float(1.3)) 返回的值为 1.0;但是 floor(float(-1.3))返回的值为-2.0。
fmod(x,y)                                                       返回  x/y  的余数。如果  为  0 ,结果不可预料  
frexp(x, out exp)               将浮点数 x 分解为尾数和指数,即 x = m* 2^exp,返回 m,并将指数存入 exp 中;如果 x 为 0,则尾数和指数都返回 0 
frac(x)                                                                    Returns the fractional portion of a scalar or each vector component 
isfinite(x)                        判断标量或者向量中的每个数据是否是有限数,如果是返回 true;否则返回false;无限的或者非数据(not-a-number NaN)
isinf(x)                           判断标量或者向量中的每个数据是否是无限,如果是返回  true ;否则返回 false; 
isnan(x)                          判断标量或者向量中的每个数据是否是非数据(not-a-number NaN),如果是返回 true;否则返回 false;
ldexp(x, n)                                     计算x∗2的值 
lerp(a, b, f)                             计算(1− )∗ + ∗a b   f 或者a∗ −(b a)的值。即在下限 a 和上限 b 之间进行插值,f 表示权值。注意,如果 a 和 b 是向 

                                        量,则权值 f 必须是标量或者等长的向量。


lit(NdotL, NdotH, m)         N 表示法向量;L 表示入射光向量;H 表示半角向量;m 表示高光系数。 

                                     函数计算环境光、散射光、镜面光的贡献,返回的 4 元向量: 

                                         1.位表示环境光的贡献,总是 1.0;
                                      2.
位代表镜面光的贡献,如果 N •L <0,则为0;否则为  N •L;
                                           3.位代表镜面光的贡献,如果
N •L<0或者  N •H <0 ,则位 0;否则为(N •H); 
                                      W 位始终位 1.0 
log(x)                     计算ln(x)的值,x 必须大于 0 
 log2(x)                计算log(2x的值,x 必须大于 0
log10(x)             计算log10(x的值,x 必须大于 0
max(a, b)           比较两个标量或等长向量元素,返回 大值
min(a,b)            比较两个标量或等长向量元素,返回 小值

      modf(x, out ip) 
      
      mul(M, N) 
      
计算两个矩阵相乘,如果 为 AxB 阶矩阵,为 BxC阶矩阵,则返回 AxC 阶矩阵。下面两个函数为其重载函数。
      mul(M, v)
      计算矩阵和向量相乘
      mul(v, M)
      计算向量和矩阵相乘 
      noise( x) 
      噪声函数,返回值始终在 0之间;对于同样的输入,始终返回相同的值(也就是说,并不是真正意义上的随机噪声)。
      pow(x, y) 
      
      radians(x)
      函数将角度值转换为弧度值 
      round(x)
      Round-to-nearest,或 closest integer to x 即四舍五入
      rsqrt(x) 
      的反平方根,必须大于 
      saturate(x) 
      
如果 小于 0,返回 0;如果 大于 1,返回1;否则,返回 x
      sign(x) 
      
如果 大于 0,返回 1;如果 小于 0,返回01;否则返回 0
      sin(x) 
      输入参数为弧度,计算正弦值,返回值范围[1,1] 
      sincos(float x, out s, out c) 
      该函数是同时计算 的 sin 值和 cos 值,其中s=sin(x)c=cos(x)。该函数用于“同时需要计算 sin 值和 cos 值的情况”,比分别运算要快很多!
      sinh(x) 
      计算双曲正弦(hyperbolic sine)值。
      smoothstep(min, max, x)
      
值 位于 minmax 区间中。如果 x=min,返回 0;如果 x=max,返回 1;如果 在两者之间,按照下列公式返回数据: 
                xmin                  xmin
2*( )+3*(  )2 
        maxmin                 maxmin
      step(a, x)
      如果 x<a,返回 0;否则,返回 1
      sqrt(x) 
      求 的平方根, 必须大于 0
      tan(x) 
      输入参数为弧度,计算正切值 
      tanh(x)
      计算双曲正切值
      transpose(M) 
      为矩阵,计算其转置矩阵

几何函数(Geometric Functions
Cg 语言标准函数库中有
3 个几何函数会经常被使用到,分别是:normalize 函数,对向量进行归一化;reflect 函数,计算反射光方向向量;refract 函数,计算折射光方向向量。注意: 
        1  着色程序中的向量 好进行归一化之后再使用,否则会出现难以预料的错误; 
        2  reflect 函数和 refract 函数都存在以“入射光方向向量”作为输入参数,注意这两个函数中使用的入射光方向向量,是从外指向几何顶点的;平时我们在着色程序中或者在课本上都是将入射光方向向量作为从顶点出发。
      函数
      功能
      distance( pt1, pt2) 
      两点之间的欧几里德距离(Euclidean distance
      faceforward(N,I,Ng)
      如果Ng I• < ,返回 N;否则返回-N
      length(v) 
      返回一个向量的模,即 sqrt(dot(v,v)) 
      normalize( v) 
      归一化向量 
      reflect(I, N) 
      根据入射光方向向量 I,和顶点法向量 N,计算反射光方向向量。其中 和 必须被归一化,需要非常注意的是,这个 是指向顶点的;函数只对三元向量有效
      refract(I,N,eta) 
      计算折射向量,为入射光线,为法向量,eta 为折射系数;其中 和 必须被归一化,如果 和 之间的夹角太大,则返回(000),也就是没有折射光线;是指向顶点的;函数只对三元向量有效
   
   
   

纹理映射函数(Texture Map Functions
                                    函数 
      
tex1D(sampler1D tex, float s)                         一维纹理查询
      
tex1D(sampler1D tex, float s, float dsdx, float dsdy)       使用导数值(derivatives)查询一维纹理
      
Tex1D(sampler1D tex, float2 sz)                     一维纹理查询,并进行深度值比较
      
Tex1D(sampler1D tex, float2 sz, float dsdx,float dsdy)      使用导数值(derivatives)查询一维纹理, 并进行深度值比较
      
Tex1Dproj(sampler1D tex, float2 sq)                 一维投影纹理查询
      
Tex1Dproj(sampler1D tex, float3 szq)                一维投影纹理查询,并比较深度值
      
Tex2D(sampler2D tex, float2 s)                       二维纹理查询
      
Tex2D(sampler2D tex, float2 s, float2 dsdx, float2 dsdy)     使用导数值(derivatives)查询二维纹理
      
Tex2D(sampler2D tex, float3 sz)                     二维纹理查询,并进行深度值比较
      
Tex2D(sampler2D tex, float3 sz, float2 dsdx,float2 dsdy)  使用导数值(derivatives)查询二维纹理,并进行深度值比较
      
Tex2Dproj(sampler2D tex, float3 sq)                 二维投影纹理查询
      
Tex2Dproj(sampler2D tex, float4 szq)                二维投影纹理查询,并进行深度值比较
      texRECT(samplerRECT tex, float2 s)
      texRECT (samplerRECT tex, float2 s, float2 dsdx, float2 dsdy)  
      texRECT (samplerRECT tex, float3 sz) 
      texRECT (samplerRECT tex, float3 sz, float2 dsdx,float2 dsdy) 
      texRECT proj(samplerRECT tex, float3 sq) 
      texRECT proj(samplerRECT tex, float3 szq) 
      
Tex3D(sampler3D tex, float s)                          三维纹理查询
      
Tex3D(sampler3D tex, float3 s, float3 dsdx, float3 dsdy)       结合导数值(derivatives)查询三维纹理
      
Tex3Dproj(sampler3D tex, float4 szq)                   查询三维投影纹理,并进行深度值比较
      
texCUBE(samplerCUBE tex, float3 s)                    查询立方体纹理
      
texCUBE (samplerCUBE tex, float3 s, float3 dsdx, float3 dsdy)    结合导数值(derivatives)查询立方体纹理
      
texCUBEproj (samplerCUBE tex, float4 sq) 查询投影立方体纹理

       s  象征一元、二元、三元纹理坐标;代表使用深度比较(depth comparison” 的值;表示一个透视值(perspective value,其实就是透视投影后所得到的齐次坐标的 后一位),这个值被用来除以纹理坐标(S),得到新的纹理坐标(已归一化到 和 之间)然后用于纹理查询。
   纹理函数非常多,总的来说,按照纹理维数进行分类,即:1D 纹理函数, 2D 纹理函数,3D 纹理函数,已经立方体纹理。需要注意,TexREC 函数查询的纹理实际上也是二维纹理。 3D 纹理,另一个比较学术化的名称是“体纹理(Volume Texture)”,体纹理通常用于体绘制,体纹理用于记录空间中的体细节数据。

   还有一类较为特殊的纹理查询函数以 proj 结尾,主要是针对投影纹理进行查询。所谓投影纹理是指:将纹理当做一张幻灯片投影到场景中,使用投影纹理技术需要计算投影纹理坐标,然后使用投影纹理坐标进行查询。使用投影纹理坐标进行查询的函数就是投影纹理查询函数。 
  本质来说,投影纹理查询函数和普通的纹理查询函数没有什么不同,唯一的区别在于“投影纹理查询函数使用计算得到的投影纹理坐标,并在使用之前会将该投影纹理坐标除以透视值”。举例而言,计算得到的投影纹理坐标为 float4 uvproj,使用二维投影纹理查询函数: 
    tex2Dproj(texture,uvproj); 等价于按如下方法使用普通二维纹理查询函数:           
     float4 uvproj = uvproj/uvproj.q; tex2D(texture,uvproj);
偏导函数(Derivative Functions

                          函数 
                          功能 
                          ddx(a) 
          参数 对应一个像素位置,返回该像素值在 轴上的偏导数 
                          ddy(a) 
          参数 对应一个像素位置,返回该像素值在 轴上的偏导数 
  1. 函数 ddx 和 ddy 用于求取相邻像素间某属性的差值;
  2. 函数 ddx 和 ddy 的输入参数通常是纹理坐标;
  3. 函数 ddx 和 ddy 返回相邻像素键的属性差值; 

Guess you like

Origin blog.csdn.net/hackdjh/article/details/34097575#comments_25163848