Math Common Functions
variable abs (variable arg)
Returns an absolute value of an argument.Arguments
Return value
Absolute value of arg.Variable bezier (vec4 times, variable values, float time)
Calculates intersection between a cubic Bezier curve and a line for 2D flowgraphs.Arguments
- vec4 times - Control points on the T axis (i.e. horizontal X axis).
- variable values - Control points on the V axis (i.e. vertical Y axis). They can either be of vec4 or dvec4 types.
- float time - Time t.
Return value
Value of the Bezier function.variable ceil (variable arg)
Ceiling function that returns the smallest integral value that is not less than arg. For vectors ceiling values are obtained per component.Arguments
Return value
Smallest integer value not less than arg. The type of returned value will depend on the argument type:- float for int, long, float arguments
- double for double arguments
- vec4 for vec4 arguments
- dvec3 for dvec3 arguments
- dvec4 for dvec4 arguments
Examples
float a = ceil(2.3) // a = 3.0
variable clamp (variable v, variable min, variable max)
Clamps a value within the specified min and max limits.Arguments
- variable v - Argument. Can be the following types:
- variable min -
Minimum limit. Type must be the same as the type of v.
If the first argument is int, it can also be string:
- If a string is empty, it will equal to int(0).
- If a string is nonempty, it will equal to int(1).
- variable max -
Maximum limit. Type must be the same as the type of v.
If the first argument is int, it can also be string:
- If a string is empty, it will equal to int(0).
- If a string is nonempty, it will equal to int(1).
Return value
A value within min and max limits. max, if v > max; min, if v < min; otherwise, v.int compare (variable v0, variable v1, variable epsilon = 1e-6)
Compares two variables of the same type according to the specified degree of precision. The following formula is used:
fabs(v0 - v1) < (fabs(v0) + fabs(v1) + 1.0) * eps
Arguments
- variable v0 - Variable of one of the following types:
- variable v1 - Variable of the same type as v0.
- variable epsilon - Epsilon. Can be the following types:
Return value
1 if v0 is equal to v1; otherwise, 0.Variable cross (variable v0, variable v1)
Cross product of two vectors (either vec3 or dvec3).Arguments
- variable v0 - First vector. Can be of the following types:
- variable v1 - Second vector (of the same type as v0 vector).
Return value
Cross product of v0 and v1 (vec3, dvec3 or ivec3).Variable dot3 (variable v0, variable v1)
Dot product only of three components of vectors. In case an argument is a four-component vector, its w component is ignored.It is possible to obtain a dot product of vec3 and vec4, as well as a dot product of dvec3 and dvec4. But a dot product of vec3 and dvec3, for example, cannot be computed.
Arguments
- variable v0 - First vector. Can be of the following types:
- variable v1 - Second vector. Can be one of similar types as v0.
Return value
Dot product of v0 and v1:Examples
printf("%f\n",dot3(vec3(1,2,3),vec3(1,2,3)));
printf("%f\n",dot3(vec3(1,2,3),vec4(1,2,3,4)));
/*
result is:
14.000000
14.000000
*/
Variable dot (variable v0, variable v1)
Dot product of vectors:- It is possible to obtain a dot product of vec3 and vec4, as well as a dot product of dvec3 and dvec4. In this case, w component of the four-component vector is added to the dot product of first three components of vectors.
- A dot product of ivec3, ivec4 and quat requires the second argument to be exactly of the same type.
Arguments
- variable v0 - First argument. Can be of the following types:
- variable v1 - Second argumet. Can be one of the same of similar types as v0.
Return value
Dot product of v0 and v1:Examples
printf("%f\n",dot(vec3(1,2,3),vec3(1,2,3)));
printf("%f\n",dot(vec3(1,2,3),vec4(1,2,3,4)));
/*
result is:
14.000000
18.000000
*/
variable floor (variable arg)
Round an argument down to the nearest integer. For vectors values are obtained per component.Arguments
Return value
Largest integer value not greater than arg. The type of returned value will depend on the argument type:- float for int, long, float arguments
- double for double arguments
- vec4 for vec4 arguments
- dvec3 for dvec3 arguments
- dvec4 for dvec4 arguments
Examples
float a = floor(2.3) // a = 2.0
variable frac (variable arg)
Returns a fraction part of an argument. For vectors values are obtained per component.Arguments
Return value
Fraction part of arg (arg - floor(arg)). The type of returned value will depend on the argument type:
- float for int, long, float arguments
- double for double arguments
- vec4 for vec4 arguments
- dvec3 for dvec3 arguments
- dvec4 for dvec4 arguments
Variable length2 (variable vector)
Calculates the square length of a vector.Arguments
Return value
Square vector length:- float for vec3 and vec4 arguments
- double for dvec3 and dvec4 arguments
- int for ivec3 and ivec4 arguments
Variable length (variable vector)
Calculates the length of a vector.Arguments
Return value
Vector length:Variable lerp (variable v0, variable v1, variable k)
Linear interpolation between two values according to the following formula:v0 + (v1 - v0) * k
For quaternions, the function uses a spherical interpolation algorithm (slerp).
Arguments
- variable v0 - Start value. Can be the following types:
- variable v1 - End value. Must be the same type as v0.
- variable k - Value from 0 to 1 indicating the weight of v1. Can be the following types:
Return value
Interpolated value.variable max (variable arg1, variable arg2)
Finds the highest value.Arguments
- variable arg1 - First argument. Can be the following types:
- variable arg2 -
Second argument. Type must be the same as the type of arg1.
If arg1 is int, the second argument can also be string:
- If a string is empty, it will equal to int(0).
- If a string is nonempty, it will equal to int(1).
Return value
Numerically highest of the parameter values.variable min (variable arg1, variable arg2)
Finds the lowest value.Arguments
- variable arg1 - First argument. Can be the following types:
- variable arg2 -
Second argument. Type must be the same as the type of arg1.
If arg1 is int, the second argument can be string:
- If a string is empty, it will equal to int(0).
- If a string is nonempty, it will equal to int(1).
Return value
Numerically lowest of the parameter values.Variable normalize3 (variable v)
Depending on the argument type, function performs the following:- Normalizes the X, Y, Z components of the given vector.
- Recalculates the w component of the given quaternion.
Arguments
- variable v - Argument of one of the following types:
- vec4 v - A vector of 4 float components.
- dvec4 v - A vector of 4 double components.
- quat v - A quaternion.
Return value
Normalized vector or quaternion.Examples
You can use the normalize3() function when you need to restore the w component of the quaternion, for example:
vec3 t = vec3(1.0f,0.0f,0.0f); // the tangent basis: tangent vector
vec3 b = vec3(0.0f,1.0f,0.0f); // binormal vector
vec3 n = vec3(0.0f,0.0f,1.0f); // normal vector
vec4 tangent = orthoTangent(t,b,n); // this is the mesh compressed tangent vector
quat compact_tangent_quat = quat(tangent); // convert vec4 to quat; currently the last component of the tangent vector contains the sign of the binormal
quat normalized_quat = normalize3(compact_tangent_quat); // normalize quat; now you have the correct quaternion
variable normalize (variable vector)
Normalizes a vector.Arguments
Return value
Normalized vector.int npot (int arg)
Rounds up to the nearest power of two value.Arguments
- int arg - Argument.
Return value
The nearest upper power of 2 number.variable rcp (variable arg)
Inverses a value. For vectors values are obtained per component.Arguments
Return value
1.0 / argThe type of returned value will depend on the argument type:
- float for int, long, float arguments
- double for double arguments
- vec4 for vec4 arguments
- dvec3 for dvec3 arguments
- dvec4 for dvec4 arguments
Variable reflect (variable v0, variable v1)
Reflects a three-component vector from another (either vec3 or dvec3).Arguments
- variable v0 - Vector to reflect (vec3 or dvec3)
- variable v1 - Vector, from which v0 will be reflected (of the same type as v0 vector).
Return value
Reflected vector.variable saturate (variable v)
Clamps values within 0.0 and 1.0.Arguments
- variable v - Argument. Can be the following types:
Return value
Clamped argument within 0.0 and 1.0. 1.0, if v > 1.0; 0.0 if v < 0.0; otherwise, v.variable sign (variable arg)
Returns a sign of an argument. For vectors values are obtained per component.Arguments
Return value
Sign of arg. 1.0 if arg >= 0.0; -1.0 if arg < 0.0.The type of returned value will depend on the argument type:
- float for int, long, float arguments
- double for double arguments
- vec4 for vec4 arguments
- dvec3 for dvec3 arguments
- dvec4 for dvec4 arguments
int udiv (int x, int y)
Performs integer division with rounding up.Arguments
- int x - The first argument.
- int y - The second argument.
Return value
The rounded up quotient of two arguments that satisfies the following condition: y * z >= x.Last update: 2017-07-03
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)