接下來我們一起來看一下在webgl中的通用函式,這些函式非常的有特點
第一個通用函式:float abs(float x)
此函式會返回x的無符號絕對值,即如果x大於0則返回x,否則返回-x。

第二個通用函式:float sign(float x)
此函式又稱為符號函式,如果x>0返回1.0,如果x=0返回0.0,否則返回-1.0

第三個通用函式:float floor(float x)
此函式會返回小於等於x並且最接近x的整數,通俗來說就是像下取整。

第四個通用函式:float ceil(float x)
此函式會返回大於等於x並且最接近x的整數,通俗來說就是向上取整。

第五個通用函式:float fract(float x)
此函式會返回x的小數部分,即x-floor(x)。

第六個通用函式:float mod(float x, float y)
此函式會返回x除以y的餘數。

第七個通用函式:float min(float x, float y)
此函式會返回x和y兩個值中的最小值。
第八個通用函式:float max(float x, float y)
此函式會返回x和y兩個值中的最大值。
第九個通用函式:float clamp(float x, float minVal, float maxVal)
此函式會將x限制在minVal和maxVal之間。

上面的影象中我將minVal的值調節為0.0,將maxVal的值調節為1.0,那麼x的值比0.0小的時候,就會返回0.0,在0.0到1.0之間就會返回x值本身,而大於1.0的時候就會返回1.0。
第十個通用函式:float mix(float x, float y, float a)
此函式會返回x和y的線性混合,即x*(1-a)+y*a
下面我們看一下y = mix(0.,1.,x);這個函式的影象。

第十一個通用函式:float step(float edge, float)
此函式會根據兩個數值生成階梯函式,如果x<edge則返回0.0,否則返回1.0

第十二個通用函式:float smoothstep(float edge0, float edge1, float x)
如果x<=edge0則返回0.0,如果x>=edge1則返回1.0,否則
t=clamp((x-edge0)/(edge1-edge0), 0, 1)
return t*t(3-2*t)

以上就是webgl內建函式中的通用函式,變化多種多樣,實際的使用價值還在等著你不斷的發掘。