Shaders · Chapter 10 of 11

Shaders: Layer FX in Depth

Written for version 2.7.0

Shade a whole layer of your picture: Layer Source and Layer Output, alpha, every node in the Layer folder, then glows, outlines, grading and depth.

The last chapter took a graph from a flat square to a lit surface on a model. A material's graph has one limit that no node can get round: it colours one point at a time and cannot see the points beside it. This chapter is about the other place a shader graph lives, a layer's Shader FX, which works on a finished picture and so can blur it, outline it, grade it and bend it.

Why shade a whole layer

Think of what a soft glow needs. To blur a bright spot, each pixel has to gather colour from its neighbours. A material's graph runs for one point of one surface and knows nothing about the rest of the picture. A glow also has to spread past the edge of the object, onto pixels the object does not cover, where its material never runs at all.

A layer's Shader FX graph runs after the layer has been drawn, once for every pixel of the layer's picture, and it can read that picture anywhere it likes. These are the effects that need it.

EffectWhy it needs the finished picture
Blur and glowEach pixel gathers from the pixels around it
OutlinesThe edge of a shape is only known once the shape is drawn
Colour gradingOne change applied to everything on the layer, whatever its materials are
DistortionThe picture is read from a different place than where it is drawn
Depth effectsFog and focus need to know how far away each pixel is

What a render layer is

A render layer is a group of your nodes drawn into a picture of their own. Every rig has one called Main, which is the picture you see. A layer you add is drawn first, into an image that is kept as a texture. The Rendering chapter of the Rig Editor path covers adding layers and putting nodes on them.

  1. In the Selection Panel, under Rendering, tap + beside Layers. A layer called Layer appears above Main.
  2. Tap a node you want on the layer. In its panel, under Rendering, tap the plus beside Add to Layer and choose Layer. The node is now on both layers. Tap the red minus beside Main if it should be drawn on the new layer only.
  3. Put a light on the layer the same way, or the layer's nodes are lit only by the environment. A light shines only on the layers it is on.
  4. Tap Layer in the Layers list. The viewport now shows only what is on that layer.

Switching Shader FX on

A layer's panel ends with a section called Post FX Passes. It has two switches. Shader FX gives the layer a shader graph, and it is the subject of this chapter. Filter FX is a separate graph of ready-made image filters that runs afterwards.

Tap the layer and the viewport shows only what is on it. In its panel, under Post FX Passes, switch on Shader FX. The layer's graph docks under the viewport, and Shader FX and FX Preview rows appear under the layer.
  • The new graph is two nodes: Layer Source wired straight into Layer Output. The picture goes in and comes out unchanged, so nothing looks different until you edit it.
  • The Shader FX row under the layer selects the graph. Edit Shader FX Graph in the layer's panel does the same.
  • Switched off, the pass does not run at all and costs nothing. The graph is kept for when you switch it back on.
  • If a graph cannot be compiled, the panel says so in red and the pass is skipped until it is fixed.

FX Preview and the pin

FX Preview switches the viewport between Pre, the layer as it is drawn, and Shader, the picture after the pass. On Pre the graph pane closes. The pin on the Shader FX row keeps the graph docked while you work on Main.
  • Shader shows the finished pixels of the layer, fitted into the viewport, without the grid or gizmos. Pre shows the layer's nodes live, so you can still move them.
  • To edit the graph, FX Preview has to be on Shader, or the graph has to be pinned.
  • The pin is on the Shader FX row and again in the bar of the graph pane. Pinned, the graph stays docked whatever you select, so you can watch what the effect does to the whole picture from Main.
The pictures on this page

The results below are worked out from a stand-in for a layer's picture: two striped balls on a see-through background, the far one small. The layer is set to a Custom size of 256 by 256 under Render Target, so pixel numbers are easy to see. The checkerboard is where the picture is see-through.

How a layer graph differs

It is the same editor with the same wires, inspector and Add Node card. What changes is what the graph is about: a flat picture, not a surface.

The Add Node card of a layer graph. There is no Shading and no Material folder, Input holds only the two clocks, and a Layer folder appears. Gaussian Blur is added from it and wired into Layer Output.
In a material's graphIn a layer's graph
Ends in Surface OutputEnds in Layer Output, with the same Color and Opacity inputs
Shading folder, with PBRNot there. A picture is not lit a second time
Material folderNot there. A layer has no material panel
Input folder: UVs, position, normals, camera, clocksOnly Elapsed Time and Delta Time. A flat picture has no surface or camera to ask about
No Layer folderLayer folder: the layer's picture, its place, size and depth, and six effects
Graph Inputs in the material's panelShader FX Inputs in the layer's panel

Everything else is shared: Math, Logic, Color, Channels, Coordinates, Procedural and Blend all work on a layer exactly as they did on the square. In fact a layer graph is the closest thing in the app to the flat square of the early chapters. Wire a Checker into Layer Output and you get a checker.

Layer Source and Layer UV

Layer Source is the layer's picture as it was drawn. It has two outputs, Color and Alpha, and one input, UV: the place to read the picture at. With no wire, it reads the pixel the graph is working on, so the picture passes through as it is.

Layer UV is that place as a value you can work with: 0 to 1 across the layer, and 0 to 1 down it. Change the place before it reaches Layer Source and the picture is read from somewhere else, which is how every distortion works.

Pixelize Coords snaps every place to the middle of a cell. Wired into Layer Source's UV, each cell reads one spot of the picture: a mosaic. Fewer Cells gives bigger blocks, and uneven Cells gives bars.
  • Any Coordinates node can sit in front of Layer Source: Offset Coords slides the picture, Scale Coords zooms it, Rotate Coords turns it, Swirl Coords twists it.
  • Add a small Noise to the place for a heat haze, or a Wave for ripples.
  • A place outside 0 to 1 is off the edge of the layer. Layer Source gives see-through black there.
  • V runs downward on a layer: 0 is the top edge. On the square of the early chapters it ran upward.

Alpha, and why the edges matter

A layer of your own starts with a see-through background, so its picture has an alpha: 1 where something was drawn, 0 where nothing was, and in between along soft edges. A layer effect has to look after that alpha, or the layer stops sitting cleanly over whatever is behind it.

With no wire into Opacity the layer keeps its own alpha. A plain 1 makes every pixel solid, so the empty part shows as black. Layer Source's Alpha wired in gives the same result as no wire at all.
  • Layer Output's Opacity with no wire keeps the alpha the pixel had before the pass. That is the right choice for any effect that only changes colour.
  • Wire something into Opacity when the effect changes the shape of the picture: a blur that spreads, an outline, anything that moves pixels.
  • The colour of an empty pixel is black. Make an empty pixel solid and black is what you see.
  • Inside the graph, Color and Alpha are kept apart: the colour at a soft edge is the full colour of the object, and Alpha alone says how much of it is there. So tinting, grading and mixing colours never darken the edges. The app puts the two back together after Layer Output.

The Layer folder, node by node

The six effect nodes all work the same way. Each one reads the layer's picture several times, at places spread in its own pattern, and averages what it finds. Each has a UV input like Layer Source, and Color and Alpha outputs. Sizes are in pixels of the layer, angles are in radians, and a Center is a place in the layer, where 0.5 and 0.5 is the middle.

Gaussian Blur

The plain, even blur. Radius (px) is how far it reaches.

Gaussian Blur wired into Color alone is cut off at the old outline, because the layer keeps its sharp alpha. Wiring Alpha into Opacity lets the soft edge through. Radius (px) sets how far the blur reaches.
  • Always wire Alpha as well as Color when a blur should spread past the shape.
  • The blur reads a fixed grid of 9 by 9 spots, spread wider as Radius grows. At small sizes it is smooth. At large ones the spots are far apart and the blur starts to look like overlapping copies.
  • A Radius of 0 is the picture unchanged.

Directional Blur

A smear along one straight line, the look of something moving fast.

Directional Blur smears the picture along a line. Length (px) is how long the smear is. Angle (rad) turns the line: 0 is sideways and 1.57, a quarter turn, is up and down.
  • A full turn is about 6.28 radians. The Radians node, under Math in Trigonometry, turns degrees into radians if you prefer to think in degrees.
  • The smear runs both ways from each pixel, so the shape does not shift, it stretches.

Zoom Blur and Spin Blur

Zoom Blur pulls the picture toward Center, as if rushing at it, more strongly as Strength rises. Spin Blur turns the picture about Center through Angle (rad), like a spinning wheel.
  • Zoom Blur's Strength runs from 0 to 1. Nothing moves at Center itself, and the smear grows with distance from it.
  • Set Center to the place of the thing you want to stay sharp.
  • Spin Blur's Angle is the whole arc of the smear. 0.35 is a gentle turn, 6.28 would be a full circle.

Chromatic Aberration

A cheap lens bends colours by slightly different amounts, so edges far from the middle of a photo show red on one side and blue on the other. This node does that on purpose.

Chromatic Aberration reads red and blue from either side of each pixel, along the line from Center. Nothing splits at Center, and the split grows with distance. Moving Center to the left edge makes the whole picture fringe one way.
  • Strength (px) is how far the colours part, in pixels, a whole picture-width from Center. Halfway there the split is half as much.
  • The alpha comes from the green, which does not move, so the outline stays where it was and the fringes can be cut short at it.

Convolution 3×3

A kernel is a small grid of numbers, here three rows of three. For every pixel, the node takes that pixel and its eight neighbours, multiplies each by the number in the matching cell, and adds the nine results. That sum is the new pixel. The middle cell is the pixel itself. The three rows run from the top of the picture down.

The kernel is shown under the result. Only the middle at 1 changes nothing. A strong middle with negative neighbours sharpens. An uneven kernel embosses. Numbers that add up to 0 leave only the outline.
KernelRow 1Row 2Row 3
Unchanged0, 0, 00, 1, 00, 0, 0
Sharpen0, -1, 0-1, 5, -10, -1, 0
Soften0.11, 0.11, 0.110.11, 0.11, 0.110.11, 0.11, 0.11
Emboss-2, -1, 0-1, 1, 10, 1, 2
Edges-1, -1, -1-1, 8, -1-1, -1, -1
  • Each row is one input with three numbers, X, Y and Z in the inspector: left, middle and right.
  • When the nine numbers add up to 1, the picture keeps its brightness. Above 1 it brightens, below 1 it darkens.
  • The alpha goes through the same sum as the colour. A kernel that adds up to 0 gives an alpha of 0 wherever the picture is even, so only places where the alpha changes, the outline, survive.
  • The result is held between 0 and 1.
  • The neighbours are one pixel away, so on a large layer the effect is fine. For a wider effect, use a blur.

Layer Size

Layer Size gives two pairs of numbers. Size (px) is the width and height of the layer in pixels. Texel is the step from one pixel to the next, as a distance in the layer's UV: one divided by the size. Multiply Texel by a number of pixels and you have that many pixels as a distance you can add to a place.

Texel times a Float of 40 is a distance of exactly 40 pixels. Offset Coords adds it to the place Layer Source reads, so the picture is read from 40 pixels further right and down, and appears to move up and to the left.
  • Wire a Float node into Multiply for the number of pixels. A number typed into B would set only its first lane and move the picture sideways alone.
  • Use it for effects that should be the same size in pixels on any layer: a one pixel offset, a drop shadow a set distance away.
  • Divide Size's X by its Y, after a Split Vector, to get the shape of the layer, for circles that stay round on a wide layer.

Layer Depth

When a layer is drawn, the app also records how far from the camera each pixel is. Layer Depth reads that record. It has three outputs.

OutputWhat it is
Depth0 at the camera, rising to 1 far away. Also 1 where nothing was drawn
RawThe same fact as the app stores it: 1 at the camera, falling toward 0 with distance, and exactly 0 where nothing was drawn
Coverage1 where something was drawn, 0 on the empty background
Depth wired into Color shows both balls as plain white: it is within a hair of 1 on anything beyond arm's length. Raw times a few thousand is a usable nearness: bright near, dim far. As the T of a Mix it fades the far ball into fog.
  • The numbers are crowded because the camera can see things very close to it. Almost the whole range from 0 to 1 is used up in the first few centimetres.
  • Raw falls off as one over the distance, so twice as far away is half the Raw. Multiply it by a large number, then use Saturate to hold it between 0 and 1. The right number depends on your scene: start in the thousands and adjust until near things are white and far things grey.
  • Fog: use One Minus on that nearness and wire it into the T of a Mix, with Layer Source's Color in A and a fog colour in B.
  • Tint by distance: wire the nearness into a Color Ramp.
  • A focus effect: Mix between Layer Source's Color in A and Gaussian Blur's Color in B, with the far value as T. Near things stay sharp and far things soften.
  • Coverage is a hard mask of the objects with no soft edge. Layer Source's Alpha is the soft one.

Combining them

The Layer folder gives you the picture and blurred copies of it. Everything you learned on the square turns those into finished effects.

Grading and a vignette

Grading means changing the colours of the whole picture at once. Put any node from the Color folder between Layer Source and Layer Output. A vignette is a darkening toward the corners, and it is the circle from the Shapes chapter, used as a mask.

This layer has a Background colour, so it is solid everywhere. Hue/Saturation/Value regrades the whole picture. A Spherical Gradient through Smoothstep and One Minus is a mask that is 1 in the middle and 0 in the corners, and Multiply darkens by it.
  • Hue/Saturation/Value: Hue at 0.5 leaves colours alone, Saturation above 1 makes them richer, Value is brightness. Brightness/Contrast, Gamma and RGB Curves are in the same folder.
  • For a tint, Multiply the colour by a Color node. For black and white, use Saturation with Amount at 0.
  • A vignette darkens colour, so it shows only where the layer is solid. Give the layer a Background colour in its panel, or use the effect on a layer that fills the frame.

A glow

A glow is a blurred copy of the picture laid back over the sharp one with a Blend node set to Screen, with the larger of the two alphas as Opacity. The Shader FX Tutorial builds it step by step.

An outline

The alpha of a blurred copy reaches a little past the object. The sharp alpha stops at its edge. Take the sharp one from the blurred one and what is left is a band just outside the outline.

Gaussian Blur's Alpha minus Layer Source's Alpha leaves a rim outside the shape. Smoothstep firms it up, Mix paints it a colour, and Max keeps the picture solid wherever the object or the rim is. Radius sets the thickness.
  1. Add Gaussian Blur, from Layer, and set Radius (px) to 6.
  2. Add Subtract, from Math then Common. Wire Gaussian Blur's Alpha into A and Layer Source's Alpha into B.
  3. Add Smoothstep. Set Edge 0 to 0.05 and Edge 1 to 0.25, and wire Subtract's Out into Value.
  4. Add a Color node, from Parameters, for the outline colour.
  5. Add Mix (Lerp). Wire Layer Source's Color into A, the Color node's RGB into B and Smoothstep's Out into T. Wire its Out into Layer Output's Color.
  6. Add Max. Wire Layer Source's Alpha into A and Smoothstep's Out into B. Wire its Out into Layer Output's Opacity.

Using time

Elapsed Time and Delta Time are the two Input nodes a layer graph keeps, so everything from the Time chapter works here. Wire Elapsed Time through Ping-Pong and a Multiply into Radius (px) for a blur that breathes. Use Scroll Coords in front of Layer Source to slide the picture. Add a Wave that reads a scrolling place to the place Layer Source reads, and the picture ripples.

One pass, one look at the picture

Every node in the Layer folder reads the layer as it was drawn, not the result of the nodes before it. You cannot blur a picture you have already graded, or blur a blur. Do colour work after the blur, on its output. For two passes in a row, give the result to a second layer through a material and add Shader FX there.

Getting the result on screen

A layer of your own is not laid over the screen by itself. It draws into an image, and that image is a texture the rest of the rig can use. It is named after the layer with Target on the end, so a layer called Layer makes Layer Target. Main, the layer you see and the one a snapshot is taken of, shows it only where something uses that texture.

  1. Tap Main in the Layers list to see the whole picture again.
  2. Add a Plane, turn it to face the camera, and size it to fill the view.
  3. Tap + beside Materials, choose New Unlit Material, and give it to the plane. Unlit shows the picture as it is, without lighting it again.
  4. In the material's panel, under Texture Slots, tap Base Color and choose Layer Target.
  5. Set Alpha to Blend, so the empty part of the layer's picture stays see-through.
  • In a graph material, a Texture Object node can hold the layer's target, and Sample Texture reads it like any other picture. A screen in your scene can show another layer this way.
  • Main's own Background can be set to a texture, and a layer's target can be used there, which puts the layer behind everything on Main.
  • The texture always holds the result of the passes that are switched on, so whatever samples it gets the finished effect.
  • If the processed nodes are still on Main as well, you will see them twice: once as they are and once through the texture. Take them off Main in their Rendering section.

Costs and limits

  • A pass that is switched off costs nothing. A pass that is on redraws every pixel of the layer on every frame, even when its graph changes nothing.
  • The effect nodes read the picture many times for each pixel: Gaussian Blur 81 times, Directional Blur 17, Zoom Blur and Spin Blur 16 each, Convolution 9, Chromatic Aberration 3 and Layer Source once. Several blurs in one graph add up.
  • The cost grows with the size of the layer. Under Render Target in the layer's panel, Custom lets you give a layer a smaller size than the scene. A glow rarely needs full size.
  • Pixel sizes are pixels of the layer. Halve the layer and a Radius of 8 reaches twice as far across the picture.
  • A graph can hold up to 8 Texture Object nodes.
  • The pass works on what the layer drew this frame. With Clear Each Frame switched off under Render Target, the layer keeps what it drew before and draws over it, which leaves trails.

In the Ray Traced engine

  • Under Ray Traced, Main is ray traced and every layer of your own is still drawn by the Physically Based engine, with its Shader FX. The layer rows are marked PBR.
  • The layer's target can still be sampled by materials, and those materials do appear in the ray traced picture.
  • A node that is not on Main stays out of the ray traced world and is seen only through its layers' textures.
  • Main has no Shader FX of its own in either engine. Under Ray Traced its panel offers a Filter FX pass for the finished picture, which is a graph of image filters, not a shader graph.

When it does not look right

What you seeWhat to check
The blur stops dead at the outlineWire the blur's Alpha into Layer Output's Opacity
A black box round the objectSomething solid is wired into Opacity. Remove the wire, or wire in an Alpha
Nothing changesFX Preview is on Pre, or you are looking at Main and nothing there uses the layer's target
The graph pane closedFX Preview is on Pre. Tap Shader, or pin the graph
The effect vanished on MainIt is still on its layer. Main shows it only through a material that uses the layer's target
Depth shows plain whiteUse Raw, multiplied by a large number
A big blur looks like overlapping copiesThe Radius is too large for the blur's fixed grid. Use a smaller layer and a smaller Radius

Now you try

  1. A dream look. Wire Gaussian Blur with a Radius of 10 into the Blend input of a Blend node set to Soft Light, Layer Source's Color into Base, and the result into Layer Output's Color.
  2. A speed streak. Use Directional Blur with a Length of 80, and wire its Color and Alpha into Layer Output.
  3. A drop shadow. Slide the place Layer Source reads by a few pixels with Layer Size, Multiply and Offset Coords, as in the picture above, and use that shifted copy's Alpha as a dark shape behind the sharp picture, with Mix and Max as in the outline.
  4. Old film. Set Saturation's Amount to 0, Multiply by a warm Color node, and add the vignette.
Something unclear?

Tell us which node or picture confused you, what you wired into Layer Output, and what the layer showed instead of what you expected.

Send Feedback