1.阈值函数,float step(float edge, float x)
x<edge是0,大于等于是1。经常用来代替if
float edge = 0.5; float x = 0.3; float result = step(edge, x); // will return 0.0 since `x` < `edge`2.光滑阈值函数,smoothInterpolation = smoothstep(edge0, edge1, x)
x<edge0是0,>edge1是1,中间是0-1光滑曲线。
// result will be a smooth color interpolation between red and blue vec3 out = mix(color1, color2, smoothstep(0.2, 0.8, factor));3.绝对值函数,float abs(float x);
float number = -5.0; float absValue = abs(number); // will be 5.04.向上取整,ceil()
float number = 3.5; float ceilVal = ceil(number); // will be 4.0 vec3 vector = vec3(2.3, 5.8, -4.7); vec3 ceilVec = ceil(vector); // will be vec3(3.0, 6.0, -4.0)5.向下取整,floor()
float val = 3.7; float flooredVal = floor(val); // will be 3.0 float val = -3.7; float flooredVal = floor(val); // will be -4.06.截断成整数,int()
float floatVal = 3.7; int intVal = int(floatVal); // will be 37.取小数部分,fract()
float val = 3.7; float fractVal = fract(val); //fractVal=0.78.计算向量长度,length()
vec3 vec = vec3(2.0, 4.0, 4.0); float vecLength = length(vec); // will be 6.09.计算向量距离,float distance(vec p0, vec p1)
vec3 p0 = vec3(1.0, 2.0, 2.0); vec3 p1 = vec3(4.0, 2.0, 6.0); float dist = distance(p0, p1); // will be 5.010.计算与x轴正方向夹角,float atan(float y, float x)
float dx = targetX - positionX; float dy = targetY - positionY; float angle = atan(dy, dx); // Calculate angle for rotation11.sin周期函数,float sin(float angle)
float angle = uv.x * 2.0 * PI; //一个周期 float angle = uv.x * frequency * 2.0 * PI; //frequency 个周期12.向量归一化函数,normalize(V)
N = normalize(V);向量归一化后,取normalize.x就是向量和x正方向夹角的cos值,normalize.y就是向量和y正方向夹角的sin值。
13.点乘,可以是两个float,也可以是两个向量,float dot(float x, float y);float dot(vec2 x, vec2 y)
代数意义:v1 * v2 = v1[0] * v2[0] + … + v1[n] * v2[n];
几何意义:①:非归一化向量dot表示cos值乘上两个向量的长度
v1 * v2 = length(v1) * length(v2) * cos(angle between v1 and v2)
②:归一化向量dot表示cos值:
v1 * v2 = cos(angle between v1 and v2);
14.上限和下限值限制函数clamp,float clamp(float value, float minVal, float maxVal):
超过maxVal部分都等于maxVal,小于minVal部分都等于minVal
clamp(0.8, 0.0, 1.0); // will be 0.8, since value is between 0.0 and 1.0 clamp(-0.5, 0.0, 1.0); // will be 0.0, since value is less than 0.0 clamp(1.5, 0.0, 1.0); // will be 1.0, since value is greater than 1.015.纹理采样函数,vec4 color = texture(<Name>, texCoord);
uniform sampler2D <Name> vec4 color = texture(<Name>, texCoord);16.线性插值函数,T mix(T start, T end, T t)
注意start是1-t:mix(start, end, t) = start * (1 - t) + end * t
vec3 color1 = vec3(1.0, 0.0, 0.0); // red color vec3 color2 = vec3(0.0, 0.0, 1.0); // blue color float factor = 0.5; // interpolation factor // result will be a purple color (halfway between red and blue) vec3 resultColor = mix(color1, color2, factor);17.取余。float mod(float x, float y);
float result = mod(10.0, 3.0); // Result will be 1.0 (10 / 3 has a remainder of 1) float result = mod(-10.0, 3.0); // Result will be 2.0 (-10 / 3 has a remainder of 2)18.计算反正切值。atan(y, x)函数用于计算(x, y)二维笛卡尔坐标系中某一点与正x 轴之间的弧度角
float dx = targetX - positionX; float dy = targetY - positionY; float angle = atan(dy, dx); // Calculate angle for rotation