GLSL内置数学函数部分解析

阶跃函数

step函数:Generate a step function by comparing two values

For element i of the return value, 0.0 is returned if x[i] < edge[i], and 1.0 is returned otherwise.

edge specifies the location of the edge of the step function.

x specify the value to be used to generate the step function.

float step(float edge, float x);

在这里插入图片描述

平滑阶梯函数,平滑过渡函数

smoothstep函数:Perform Hermite interpolation between two values

    genType t;  /* Or genDType t; */
    t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
    return t * t * (3.0 - 2.0 * t);
inverse_smoothstep = 0.5 - sin(asin(1.0 - 2.0 * x) / 3.0);

edge0` specifies the value of the lower edge of the Hermite function.

edge1 specifies the value of the upper edge of the Hermite function.

x specifies the source value for interpolation.

float smoothstep(float edge0, float edge1 float x);

在这里插入图片描述

atan函数

atan会返回一个介于-PI到PI的弧度值(-3.14 to 3.14)

distance函数:

p0 specifies the first of two points

p1 specifies the second of two points

distance() returns the distance between the two points p0 and p1.

float distance(float p0, float p1);
float distance(vec2 p0, vec2 p1);
// The DISTANCE from the pixel to the center
pct = distance(st,vec2(0.5));
// The LENGTH of the vector from the pixel to the center ,vec2 toCenter = vec2(0.5)-st;
pct = length(toCenter);
// The SQUARE ROOT of the vector from the pixel to the center
vec2 tC = vec2(0.5)-st; 
pct = sqrt(tC.x*tC.x+tC.y*tC.y);

以上三种情况得到的是一样的结果!

length函数

x specifies a vector of which to calculate the length.

length() returns the length of the vector.

float length(float x)  
float length(vec2 x)  
float length(vec3 x)  
float length(vec4 x)

reflect:几何函数,根据入射方向和法向求反射方向

在这里插入图片描述


版权声明:本文为qq_30070433原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。