Search This Blog

Labels

Sunday, December 5, 2010

Introduction to Composite Nodes

Introduction to Composite Nodes in Blender
Viewer nodes showing Combined, Alpha, Diffuse, and Specular render passes.

I’ve been experimenting with compositing nodes lately; not so much for achieving specific results as for understanding how they work. The subject is also educational for increasing understanding of Blender’s rendering pipeline. Believe it or not, it’s possible to get a grasp on this without diving into the code…

Blender Underground’s focus thus far has been to deal more with the nuts and bolts of how things work than how to achieve this or that specific effect. This series will continue the tradition. I won’t rule out how-to’s on certain subjects, but I will try to focus more on theory and usage than on the end result – leaving that up to you, the artist, hobbyist, and/or professional. I’m aiming to help you understand the tools at your disposal and how they work, focusing less on how to obtain a specific result.

In this first installment, we’ll take a look at render passes, a basic node setup – which involves assembling some passes into a composite – and pixel math, for understanding how Blender combines passes into the final render.

Render Passes

Render passes in composite nodes - diffuse, specular, shadow, alpha
Left to Right, Top to Bottom: Combined, Alpha, Z Buffer, Color, Diffuse, Specular, Shadow, Ambient Occlusion, Reflection.

Blender’s composite nodes give the user access to data from its render passes. When Blender calculates things like shadows or specular highlights, it stores the results as pixel data – that is, images – to be combined later using pixel math. More on that later. The important part to understand right now is that with nodes, you can access each of these passes individually, modify them, and put them back together in whatever way you see fit. This is known as compositing.

The primary definition of a composite is something made up of differing parts, combined to make a whole. In Blender, when you press F12 and render your scene, what you’re seeing is a composite of render passes – a set of separate images combined together into one final, hopefully beautiful, picture. With CompositeNodes, you’re given control over how these render passes get assembled, and what happens to them along the way. Let’s grab a look at some of the component parts of a render.

Render pass Combined
Combined – This is the renderer’s composite of all the render passes.

Render pass Alpha
Alpha – This pass is an 8-bit graphic representing the combined render’s transparency information. White pixels represent full opacity and black pixels indicate full transparency.

Render pass Z-Buffer
Z-buffer – While you might not be able to see the data represented here, it’s all in there. Z-buffer information is stored as a 32 bit floating point number. So while all the values might look white, with some manipulation (such as in Photoshop or GIMP) the values become apparent.

Render pass Color
Color – This is an RGB color mask containing information for each object in the scene. Since the objects in this render have a material color of white, no color is present in this pass. The diffuse pass actually already incorporates this data.

Render pass Diffuse
Diffuse – This pass contains all shading and color information from lighting calculations in the scene.

Render pass Specular
Specular – The information generated from all materials’ specular shaders are contained in this pass.

Render pass Shadow
Shadow – For ray traced or buffered shadows, this pass stores the shadow data for the scene.

Render pass Ambient Occlusion
Ambient Occlusion (AO) – This pass is the result of a separate pass using the AO algorithm. It adds a simulation of dirt and shadows that occur for objects close to one another.

Render pass Reflection
Reflection – Data generated from ray traced reflections are stored in this pass. Much of the data has values close to black, and won’t show up well until this pass is added in the composite.

These aren’t all of the available render passes; and some of these – like reflect, shadows, and AO – won’t be available unless they’re configured to run. In addition, with few exceptions, only the passes you specify will be available for compositing.

In the Render Layers panel of the Scene buttons (F10) are buttons for activating the various passes.

Render layers panel - render passes buttons
Left: Render Layers panel with render passes selected. Right: Render Layer node with corresponding render pass points.

Keep in mind these buttons don’t activate or deactivate render functionality, but instead they tell Blender what passes to make available to the Node editor. Note that Alpha is available to the Node editor (Right) all the time – there is no corresponding Alpha selection in the Render Layers panel. Let’s quickly set up the Node editor to edit composite nodes.

Note: Anytime you add a render pass for access by a node, you’ll need to re-render in order for that pass’s data to be available to the node. However once this is done, you can make all sorts of changes to the node configuration without ever having to re-render your scene. This is one of the advantages of working with nodes.

Setting Up for Node Editing

Node Editor default setup
Node editor with composite nodes button selected and “Use Nodes” button active.

You can bring up the Node editor the same way you would any other window, by configuring a pane to display it. Once you have the Node editor active, you’ll need to select the Composite Nodes button at the bottom, and turn on the Use Nodes button. The last step is turning on “Do Composite” in the Anim panel of the Scene buttons – if you don’t, all the node editing in the world won’t affect your final render.

Anim Panel Do Composite setting
Anim panel with “Do Composite” active.

Let’s set up a scene and configure Blender to work with Composite nodes.

Set up your scene as follows:

  1. UVSphere object on top of a Plane object.
  2. Basic material assigned to both objects.
  3. Turn off Ray Tracing if it’s active (Scene panel, F10).
  4. From the Render Layers panel (F10) turn on Combined, Diffuse and Specular passes.
  5. Configure a pane to display the Node editor, and activate the Composite Nodes button and the Use Nodes button.
  6. Activate the Do Composite button in the Anim panel of the Scene context (F10).
  7. Render your scene.

Simple Node Scene Blend File

Your node setup should now look like this, minus the callouts:

Node setup with callouts
Default node setup, with Combined Image output point of the Render Layer node connected to the Image input point of the Composite node.

A. Render Layer node – this node contains all the render passes.
B. Composite node – this node represents the render buffer (F11). Whatever is attached to this node gets displayed as the final render, and will get saved as an image when F3 is pressed.
C. View Configuration buttons – these control what elements get displayed in a node. Just play around with them and you’ll get the idea.
D. Node Connection Points – attachment points for input and output of a node’s data. Input points are present on the left side of the node, and output on the right.
E. Render button – re-renders the current Render Layer.
F. Mode Selector – these buttons determine whether the node editor is working with Material or Composite nodes.
G. Use Nodes button – Needs to be active to make use of Composite nodes.

Note: The Image connection point of the Render Layer node represents the combined, or composited, data from each render pass.

The above node configuration (sometimes referred to as a “noodle”) does nothing special. The Render Layer node’s Image (Combined) output point is connected to the Composite node’s Image input point. This configuration performs the same task as if we weren’t using nodes at all.

Rebuilding the Render Pipeline

Now that we’re set up to work with Composite Nodes, let’s use them to rebuild a small part of the render pipeline.

  1. With your mouse over the Node editor, add a Mix node with Spacebar->Add->Color->Mix.
  2. Attach the Render Layer node’s Diffuse point to the Mix node’s top RGB (yellow) connection point.
  3. Attach the Render Layer node’s Specular point to the Mix node’s bottom RGB connection point.
  4. Set the operation to Add instead of Mix, and set the Fac (Factor) to 1.0.
  5. Connect the Mix node’s Image output point to the Composite node’s Image input point.

Mix node combining specular and diffuse render passes
Composite using the Mix node to add the specular channel to the diffuse channel.

Press F11 to see the result.

Render result for diffuse and specular mix
Render result for the composite of Diffuse and Specular render passes.

If you’re not seeing a result, press F12 to re-render and re-check your connections. What we’ve just accomplished is using the Mix node to combine the Diffuse and Specular passes in the same way the renderer does. We’ve taken control of the compositing element of the render pipeline. The only difference is that the sky color is missing, which we’ll deal with in an upcoming installment.

Let’s check our results by using viewer nodes to inspect each of our passes.

  1. Add a Viewer node by pressing Spacebar->Add->Output->Viewer, and place it above the Render Layer node.
  2. Connect the Render Layer node’s Image point to the Viewer node’s Image input point.
  3. Turn on the “Backdrop” button from the Node editor header.
  4. Select the newly added Viewer node. You should now see that viewer node displayed as the backdrop image for the node editor.
  5. With the Viewer node selected, press SHIFT+D to duplicate it and place the new Viewer node above the Mix node.
  6. Duplicate it again and place the new node below the Mix node. To make things easier, you can collapse the Viewer nodes with the triangle button on the top left corner of the node.
  7. From the Render Layer node, connect the Diffuse point to one of the Viewer nodes and the Specular to the other. As you select each of them, you should get a full resolution view on the Node editor’s backdrop.
  8. Finally, duplicate one of the Viewer nodes one more time and place it above the Composite node. Connect the Mix node’s Image output to the viewer node.

Finished node setup with Viewer nodes
Finished node setup combining Diffuse and Specular channels, with Viewer nodes and backdrop.

As you select the different viewer nodes, the backdrop changes to reflect the node’s input. This lets you see each individual pass and result to help you keep track of what’s going on. You can reposition the backdrop with SHIFT+MMB, and the nodes with MMB.

When you click on the Viewer node above the Render Layer node, you should see the combined composite as it is assembled by the renderer. When you click on the Viewer node above the Composite node, you should see the composite as you’ve assembled it. The only difference should be the presence of the sky color. As mentioned earlier, we’ll deal with that in an upcoming installment. As you click on the Viewer nodes above and below the Mix node, you should see the Diffuse and Specular passes that are being assembled in the Mix node.

Finished Node Setup Blend

Pixel Math

Why did we “add” the specular channel to the diffuse? The term “add” is more than a literary convenience, it describes the math that is used to combine the channels together. We need to briefly discuss how basic pixel math works. Don’t worry, it’s simple but illuminating stuff.

For RGBA color, there are 8 bits per channel for a total of 32 bits (Red, Green, Blue, Alpha). Each 8 bits of data is 1 byte, with a possible range of values from 0 to 255. For the renderer’s math operations, the values are “normalized.” That is, they are represented by the values 0.0 to 1.0 instead.

R = 0 to 255 | 0.0 to 1.0
G = 0 to 255 | 0.0 to 1.0
B = 0 to 255 | 0.0 to 1.0
A = 0 to 255 | 0.0 to 1.0

When we look at any of these channels individually, they are actually values ranging from 0.0 (black) to 1.0 (white). When RGB color math takes place, it happens on a per-pixel and per-channel basis. Let’s try to wrap our head around this by looking at a couple of 1-pixel math examples.

Pixel Math Addition 1
When two medium gray pixels are added together, the result is a white pixel.

Pixel Math Addition 2
When a black pixel and a medium gray pixel are added together, the result is a medium gray pixel.

Pixel Math Addition 3
When a dark gray and a medium gray pixel are added together, the result is a light gray pixel.

Notice that for Add operations, the result is always lighter than either of the two source pixels, unless one of them is black (0.0). Add operations effectively always brighten the result. For Specular passes, this makes perfect sense, considering that specular highlights are always brighter than their diffuse counterparts.

Also remember that these operations take place individually for every pixel in the pass, which is why diffuse plus specular equals shiny.

Pixel Math per pixel diffuse specular example
The Add operation works on each pixel in the pass. When the Specular pass is added to the Diffuse pass, the result is a bright specular spot, simulating shininess.

Note: When the values of two pixels are added together, the result is often greater than 1.0. In this case the renderer will often “clamp” any value greater than 1.0 to 1.0.

Some render passes, such as the Shadow pass, aren’t added – they’re multiplied. When numbers less than 1.0 are multiplied together, the result isalways a smaller number – a darker value – unless one of the values is exactly 1.0. Let’s look at a few pixel multiplication equations:

Pixel Math Multiplication 1
Two medium gray pixels multiplied together results in a dark gray pixel.

Pixel Math Multiplication 2
A light gray pixel multiplied with a medium gray pixel results in a dark gray pixel.

Pixel Math Multiplication 3
When any pixel (in this case, medium gray) is multiplied by a white pixel (1.0) the result is unchanged.

The net result of our examination of pixel math should help you to understand a little more of what goes on under the hood of Blender. By examining Add and Multiply pixel math, we’ve gained an understanding of how many render passes are combined in the pipeline, on a per-pixel basis.

Conclusion

Understanding the basics of pixel math, along with having a knowledge of how render passes are assembled in the pipeline, will make understanding composite nodes much easier. This primer served as part 1 of a multipart series on using Composite Nodes.

In the next part of the series, we’ll assemble a more complex set of passes, and make some modifications to them along the way. We’ll introduce additional Node types and examine how they’re used to make modifications to the render passes before assembling them into the final composite.

 

 

Introduction to Composite Nodes in Blender
Composite with sky color, consisting of Combined, Modified Shadow, Reflection, and Specular passes

In Part 1 of this series, we started by looking at some of the key render passes that result in the final image as seen when we press F12 to render. We also reviewed how to set up for node editing, and we “built” a small part of the render pipeline by compositing diffuse and specular passes into a final image. Finally, we looked at the basics of pixel math, demonstrating some of the ways that Blender composites pixels internally – which can also be reproduced with a simple node setup…

In this installment, we’ll expand on the previous lessons by building a slightly more complex set of render passes. In addition, we’ll look at how to “exclude” passes as well as include them – this will help us build acomposite that displays our background color, which was missing in last week’s exercise. Finally, we’ll look at some basic ways to make modifications to render passes before a final composite.

Assembling Additional Render Passes

Let’s begin by setting up our simple scene from last time, and preparing for node editing:

  1. Create a plane with a sphere on top, and assign the same material to both objects.

Simple scene setup with a sphere on top of a plane
Simple scene setup of a plane and sphere with the same material assigned to both.

  1. Configure a view pane to display the Node editor.
  2. From the Node editor header, activate the Composite Nodes and Use Nodes buttons.

Initial Composite Node Setup
Node editor with Composite Nodes and Use Nodes buttons active.

  1. From the Anim panel of the Scene buttons (F10) activate the Do Composite button.

Do Composite button active in Anim Panel
Scene buttons, Anim panel with Do Composite button active.

  1. Also from the Render panel, turn on the Ray button to enable Ray Tracing.
  2. Select the Render Layers panel and activate the passes for Combined, Diffuse, Specular, Shadow, and Reflection.

Render passes activated in Render Layers panel
Render Layers panel with necessary passes buttons active.

  1. For the material that was assigned to the plane and sphere, activate Ray Mirror from the Mirror Transp panel, and turn the RayMir value to 0.5.
  2. Make sure Traceable is activated in the Links and Pipeline panel.
  3. Render your scene with F12.

Beginning Node Setup Blend

Let’s wire up a composite, starting with the Diffuse and Shadow passes.

  1. In the Node editor, add a Mix node with Spacebar->Add->Color->Mix.
  2. Attach the Diffuse pass to the top RGB input of the Mix node, and attach the Shadow pass to the bottom input.
  3. Set the operation to Multiply, and the Factor to 1.0.
  4. Attach the Mix node’s output to the Image input of the Composite node.
  5. Press F11 to see the result. The F11 key shows the Render Buffer, but doesn’t re-render the scene. It will always reflect the result of whatever is attached to the Composite node’s Image input.

Diffuse pass and Shadow pass muliplied with Mix node
Diffuse pass multiplied with the Shadow pass

We get the Shadow pass’s effect by multiplying it with the Diffuse pass. Remember that in our Pixel Math lesson from Part 1, multiplying one pixel with another results in darkening the image, unless one of the pixels is pure white (1.0). Since shadows darken a scene, the multiply operation is perfect for compositing shadows.

Attach a Viewer node (Add->Output->Viewer) to the Shadow pass to see what it looks like. (If your Shadow pass looks rough, add a Subsurf modifier to the sphere at level 2.) All the shadow pixels are black, resulting in black pixels for those areas in the composite. The non-shadow pixels are white, resulting in no change to the composite on those areas. Multiplying black shadow pixels (0.0) always results in black pixels in the composite. Multiplying white shadow pixels (1.0) always results in unchanged pixel values in the destination composite.

Shadow pass multiplied by Diffuse pass
In the composite, black shadow pixels become black, and white pixels are unchanged.

Even in this simple node setup, we can make changes. By adjusting the Factor setting of the mix node, we can soften the effect of the shadow. This is because Factor controls how the values are combined, so a Fac setting of 0.5 will only apply 50% of the multiply effect.

Note: Since we’re using ray traced reflections, it’s better to leave this Mix node’s Factor setting above 0.5. This is because the Reflection pass takes shadows into account. If the shadow’s Factor is too low, reflections will brighten the shadow area when the pass is added to the composite.

Let’s add the Reflection pass to our composite:

  1. Add another Mix node to the editor with Spacebar->Add->Color->Mix.
  2. Attach the first Mix node’s Image output point to the new Mix node’s top Image Input point.
  3. Attach the Reflection pass output from the Render Layer node to the new Mix Node’s second Image input point.
  4. Attach the new Mix node’s output to the Composite node. You should be getting the hang of this by now.
  5. Set the Mix node’s operation to Add and the Factor to 1.0.

Nodes Composite of Diffuse, Shadow, and Reflection passes
Composite of Diffuse, Shadow, and Reflection passes.

As you can see from the result, Reflection passes are “added” to the composite. Any pixel in the Reflection pass that is brighter than black will brighten the destination composite. Note also that the Factor setting of this second Mix node can also be tweaked to enhance or reduce the reflective effect. Just keep in mind that if you set the Reflection mix factor higher than 1.0, you’ll need to bump up the shadow’s mix as well to compensate, because the Reflection pass is “shadow aware.”

Last but not least, add another Mix node to add in the Specular pass. This should be elementary at this point, and with a little practice, you’ll be doing this level of compositing without much thought at all. You may want to tinker with the Factor setting for this node as well, taking note of how the Specular pass can be enhanced or muted with settings greater than, or less than, 1.0.

Specular pass is added to the Diffuse, Shadow and Specular passes
Diffuse, Shadow, Reflection, and Specular passes mixed together with Factor settings of 1.0.

Render of four combined passes
Combined render with all four passes.

Note: One advantage to tuning shadow, reflection, and specular intensities in the composite is that it requires no additional rendering time. If you were to make these changes to the scene itself, you would need to re-render the scene to see the results. Composite nodes give you a great way to make moderate enhancements with real-time results.

Excluding Render Passes

In the render above, we don’t see our sky color. Actually, that’s not entirely true. In the reflection pass, we see the sky color just fine. However we don’t see it in the background where we would expect it. Now you might be wondering how we’re going to add sky in a section about “excluding” passes. Let the magic happen.

In the Render Layers panel of the Scene buttons (F10) take note of the “Combined” pass. This pass corresponds directly to the “Image” attach point of the Render Layers node.

Combined pass equates to the Image point on the Render Layer
Left: The Combined render pass is the same as the Image connection point of the Render Layer (Right).

By default, the Combined (Image) pass is Blender’s internal composite for the entire render. Everything is in there: Diffuse, Specular, Shadow, Reflection, AO, Refraction, etc., regardless of whether or not that pass is included on the Render Layer node (but only if it’s configured to run, such as with Ambient Occlusion). However you can tell Blender to excludea pass from that Combined composite.

This is accomplished by using the CTRL key when clicking on the pass buttons. When this is done, a black dot appears on the button, indicating that this pass will not be included in combined. Note that this is independent of whether or not the pass is included on the Render Layer node.

Excluded render passes marked with black dot
Render passes excluded from the Combined pass (Left) are marked with a black dot – Render passes included on the Render Layer node (Right) have their buttons selected.

Let’s decode the above screen shot. First, note that not all the passes have the potential of being excluded. In the case of the Diffuse pass, it’s the minimum requirement of the Combined pass: it has to be included no matter what, so you’re not given the option of exclusion. The other passes areextra data, such as Z buffer, Vector, Normal, UV, Index, and Color. They are not actually a part of the composite, but are data passes used by Blender internally, and made available to the user for convenience and advanced effects. The Color pass is a subset of the Diffuse pass, so it’s always included as well. One additional piece of the puzzle, included in the Combined pass by default, is the sky background color.

So the above configuration gives us a Combined pass that will contain diffuse information, along with our background sky color. Our Combined pass is now configured to replace our Diffuse pass, which is why Diffuse is not selected. We don’t need it as a separate pass because we already have it in Combined. We’ve told Blender to exclude the Specular, Shadow, and Reflection passes from the Combined pass, but we’ve specified that they should definitely be included on the Render Layer node instead.

If you haven’t done so already, configure your own scene as shown above, with the Combined, Specular, Shadow, and Reflection passes selected. Using the CTRL key, select Specular, Shadow, and Reflection passes for exclusion from the Combined pass. We’re not concerned with AO, Refraction, or Radiosity because they’re not configured to affect our scene. If they were, we’d take them into account.

Here’s the new node setup:

Combined with sky color using Alpha channel
Composite with sky: the Combined node is connected to the Multiply Mix node’s first Image input; additionally, the Alpha output is connected to the Factor input.

In the above node diagram, everything’s the same as before except for two connections: 1) The Render Layer’s Image (Combined) point is connected to the first Mix node’s top Image input (because it’s replacing our Diffuse pass); 2) The Alpha output is connected to the Mix node’s Factor input. The Factor setting can take an image input – in this case, the Alpha channel.

Why did we need the Alpha channel and how is it affecting Factor? Let’s take a look at both the Alpha and Shadow passes:

Alpha pass and Shadow pass
Left: Alpha Render Pass – Right: Shadow Render Pass

Earlier we composited the Shadow pass by multiplying it with the Diffuse pass. This most recent time we multiplied it with the Combined pass, which we used to replace Diffuse. However the Shadow pass has an entire area of black pixels around the border of our objects, very much like the Alpha channel. When we multiply black pixels by any other value the result is black, so it ends up setting our sky color to all black.

However we have the Alpha channel to rescue us. When we add it as an input to Factor, it tells the Mix node to only apply the effect where the pixels are white. This means that where the pixels are black, no multiplication is taking place. The result is that our sky background color is preserved. If this isn’t making sense, remember that the Pixel Math operations are performed on a per pixel basis. The Alpha channel pixels tell the Mix node to take into account the per pixel value of the Factor input. So instead of setting Factor globally and manually, we’re using an image (the Alpha channel) to apply it per pixel.

Here’s the rendered result:

Composite render complete with sky color
Composite render with sky color visible (Specular Mix node’s Factor is 2.0)

You might notice a faint black line around the outside pixels, where the objects border the sky color. This is an artifact of the Shadow pass being multiplied with the Combined pass, and results because of edge anti-aliasing. We’ll deal with this at the end of the lesson.

Modifying Passes Before the Final Composite

Let’s touch on some quick ways to make modifications to the render passes before assembling them into the final composite. We’ve already looked at making adjustments to the Factor setting of the Mix nodes, which is very handy, but there are other ways to modify passes as well.

One of the things you might want to apply for your renders is color correction. We’ll simplify our node setup since we’ll be adding nodes for color adjustments.

Specular pass exluded in Combined pass
Simple node setup with the Specular pass included for the Render Layer, but excluded for the Combined pass.

The above node setup is very much like the one we did in Part 1 of this series. It simply adds the Specular pass to the Diffuse. There are two differences however: 1) We’re using the Combined pass instead of the Diffuse pass; 2) We’ve excluded the Specular pass from the Combined pass so that we can work on each individually. I’ve also turned off ray tracing, because we won’t be using any Shadow or Reflection passes in this example.

Let’s expand our setup to make modifications to the Combined pass, then we’ll add the Specular pass back on at the end.

  1. Add an RGB Curves node with Spacebar->Add->Color->RGB Curves.
  2. Attach the Image output of the Render Layer to the Image input of the RGB Curves node.
  3. Attach the Image output of the RGB Curves node to the Image input of the Composite Node.
  4. On the RGB Curves node, press the R button near the top (Red channel). You’ll see buttons for R, G, B, and C (Combined). This is similar to other color curves widgets you may have used in image applications.
  5. Click in the RGB Curves window to add a curve point, and drag it toward the upper left corner, as shown below.
  6. Press F11 to see the result.

Combined pass with modified RGB curves
Node setup with Combined pass being piped through an RGB Curves node.

What we’ve accomplished here is to add a red cast to our Diffuse pass by piping it through an RGB Curves node, and modifying the Red channel’s gamma. As with the Mix node, we can adjust how much of the effect is applied with the Factor setting.

One of the problems you may have noticed with this effect is that we’re not just adjusting the red channel of the scene objects, but we’re effecting the entire image, sky and all. The solution is simple. We can use the Alpha channel as the RGB Curve’s Factor input. This will signal the RGB node to only apply the effect where the Factor input sees white pixels. When we attach the Alpha to the Factor, the RGB Curve node’s effect on the sky color goes away.

The last step is using a Mix node to add the specular channel back in. This is why we excluded it from the Combined pass, so that we could deal with the Diffuse color separately.

Diffuse modified by RGB Curve node with Specular pass added
RGB modified Diffuse pass with Specular pass added on, and the Alpha channel used as Factor input for the RGB node.

Render of RGB modified Diffuse channel
Render of RGB modified Diffuse channel – the RGB node’s combined gamma has been reduced, and the Mix node’s Factor set to 2.0 for Specular.

Since we had the Diffuse and Specular passes separate, we could have chosen to make modifications to the Specular pass as well. You might want to experiment with this on your own, and take note of some other node types as well, such as the HSV node for color modification on Hue, Saturation, and Value levels.

Addressing the Shadow Pass’s Multiply Artifacts

Earlier when we composited shadows, there was a faint black line that appeared around the border of the scene objects and the background.

Black line on edge of Alpha from multiplied Shadow pass
Composite render showing faint black line resulting from the multiplication of the Shadow pass.

This happens in part because the Shadow pass not only has black pixels representing shadows, but it has black pixels around the boundary of the scene objects as well. When the Shadow pass is multiplied with the Diffuse pass, it darkens the very outside pixels of that pass.

We need a better way to deal with this than we did previously: by using Alpha as a factor to control the Mix node. Pixel anti-aliasing occurs on the edges of the Alpha channel as well as the Diffuse pass. The result is a very faint overlap of pixels between those passes at the edges. Let’s use the Alpha channel again, but in a different way.

Here’s another look at the Alpha and Shadow passes:

Alpha and Shadow passes
Alpha and Shadow passes: Almost identical except for the shadow pixels of the sphere object.

These passes are nearly identical at their edges. So identical in fact, that we can use the Alpha channel to get rid of the Shadow pass’s outer black pixels altogether. However, first we need to invert the alpha channel, then add it to the Shadow pass. The result will be a Shadow pass that only contains black shadow pixels, no black boundary.

Shadow pass excluded from Combined
Node setup with Shadow both excluded from Combined, and included in the Render Layer.

Make sure you turn on ray tracing for this example, but you can turn off Ray Transparency for the material, as we’ll only be dealing with the Shadow pass separately. The Combined pass includes Specular, but excludes Shadow. We’ve made Shadow available as a separate point on the Render Layers node.

The next step is to invert the Alpha channel.

  1. Add a ColorRamp node with Spacebar->Add->Converter->ColorRamp.
  2. Attach the Render Layer’s Alpha point to the input of the ColorRamp.
  3. Attach the ColorRamp’s output to the Composite node so that you can see the result.
  4. Render the scene (F12).
  5. On the ColorRamp node, switch the places of the black and white points on the color band.

Inverted Alpha channel with ColorRamp node
ColorRamp node is used here to invert the values of the Alpha Channel.

The ColorRamp node works like any color ramp, in that it allows you to map the luminosity values of the input to entirely new luminosity values. In this case, we’ve inverted the ramp entirely, making the white pixels black, and the black pixels white. Note from the output that the result has inverted our Alpha channel.

Back to Pixel Math for a moment: when we add the Alpha channel to the Shadow pass with a Mix node, the white pixels will combine with the black ones to give us white pixels (0.0 + 1.0 = 1.0).

Since there are no white pixels anywhere else in the inverted Alpha channel, only black pixels, those parts of the pass will remain unchanged (0.0 + x = x). Let’s do that now with the following node setup:

Inverted Alpha added to Shadow pass
Node setup adding inverted Alpha channel to Shadow pass.

Remember to set your Factor to 1.0.

The result here is a modified Shadow pass that has no black values except for the shadowed area itself. Let’s now Multiply this Shadow pass and see the result. We need to set up the Render Layer like we did before, and re-enable Ray Mirror in the Material settings.

Specular, Shadow, and Alpha excluded from Combined pass
Combined pass excludes Specular, Shadow, and Reflection – these are included in the Render Layers node.

Here’s the final wiring diagram for our completed node setup:

Final node setup with corrected Shadow pass
Corrected Shadow pass combined with Diffuse+sky, Reflections, and Specular.

Final Node Setup Blend

If you can’t tell the relationships by the diagram, here’s a rundown:

  1. The Combined pass (Diffuse + sky) is multiplied by the corrected Shadow pass, which consists of the inverted Alpha channel added to the original Shadow.
  2. The Reflection pass is added to the result with another Mix node.
  3. The Specular pass is added to the result with a final Mix node.
  4. The resulting output is piped into the Composite node.

Let’s look at the output render of the above node setup:

Final render with corrected Shadow pass
Final render with no darkened edge artifacts

In this render there is no black pixel influence on the edges of our scene objects. Our treatment of the Shadow pass has completely eliminated them, and we’ve managed to build a Composite node setup that’s ready to be modified in a number of ways on a per-pass basis. Now that you know how to assemble this type of composite, you can use this as a starting point for all sorts of node-based modifications.

Note that our earlier solution of using the Alpha channel to modulate the Factor of the first Mix node no longer applies, since we have completely reworked the Shadow pass; however you will find that the Alpha channel (or an inverted Alpha) will often serve a beneficial purpose as Factor input, to limit the effects of a variety of nodes to specific areas of your scene.

Conclusion

By understanding just a few basic node types, you gain control over how your scene is composited. You obtain the ability to modify the appearance of your scene on a pass by pass basis, and gain all sorts of interesting possibilities for creativity.

Although so far we’ve only toyed around with rebuilding Blender’s existing render composite capability, in the next lesson we’ll look at additional node types, and consider some ways to create effects that aren’t possible without Composite nodes. We’ll also take a closer look at the Z buffer.

No comments:

Post a Comment