Warning! This version of documentation is OUTDATED, as it describes an older SDK version! Please switch to the documentation for the latest SDK version.
Warning! This version of documentation describes an old SDK version which is no longer supported! Please upgrade to the latest SDK version.

# Math Common Functions (UnigineScript)

## variableabs (variable arg)

Returns an absolute value of an argument.

### Arguments

• variable arg - Argument. Can be the following types:

### Return value

Absolute value of arg.

## Variablebezier (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.

## variableceil (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

• variable arg - Argument. Can be the following types:

### 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

Source code(C++)
``float a = ceil(2.3) // a = 3.0``

## variableclamp (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.

## intcompare (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:

Source code(C++)
``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.

## Variablecross (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).

## Variabledot3 (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:
• float if case of vec3 or vec4
• double if case of dvec3 or dvec4

### Examples

Source code(UnigineScript)
``````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
*/``````

## Variabledot (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:
• float if case of vec3 or vec4
• double if case of dvec3 or dvec4
• int if case of ivec3 or ivec4

### Examples

Source code(UnigineScript)
``````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
*/``````

## variablefloor (variable arg)

Round an argument down to the nearest integer. For vectors values are obtained per component.

### Arguments

• variable arg - Argument. Can be the following types:

### 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

Source code(C++)
``float a = floor(2.3) // a = 2.0``

## variablefrac (variable arg)

Returns a fraction part of an argument. For vectors values are obtained per component.

### Arguments

• variable arg - Argument. Can be the following types:

### 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

## Variablelength2 (variable vector)

Calculates the square length of a vector.

### Arguments

• variable vector - A vector. Can be the following types:

### Return value

Square vector length:
• float for vec3 and vec4 arguments
• double for dvec3 and dvec4 arguments
• int for ivec3 and ivec4 arguments

## Variablelength (variable vector)

Calculates the length of a vector.

### Arguments

• variable vector - A vector. Can be the following types:

### Return value

Vector length:
• float for vec3 and vec4 arguments
• double for dvec3 and dvec4 arguments

## Variablelerp (variable v0, variable v1, variable k)

Linear interpolation between two values according to the following formula:
v0 + (v1 - v0) * k
Notice
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:
• int
• long
• float
• double
• string if arguments are ivec3 or ivec4:
• If a string is empty, it will equal to int(0).
• If a string is nonempty, it will equal to int(1).

### Return value

Interpolated value.

## variablemax (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.

## variablemin (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.

## Variablenormalize3 (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:

Source code(UnigineScript)
``````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``````

## variablenormalize (variable vector)

Normalizes a vector.

### Arguments

• variable vector - A vector. Can be the following types:

### Return value

Normalized vector.

## intnpot (int arg)

Rounds up to the nearest power of two value.

### Arguments

• int arg - Argument.

### Return value

The nearest upper power of 2 number.

## variablercp (variable arg)

Inverses a value. For vectors values are obtained per component.

### Arguments

• variable arg - Argument. Can be the following types:

### Return value

1.0 / 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

## Variablereflect (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.

## variablesaturate (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.

## variablesign (variable arg)

Returns a sign of an argument. For vectors values are obtained per component.

### Arguments

• variable arg - Argument. Can be the following types:

### 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

## intudiv (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