import icon Processor#

The Processor is a node that allows you to generate images with code. You can use the Processor to create nodes that create new functionality that doesn’t exist already within IlluGen.

A code editor is provided within the processor node, where you can write GLSL like code. The processor node can interact with other nodes within the project. Offering custom inputs, outputs and parameters.

What is a Shader?#

A shader is a small program that runs on the GPU.

The processor runs your shader once for every pixel of its output. Each time it runs, the shader works out the value of a single pixel and writes it to the output. Every pixel runs the same code and can know its own position in the texture using uv:

@output float result;

void main() {
    // create a gradient from left to right
    result = uv.x;
}
A horizontal gradient from black on the left to white on the right

Output

uv.x goes from 0 at the left edge to 1 at the right edge, so this shader makes a gradient from black on the left to white on the right.

Each pixel is worked out on its own and cannot see what other pixels are writing.

When to Use the Processor#

Use the processor when:

  • No existing node does what you need. For example a custom pattern, or a formula that combines your inputs in a specific way.

  • A setup needs many nodes that can be better represented using a single processor node. A few lines of code can be easier to read and change than a long chain of nodes.

If an existing node already does what you want, it is usually the better choice as they have been more optimised.

Writing a Shader#

All shaders require a main function:

void main() {
    ... // your code goes here
}

Every pixel will run this main function and then write to the outputs.

A basic processor shader can look like:

@parameter float radius    = 0.3;
@parameter float thickness = 0.05;
@output    float ring;

void main() {
    float d = length(uv - vec2(0.5));
    ring = 1.0 - smoothstep(0.0, thickness, abs(d - radius));
}

The shader above draws a white ring on a black background.

radius and thickness show up as parameters on the node, so the ring can be changed without editing the code.

A soft white ring on a black background

Output

Outputs#

Every processor node requires at least one output. These are marked in the global scope of the code with the @output tag, with a type and a name. A basic color output could look like:

// this is a color texture output
// its pin name is 'my_output'
@output vec4 my_output;

The type used for the output determines the texture type used within the illugen graph. The types map to these texture types:

Type

Texture

float

Grayscale

vec2

UV

vec3

Normal

vec4

Color

You can additionally use the label and desc options to add custom labels and descriptions that will be shown in the IlluGen UI. For example

@output(label: "The Result", desc: "My really cool output") vec4 result;

Labels must be unique amongst all outputs, an output can’t share a label with another output. A variable without a label uses its name as its label.

Inputs#

Inputs let a processor node read textures from other nodes in the graph. These are marked in the global scope of the code with the @input tag, with a type and a name. A basic color input could look like:

// this is a color texture input
// its pin name is 'my_input'
@input vec4 my_input;

The type used for the input determines the texture type used within the illugen graph. The types map to these texture types:

Type

Texture

float

Grayscale

vec2

UV

vec3

Normal

vec4

Color

An input is a texture reference, so it needs to be read with sample(texture, uv) to get the pixel content out of the texture:

@input  vec4 my_input;
@output vec4 my_output;

void main() {
    my_output = sample(my_input, uv);
}
The IlluGen logo used as the input

Input

The output, an unchanged copy of the IlluGen logo

Output

Sampling at uv reads the input at the same position as the pixel being written, so the shader above copies the input unchanged. Sampling at a different position moves the image around. This shader bends the input into ripples spreading out from the center:

@parameter float speed     = 4.0;
@parameter float intensity = 0.1;
@input     vec4  my_input;
@output    vec4  my_output;

void main() {
    vec2  offset = uv - vec2(0.5);
    float d      = length(offset);
    float wave   = sin(d * 40.0 - time * speed) * intensity;
    my_output = sample(my_input, uv + offset * wave);
}
The IlluGen logo used as the input

Input

Output

Each pixel reads the input a little closer to or further from the center, which bends the image into rings.

By default inputs are sampled with linear filtering, blending between the nearest pixels, and the UV is clamped to the edge of the texture so coordinates outside of 0 to 1 return the colour of the nearest edge pixel. To change this you can give the input a filter and a border option:

Option

Description

linear

Bilinear filtering, blending between the nearest pixels. This is the default.

nearest

Point filtering, using the colour of the nearest pixel.

clamp

Clamps to the edge pixel’s colour. This is the default.

mirror

Mirrors along the edge pixel’s colour.

repeat

Wraps and repeats the edge pixel colours from the other side.

You can additionally use the label and desc options to add custom labels and descriptions that will be shown in the IlluGen UI. For example

@input(nearest, repeat, label: "Base Colour", desc: "The colour to tint") vec4 base;

Only one filter and one border option can be given per input. Labels must be unique amongst all inputs and parameters, an input can’t share a label with another input or a parameter. A variable without a label uses its name as its label.

Parameters#

Parameters add values to the node details that can be edited, animated or connected to modulators in the graph. These are marked in the global scope of the code with the @parameter tag, with a type, a name and an optional default value. A basic float parameter could look like:

// this is a float parameter named 'strength'
// with a default value of 0.5
@parameter float strength = 0.5;

The default value must be a constant. A parameter without a default value starts at zero.

The type used for the parameter determines the fields shown in the node details. Parameters can be any of these types:

Type

Field

float, vec2, vec3, vec4

One decimal slider per component

int, ivec2, ivec3, ivec4

One whole number slider per component

bool

A checkbox

Parameters are limited to 16 components per node. A scalar type takes up a single component while a vec4 takes up 4 components.

Parameters can be used anywhere within the shader like a regular variable:

@parameter float strength = 0.5;
@input     vec4  my_input;
@output    vec4  my_output;

void main() {
    my_output = sample(my_input, uv) * strength;
}
The IlluGen logo used as the input

Input

The IlluGen logo darkened by the strength parameter

Output

You can additionally give a parameter a list of options to control how it shows up and behaves within the node details:

Option

Description

bounds: MIN..MAX

The range of the parameter’s slider. MIN and MAX must be constant numbers and MIN must be less than MAX. Only numeric parameters can have bounds.

bounded

Clamps the value so it can’t go below MIN or above MAX.

bound_lower

Clamps the value so it can’t go below MIN.

bound_upper

Clamps the value so it can’t go above MAX.

power: VALUE

The response curve of the slider. Values above 1 give finer control near MIN. Must be a positive constant number, defaults to 1.

units: "TEXT"

The units displayed alongside the value.

label: "TEXT"

The name displayed in the node details. Defaults to the variable name.

desc: "TEXT" description: "TEXT"

The description displayed when hovering over the parameter.

static

Marks the parameter as static, so it can’t be animated, randomized or connected to a pin.

color

Shows a color picker instead of sliders. Only for vec3 and vec4 parameters, and can’t be combined with bounds, power or units.

For example

@parameter(bounds: 0..360, bounded, units: "deg", label: "Angle", desc: "Rotation angle in degrees")
float angle = 45;

@parameter(bounds: 0..1, power: 2) float softness = 0.25;

@parameter(static, label: "Iterations") int iterations = 4;

Labels must be unique amongst all inputs and parameters, a parameter can’t share a label with an input or another parameter. A variable without a label uses its name as its label.

Types#

The processor supports various scalar, vector and matrix types. These map to the GLSL types of the same name.

Matrices are represented as columns x rows and are stored in column major form. That is to say a mat2x4 matrix has 2 columns and 4 rows.

All the supported types are:

Type

Description

float

A 32 bit floating point scalar.

vec2

A vector of 2 32 bit floating point values.

vec3

A vector of 3 32 bit floating point values.

vec4

A vector of 4 32 bit floating point values.

mat2x2 / mat2

A 2x2 matrix of 32 bit floating point values.

mat2x3

A 2x3 matrix of 32 bit floating point values.

mat2x4

A 2x4 matrix of 32 bit floating point values.

mat3x2

A 3x2 matrix of 32 bit floating point values.

mat3x3 / mat3

A 3x3 matrix of 32 bit floating point values.

mat3x4

A 3x4 matrix of 32 bit floating point values.

mat4x2

A 4x2 matrix of 32 bit floating point values.

mat4x3

A 4x3 matrix of 32 bit floating point values.

mat4x4 / mat4

A 4x4 matrix of 32 bit floating point values.

int

A 32 bit integer.

ivec2

A vector of 2 32 bit integer values.

ivec3

A vector of 3 32 bit integer values.

ivec4

A vector of 4 32 bit integer values.

uint

A 32 bit unsigned integer.

uvec2

A vector of 2 32 bit unsigned integer values.

uvec3

A vector of 3 32 bit unsigned integer values.

uvec4

A vector of 4 32 bit unsigned integer values.

bool

A true/false scalar.

bvec2

A vector of 2 true/false values.

bvec3

A vector of 3 true/false values.

bvec4

A vector of 4 true/false values.

Globals#

Globals are variables that live outside of any function. Some are built in and provided by the processor alongside ones that you declare your own.

Builtin Globals#

The processor has some builtin globals that you can use to drive your shader. They are read-only, so they can not be assigned to.

Global

Type

Description

uv

vec2

The UV coordinate of the pixel.

time

float

The timeline time in seconds.

resolution

ivec2

The resolution of the node.

User Defined Globals#

Variables declared outside of functions are global, and can be read and written by every function declared after them:

float total;

void add(float x) {
    total += x;
}

void main() {
    add(1.0);
    add(2.0);
    float result = total; // 3.0
}

Builtin Functions#

The processor has access to many builtin functions. These match the GLSL functions of the same name.

Trigonometry#

Function

Description

float sin(float angle)
vec2 sin(vec2 angle)
vec3 sin(vec3 angle)
vec4 sin(vec4 angle)

The sine of angle in radians.

float cos(float angle)
vec2 cos(vec2 angle)
vec3 cos(vec3 angle)
vec4 cos(vec4 angle)

The cosine of angle in radians.

float tan(float angle)
vec2 tan(vec2 angle)
vec3 tan(vec3 angle)
vec4 tan(vec4 angle)

The tangent of angle in radians.

float asin(float x)
vec2 asin(vec2 x)
vec3 asin(vec3 x)
vec4 asin(vec4 x)

The arc sine of x, in radians. x must be within -1 and 1.

float acos(float x)
vec2 acos(vec2 x)
vec3 acos(vec3 x)
vec4 acos(vec4 x)

The arc cosine of x, in radians. x must be within -1 and 1.

float atan(float y_over_x)
vec2 atan(vec2 y_over_x)
vec3 atan(vec3 y_over_x)
vec4 atan(vec4 y_over_x)

The arc tangent of y_over_x, in radians between -PI/2 and PI/2.

float atan(float y, float x)
vec2 atan(vec2 y, vec2 x)
vec3 atan(vec3 y, vec3 x)
vec4 atan(vec4 y, vec4 x)

The arc tangent of y / x, in radians between -PI and PI, using the signs of both to pick the quadrant.

float radians(float degrees)
vec2 radians(vec2 degrees)
vec3 radians(vec3 degrees)
vec4 radians(vec4 degrees)

Converts degrees to radians.

float degrees(float radians)
vec2 degrees(vec2 radians)
vec3 degrees(vec3 radians)
vec4 degrees(vec4 radians)

Converts radians to degrees.

Exponential#

Function

Description

float pow(float x, float y)
vec2 pow(vec2 x, vec2 y)
vec3 pow(vec3 x, vec3 y)
vec4 pow(vec4 x, vec4 y)

x raised to the power y.

float exp(float x)
vec2 exp(vec2 x)
vec3 exp(vec3 x)
vec4 exp(vec4 x)

e raised to the power x.

float exp2(float x)
vec2 exp2(vec2 x)
vec3 exp2(vec3 x)
vec4 exp2(vec4 x)

2 raised to the power x.

float log(float x)
vec2 log(vec2 x)
vec3 log(vec3 x)
vec4 log(vec4 x)

The natural logarithm of x.

float log2(float x)
vec2 log2(vec2 x)
vec3 log2(vec3 x)
vec4 log2(vec4 x)

The base 2 logarithm of x.

float sqrt(float x)
vec2 sqrt(vec2 x)
vec3 sqrt(vec3 x)
vec4 sqrt(vec4 x)

The square root of x.

float inversesqrt(float x)
vec2 inversesqrt(vec2 x)
vec3 inversesqrt(vec3 x)
vec4 inversesqrt(vec4 x)

One divided by the square root of x.

Common#

Function

Description

float abs(float x)
vec2 abs(vec2 x)
vec3 abs(vec3 x)
vec4 abs(vec4 x)
int abs(int x)
ivec2 abs(ivec2 x)
ivec3 abs(ivec3 x)
ivec4 abs(ivec4 x)
uint abs(uint x)
uvec2 abs(uvec2 x)
uvec3 abs(uvec3 x)
uvec4 abs(uvec4 x)

The absolute value of x.

float sign(float x)
vec2 sign(vec2 x)
vec3 sign(vec3 x)
vec4 sign(vec4 x)
int sign(int x)
ivec2 sign(ivec2 x)
ivec3 sign(ivec3 x)
ivec4 sign(ivec4 x)
uint sign(uint x)
uvec2 sign(uvec2 x)
uvec3 sign(uvec3 x)
uvec4 sign(uvec4 x)

-1, 0 or 1 depending on whether x is negative, zero or positive.

float floor(float x)
vec2 floor(vec2 x)
vec3 floor(vec3 x)
vec4 floor(vec4 x)

The largest whole number less than or equal to x.

float ceil(float x)
vec2 ceil(vec2 x)
vec3 ceil(vec3 x)
vec4 ceil(vec4 x)

The smallest whole number greater than or equal to x.

float round(float x)
vec2 round(vec2 x)
vec3 round(vec3 x)
vec4 round(vec4 x)

x rounded to the nearest whole number.

float trunc(float x)
vec2 trunc(vec2 x)
vec3 trunc(vec3 x)
vec4 trunc(vec4 x)

x with its fractional part removed, rounding towards zero.

float fract(float x)
vec2 fract(vec2 x)
vec3 fract(vec3 x)
vec4 fract(vec4 x)

The fractional part of x, x - floor(x).

float mod(float x, float y)
vec2 mod(vec2 x, vec2 y)
vec3 mod(vec3 x, vec3 y)
vec4 mod(vec4 x, vec4 y)
vec2 mod(vec2 x, float y)
vec3 mod(vec3 x, float y)
vec4 mod(vec4 x, float y)

The remainder of x / y, x - y * floor(x / y).

float min(float a, float b)
vec2 min(vec2 a, vec2 b)
vec3 min(vec3 a, vec3 b)
vec4 min(vec4 a, vec4 b)
int min(int a, int b)
ivec2 min(ivec2 a, ivec2 b)
ivec3 min(ivec3 a, ivec3 b)
ivec4 min(ivec4 a, ivec4 b)
uint min(uint a, uint b)
uvec2 min(uvec2 a, uvec2 b)
uvec3 min(uvec3 a, uvec3 b)
uvec4 min(uvec4 a, uvec4 b)
vec2 min(vec2 a, float b)
vec3 min(vec3 a, float b)
vec4 min(vec4 a, float b)
ivec2 min(ivec2 a, int b)
ivec3 min(ivec3 a, int b)
ivec4 min(ivec4 a, int b)
uvec2 min(uvec2 a, uint b)
uvec3 min(uvec3 a, uint b)
uvec4 min(uvec4 a, uint b)

The smaller of a and b.

float max(float a, float b)
vec2 max(vec2 a, vec2 b)
vec3 max(vec3 a, vec3 b)
vec4 max(vec4 a, vec4 b)
int max(int a, int b)
ivec2 max(ivec2 a, ivec2 b)
ivec3 max(ivec3 a, ivec3 b)
ivec4 max(ivec4 a, ivec4 b)
uint max(uint a, uint b)
uvec2 max(uvec2 a, uvec2 b)
uvec3 max(uvec3 a, uvec3 b)
uvec4 max(uvec4 a, uvec4 b)
vec2 max(vec2 a, float b)
vec3 max(vec3 a, float b)
vec4 max(vec4 a, float b)
ivec2 max(ivec2 a, int b)
ivec3 max(ivec3 a, int b)
ivec4 max(ivec4 a, int b)
uvec2 max(uvec2 a, uint b)
uvec3 max(uvec3 a, uint b)
uvec4 max(uvec4 a, uint b)

The larger of a and b.

float clamp(float x, float low, float high)
vec2 clamp(vec2 x, vec2 low, vec2 high)
vec3 clamp(vec3 x, vec3 low, vec3 high)
vec4 clamp(vec4 x, vec4 low, vec4 high)
int clamp(int x, int low, int high)
ivec2 clamp(ivec2 x, ivec2 low, ivec2 high)
ivec3 clamp(ivec3 x, ivec3 low, ivec3 high)
ivec4 clamp(ivec4 x, ivec4 low, ivec4 high)
uint clamp(uint x, uint low, uint high)
uvec2 clamp(uvec2 x, uvec2 low, uvec2 high)
uvec3 clamp(uvec3 x, uvec3 low, uvec3 high)
uvec4 clamp(uvec4 x, uvec4 low, uvec4 high)
vec2 clamp(vec2 x, float low, float high)
vec3 clamp(vec3 x, float low, float high)
vec4 clamp(vec4 x, float low, float high)
ivec2 clamp(ivec2 x, int low, int high)
ivec3 clamp(ivec3 x, int low, int high)
ivec4 clamp(ivec4 x, int low, int high)
uvec2 clamp(uvec2 x, uint low, uint high)
uvec3 clamp(uvec3 x, uint low, uint high)
uvec4 clamp(uvec4 x, uint low, uint high)

x limited to the range low to high.

float mix(float a, float b, float t)
vec2 mix(vec2 a, vec2 b, vec2 t)
vec3 mix(vec3 a, vec3 b, vec3 t)
vec4 mix(vec4 a, vec4 b, vec4 t)
vec2 mix(vec2 a, vec2 b, float t)
vec3 mix(vec3 a, vec3 b, float t)
vec4 mix(vec4 a, vec4 b, float t)

Linear blend between a and b, a * (1 - t) + b * t.

float step(float edge, float x)
vec2 step(vec2 edge, vec2 x)
vec3 step(vec3 edge, vec3 x)
vec4 step(vec4 edge, vec4 x)
vec2 step(float edge, vec2 x)
vec3 step(float edge, vec3 x)
vec4 step(float edge, vec4 x)

0 when x is less than edge, otherwise 1.

float smoothstep(float low, float high, float x)
vec2 smoothstep(vec2 low, vec2 high, vec2 x)
vec3 smoothstep(vec3 low, vec3 high, vec3 x)
vec4 smoothstep(vec4 low, vec4 high, vec4 x)
vec2 smoothstep(float low, float high, vec2 x)
vec3 smoothstep(float low, float high, vec3 x)
vec4 smoothstep(float low, float high, vec4 x)

Smooth Hermite curve from 0 to 1 as x goes from low to high.

float fma(float a, float b, float c)
vec2 fma(vec2 a, vec2 b, vec2 c)
vec3 fma(vec3 a, vec3 b, vec3 c)
vec4 fma(vec4 a, vec4 b, vec4 c)

a * b + c.

Geometry#

Function

Description

float length(float v)
float length(vec2 v)
float length(vec3 v)
float length(vec4 v)

The length of the vector v.

float distance(float a, float b)
float distance(vec2 a, vec2 b)
float distance(vec3 a, vec3 b)
float distance(vec4 a, vec4 b)

The distance between points a and b.

float dot(float a, float b)
float dot(vec2 a, vec2 b)
float dot(vec3 a, vec3 b)
float dot(vec4 a, vec4 b)

The dot product of a and b.

vec3 cross(vec3 a, vec3 b)

The cross product of a and b.

float normalize(float v)
vec2 normalize(vec2 v)
vec3 normalize(vec3 v)
vec4 normalize(vec4 v)

v scaled to a length of 1.

float reflect(float incident, float normal)
vec2 reflect(vec2 incident, vec2 normal)
vec3 reflect(vec3 incident, vec3 normal)
vec4 reflect(vec4 incident, vec4 normal)

Reflects incident off a surface with the normalized normal.

float refract(float incident, float normal, float eta)
vec2 refract(vec2 incident, vec2 normal, float eta)
vec3 refract(vec3 incident, vec3 normal, float eta)
vec4 refract(vec4 incident, vec4 normal, float eta)

Refracts incident through a surface with the normalized normal and ratio of refraction indices eta.

Matrices#

Function

Description

mat2 transpose(mat2 m)
mat3x2 transpose(mat2x3 m)
mat4x2 transpose(mat2x4 m)
mat2x3 transpose(mat3x2 m)
mat3 transpose(mat3 m)
mat4x3 transpose(mat3x4 m)
mat2x4 transpose(mat4x2 m)
mat3x4 transpose(mat4x3 m)
mat4 transpose(mat4 m)

The transpose of m, swapping its rows and columns.

float determinant(mat2 m)
float determinant(mat3 m)
float determinant(mat4 m)

The determinant of the square matrix m.

mat2 inverse(mat2 m)
mat3 inverse(mat3 m)
mat4 inverse(mat4 m)

The inverse of the square matrix m.

Boolean Vectors#

Function

Description

bool any(bvec2 v)
bool any(bvec3 v)
bool any(bvec4 v)

True if any component of v is true.

bool all(bvec2 v)
bool all(bvec3 v)
bool all(bvec4 v)

True if all components of v are true.

Textures#

These take an @input directly as their first argument. sample and fetch return the same type as the input, so a float input returns a float and a vec4 input returns a vec4.

Function

Description

float sample(@input float input, vec2 uv)
vec2 sample(@input vec2 input, vec2 uv)
vec3 sample(@input vec3 input, vec2 uv)
vec4 sample(@input vec4 input, vec2 uv)

The filtered value of input at the normalized coordinate uv, where (0, 0) and (1, 1) are opposite corners.

float fetch(@input float input, ivec2 pixel)
vec2 fetch(@input vec2 input, ivec2 pixel)
vec3 fetch(@input vec3 input, ivec2 pixel)
vec4 fetch(@input vec4 input, ivec2 pixel)

The unfiltered value of the pixel at integer coordinate pixel in input.

ivec2 textureSize(@input float input)
ivec2 textureSize(@input vec2 input)
ivec2 textureSize(@input vec3 input)
ivec2 textureSize(@input vec4 input)

The width and height of input in pixels.

Code Editor Shortcuts#

The processor node’s code editor has many shortcuts:

Command

Windows and Linux

macOS

Description

Move left

Left

Left

Move the caret one character left.

Move right

Right

Right

Move the caret one character right.

Move up

Up

Up

Move the caret up one row.

Move down

Down

Down

Move the caret down one row.

Move to previous word

Ctrl+Left

Option+Left

Move the caret to the start of the previous word.

Move to next word

Ctrl+Right

Option+Right

Move the caret to the end of the next word.

Move to line start

Home

Cmd+Left
  or
Home

Move the caret to the start of the line.

Move to line end

End

Cmd+Right
  or
End

Move the caret to the end of the line.

Move page up

Page Up

Page Up

Move the caret up one page.

Move page down

Page Down

Page Down

Move the caret down one page.

Move to start

Ctrl+Home

Cmd+Up

Move the caret to the start of the code.

Move to end

Ctrl+End

Cmd+Down

Move the caret to the end of the code.

Select left

Shift+Left

Shift+Left

Extend the selection one character left.

Select right

Shift+Right

Shift+Right

Extend the selection one character right.

Select up

Shift+Up

Shift+Up

Extend the selection up one row.

Select down

Shift+Down

Shift+Down

Extend the selection down one row.

Select to previous word

Ctrl+Shift+Left

Option+Shift+Left

Extend the selection to the start of the previous word.

Select to next word

Ctrl+Shift+Right

Option+Shift+Right

Extend the selection to the end of the next word.

Select to line start

Shift+Home

Cmd+Shift+Left
  or
Shift+Home

Extend the selection to the start of the line.

Select to line end

Shift+End

Cmd+Shift+Right
  or
Shift+End

Extend the selection to the end of the line.

Select page up

Shift+Page Up

Shift+Page Up

Extend the selection up one page.

Select page down

Shift+Page Down

Shift+Page Down

Extend the selection down one page.

Select to start

Ctrl+Shift+Home

Cmd+Shift+Up

Extend the selection to the start of the code.

Select to end

Ctrl+Shift+End

Cmd+Shift+Down

Extend the selection to the end of the code.

Select all

Ctrl+A

Cmd+A

Select all of the code.

Select line

Ctrl+L

Cmd+L

Select the current lines, including the newline.

Add next occurrence

Ctrl+D

Cmd+D

Select the word under the caret, or add a selection at its next occurrence.

Split selection into lines

Ctrl+Shift+L

Cmd+Shift+L

Split each selection into one caret per line.

Add caret above

Ctrl+Alt+Up

Option+Cmd+Up

Add a caret on the row above each caret.

Add caret below

Ctrl+Alt+Down

Option+Cmd+Down

Add a caret on the row below each caret.

Align carets

Alt+A

Option+A

Add spaces so all carets line up in the same column.

Clear extra carets

Esc

Esc

Remove all carets except the most recent one.

Cancel selection

Esc

Esc

Collapse the selection to its caret, when only one caret is left.

Backspace

Backspace
  or
Shift+Backspace
Backspace
  or
Shift+Backspace

Delete the selection, or the character before the caret.

Delete

Delete

Delete

Delete the selection, or the character after the caret.

Delete previous word

Ctrl+Backspace

Option+Backspace

Delete back to the start of the previous word.

Delete next word

Ctrl+Delete

Option+Delete

Delete forward to the end of the next word.

Copy

Ctrl+C

Cmd+C

Copy the selections. With no selection, copies the whole line.

Cut

Ctrl+X

Cmd+X

Cut the selections. With no selection, cuts the whole line.

Paste

Ctrl+V

Cmd+V

Paste. With several carets and several copied selections, each caret gets one of them. A line copied without a selection is pasted above the caret’s line.

New line

Enter

Return

Insert a new line, keeping the indentation and indenting once more after an opening bracket.

Insert line below

Ctrl+Enter

Cmd+Return

Insert a new line below the current line.

Insert line above

Ctrl+Shift+Enter

Cmd+Shift+Return

Insert a new line above the current line.

Indent

Tab
  or
Ctrl+]
Tab
  or
Cmd+]

Indent the selected lines, or insert indentation at the caret.

Unindent

Shift+Tab
  or
Ctrl+[
Shift+Tab
  or
Cmd+[

Unindent the selected lines.

Duplicate lines

Ctrl+Shift+D

Cmd+Shift+D

Duplicate the selected lines.

Delete lines

Ctrl+Shift+K

Cmd+Shift+K

Delete the selected lines.

Move lines up

Alt+Up
  or
Ctrl+Up

Option+Up

Move the selected lines up one line.

Move lines down

Alt+Down
  or
Ctrl+Down

Option+Down

Move the selected lines down one line.

Join lines

Ctrl+J

Cmd+J

Join the selected lines, or the line below, into one line.

Transpose

Ctrl+Shift+T

Cmd+Shift+T

Rotate the text between the selections.

Toggle comment

Ctrl+/

Cmd+/

Comment or uncomment the selected lines.

Undo

Ctrl+Z

Cmd+Z

Undo the last change.

Redo

Ctrl+Y
  or
Ctrl+Shift+Z

Cmd+Shift+Z

Redo the last undone change.

Compile

Ctrl+B
  or
Ctrl+R
Cmd+B
  or
Cmd+R

Compile the code.

Extract parameter

Ctrl+Shift+P

Cmd+Shift+P

Turn the selected value, such as 0.5 or vec3(1.0, 0.5, 0.2), into a @parameter and select both names so they can be renamed.

Save project

Ctrl+S

Cmd+S

Save the project.

Errors#

If you have an error in your code then the processor node will show a compiler error. The line that contains the error will be highlighted red and you will see a list of error messages at the bottom of the node. The processor node will only run your code when there are no errors present.

Clicking on an error in the list will take you to the error within the code.

Language#

This section is an overview of the language used by the processor. It is close to GLSL, so most things will look familiar, but there are some differences that are pointed out along the way.

Comments#

Single line comments begin with //:

// A comment

float x = 1.0; // A comment at the end of a line

Multi-line comments begin with /* and end with */:

/*
    A comment
    spanning multiple lines
*/

Multi-line comments cannot be nested. The first */ always ends the comment.

Literals#

Numbers without a decimal point or exponent are int literals. Numbers with either are float literals:

int   a = 42;
float b = 1.5;
float c = .5;
float d = 2.;
float e = 1e-3;

There are no hexadecimal literals and no u or f suffixes. To get a uint, convert an int:

uint a = uint(5);
uint b = 5u; // ERROR: invalid number literal '5u'

The boolean literals are true and false.

A literal must fit in its type:

int a = 3000000000; // ERROR: literal '3000000000' does not fit in 'int'

Variable declarations#

A variable declaration declares a new variable for the current scope. It is written as a type followed by a name:

float x;          // declares x to have type float
vec3  y = vec3(1.0); // declares y to have type vec3 with an initial value

Variables are initialized to zero by default unless specified otherwise.

Each declaration declares a single variable. Unlike GLSL, declaring several variables in one statement is not allowed:

float a, b; // ERROR

A variable cannot be declared twice in the same scope, but it can be redeclared in an inner scope, which hides the outer one:

float x = 1.0;
float x = 2.0; // ERROR: redeclaration of 'x'

{
    float x = 3.0; // a different x, only visible inside these braces
}

Names are made of letters, digits and underscores, and must not start with a digit. They cannot be longer than 128 characters, cannot contain two underscores in a row (__), and cannot be a keyword or the name of a type or builtin function:

float my_value;  // fine
float my__value; // ERROR: cannot contain '__'
float struct;    // ERROR: 'struct' is a keyword and cannot be used as a name
float sin;       // ERROR: redeclaration of built-in 'sin'

The keywords are if, else, for, while, do, break, continue, return, struct, true and false.

Assignment statements#

The assignment statement assigns a new value to a variable:

float x = 1.0;
x = 2.0;

= is the assignment operator. Each arithmetic and bitwise operator also has an assignment form:

float x = 1.0;
x += 1.0; // same as x = x + 1.0
x -= 1.0;
x *= 2.0;
x /= 2.0;

int i = 9;
i %= 4;
i &= 3;
i |= 4;
i ^= 1;
i <<= 1;
i >>= 1;

++ and -- increment and decrement a value by one. They can be written before or after the value, and behave like they do in GLSL:

int i = 0;
int a = i++; // a is 0, i is 1
int b = ++i; // b is 2, i is 2

Unlike GLSL, ++ and -- only work on scalars. Use += 1.0 and -= 1.0 for vectors and matrices:

vec2 v = vec2(0.0);
v.x++;     // fine, v.x is a float
v += 1.0;  // fine
v++;       // ERROR: cannot increment or decrement 'vec2'

Inputs, parameters and the globals uv, time and resolution are read-only. Outputs can be both written and read:

@parameter float strength = 1.0;
@output    float result;

void main() {
    result = strength;
    result = result * 2.0; // reading an output is fine
    strength = 2.0;        // ERROR: cannot assign to 'strength', it is a read-only parameter
    uv = vec2(0.0);        // ERROR: cannot assign to 'uv', it is a read-only built-in
}

Type conversions#

Values are converted automatically when no information is lost. An int converts to a uint or a float, and a uint converts to a float. The same applies to vectors and matrices of the same size:

float a = 1;           // int to float
vec2  b = ivec2(1, 2); // ivec2 to vec2

Any other conversion must be written out by using the type like a function:

int   a = 2.7;        // ERROR: cannot convert 'float' to 'int'
int   b = int(2.7);   // b is 2
float c = float(b);
bool  d = bool(1);

Vectors#

Vectors are built by calling the vector type with either a single value, which fills every component, or enough values to fill each component:

vec4 a = vec4(1.0);           // (1, 1, 1, 1)
vec4 b = vec4(uv, 0.0, 1.0);  // uv fills the first two components
vec3 c = vec3(a);             // takes the first three components of a
vec2 d = vec2(1.0, 2.0, 3.0); // ERROR: 'vec2' needs 2 components, got 3

Components are accessed with a swizzle. A swizzle picks up to 4 components by name, using xyzw, rgba or stpq. One swizzle cannot mix letters from different sets:

vec4 color = vec4(uv, 0.0, 1.0);
float r = color.r;
vec3  v = color.xyz;
vec4  w = color.bgra;
vec2  s = color.xg; // ERROR: invalid swizzle 'xg' for 'vec4'

Swizzles can also be assigned to:

color.rg = uv;

A component can also be read by index, like an array:

vec3  v = vec3(1.0, 2.0, 3.0);
float z = v[2]; // 3.0

Arithmetic on vectors works per component. A scalar used with a vector is applied to every component:

vec4 a = vec4(1.0);
vec4 b = a * 2.0 + 1.0; // (3, 3, 3, 3)
vec4 c = a * b;         // (3, 3, 3, 3)

Matrices#

Matrices are built by calling the matrix type. A single value fills the diagonal, otherwise the values fill the matrix one column at a time:

mat2 a = mat2(1.0);                     // identity matrix
mat2 b = mat2(1.0, 2.0, 3.0, 4.0);      // first column is (1, 2)
mat2 c = mat2(vec2(1.0), vec2(2.0));    // first column is (1, 1)

Indexing a matrix gives a column:

mat4  m = mat4(1.0);
vec4  column  = m[0];
float element = m[3][3];

* between two matrices, or a matrix and a vector, is a matrix multiplication:

vec4 p = m * vec4(uv, 0.0, 1.0);

Arrays#

Unlike GLSL, the size of an array is written before the element type:

[4]float a;       // an array of 4 floats, all zero
[2][3]float grid; // an array of 2 arrays of 3 floats

a[0] = 1.0;
grid[1][2] = 1.0;

The size must be a positive constant integer, up to 65536.

Arrays are built by calling the array type with one value per element. Leaving the size out of the brackets lets it be counted from the values:

[3]float a = [3]float(1.0, 2.0, 3.0);
[]float  b = []float(1.0, 2.0);     // b has type [2]float
[]float  c = [3]float(1.0, 2.0);    // ERROR: '[3]float' needs 3 elements, got 2

Indexing with a constant that is out of range is an error:

[4]float a;
float x = a[4];   // ERROR: index 4 out of bounds for '[4]float'

Structs#

A struct is a collection of named members. Unlike GLSL, there is no ; after the closing brace:

struct Light {
    vec3  color;
    float intensity;
}

Structs must be declared outside of functions. They must have at least one member, and members cannot have default values.

A struct is built by calling it with a value for each member in order, or with no values to set every member to zero:

Light a = Light(vec3(1.0), 2.0);
Light b = Light(); // every member is zero

Struct members are accessed by using a dot:

a.intensity *= 0.5;
vec3 light = a.color * a.intensity;

Structs cannot be compared with == or !=.

Operators#

The operators are the same as GLSL’s:

Operator

Works on

+ - * /

Numbers, vectors and matrices

++ --

Number scalars

%

Integers

& | ^ ~ << >>

Integers

< <= > >=

Number scalars

== !=

Anything except structs and matrices

&& || !

bool

? :

Picks between two values with a bool condition

% only works on integers. Use mod for floats:

float a = 5.0 % 2.0;     // ERROR: invalid operands 'float' and 'float' to '%'
float b = mod(5.0, 2.0); // 1.0

<, <=, > and >= only compare scalars. To compare vectors, compare their components:

bool a = uv < vec2(0.5); // ERROR: invalid operands 'vec2' and 'vec2' to '<'
bool b = uv.x < 0.5 && uv.y < 0.5;

== and != on vectors compare the whole vector and give a single bool:

bool centered = uv == vec2(0.5);

Integer division with a constant zero, and shifting by a constant amount that is too large, are errors:

int a = 1 / 0;   // ERROR: integer division by zero
int b = 1 << 40; // ERROR: shift amount 40 is out of range for 'int'

if statement#

The condition of an if statement is surrounded by parentheses ( ). Braces { } are only needed when the body has more than one statement:

if (uv.x > 0.5) {
    result = 1.0;
} else {
    result = 0.0;
}

if (uv.x > 0.5) result = 1.0;
else            result = 0.0;

Unlike GLSL, the condition must be a bool. Numbers are not treated as true or false:

int i = 1;
if (i)      result = 1.0; // ERROR: condition must be 'bool', got 'int'
if (i != 0) result = 1.0;

Loops#

The processor has three kinds of loop. The for loop has three components separated by semicolons, an initial statement, a condition and a step:

float total = 0.0;
for (int i = 0; i < 10; i++) {
    total += 1.0;
}

Any of the three components can be left out. A for loop with no condition loops until it is stopped with break:

for (;;) {
    break;
}

The while loop checks its condition before each iteration, and the do loop checks it after:

int i = 0;
while (i < 4) {
    i += 1;
}

do {
    i -= 1;
} while (i > 0);

break stops the loop it is in, and continue skips to the next iteration. Both are errors outside of a loop:

for (int i = 0; i < 10; i++) {
    if (i == 3) continue; // skips 3
    if (i == 8) break;    // stops at 8
    total += 1.0;
}

Note

Every loop stops after 1000 iterations, even if its condition is still true, so a loop can never run forever.

Functions#

A function is written as a return type, a name, a list of parameters and a body:

float square(float x) {
    return x * x;
}

void main() {
    result = square(uv.x);
}

A function with a void return type does not return a value. Any other function must return a value of its return type:

void helper() {
    return 1.0; // ERROR: void function 'helper' cannot return a value
}

Parameters are passed by value, so a function gets its own copy and changing it does not affect the caller:

float twice(float x) {
    x *= 2.0; // only changes this copy of x
    return x;
}

Functions can take and return arrays and structs:

float sum([3]float values) {
    return values[0] + values[1] + values[2];
}

Light dim(Light light) {
    light.intensity *= 0.5;
    return light;
}

A function can only be called after it has been declared, and there are no forward declarations. This also means a function cannot call itself:

void main() {
    result = later(); // ERROR: undeclared function 'later'
}

float later() {
    return 1.0;
}

Unlike GLSL, two functions cannot share the same name, even with different parameters.

The main function#

Every shader needs a main function. It runs once for every pixel, and must return void and take no parameters:

void main() {
    // your code goes here
}