glsl常用函数

常用函数:

genType abs (genType x)  -----genType代表范型

返回x的绝对值

genType sign (genType x)

如果x>0,返回1.0;如果x=0,返回0,如果x<0,返回-1.0

genType floor (genType x)

返回小于等于x的最大整数值

floor(3.2) ->3;    floor(3.8)->3;

genType ceil (genType x)

返回大于等于x的最小整数值

ceil(3.3)->4; ceil(3.8)->4

genType fract (genType x)

返回x-floor(x),即返回x的小数部分

genType clamp (genType x, genType minVal, genType maxVal)

genType clamp (genType x, float minVal, float maxVal)

获取x和minVal之间较大的那个值,然后再拿较大的那个值和最后那个最大的值进行比较然后获取较小的那个,意思就明白了,clamp实际上是获得三个参数中大小处在中间的那个值。函数有个说明:如果minVal > minMax的话,函数返回的结果是未定的。也就是说x的值大小没有限制,但是minval的值必须比maxVal小。

clamp(0.5,0,1) ->0.5;    clamp(-0.5,0,1) ->0;    clamp(1.5,0,1) ->1

genType mix (genType x, genType y, genType a)

genType mix (genType x, genType y, float a)

返回线性混合的x和y,如:x⋅(1−a)+y⋅a

genType step (genType edge, genType x),genType step (float edge, genType x)

如果edge>x,返回0.0,否则返回1.0

smoothstep(),平滑阶梯函数,平滑过渡函数

曲线图:

对于y=smooth_step(e0,e1,x)曲线图为:

注:x小于e0时为0,x大于e1时为e1,x在e0到e1之间时线性变化

如下图:


曲线图

几何函数:

length()函数:

返回向量的长度(到(0,0)的距离)

normalize (genType x):

标准化向量,返回一个方向和x相同但长度为1的向量

float distance (genType p0, genType p1):

计算向量p0,p1之间的距离

float dot (genType x, genType y)

向量x,y之间的点积

vec3 cross (vec3 x, vec3 y)

向量x,y之间的叉积


角度和三角函数:

1. radian函数是将角度转换为弧度,degrees函数是将弧度转换为角度

genType有点像面向对象中泛型,即如果genType是float型的,那么

genType pow (genType x, genType y)

就变成了:

float pow (float x, float y)

同理,如果genType是int型的,那么就变成了:

int pow (int x, int y);

2. sin, cos, tan都是标准的三角函数。asin, acos, atan是反三角函数

你可能感兴趣的:(glsl常用函数)