Math Script API

From DF21 Wiki
Revision as of 16:55, 14 December 2024 by Lucius (talk | contribs)

Core math API used by the script system. It can be accessed using the math interface and consists of a number of constants, functions, and built-in vector and matrix types.

Basic Types

Basic math types available to the script system include:

  • int - 32-bit signed integer
  • uint - 32-bit unsigned integer
  • float - 32-bit floating point value

Vector and Matrix Types

  • float2, float3, float4 - vector types of the indicated size. A float3, for example, consists of 3 floats in vector format. Components can be accessed as x, y, z, w or by index (see the examples below). GLSL style swizzling is supported, as well as array syntax. Swizzling can be used to copy out smaller vectors as well.
  • float2x2, float3x3, float4x4 - matrix types of the indicated size (2x2, 3x3, or 4x4 matrix). Rows can be accessed using the array syntax and are of the associated vector type (float2x2 = 2 x float2).

Vector Operations

Vector types (float, float2, float3, float4) can be referenced using array and component syntax. Swizzling is supported.

Example Swizzles:

float2 v(1, 2);
system.print("{}", v);    // (1, 2)
system.print("{}", v.yx); // (2, 1)
system.print("{}", v.xx); // (1, 1)
float a = v.y;
system.print("{}", a); // 2

float3 v3(1, 2, 3);
float2 v2 = v3.xz;

Example of Array Access:

float2 v2(2,3);
system.print("{}", v2[0]);  // 2
system.print("{}", v2[1]);  // 3

float4 v4(1, 2, 3, 4);
system.print("{}", v4[1]); // 2
system.print("{}", v4[3]); // 4

Example of mathematical operations, basic operations are supported:

float2 a(1,2);
float2 b(2,3);
float4 a4(1,2,3,4);
float2 c = a + b;   // c = (1,2) + (2,3) = (3,5)
float2 d = a * b;   // d = (1,2) * (2,3) = (2,6)
float2 e = a * 2.0; // e = (1,2) * 2 = (2,4)
float3 f = a4.xyz * a.y; // f = (1,2,3) * (2) = (2,4,6)

Matrix Operations

Matrices are stored as a series of vectors, which can be accessed using array syntax. Matrix types support matrix/matrix and matrix/vector multiplication, as well as matrix/float scaling. The following types are supported: float2x2, float3x3, float4x4.

float2x2 m2x2(1, 0, 0, 1); // 2x2 matrix: [1 0]
                                          [0 1]
float3x3 m0(float3(1, 0, 0), float3(0, 1, 0), float3(0, 0, 1)); // 3x3 identity.
float3x3 m1;

float3x3 m2 = m0 * m1; // concatenate matrices m0 and m1.
float3 pos;
float3 r = m2 * pos;  // multiply 'pos' by matrix m2.

float3 v = m0[1]; // v = (0, 1, 0)

float2x2 rot2x2 = math.rotationMatrix2x2(angle);
float3x3 rot3x3 = math.rotationMatrix3x3(pitch, yaw, roll);
// Transform a 2D point.
float2 point;
float2 pivot; // center point of rotation.
float2 rotatedPoint = rot2x2 * (point - pivot) + pivot;

Math Examples

float2 s(2, 3);
float2 t(4, 5);
float2 q(3, 4);
float3 r(4, 5, 6);
float3 color0(1.0, 0.5, 0.5);
float3 color1(0.25, 0.25, 1.0);
float dist = math.distance(s, q);  // distance between (2,3) and (3,4).
float blend = math.smoothstep(0, 5, dist);  // produce a blend factor between [0,1]
float2 value = math.mix(s, t, blend);
float2 sum = r.xy + r.yz;
float3 unit = math.normalize(r);
float3 color = math.mix(color0, color1, blend);

Math Constants

Constant Description
float pi value of π
float twoPi value of 2*π
float e value of the natural number e

Math Functions

countBits(x)

Counts the number of bits in an integer.

uint countBits(uint)
int countBits(int)

abs(x)

Returns the absolute value of the input, operates per-component.

int abs(int)
float abs(float)
float2 abs(float2)
float3 abs(float3)
float4 abs(float4)

all(x)

Returns true if all components are non-zero.

bool all(float)
bool all(float2)
bool all(float3)
bool all(float4)

any(x)

Returns true if any component is non-zero.

bool any(float)
bool any(float2)
bool any(float3)
bool any(float4)

acos(x)

Arccosine of angles in radians, operates per-component.

float acos(float)
float2 acos(float2)
float3 acos(float3)
float4 acos(float4)

asin(x)

Arcsine of angles in radians, operates per-component.

float asin(float)
float2 asin(float2)
float3 asin(float3)
float4 asin(float4)

atan(x)

Arctangent of angles in radians, operates per-component.

float atan(float)
float2 atan(float2)
float3 atan(float3)
float4 atan(float4)

atan2(y, x)

Returns the angle θ between the positive x-axis and the ray from the origin to the point (x, y), confined to (−π, π].

float atan2(float, float)
float2 atan2(float2, float2)
float3 atan2(float3, float3)
float4 atan2(float4, float4)

cos(x)

Computes the cosine of the input angle in radians.

float cos(float)
float2 cos(float2)
float3 cos(float3)
float4 cos(float4)

sin(x)

Computes the sine of the input angle in radians.

float sin(float)
float2 sin(float2)
float3 sin(float3)
float4 sin(float4)

tan(x)

Computes the tangent of the input angle in radians.

float tan(float)
float2 tan(float2)
float3 tan(float3)
float4 tan(float4)

sincos(x)

Compute the sine of the input angle and store it in x, and cosine of the angle in y.

float2 sincos(float)

degrees(x)

Converts from radians to degrees, operates per-component.

float degrees(float)
float2 degrees(float2)
float3 degrees(float3)
float4 degrees(float4)

radians(x)

Converts from degrees to radians, operates per-component.

float radians(float)
float2 radians(float2)
float3 radians(float3)
float4 radians(float4)

exp(x)

Computes the natural number e to the power of x, and operates per component.

float exp(float)
float2 exp(float2)
float3 exp(float3)
float4 exp(float4)

exp2(x)

Computes 2 to the power of x, and operates per component.

float exp2(float)
float2 exp2(float2)
float3 exp2(float3)
float4 exp2(float4)

log(x)

Computes the natural (base e) logarithm of the input.

float log(float)
float2 log(float2)
float3 log(float3)
float4 log(float4)

log2(x)

Computes the base 2 logarithm of the input.

float log2(float)
float2 log2(float2)
float3 log2(float3)
float4 log2(float4)

log10(x)

Computes the base 10 logarithm of the input.

float log10(float)
float2 log10(float2)
float3 log10(float3)
float4 log10(float4)