Vectors

This section provides utility functions for creating and manipulating vectors in 2D, 3D, and 4D space. These return a corresponding THREE.js vector object.

import { vec2, vec3, vec4 } from "meowshapes";

const v2 = vec2(1, 2);
const v3 = vec3(1, 2, 3);
const v4 = vec4(1, 2, 3, 4);

Additionally, a set of predefined direction constants is available for common directions.

import { DIR } from "meowshapes";

const right = DIR.X; // (1, 0, 0)
const up = DIR.Y; // (0, 1, 0)
const forward = DIR.Z; // (0, 0, 1)

const left = DIR.NEG_X; // (-1, 0, 0)
const down = DIR.NEG_Y; // (0, -1, 0)
const back = DIR.NEG_Z; // (0, 0, -1)

A note on vector usage

Vector arguments and input are often typed with the Vec2, Vec3, or Vec4 types. These types are flexible and can accept a variety of input formats for convenience.

// Using vec3()
sphere(32).pos(vec3(1, 2, 3));

// Using an array
sphere(32).pos([1, 2, 3]);

// "Splatting" a single value across all components. Equivalent to vec3(20, 20, 20)
sphere(32).pos(20);

In addition, these are automatically converted to their higher order counterparts when needed. For example, a Vec2 can be used where a Vec3 is expected, with the missing component defaulting to 0:

// Using a Vec2 where a Vec3 is expected. Equivalent to vec3(1, 2, 0)
sphere(32).pos([1, 2]);