[effects]docs
fx reference / fx.space

3D space

fx draws in two dimensions, and this part of it stays that way: there is no mesh, no lighting and no depth buffer. What it gives you is a camera that turns a point in space into a position on the stage and a size, and you draw there with the tools you already have. That is enough for point clouds, constellations, orbits, paths through space and out-of-focus lights, which is most of what motion graphics asks of 3D.

cam := fx.Cam{yaw = 20 * s.t, pitch = -12, dist = 9}
if q, ok := fx.project(cam, {1, 0, 0}); ok do fx.circle(q.p, 0.05 * q.s, fx.theme.na)

The camera#

Vec3 :: [3]f32

Cam :: struct {
	yaw, pitch: f32,
	dist:       f32,
	target:     Vec3,
	fov:        f32, // vertical field of view in degrees, 0 = 50
	center:     Vec2, // where target lands on the stage, {0, 0} = its middle
	roll:       f32, // degrees
}

A Cam orbits a point. It always looks at target from dist world units away; yaw and pitch (degrees) say from where. It is a plain value: build it from the time at the top of the scene and pass it to everything that draws.

Field Meaning
yaw Turn around the vertical axis. At 0 the camera looks along +z with +x to its right. Raising it carries the camera to the left, so the right side of the scene swings away from you.
pitch Tilt. A negative pitch lifts the camera and looks down on the target, a positive one drops it and looks up.
dist Distance from the target, in world units.
target The point in the world the camera looks at.
fov Vertical field of view in degrees; 0 means 50. Small is a long lens (flat), large a wide one.
center Where target lands on the stage in px; {0, 0} means the middle.
roll Turns the picture, counterclockwise for positive degrees.

World units are whatever you want them to be, y points up, and nothing has a size until the camera gives it one: with the default lens, one world unit at 5 units' distance is about 230 px.

A frame of the space-cam demo
3.72 / 6.00 sspace-cam
space_cam :: proc(s: fx.Shot) {
    fx.backdrop(.Night, grid = false)
    cam := fx.Cam{yaw = knob("yaw", 25, -180, 180), pitch = knob("pitch", -25, -89, 89), dist = knob("dist", 11, 5, 30)}
    for i in -4 ..= 4 { // a floor at y = 0
        fx.line3(cam, {f32(i), 0, -4}, {f32(i), 0, 4}, fx.stroke(fx.theme.ns, 2))
        fx.line3(cam, {-4, 0, f32(i)}, {4, 0, f32(i)}, fx.stroke(fx.theme.ns, 2))
    }
    axes := [?]struct {
        name: string,
        p:    fx.Vec3,
    }{{"x", {3, 0, 0}}, {"y", {0, 3, 0}}, {"z", {0, 0, 3}}}
    for a in axes {
        fx.line3(cam, {}, a.p, fx.stroke(fx.theme.nt, 6))
        if q, ok := fx.project(cam, a.p); ok do fx.label(a.name, q.p + {0, -44}, fx.mono(52, 500), fx.theme.nt, {0.5, 0.5})
    }
    // something going round on the floor
    a := s.p * math.TAU
    if q, ok := fx.project(cam, {2 * math.cos(a), 0, 2 * math.sin(a)}); ok do fx.circle(q.p, 0.2 * q.s, fx.theme.na)
}

Projecting points#

Projected :: struct {
	p: Vec2, // stage position
	z: f32, // distance from the camera along its view
	s: f32, // stage px per world unit at that distance
}

project :: proc(c: Cam, p: Vec3, near: f32 = 0.05) -> (Projected, bool)
line3   :: proc(c: Cam, a, b: Vec3, p: sk.Paint, near: f32 = 0.05)

project is the whole of the 3D: it answers where a world point is on the stage (p), how far from the camera (z) and how big things are there (s). Multiply a world size by s and you have px, so fx.circle(q.p, 0.07 * q.s, c) is a ball of radius 0.07 that shrinks with distance. The second result is false when the point is behind the camera (closer than near): skip it.

line3 draws a straight line between two world points and clips it at the camera. Its paint is any stroke: fx.stroke(color, width), or that wrapped in additive or soft (see drawing). The width is in px and does not change with depth.

A frame of the space-project demo
4.96 / 8.00 sspace-project
space_project :: proc(s: fx.Shot) {
    fx.backdrop(.Night)
    cam := fx.Cam{yaw = 45 * s.lt, pitch = 22, dist = knob("dist", 5, 3, 12), fov = knob("fov", 50, 15, 110)}
    corner :: proc(i: int) -> fx.Vec3 {
        return {i & 1 == 0 ? -1 : 1, i & 2 == 0 ? -1 : 1, i & 4 == 0 ? -1 : 1}
    }
    for i in 0 ..< 8 {
        // an edge to each corner that differs in one coordinate
        for bit in ([?]int{1, 2, 4}) {
            if i & bit == 0 do fx.line3(cam, corner(i), corner(i | bit), fx.stroke(fx.theme.ns, 4))
        }
    }
    for i in 0 ..< 8 {
        q, ok := fx.project(cam, corner(i))
        if !ok do continue
        fx.circle(q.p, 0.08 * q.s, fx.theme.na) // q.s is px per world unit there: nearer is bigger
        fx.label(fmt.tprintf("z %.1f", q.z), q.p + {0, -0.08 * q.s - 34}, fx.mono(36), fx.theme.nt, {0.5, 0.5})
    }
}

Nothing is sorted by depth. Things appear in the order you draw them, which does not matter for lines and for light that adds up (bokeh, spark, additive paints). For opaque shapes that overlap, sort them by z yourself, far ones first.

Two smaller pieces are public for when you need the numbers in between:

view  :: proc(c: Cam, p: Vec3) -> Vec3 // p in the camera's space: x right, y up, z away from the camera
focal :: proc(c: Cam) -> f32           // stage px per world unit one unit in front of the camera

Points to draw#

fib_sphere :: proc(i, n: int) -> Vec3     // the i-th of n points spread evenly over the unit sphere
gauss3     :: proc(seed: u32, i: int) -> Vec3 // a stable random point from a 3D gaussian (σ = 1)

Two ways to get many points without storing any. fib_sphere walks a spiral from the top of the unit sphere to the bottom and covers it evenly for any n; multiply the result for a bigger sphere. gauss3 is a cloud: dense in the middle, thinning out, with nearly all points within 3 units. Like hash01 it has no state, so point i of cloud seed is in the same place on every frame.

A frame of the space-sphere demo
4.96 / 8.00 sspace-sphere
space_sphere :: proc(s: fx.Shot) {
    fx.backdrop(.Night)
    n := int(knob("points", 700, 20, 2500))
    cam := fx.Cam{yaw = 45 * s.lt, pitch = knob("pitch", 20, -90, 90), dist = 2.9}
    for i in 0 ..< n {
        p := fx.fib_sphere(i, n) // on the unit sphere around the origin
        c := i % 7 == 0 ? fx.theme.na : fx.theme.nt
        // focused on the near side, 1.9 away: the far side goes soft
        fx.bokeh(cam, p, 0.014, c, focus = 1.9, aperture = 0.8)
    }
}

Depth of field#

bokeh :: proc(c: Cam, p: Vec3, r: f32, color: Color, focus: f32, aperture: f32 = 0.6, min_px: f32 = 0.6)

bokeh draws a dot of world radius r the way a lens would see it. At focus world units from the camera it is sharp. The further it is from that distance, the bigger and fainter its disc gets, the same light spread over more area, and well out of focus it gets the slightly brighter rim of a real lens. aperture is how fast that happens; at 0 everything is sharp. min_px keeps distant dots from vanishing: no dot is drawn smaller than that radius.

Its light is added to what is below, so overlapping dots brighten each other and their order does not matter. Dots off the stage are skipped.

focus is a distance like Projected.z. To keep the target sharp, set it to cam.dist; to pull focus from the front of a cloud to its back, animate it.

A frame of the space-bokeh demo
7.44 / 12.00 sspace-bokeh
space_bokeh :: proc(s: fx.Shot) {
    fx.backdrop(.Night)
    focus := knob("focus", 7, 3, 12)
    aperture := knob("aperture", 1, 0, 3)
    cam := fx.Cam{yaw = 30 * s.lt, pitch = 10, dist = 7}
    for i in 0 ..< 500 {
        p := fx.gauss3(11, i) * 2 // a cloud around the origin, at twice its size
        h := fx.hash01(12, i)
        c := h < 0.2 ? fx.theme.na : fx.theme.nt
        fx.bokeh(cam, p, 0.02 + 0.04 * h * h, fx.alpha(c, 0.8), focus, aperture)
    }
}

Sparks and paths#

spark   :: proc(p: Vec2, r: f32, color: Color, a: f32 = 1)
spline3 :: proc(pts: []Vec3, p: f32) -> Vec3
lerp3   :: proc(a, b: Vec3, p: f32) -> Vec3
length3 :: proc(v: Vec3) -> f32

spark is a soft point of light at a stage position (it is 2D: project first): a bright core that falls off to nothing at radius r px, added to what is below. a is its strength, 0 to 1. Two on top of each other, a wide one in a color and a small pale one, make a glowing head.

spline3 is a smooth curve through all of pts (a Catmull-Rom spline): p = 0 is the first point, p = 1 the last, and each stretch between two points takes an equal share of p. Draw it as short line3 pieces, and stop at an animated p to make it grow.

A frame of the space-spline demo
3.72 / 6.00 sspace-spline
space_spline :: proc(s: fx.Shot) {
    fx.backdrop(.Night)
    cam := fx.Cam{yaw = 20 + 25 * math.sin(s.p * math.TAU), pitch = 16, dist = 6.4}
    pts := [?]fx.Vec3{{-3.4, -1.4, 0}, {-1.7, 1.5, 1.6}, {0, -1, -1.6}, {1.7, 1.6, 0.6}, {3.4, -1.2, -0.5}}
    for p in pts {
        if q, ok := fx.project(cam, p); ok do fx.ring(q.p, 0.1 * q.s, fx.theme.ns, 3)
    }
    // the curve so far, as short straight pieces
    travel := fx.ep(s.lt, 0.4, 4.6, fx.ease_in_out)
    o := 1 - fx.ep(s.lt, 5.3, 5.9)
    SEG :: 90
    prev := pts[0]
    for i in 1 ..= SEG {
        u := f32(i) / SEG
        q := fx.spline3(pts[:], travel * u)
        fx.line3(cam, prev, q, fx.stroke(fx.alpha(fx.theme.na, (0.25 + 0.75 * u) * o), 3 + 6 * u))
        prev = q
    }
    if head, ok := fx.project(cam, prev); ok {
        fx.spark(head.p, 110, fx.theme.na, 0.9 * o)
        fx.spark(head.p, 24, fx.theme.nt, o)
    }
}

lerp3 is lerp for points and length3 the length of a vector. Vec3 is an Odin array, so the rest of the arithmetic is built in: a + b, p * 2, (a - b) * 0.5.

Reference#

Procedure Returns or draws
project(cam, p, near) Where p is on the stage, its distance and its scale; false behind the camera.
view(cam, p) p in the camera's space: x right, y up, z away.
focal(cam) Stage px per world unit at distance 1.
line3(cam, a, b, paint, near) A line between two world points, clipped at the camera.
bokeh(cam, p, r, color, focus, aperture, min_px) A dot with depth of field, its light added.
spark(p, r, color, a) A soft point of light at a stage position.
fib_sphere(i, n) The i-th of n points spread evenly over the unit sphere.
gauss3(seed, i) A stable random point of a gaussian cloud.
spline3(pts, p) The point p (0..1) of the way along a smooth curve through pts.
lerp3(a, b, p), length3(v) A point between two points; a vector's length.

Edit this page · Plain Markdown · All docs in one file