Math Common Functions
variable abs(variable arg)
Returns the absolute value of the argument.Arguments
Return value
Absolute value of arg.Variable bezier(vec4 times, variable values, float time)
Calculates the value of a cubic Bezier function for t = time. A cubic Bezier curve is represented by 4 points. Po is the start point, P1 and P2 are control points 1 and 2 and P3 is the end point. The start and end point denote the beginning and end points of the path and the control points determine how the path moves from the start to the finish. As can be seen from the image, the only variable changing is t which determines how far the path has progressed from P0 to P3. Cubic Bezier curves are used as timing functions particularly for keyframe interpolation.Arguments
- vec4 times - Coordinates of the four points of the curve along the horizontal T (times) axis in the range [0.0f, 1.0f].
- variable values - Coordinates of the four points of the curve along the vertical V (values) axis in the range [0.0f, 1.0f]. They can either be of vec4 or dvec4 types.
- float time - Time in the range [0, 1], for which the value of the Bezier function is to be calculated.
Return value
Value of the Bezier function.variable ceil(variable arg)
Ceiling function that returns the smallest integer 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 return value depends 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
Clamped 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 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 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 floor(variable arg)
Rounds 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 return value depends on the argument type:Examples
float a = floor(2.3) // a = 2.0
variable frac(variable arg)
Returns the fractional part of the argument. For vectors values are obtained per component.float a = frac(3.141593); // a = 0.141593
Arguments
Return value
Fractional part of arg (arg - floor (arg)). The type of return value depends 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 length(variable vector)
Calculates the length of a given vector.Arguments
Return value
Vector length:Variable length2(variable vector)
Calculates the squared length of a given vector. This method is much faster than length() - the calculation is basically the same only without the slow Sqrt call. If you are using lengths simply to compare distances, then it is faster to compare squared lengths against the squares of distances as the comparison gives the same result.Arguments
Return value
Squared length of the vector (X2 + Y2 + Z2):- float for vec3 and vec4 arguments
- double for dvec3 and dvec4 arguments
- int for ivec3 and ivec4 arguments
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)
Returns the reciprocal of the specified argument. For vectors values are obtained per component.Arguments
Return value
1.0 / argThe type of return value depends on the argument type:
variable round(variable arg)
Rounds an argument to the nearest integer value. For vectors values are obtained per component.In halfway cases, when an argument has a fractional part of exactly 0.5, the function rounds away from zero to the integer with larger magnitude.
- 3.5 -> 4
- - 3.5 -> - 4
Arguments
Return value
Nearest integer value. The type of return value depends on the argument type:Examples
float a = round(2.3) // a = 2.0
float b = round(5.5) // b = 6.0
float c = round(-5.5) // c = -6.0
Vec3 d = round(Vec3(1.2, 1.8, -1.5)) // d = (1.0, 2.0, -2.0)
variable saturate(variable v)
Clamps values within the range of 0.0 to 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 the sign of the 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 return value depends on the argument type:
int udiv(int x, int y)
Performs integer division with rounding up.Arguments
- int x - First argument.
- int y - Second argument.
Return value
The rounded up quotient of two arguments that satisfies the following condition: y * z >= x.Last update: 2017-12-21
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)