Shader 函数介绍

1,187 阅读5分钟

sin

返回参数的正弦值 (Return the sine of the parameter)

声明 Declaration

float sin(float angle) 
vec2 sin(vec2 angle) 
vec3 sin(vec3 angle) 
vec4 sin(vec4 angle)

参数 Parameters

angle 指定要返回正弦值的量(以弧度为单位)。 specify the quantity, in radians, of which to return the sine.

描述 Description

sin() 返回角的三角正弦值。 returns the trigonometric sine of angle.

image.png

y = sin(x); 

Cos

Return the cosine of the parameter

声明 Declaration

float cos(float angle)  
vec2 cos(vec2 angle)  
vec3 cos(vec3 angle)  
vec4 cos(vec4 angle)

参数 Parameters

angle specify the quantity, in radians, of which to return the cosine.

描述 Description

cos() returns the trigonometric cosine of angle.

image.png

y = cos(x); 

Tan

Return the tangent of the parameter

声明 Declaration

float tan(float angle)  
vec2 tan(vec2 angle)  
vec3 tan(vec3 angle)  
vec4 tan(vec4 angle)

参数 Parameters

angle specify the quantity, in radians, of which to return the tangent.

描述 Description

tan() returns the trigonometric tangent of angle.

image.png

y = tan(x); 

Asin

Return the arcsine of the parameter

声明 Declaration

float asin(float x)  
vec2 asin(vec2 x)  
vec3 asin(vec3 x)  
vec4 asin(vec4 x)

参数 Parameters

x specify the value whose arcsine to return.

描述 Description

asin() returns the angle whose trigonometric sine is x.

image.png

y = asin(x); 

Acos

Return the arccosine of the parameter

声明 Declaration

float acos(float x)  
vec2 acos(vec2 x)  
vec3 acos(vec3 x)  
vec4 acos(vec4 x)

参数 Parameters

x specify the value whose arccosine to return.

描述 Description

acos() returns the angle whose trigonometric cosine is x.

image.png

y = acos(x); 

Atan

Return the arc-tangent of the 参数 Parameters

声明 Declaration

float atan(float y, float x)  
vec2 atan(vec2 y, vec2 x)  
vec3 atan(vec3 y, vec3 x)  
vec4 atan(vec4 y, vec4 x)

float atan(float y_over_x)  
vec2 atan(vec2 y_over_x)  
vec3 atan(vec3 y_over_x)  
vec4 atan(vec4 y_over_x)

参数 Parameters

y specify the numerator of the fraction whose arctangent to return.

x specify the denominator of the fraction whose arctangent to return.

y_over_x specify the fraction whose arctangent to return.

描述 Description

atan() returns the angle whose trigonometric arctangent is y,x or y_over_x, depending on which overload is invoked. In the first overload, the signs of y and x are used to determine the quadrant that the angle lies in. The values returned by atan in this case are in the range -PI and PI. Results are undefined if x is zero.

For the second overload, atan() returns the angle whose tangent is y_over_x. Values returned in this case are in the range -PI to PI.

Pow

返回第一个参数的第二个幂的值。

Return the value of the first parameter raised to the power of the second.

声明 Declaration

float pow(float x, float y)  
vec2 pow(vec2 x, vec2 y)  
vec3 pow(vec3 x, vec3 y)  
vec4 pow(vec4 x, vec4 y)

参数 参数 Parameters

x specify the value to raise to the power y.

y specify the power to which to raise x.

描述 Description

pow() returns the value of x raised to the y power.

image.png

y = pow(x,3.0); 

Exp

Return the natural exponentiation of the parameter

声明 Declaration

float exp(float x)  
vec2 exp(vec2 x)  
vec3 exp(vec3 x)  
vec4 exp(vec4 x)

参数 Parameters

x specify the value to exponentiate.

描述 Description

exp() returns the natural exponentiation of x.

image.png

y = exp(x); 

Log

Return the natural logarithm of the parameter

声明 Declaration

float log(float x)  
vec2 log(vec2 x)  
vec3 log(vec3 x)  
vec4 log(vec4 x)

参数 Parameters

x specify the value of which to take the natural logarithm.

描述 Description

log() returns the natural logarithm of x.

image.png

y = log(x); 

Sqrt

Return the square root of the parameter

声明 Declaration

float sqrt(float x)  
vec2 sqrt(vec2 x)  
vec3 sqrt(vec3 x)  
vec4 sqrt(vec4 x)

参数 Parameters

x specify the value of which to take the square root.

描述 Description

sqrt() returns the square root of x.

image.png

y = sqrt(x); 

示例

// url https://thebookofshaders.com/glossary/?search=sqrt
// Author @patriciogv - 2015
// http://patriciogonzalezvivo.com

#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

void main(){
    vec2 st = gl_FragCoord.xy/u_resolution;
    float pct = 0.0;

    // a,b,c三种示例

    // a. The DISTANCE from the pixel to the center
    pct = distance(st,vec2(0.5));

    // b. The LENGTH of the vector
    //    from the pixel to the center
    // vec2 toCenter = vec2(0.5)-st;
    // pct = length(toCenter);

    // c. 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);

    vec3 color = vec3(pct);

	gl_FragColor = vec4( color, 1.0 );
}

Abs

Return the absolute value of the parameter.

声明 Declaration

float abs(float x)  
vec2 abs(vec2 x)  
vec3 abs(vec3 x)  
vec4 abs(vec4 x)

参数 Parameters

x specify the value of which to return the absolute.

描述 Description

abs() returns the absolute value of x.

image.png

y = abs(x);

Sign

Extract the sign of the parameter

声明 Declaration

float sign(float x)  
vec2 sign(vec2 x)  
vec3 sign(vec3 x)  
vec4 sign(vec4 x)

参数 Parameters

x specify the value from which to extract the sign.

描述 Description

sign() returns -1.0 if x is less than 0.0, 0.0 if x is equal to 0.0, and +1.0 if x is greater than 0.0.

image.png

y = sign(x);

Floor

Find the nearest integer less than or equal to the parameter

声明 Declaration

float floor(float x)  
vec2 floor(vec2 x)  
vec3 floor(vec3 x)  
vec4 floor(vec4 x)

参数 Parameters

x specify the value to evaluate.

描述 Description

floor() returns a value equal to the nearest integer that is less than or equal to x.

image.png

y = floor(x); 

Ceil

Find the nearest integer that is greater than or equal to the parameter

声明 Declaration

float ceil(float x)  
vec2 ceil(vec2 x)  
vec3 ceil(vec3 x)  
vec4 ceil(vec4 x)

参数 Parameters

x specify the value to evaluate.

描述 Description

ceil() returns a value equal to the nearest integer that is greater than or equal to x.

image.png

y = ceil(x); 

Fract

Compute the fractional part of the argument

声明 Declaration

float fract(float x)  
vec2 fract(vec2 x)  
vec3 fract(vec3 x)  
vec4 fract(vec4 x)

参数 Parameters

x specify the value to evaluate.

描述 Description

fract() returns the fractional part of x. This is calculated as x - floor(x).

image.png

y = fract(x); 

Mod

Compute value of one parameter modulo another

声明 Declaration

float mod(float x, float y)  
vec2 mod(vec2 x, vec2 y)  
vec3 mod(vec3 x, vec3 y)  
vec4 mod(vec4 x, vec4 y)

vec2 mod(vec2 x, float y)  
vec3 mod(vec3 x, float y)  
vec4 mod(vec4 x, float y)

参数 Parameters

x specify the value to evaluate. y specify the value to obtain the modulo of.

描述 Description

mod() returns the value of x modulo y. This is computed as x - y * floor(x/y).

image.png

y = mod(x,1.5);

Min

Return the lesser of two values

声明 Declaration

float min(float x, float y)  
vec2 min(vec2 x, vec2 y)  
vec3 min(vec3 x, vec3 y)  
vec4 min(vec4 x, vec4 y)

vec2 min(vec2 x, float y)  
vec3 min(vec3 x, float y)  
vec4 min(vec4 x, float y)

参数 Parameters

x specify the first value to compare.

y specify the second value to compare.

描述 Description

min() returns the minimum of the two 参数 Parameters. It returns y if y is less than x, otherwise it returns x.

image.png

y = min(x,0.5); 

Max

Return the greater of two values

声明 Declaration

float max(float x, float y)  
vec2 max(vec2 x, vec2 y)  
vec3 max(vec3 x, vec3 y)  
vec4 max(vec4 x, vec4 y)

vec2 max(vec2 x, float y)  
vec3 max(vec3 x, float y)  
vec4 max(vec4 x, float y)

参数 Parameters

x specify the first value to compare.

y specify the second value to compare.

描述 Description

max() returns the maximum of the two 参数 Parameters. It returns y if y is greater than x, otherwise it returns x.

image.png

y = max(x,0.5); 

Clamp

Constrain a value to lie between two further values

声明 Declaration

float clamp(float x, float minVal, float maxVal)  
vec2 clamp(vec2 x, vec2 minVal, vec2 maxVal)  
vec3 clamp(vec3 x, vec3 minVal, vec3 maxVal)  
vec4 clamp(vec4 x, vec4 minVal, vec4 maxVal)

vec2 clamp(vec2 x, float minVal, float maxVal)  
vec3 clamp(vec3 x, float minVal, float maxVal)  
vec4 clamp(vec4 x, float minVal, float maxVal)

参数 Parameters

x specify the value to constrain.

minVal specify the lower end of the range into which to constrain x.

maxVal specify the upper end of the range into which to constrain x.

描述 Description

clamp() returns the value of x constrained to the range minVal to maxVal. The returned value is computed as min(max(x, minVal), maxVal).

image.png

y = clamp(x,0.,1.); 

文章从thebookofshaders收集