Molten

Vectors, quaternions and matrices

Cast has built-in float vector and matrix types that follow GLSL. They are value types: assignment copies them.

Type Components
vec2, vec3, vec4 x y z w (also r g b a and s t p q)
quat A rotation: x y z w, identity quat(0, 0, 0, 1)
mat3, mat4 Column-major matrices

Constructing

export function main(): void {
    print(vec2(3), vec3(1, vec2(2, 3)), vec4(vec3(1, 2, 3), 4));
    print(vec3(vec4(1, 2, 3, 4)), quat(vec3(0), 1));
}
[3, 3] [1, 2, 3] [1, 2, 3, 4]
[1, 2, 3] [0, 0, 0, 1]

vec3(s) fills every component; mixed forms such as vec4(v.xyz, w) work as in GLSL. mat3(2) is a diagonal matrix; mat3(a, b, c) takes three column vectors; mat4(m3) extends a mat3.

Arithmetic

export function main(): void {
    const v = vec3(2, 4, 8);
    print(v + 1, 10 - v, v * vec3(2, 3, 4), v / 2, -v);
    print(v == vec3(2, 4, 8), v != vec3(2, 4, 9));
    const q = quat(0, 0, 1, 0);       // 180 degrees about Z
    print(q * vec3(1, 2, 3), q * q);
    const m = mat3(vec3(1, 2, 3), vec3(4, 5, 6), vec3(7, 8, 9));
    print(m * vec3(1, 0, 0), vec3(1, 0, 0) * m);
}
[3, 5, 9] [8, 6, 2] [4, 12, 32] [1, 2, 4] [-2, -4, -8]
true true
[-1, -2, 3] [0, 0, 0, -1]
[1, 2, 3] [1, 4, 7]
  • + - * / work component-wise between vectors of the same size, and with a scalar on either side.
  • mat * vec and mat * mat are matrix products; vec * mat multiplies by the transpose.
  • quat * quat composes rotations; quat * vec3 rotates the vector.
  • == compares every component.
  • A vector is not a condition: if (v) is an error.

Swizzles

Read any combination of components; write combinations that don't repeat a component.

export function main(): void {
    let v = vec4(1, 2, 3, 4);
    print(v.xz, v.bgra, v.ww);
    v.xy = v.yx;
    v.zw += vec2(10);
    print(v);
}
[1, 3] [3, 2, 1, 4] [4, 4]
[2, 1, 13, 14]

v.xx = ... is an error (writable swizzles cannot repeat components), and so is reading a component the type doesn't have (invalid vec2 swizzle 'z').

Vector math

export function main(): void {
    const u = vec3(3, 4, 0);
    print(dot(u, vec3(2)), cross(vec3(1, 0, 0), vec3(0, 1, 0)), length(u));
    print(normalize(u), distance(u, vec3(0)), max(u, 2), clamp(u, vec3(1), vec3(3)));
    print(lerp(vec3(0), vec3(10), 0.25));
}
14 [0, 0, 1] 5
[0.6, 0.8, 0] 5 [3, 4, 2] [3, 3, 1]
[2.5, 2.5, 2.5]

Rotations

Cast has no euler-angle helper. Build a quaternion from an axis and angle (radians):

function axisAngle(axis: vec3, radians: float): quat {
    const s = sin(radians * 0.5);
    const a = normalize(axis) * s;
    return quat(a.x, a.y, a.z, cos(radians * 0.5));
}

export function main(): void {
    const quarter = axisAngle(vec3(0, 1, 0), 3.14159265 / 2);
    const turned = quarter * vec3(1, 0, 0);
    print(round(turned.x), round(turned.z));
}
0 -1

In concept files, euler(x, y, z) takes degrees and applies Z, then X, then Y.

Double-precision positions

dvec3(x, y, z) holds three float64 lanes. It is the type of Transform.position and world positions in the engine API. Cast automatically widens vec3 to dvec3. Use vec3(p) when you want float coordinates.

const origin = dvec3(1000000, 0, 0);
const target = dvec3(1000000.0125, 0, 0);
const relative = target - origin;
const direction: vec3 = normalize(relative);
const distanceMeters: float = length(relative);

Adding or subtracting a vec3 and a dvec3 yields a dvec3. Subtracting two dvec3 values also yields a dvec3. length and distance return float. normalize returns vec3. Single-lane swizzles return float64, three-lane swizzles return dvec3, and two- and four-lane swizzles return float vectors.

View this page's source on GitHub