Shaders
A shader paints a shape pixel by pixel on the graphics card. In Formo a shader is a fill (or a stroke) like a colour or a gradient: pick one of about 100 built-in shaders from the fill's Shader tab, or write your own. Your shader can read any node's properties and the mouse, its settings appear in the properties panel, and you can step through its code pixel by pixel in the debugger.
Try it
Open a shape's fill, choose Shader, press + and replace the code with this:
// @name("Rings")
// @description("Rings running out from the centre")
// @for("fill", "stroke")
uniform Color ink; // @default(0.07, 0.08, 0.18, 1.0) @label("Ink")
uniform float count; // @default(12.0) @range(1.0, 40.0) @label("Rings")
vec4 frag() {
float d = length(uv - 0.5); // distance from the centre of the shape
float width = clamp(Knob.rotation / 360.0, 0.1, 0.9);
float ring = step(width, fract(d * count - time));
return mix(vec4(1.0), ink, ring);
}The panel now shows Ink and Rings for this fill. The rings run outwards with time, and turning a shape called Knob makes them thinner or thicker.
One file: description, code, panel
A .shader file holds three things:
- Header —
// @name("…"),// @description("…"),// @tags(…)and// @for("fill", "stroke"), which says where the shader can be used (fill,stroke, orfxfor camera effects). - Code — GLSL ES 3.00 (WebGL 2) with exactly one function,
vec4 frag(), that returns the colour of the current pixel as straight RGBA. Do not multiply by coverage or opacity: Formo applies the shape's edge, opacity and mask afterwards. - Panel — every
uniformbecomes a field in the properties panel, described by a comment on the same line.
Formo writes main() for you. The vertex stage will go in the same file as a vert() function. That is not supported yet: for now Formo supplies the vertex stage, and a vert() is reported as an error.
What the code can read
Declared for you, only when you use them:
uv— position inside the shape's box, 0 to 1, from the top-left corner.pos— position in pixels from the node's origin.size— the box size in pixels.time— seconds of play time; animated shaders also animate in MP4 and GIF export.coverage,distanceToEdge— how much of the pixel the shape covers, and how far it is from the outline.backdrop()— the colour already drawn behind the shape (glass, heat haze).
Reading the scene
Write a node property straight into the GLSL, the same way as in a formula: Rect1.y, Ball.rotation, mouse.x. Formo turns each one into a uniform and updates it every frame, so the shader redraws when the node moves, a keyframe plays or a formula changes the value. A missing node or a value that is not a number reads as 0. A uniform you declare yourself wins over a node with the same name.
Panel fields
The comment after a uniform describes its field:
uniform float speed; // @default(1.0) @range(0.0, 5.0) @step(0.1) @label("Speed")
uniform Color tint; // @default(1.0, 0.4, 0.3, 1.0) @label("Tint")
uniform Point center; // @label("Center")
uniform Angle turn; // @default(45.0) @label("Turn")@default(…)— the starting value (one number, or up to four for vectors and colours).@range(min, max),@step(n)— limits and step of a number field.@label("…")— the name shown in the panel.
Types with their own field:
Color— a colour picker, linked to the document's palette.Point— a handle you drag on the canvas; the shader receives it inuvunits.Angle— degrees in the panel, radians in the code.Image,Light,Perspective— pick an image, a light or a perspective from the scene.float,int,bool,vec2— plain fields.- Arrays, such as
Point pts[8]; Color cols[8]; int count; // @count(pts), make a list of stops like a gradient.
Handles on the canvas can be tied to fields with @color(c), @radius(p), @length(a, p), @anchor(p) and @count(pts).
Formulas and keyframes on shader fields
Each field is a property of the paint, so formulas and keyframes reach it:
// Star1.fills[0].params.count
10 + sin(time) * 4fills[0].params.speed— a number, int, bool (0or1) or angle (in degrees).fills[0].params.center.x— one part of aPointor vector;params.centeris the whole[x, y].fills[0].params.tint.color— the whole colour;params.tint.rand the other channels work too.
A field driven by a formula is locked in the panel, and its canvas handle stops moving.
The debugger
In the shader editor, open the side panel and click a pixel in the preview. Time stops and the code halts on its first line, for that pixel:
- step into, over, out or back, with buttons or F5, F10 and F11;
- every variable's value at this pixel, colours as swatches,
vec2values as markers on the preview, and a zoomed 3×3 neighbourhood; - click the gutter for a breakpoint; give it a condition such as
v > 0.9and Run plays time forward until the condition holds at the picked pixel. Conditions can read the scene too (Rect3.x > 100).
Other pixels at the same moment
While the code is stopped on a line, click another pixel, or drag across the preview or the zoom lens. The new pixel runs to the same place: the same line, in the same function call and the same pass of every loop. Every variable now shows that pixel's value, at the point where you stopped.
On a graphics card each pixel runs the code on its own, and the usual tools show you only the colour that comes out. Here you can stop in the middle of the code and look across the pixels instead. Find the edge where a value jumps, see which pixels take the other side of an if, or watch a loop variable change from one cell of a pattern to the next. If a pixel never reaches that line, for example because it took the other branch, the debugger says so and shows the pixel's final colour instead.
Moving to another pixel while stopped is part of every paid plan. On Free, pick the pixel you want before you step; picking another one starts it from the top.
The debugger runs your shader in a software copy of the graphics pipeline and checks texture reads against a real graphics card on our server, so it needs you to be signed in and online. Camera effects (@for("fx")) cannot be debugged yet, and filtered texture reads are approximate.