Math Script API: Difference between revisions

From DF21 Wiki
(Created page with "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,...")
 
No edit summary
Line 27: Line 27:
float2 v2 = v3.xz;
float2 v2 = v3.xz;
</pre>
</pre>
Example of Array Access:
<pre>
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
</pre>
Example of mathematical operations, basic operations are supported:
<pre>
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)
</pre>
== 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.
<pre>
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)
</pre>
== Math Examples ==
<pre>
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);
</pre>
== Math Constants ==

Revision as of 16:37, 14 December 2024

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)

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