[effects]docs
fx reference / fx.group

Groups and grading

A group is a scope: everything drawn inside it is moved, scaled, faded, blurred, graded or clipped together, as if it were one picture. It is how a card slides in, how a title comes into focus, how a shot is turned black and white, and how a whole film gets its look. There is one struct to learn, Xf, and one procedure that takes it.

The idiom#

group :: proc(xf: Xf) -> bool
if fx.group({y = 40 * (1 - k), o = k}) {
	// drawn 40 px lower and transparent at k = 0, in place and opaque at k = 1
}

group applies xf to everything drawn until the if ends, and undoes it there. Fields you leave out do nothing, so a group reads as a list of what changes. The coordinates inside are the ones you would use without the group: you draw the card where it ends up and let the group displace it.

It returns false, and the body is skipped, when the group would be invisible (o at or below 0.002). So nothing is laid out or drawn for a scene that has not faded in yet.

A frame of the group-basic demo
2.48 / 4.00 sgroup-basic
group_basic :: proc(s: fx.Shot) {
    fx.backdrop(.Night)
    k := fx.win(s.lt, 0.3, 3.7, 0.7, 0.5) // 0 → 1, held, back to 0
    card := fx.Rect{510, 330, 900, 420}
    // everything between the braces rises, sharpens and fades in as one picture
    if fx.group({y = 80 * (1 - k), o = k, blur = 16 * (1 - k)}) {
        fx.shadow(card, 40, 30, 80, 0, 0x80000000)
        fx.rrect(card, 40, fx.theme.night2)
        fx.border(card, fx.theme.nb, 2, 40)
        fx.circle({650, 540}, 70, fx.theme.na)
        fx.label("One group", {770, 500}, fx.sans(84, 600, -0.03), fx.theme.nt, {0, 0.5})
        fx.label("moves as one", {770, 590}, fx.sans(44), fx.theme.nm, {0, 0.5})
    }
}

Note: The if is what ends the group. group is declared @(deferred_out = end_group), so Odin calls end_group when the scope the call was made in closes. Written as a statement of its own, fx.group({…}) lasts until the end of the enclosing block.

Xf#

Xf :: struct {
	x, y:     f32,
	s:        Maybe(f32),
	sx, sy:   f32,
	rot:      f32,
	rx, ry:   f32,
	persp:    f32,
	origin:   Vec2,
	o:        Maybe(f32),
	blur:     f32,
	mblur:    Vec2,
	gray:     f32,
	bright:   f32,
	contrast: f32,
	sat:      Maybe(f32),
	tint:     Color,
	invert:   f32,
	blend:    Maybe(sk.Blend),
	post:     Post,
	glow:     Glow,
	clip:     Maybe(Rect),
	radius:   f32,
	bounds:   Maybe(Rect),
}
Field Unit What it does Left out
x, y px Moves the contents. 0
s factor Scales around origin. 1
sx, sy factor Extra scale along one axis, multiplied with s: squash and stretch. 1
rot degrees Turns clockwise around origin. 0
rx, ry degrees Tilts in 3D around the horizontal and the vertical axis through origin. 0
persp px The viewing distance for the tilt. Smaller is more dramatic. 1600
origin px The fixed point of s, sx, sy, rot, rx and ry. {0, 0}
o 0..1 Opacity of the contents as a whole. 1
blur px Gaussian blur, sigma. 0
mblur px Blur along x and along y only, sigma each: motion blur. {0, 0}
gray 0..1 Desaturates. 0
bright factor Multiplies the brightness. 1
contrast −1.. 0.5 is 50% more contrast, negative flattens, −1 is flat gray. 0
sat factor Saturation: 0 is gray, 1.4 vivid. 1
tint color Multiplies every color by this one. none
invert 0..1 Blends toward the negative. 0
blend sk.Blend How the contents land on what is below. normal
post Post A pixel filter over the contents. none
glow Glow Bloom: the bright parts blurred and added back. none
clip px Nothing is drawn outside this rect. no clip
radius px Corner radius of clip. 0
bounds px A hint about where the contents are, to keep the layer small. whole frame

s, o, sat, blend, clip and bounds are Maybe values because their zero means something (s = 0 is a real scale, o = 0 is invisible). You write them like the others, {s = 0.5, o = k, blend = .Screen}, except that a rect has to name its type: clip = fx.Rect{0, 0, 400, 300}, or a variable that is one.

Move, scale, turn#

The transform fields are applied to the contents in a fixed order, whatever order you write them in: scale first, then the turn or tilt, then the move. x and y are therefore always in the parent's pixels, never scaled or rotated themselves.

origin is the point that stays where it is while everything else scales and turns around it. It defaults to {0, 0}, the top-left corner of the stage (or of the group around this one), which is almost never what you want: pass the center of the thing you are transforming (fx.center(r)), or the point it should grow out of. Drag the origin in the demo to see the square swing around it.

A frame of the group-transform demo
3.72 / 6.00 sgroup-transform
group_transform :: proc(s: fx.Shot) {
    fx.backdrop(.Night)
    r := fx.Rect{760, 340, 400, 400}
    origin := fx.Vec2{knob("origin.x", 960, 760, 1160), knob("origin.y", 540, 340, 740)}
    fx.border(r, fx.theme.ns, 2, 56) // where the square is drawn without the group
    if fx.group({rot = 360 * s.p, s = knob("s", 1, 0.3, 2), origin = origin}) {
        fx.rrect(r, 56, fx.theme.nt)
        fx.label("fx", fx.center(r), fx.sans(220, 700, -0.04), fx.theme.night, {0.5, 0.5})
    }
    fx.circle(origin, 16, fx.theme.na) // it turns and scales around this point
}

sx and sy scale one axis on top of s. Their zero value means 1, so setting only sx leaves the height alone. With the origin at the point of contact they make the squash of an impact or the stretch of a fast move:

A frame of the group-squash demo
1.98 / 3.20 sgroup-squash
group_squash :: proc(s: fx.Shot) {
    fx.backdrop(.Night)
    floor, r := f32(840), f32(100)
    p := math.mod(s.lt, 0.8) / 0.8 // progress through one bounce of 0.8 s
    h := 4 * p * (1 - p) // its height, 0 → 1 → 0
    hit := max(1 - p / 0.14, 1 - (1 - p) / 0.14, 0) // 1 at the moment it touches the floor
    amount := knob("amount", 0.35, 0, 0.7)
    y := floor - r - 520 * h
    // wider and flatter on impact, around the point where it touches
    if fx.group({sx = 1 + amount * hit, sy = 1 - amount * hit, origin = {960, y + r}}) {
        fx.circle({960, y}, r, fx.theme.na)
    }
    fx.hline(560, floor, 800, fx.theme.ns, 3)
}

3D tilt#

rx and ry turn the contents out of the screen plane, like a CSS rotateX/rotateY under a perspective. A positive rx leans the top away from the viewer, a positive ry the right edge. persp is how far the viewer is from the plane in px: 1600 by default, 400 for a wide-angle look, several thousand for almost no foreshortening. When a group has a tilt, its rot turns in the same 3D space, around the axis pointing at the viewer.

The contents stay a flat card: there is no depth between the things inside one group. For points and lines in real 3D see 3D.

A frame of the group-tilt demo
3.72 / 6.00 sgroup-tilt
group_tilt :: proc(s: fx.Shot) {
    fx.backdrop(.Night)
    card := fx.Rect{560, 240, 800, 600}
    rx, persp := knob("rx", 12, -60, 60), knob("persp", 1600, 400, 6000)
    ry := 50 * math.sin(s.p * math.TAU) // swings left and right
    if fx.group({rx = rx, ry = ry, persp = persp, origin = fx.center(card)}) {
        fx.rrect(card, 36, fx.theme.sf)
        fx.rect({card.x, card.y + 110, card.w, 2}, fx.theme.bd)
        fx.label("Revenue", {card.x + 56, card.y + 56}, fx.sans(44, 600), fx.theme.ink, {0, 0.5})
        for i in 0 ..< 7 {
            h := 110 + 300 * fx.hash01(3, i)
            bar := fx.Rect{card.x + 62 + f32(i) * 100, card.y + 540 - h, 72, h}
            fx.rrect(bar, 10, i == 5 ? fx.theme.red : fx.theme.ink)
        }
    }
}

Opacity and blur#

o fades the group's contents as one picture. That is not the same as making each color translucent: shapes that overlap inside the group do not show through each other.

A frame of the group-layer demo
3.72 / 6.00 sgroup-layer
group_layer :: proc(s: fx.Shot) {
    fx.backdrop(.Night)
    o := knob("o", 0.5, 0, 1)
    pair :: proc(cx: f32, turn: f32, c: fx.Color) {
        if fx.group({rot = turn, origin = {cx, 480}}) {
            fx.rrect({cx - 230, 290, 300, 300}, 48, c)
            fx.circle({cx + 90, 560}, 170, c)
        }
    }
    // left: each shape is translucent on its own, so the overlap shows
    pair(520, 360 * s.p, fx.alpha(fx.theme.nt, o))
    // right: the shapes are drawn opaque into a layer, and the layer is faded
    if fx.group({o = o}) do pair(1400, 360 * s.p, fx.theme.nt)
    fx.label("fx.alpha(c, o)", {520, 900}, fx.mono(36), fx.theme.nm, {0.5, 0.5})
    fx.label("fx.group({o = o})", {1400, 900}, fx.mono(36), fx.theme.nm, {0.5, 0.5})
}

blur is a gaussian blur over the contents, mblur the same along x and along y separately. A blur that goes to 0 as a title arrives reads as coming into focus; an mblur in proportion to the speed of a move reads as a shutter that stayed open. The demo's square moves at up to 1,885 px a second, and its mblur is the distance it covers in shutter sixtieths of a second.

A frame of the group-blur demo
2.48 / 4.00 sgroup-blur
group_blur :: proc(s: fx.Shot) {
    fx.backdrop(.Night)
    k := fx.win(s.lt, 0.2, 3.8, 1.0, 0.6)
    // blur: a gaussian over everything in the group, here easing to 0 as the word arrives
    if fx.group({blur = knob("blur", 40, 0, 100) * (1 - k), o = k}) {
        fx.label("Focus", {960, 320}, fx.sans(260, 700, -0.04), fx.theme.nt, {0.5, 0.5})
    }
    // mblur: blur along one direction, here as long as the square is fast
    x := 1200 * (0.5 - 0.5 * math.cos(s.lt * math.PI)) // left to right and back in 2 s
    speed := abs(600 * math.PI * math.sin(s.lt * math.PI)) // px per second
    shutter := knob("shutter", 1, 0, 3) / 60 // how long the "shutter" is open, in frames
    if fx.group({x = x, mblur = {speed * shutter, 0}}) {
        fx.rrect({260, 640, 200, 200}, 44, fx.theme.na)
    }
}

When a group costs something#

A group that only moves, scales, turns, tilts and clips is free: it changes the canvas's matrix and clip, and the contents are drawn straight to the frame.

Everything else needs a layer: the contents are drawn into an offscreen picture first, which is then filtered and put on the frame. group makes a layer exactly when one of these holds:

  • o is below 0.998
  • blur, mblur.x or mblur.y is above 0.05
  • any of gray, bright, contrast, sat, tint, invert changes the picture
  • blend is set, to any mode
  • post is set
  • glow has an amount above 0.003 and a radius or streak above 0.05

So a group whose opacity has reached 1 and whose blur has reached 0 stops costing anything by itself: {o = k, blur = 12 * (1 - k)} is a layer during the entrance and a plain transform afterwards.

A layer covers the whole frame unless something limits it. clip does, and so does bounds:

if fx.group({o = k, blur = 20 * (1 - k), bounds = fx.Rect{560, 340, 800, 400}}) {
	// a card that lives in that rect
}

bounds tells the renderer where the contents are, in the same coordinates you draw them in, so the layer and its blur only cover that area. The room that blur, mblur, post and glow need around it is added for you. It is a hint and not a clip: it positions nothing, and what you draw outside it may or may not be cut off. Use it on small faded or blurred things when there are many of them (the library's own letter animations and layout elements do); leave it out when the group fills the frame anyway.

Within a layer the steps run in this order: blur, post, glow, the grade, then opacity and blend as the layer lands on what is below.

Grading#

The six grading fields change the colors of everything in the group. Together they are one color matrix, which is cheap wherever it is used, on one picture or on the whole film.

A frame of the group-grade demo
2.48 / 4.00 sgroup-grade
group_grade :: proc(s: fx.Shot) {
    fx.backdrop(.Night)
    img := fx.load_image("assets/frame.jpg")
    k := (0.5 - 0.5 * math.cos(s.p * math.TAU)) * knob("amount", 1, 0, 1) // 0 → 1 → 0
    looks := [?]struct {name: string, xf: fx.Xf} {
        {"gray", {gray = k}},
        {"sat", {sat = 1 + 1.5 * k}},
        {"bright", {bright = 1 + 1.5 * k}},
        {"contrast", {contrast = 0.8 * k}},
        {"tint", {tint = fx.mix(0xffffffff, fx.theme.na, k)}},
        {"invert", {invert = k}},
    }
    for look, i in looks {
        r := fx.Rect{150 + f32(i % 3) * 550, 110 + f32(i / 3) * 460, 520, 340}
        if fx.group(look.xf) do fx.image(img, r, focus = {0.6, 0.3}, zoom = 1.6)
        fx.label(look.name, {r.x, r.y + r.h + 20}, fx.mono(32), fx.theme.nm)
    }
}
Field | Detail |

| --- | --- | | gray | 0..1, how far toward black and white. It multiplies with sat: the saturation used is sat * (1 - gray). | | sat | 1 is unchanged, 0 gray, above 1 more vivid. Brightness is weighed the way the eye does (Rec. 709), so green stays lighter than blue. | | bright | Multiplies red, green and blue. 0 means unchanged, like 1; for black use tint = 0xff000000. | | contrast | Scales the colors away from middle gray by 1 + contrast. | | tint | Multiplies the colors by the tint's red, green and blue: white is no change, 0xffff0000 leaves only the red channel. Its alpha is ignored. | | invert | 1 is the negative, 0.5 is flat gray on the way there. |

They are applied in that order: saturation, brightness, contrast, tint, invert. A group around all scenes of a video, Video.master, is where a film's overall grade goes.

Blend#

blend decides how the group, once drawn, is combined with what is already on the frame. The contents are first drawn normally among themselves, then the finished layer lands with the mode.

A frame of the group-blend demo
3.72 / 6.00 sgroup-blend
group_blend :: proc(s: fx.Shot) {
    fx.backdrop(.Night)
    modes := [?]sk.Blend{.Plus, .Screen, .Multiply, .Overlay, .Difference, .Exclusion}
    for mode, i in modes {
        r := fx.Rect{150 + f32(i % 3) * 550, 110 + f32(i / 3) * 460, 520, 340}
        fx.rrect_paint(r, 28, fx.linear({r.x, 0}, {r.x + r.w, 0}, fx.theme.night, fx.theme.na, fx.theme.nt))
        x := r.x + r.w * (0.5 + 0.32 * math.sin(s.p * math.TAU))
        // the circle is drawn into a layer, and the layer lands on the gradient with this mode
        if fx.group({blend = mode, clip = r, radius = 28}) {
            fx.circle({x, r.y + r.h / 2}, 120, 0xff3d7bd9)
        }
        fx.label(fmt.tprintf(".%v", mode), {r.x, r.y + r.h + 20}, fx.mono(32), fx.theme.nm)
    }
}

The modes are Skia's, the enum sk.Blend (import sk "effects:sk"). In a group literal the short form is enough: blend = .Screen.

Kind Modes
Lighten .Plus (add), .Screen, .Lighten, .Color_Dodge
Darken .Multiply, .Darken, .Color_Burn, .Modulate
Contrast .Overlay, .Hard_Light, .Soft_Light
Compare .Difference, .Exclusion
Color .Hue, .Saturation, .Color, .Luminosity
Compositing .Clear, .Src, .Dst, .Src_Over (normal), .Dst_Over, .Src_In, .Dst_In, .Src_Out, .Dst_Out, .Src_Atop, .Dst_Atop, .Xor

.Plus is the one for light: glows, sparks and beams that should add up where they overlap. For a single shape, additive does the same on a paint without a layer.

The compositing modes are for masks. .Dst_In keeps of what is below only the part the group covers, so a picture can be cut to the shape of anything you can draw. The outer group is there to give the picture a layer of its own to be cut out of (setting blend, even to the normal mode, makes one):

if fx.group({blend = .Src_Over}) {
	fx.image(img, {100, 100, 800, 500})
	if fx.group({blend = .Dst_In}) { // keep of the image only what the letters cover
		fx.label("MASK", {500, 350}, fx.sans(300, 700, -0.04), 0xffffffff, {0.5, 0.5})
	}
}

Clip#

clip cuts the contents to a rect, with rounded corners when radius is set. The rect is in the group's coordinates and goes through the group's transform with the contents, so a clipped card can still slide, scale and tilt as a whole. Clipping needs no layer.

A frame of the group-clip demo
2.48 / 4.00 sgroup-clip
group_clip :: proc(s: fx.Shot) {
    fx.backdrop(.Night)
    win := fx.Rect{610, 190, 700, 700}
    radius := knob("radius", 60, 0, 350)
    fx.rrect(win, radius, fx.theme.night2)
    // nothing drawn in the group shows outside win
    if fx.group({clip = win, radius = radius}) {
        for i in 0 ..< 13 {
            y := win.y + f32(i) * 140 - 140 * 7 * s.p // the list scrolls up by seven rows a loop
            fx.rrect({win.x + 50, y + 20, win.w - 100, 100}, 24, i % 7 == 3 ? fx.theme.na : fx.theme.nb)
            fx.circle({win.x + 100, y + 70}, 26, i % 7 == 3 ? fx.theme.nt : fx.theme.ns)
        }
    }
    fx.border(win, fx.theme.ns, 2, radius)
}

A clip is also how an image gets rounded corners, and how something is revealed: animate the clip's width and the contents are wiped in.

Post filters#

post runs a pixel filter over the group: the contents are drawn into a layer and every pixel of the result is computed from it by a shader.

if fx.group({post = fx.rgb_split(8)}) {
	fx.caption("split", {960, 540}, 220, fx.theme.nt, {0.5, 0.5})
}

The built-in ones (rgb_split, glitch, zoom_blur, pixelate, duotone, scanlines, halftone, dither, lens, shockwave, kaleido, warp, stretch) and writing your own are on Post filters. On the CPU a filter that reads each pixel three times costs about 0.05 s per 1080p frame, so keep them to the frames that need them. A group has one post; nest groups to chain them. Because a group's post runs before its grade, a filter that should see graded colors (a dither over a black-and-white picture) goes on a group around the graded one.

Glow#

glow is bloom: what is brighter than a threshold is blurred and added back on top, optionally with a wide horizontal streak.

if fx.group({glow = fx.glow(24, 0.8, 0.5)}) { // radius in px, amount, threshold
	…
}

It runs after the group's blur and post, and works best on a group that paints its own background. It is made of Skia's own blur and costs about 0.02 s per 1080p frame on the CPU. The Glow fields and examples are on Post filters.

Nesting#

Groups nest, and each one works in the coordinates of the one around it: an inner group's x and origin are positions inside the outer group, and its opacity multiplies with the outer one's.

if fx.group({x = 1400, y = 500, rot = 360 * s.p}) {       // the planet's place, turning once a loop
	fx.circle({0, 0}, 60, fx.theme.na)                     // {0, 0} is now {1400, 500}
	if fx.group({x = 260, rot = -720 * s.p}) {             // 260 px out, carried around by the turn
		fx.circle({0, 0}, 30, fx.theme.nt)
		fx.circle({70, 0}, 12, fx.theme.nm)                // a moon of the moon
	}
}

Nesting is also how you choose the order of things one group does in a fixed order: to move first and scale afterwards, put the move in an inner group and the scale in an outer one. A layer inside a layer is a second offscreen picture, so keep the filtered groups as far out as the look allows: one blur around ten cards, not ten blurred cards.

Without the scope#

group_begin :: proc(xf: Xf) -> bool
end_group   :: proc(ok: bool)

group_begin is group without the automatic end, for when a group starts in one procedure and ends in another. Hand its result to end_group, and skip the drawing when it returned false:

ok := fx.group_begin({o = 0.5, bounds = fx.Rect{100, 700, 600, 300}})
if ok {
	fx.rect({100, 700, 400, 200}, fx.theme.nt)
	fx.rect({300, 800, 400, 200}, fx.theme.nt)
}
fx.end_group(ok)

Frosted glass#

frosted :: proc(r: Rect, sigma: f32, radius: f32 = 0) -> bool

frosted blurs what is already drawn behind r by sigma px, and then draws the rest of its scope on top, clipped to r (with corners rounded by radius). It is used like a group and always returns true:

A frame of the group-frosted demo
3.72 / 6.00 sgroup-frosted
group_frosted :: proc(s: fx.Shot) {
    fx.backdrop(.Night)
    colors := [?]fx.Color{fx.theme.na, 0xff3d7bd9, 0xff2f9e6b}
    for c, i in colors {
        a := s.p * math.TAU + f32(i) * math.TAU / 3
        fx.circle({960 + 420 * math.cos(a), 540 + 240 * math.sin(2 * a)}, 150, c)
    }
    glass := fx.Rect{560, 290, 800, 500}
    // what is already on the canvas behind glass is blurred; the scope draws on top of it
    if fx.frosted(glass, knob("sigma", 30, 0, 80), 48) {
        fx.rect(glass, 0x14ffffff)
        fx.label("Frosted", fx.center(glass), fx.sans(120, 600, -0.03), fx.theme.nt, {0.5, 0.5})
    }
    fx.border(glass, 0x40ffffff, 2, 48)
}

Only what was drawn before the call is blurred, so the order of the calls is the order of the layers: background first, then the glass, then whatever floats above it. A translucent fill and a hairline border are what make it read as glass. Inside a group that is a layer, "behind" is what that group has drawn so far.

Reference#

Name What it is
Xf What a group does to its contents: transform, opacity, blur, grade, blend, post, glow, clip.
group(xf) Applies xf to what is drawn until the scope ends; false (and nothing drawn) when invisible.
group_begin(xf), end_group(ok) The same in two calls.
frosted(r, sigma, radius) Blurs what is behind r and draws its scope on top.
grade_matrix(xf) The 4×5 color matrix (row-major, as [20]f32) for an Xf's grading fields, and false when they change nothing.
tilt_matrix(origin, rx, ry, rz, persp) The 4×4 matrix of a tilt, see the stage.

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