Pipelines

Snippets and the Toolbox

Reusable GLSL, as code or as nodes.

The Toolbox is the second activity in the bar on the left. It holds more than 400 snippets of GLSL, in nine families (a few, such as optical flow, are advanced):

FamilyWhat is in it
ShapesSigned distance functions in 2D and 3D, the operations that combine them, and what turns a distance into a picture.
Noise & patternsNoise of every kind, patterns and terrain.
ColorColor conversions, the blend modes (by lighten, darken, contrast…) and compositing.
Image & filtersSampling, kernels, morphology, edges, anti-aliasing and post-processing.
Space & cameraTransforms in 2D and 3D, coordinates and camera.
Lighting & 3DLighting, atmosphere, raymarching, and the maps (normal, height, displacement).
MathCurves, vectors and the small functions that every shader needs.
Audio & analysisReading sound, optical flow and analysis.
Text & SpritesText rendering and sprite handling.

Each family opens into groups, and a pill says how many snippets there are under it. The name of a snippet and the folder it was kept in are both searched: typing sdf finds the shapes.

Snippet or macro?

The Toolbox keeps two kinds of reusable things, and they are not the same:

SnippetMacro
What it isOne GLSL function, with its helpers: code.A group of nodes of a pipeline: nodes.
In a pipelineA node, with the inputs and the output of its function. Its code is shown by Show Code and, for one you wrote, changed by Edit Code….A group that you open, read and change like any other, with its own inputs and outputs.
In a shaderYes: the function is put above main().No: a macro is made of nodes, which a shader does not have.
Made byNearly 300 come with the app; you write yours (see below).You: select nodes, Create Macro.
Kept inThe Snippets and My Snippets sections.The Macros section.
When you change itA copy already placed keeps its own code.The copies follow the macro: saving it updates them (see Macros).
SharedThe code is in the pipeline that uses it: nothing else to send.The pipeline carries its copy; to give someone your macros themselves, export a pack (.istmacro).

A rule of thumb: if you can say it as a formula (a noise, a distance, a color conversion), it is a snippet. If it is a way of wiring nodes together (a blur with its passes, a correction with its inputs), it is a macro. A macro can hold snippet nodes, so a snippet you wrote can travel inside a macro too. A whole shader or pipeline of your library, used as one node, is something else again: see Composing.

Use one

Drag a snippet from the list onto the shader or the pipeline you have open: while you drag, the editor shows a dashed frame, Drop to add the snippet. In a shader the snippet is added to the fragment code, as one edit you can undo; in a pipeline it becomes a node. You can also right-click a snippet and choose Add to Shader or Add to Pipeline. The hint at the top of the list says which one the open document takes. A snippet written for a vertex shader is greyed while you edit a fragment one, and the other way round.

See one at work

Right-click a snippet in the list, or its node in a pipeline, and choose Open Example. The example opens in a tab of its own: a small pipeline that uses the snippet, with notes (comments) beside the nodes that say what each stage is for and what to try. Some snippets that are used together share one example. An example that needs a sound or a moving picture brings one: the audio ones listen to a sample sound, and the ones that follow motion play a sample video, until you plug your own source.

An example is a copy to play with: change it, run it, break it. It is not a file, and nothing you do to it changes the example itself. When you close a tab you have changed, ISF Studio asks whether to keep your version, in your library or in any folder (Save As), or to let it go. Opening the example again gives you the original.

See the code of a node

Right-click the node of a snippet in a pipeline and choose Show Code. The box is the one a double click on a snippet of the list opens. For a snippet of the library it shows its details too; for a node that has code of its own (the ones you write, or a pipeline import makes), it shows that code, as it is in the pipeline: a snippet you wrote has Edit Code… instead.

Write your own

Right-click the canvas and choose Functions > Snippet… (it is in the search field too). A box opens with a function to start from: change it, then press Save (Cmd+Enter). The last function of the code is the snippet: its parameters are the inputs of the node, its value is the output called result, and an out parameter is one more output. The functions and constants above it are its helpers, and travel with it.

float hash12(vec2 p) {
  return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);
}

vec4 finishColor(vec3 color, vec2 fragCoord, float grainAmount) {
  float noise = hash12(fragCoord + fract(TIME) * 61.7) - 0.5;
  color += noise * 0.05 * grainAmount;
  return vec4(clamp(color, 0.0, 1.0), 1.0);
}

This one is a node with three inputs (color, fragCoord, grainAmount) and one output, and hash12 is its helper.

Right-click the node and choose Edit Code… to change it later. The inputs that are still in the code keep their wires; the ones it no longer has lose them. The box tells you when the code has no function, or when another snippet of the pipeline already has a function of the same name (the helpers too) with other code (a shader has one function of a name). Undo brings the old code back.

The code is kept in the pipeline, so the pipeline works anywhere, with nothing else installed.

My Snippets

The My Snippets view of the toolbox lists the snippets you kept, the newest first. They belong to your computer, not to a pipeline or a library, and every window sees them. There are three ways to make one:

  • From a shader. Select a function (and the helpers above it, if it has some) in the code of a fragment shader, right-click the selection and choose Make a Snippet…. A box opens with the code and the name of the function; give it a description if you like, and the same details the snippets of ISF Studio have (their sources apart): a Version, a Credit and a License, all optional, then press Keep. The credit and the license show under the description in the list, and are written as // Credit: and // License: comments above the code when you add the snippet to a shader or a pipeline, as the snippets of ISF Studio do. The entry is greyed out while nothing is selected; if what is selected is not a function, the box says what is wrong and you can put it right there.
  • From a pipeline. Right-click a snippet you wrote and choose Save to My Snippets….
  • From scratch. Write one in a pipeline, then save it as above.

Drag a snippet of the list onto the code of a shader (the function goes above main()) or onto the canvas of a pipeline (a node with its own code, where you drop it), or right-click it and choose Add to Shader or Add to Pipeline (the node goes in the middle of the canvas). A helper the shader already has, as it is, is not pasted again; adding it to a shader that has a function of the same name with other code is refused with the reason: GLSL has one function of a name. Edit… (or a double click) changes it, Delete lets it go. Snippets you added to a shader or a pipeline are copies: changing or deleting the kept one does not change them.

If a snippet cannot be kept (the code is not a function, the disk cannot be written, you have 500 already), the box stays open and says why. If the file that holds your snippets is damaged, the app puts it aside as my-snippets.v1.json.broken, next to it in the folder where ISF Studio keeps its data, and starts with an empty list.

Find one

Type in the search field, or open a category. A snippet shows its code and what it takes and returns.

In a shader

Drop a snippet into the code of a shader and it is inserted there.

In a pipeline

Drop it on the canvas: it becomes a node with a port for each parameter and a result port for what it returns. The Usage of the node, in the inspector, says how it is used:

  • Call immediately (the default): the node calls the function right there, with the values wired into it, and gives the result. This is the direct way.
  • Define a function: the node only declares the GLSL function, and gives an fn port. Wire it to a Call Function node, which calls it with its own arguments, wherever and as often as you need. This is the more declarative way: one definition, several calls. The parameters are supplied by the calls, so they cannot be wired on the defined node, and the returned value belongs to the call.

Changing the usage removes the connections to the ports that the node no longer has. Inside a function, only Call immediately is possible, since GLSL has no function inside a function.

Shapes: signed distance functions

A big thank you to Inigo Quilez. A large part of the SDF snippets comes from his work: for years he has described, derived and shared these distance functions, with clear articles and live examples. To understand how they work, and to find many more, read his articles on 2D distance functions and 3D distance functions, and his other writing at iquilezles.org.

The Shapes family holds signed distance functions: for a point, they give the distance to a shape, negative inside and positive outside. The main groups are:

  • 2D: shapes in the plane (circle, boxes, triangles, stars, arcs, heart, ellipse, and more). They take the point p of the pixel, centred on the origin, and the size of the shape.
  • 3D: solids (sphere, box, torus, cones, cylinders, capsules, and more). They take a point in space, for raymarching.
  • Operations: combine, round, hollow, blend or displace distances (union, subtraction, intersection and their smooth versions, onion, round, morph, displace, extrude).
  • Rendering: turn a distance into something visible: Fill Mask and Stroke Mask, with anti-aliased edges.

Move and scale a shape by changing the point you give it, not the shape itself: subtract a position from p to move it, divide p to scale it (and multiply the distance back, or use Scale Distance Correction).

For a 3D shape from a 2D one, give the 2D distance to Shapes > Operations > Extrude, with z and halfDepth, and raymarch the result. The normals and lighting snippets then shade it.

Two other snippets go well with them: Math has Cubic Bezier Point and Quadratic Bezier Point, which give the position on a curve at the parameter t (normally between 0 and 1), and Color > Compositing > Depth Over, which puts the surface with the greater depth in front and gives the nearest depth to the next layer. A transparent sample does not hide what is behind it.

Maps: normal, height and displacement

The Maps group of Lighting & 3D holds what a texture artist makes of a surface: a height map (how high each point is), the normal map that says which way it faces, a displacement that moves a picture, and the masks that come from them. They work in tangent space: +Z comes out of the surface, +X goes right and +Y goes up, the OpenGL convention (green up) of Blender, Unity and glTF. Flip Normal Green turns a map to the DirectX one (Unreal, 3ds Max).

GroupSnippets
HeightHeight From Image (the luminance as a height, with contrast and bias), Height Gradient (the slope, by Sobel), Height Ambient Occlusion (a hollow is darker) and Curvature From Height (crests and cavities, signed).
NormalNormal From Height Map (from the red channel of a height texture), Tangent Normal From Heights (from four heights, when the height is code and not a picture), Normal To Color and Color To Normal (between -1 to 1 and 0 to 1), Flip Normal Green, Blend Normals (RNM) (the detail of one map on another, reoriented along the base) and Normal Strength.
DisplacementDisplace UV (a height moves the coordinates), Normal Offset UV (a cheap refraction), Parallax Offset (a flat picture with depth) and Parallax Occlusion UV (a ray marched through the height field, up to 64 layers).
MaskSlope Mask (steep parts from the normal: rock on slopes, snow on flats).
DepthLinearize Depth (a depth buffer value as a distance) and Depth To Unit (a distance as a 0 to 1 depth map).

Strength in the normal snippets is how many pixels tall a height of 1 is: at 1 the relief is gentle, at 32 it is deep, and at 63 on a 64-pixel ramp it is a slope of 45 degrees.

A normal map from a height, as a file

A height is drawn in a first pass, kept in a float target, and a second pass turns it into a normal. The preview shows it as colors (a normal has negative numbers, which the screen draws as black), and the snapshot as an OpenEXR 32-bit float with Values set to As drawn keeps the normal as it is, from -1 to 1.

/*{
  "DESCRIPTION": "A normal map of a height",
  "ISFVSN": "2.0",
  "CATEGORIES": ["Generator"],
  "INPUTS": [
    { "NAME": "strength", "TYPE": "float", "DEFAULT": 24.0, "MIN": 0.0, "MAX": 64.0 }
  ],
  "PASSES": [ { "TARGET": "height", "FLOAT": true }, { } ]
}*/

// Toolbox: Lighting & 3D > Maps > Height > Height Gradient, Lighting & 3D > Maps > Normal > Normal From Height Map
// (drop them above main, or onto the canvas of a pipeline).

void main() {
  if (PASSINDEX == 0) {
    // Any height will do: here, a field of bumps. For a picture, use Height From Image.
    vec2 p = isf_FragNormCoord * 6.0;
    float h = 0.5 + 0.5 * sin(p.x * 3.14159) * sin(p.y * 3.14159);
    gl_FragColor = vec4(vec3(h), 1.0);
  } else {
    gl_FragColor = vec4(normalFromHeightMap(height, isf_FragNormCoord, RENDERSIZE, strength), 1.0);
  }
}

The same two passes make a displacement or a height file: write the height in the last pass, and save it as a 32-bit float so that no step shows when the engine reads it.