phatcoreportfolioprojects
Zoom inFig. 1: Result of boolean operation.
Fig. 1: Result of boolean operation.

Composite Boolean Operations

30 September 2007

This page describes a method to perform boolean-like operations in Blender's compositor (Fig. 1). Presented here is the intersection operator used to mix materials in many objects based on other geometry in the scene. This method only alters the material, not the shape of the objects.

Requirements

Compositor nodes. Tested in Blender 2.45; should also work with versions 2.43 and 2.44.

Method

There are two operands:

  1. all regular scene geometry and lighting
  2. the object being used to cut the geometry
Zoom inFig. 2: Render layers. From the left: operand 1, operand 2 (outer) operand 2 (inner).
Fig. 2: Render layers. From the left: operand 1, operand 2 (outer) operand 2 (inner).

For any rendered image of the scene, we need to identify each pixel of operand 1 that is contained within operand 2. The images' Z (depth) and alpha (transparency) channels will be used to create a mask to mix materials.

Three render layers are needed (Fig. 2). The first operand is on one render layer (BaseLayer). The second operand is rendered on two other layers: one showing the outside faces (BoolOuter), and one showing the inside faces (BoolInner). The three layers are shown below. All are lit to show their shape.

The object on the BoolInner layer needs to have only its inner faces rendered. The Blender wiki suggests using a Blend texture mapped to the normal to control transparency, but this is unusable as it doesn't affect the z-buffer. The easiest way to render only faces on the reverse side is to enable ZInvert1 in the material.

To ensure consistency, the same mesh should be used on both the BoolOuter and BoolInner layers. A linked duplicate is ideal, but requires the material to be linked to the object instead of the mesh (Materials buttons > Links and Pipeline > OB button).

Zoom inFig. 3: The distance of geometry on each layer from the camera. In this graph, every point of operand 1 is the same distance from the camera. Operand 2 is a solid object (the blue area) that extends, at various points in the image, from behind the camera to some distance beyond operand 1. Darkened areas are outside the intersection and will have thier materials changed.
Fig. 3: The distance of geometry on each layer from the camera. In this graph, every point of operand 1 is the same distance from the camera. Operand 2 is a solid object (the blue area) that extends, at various points in the image, from behind the camera to some distance beyond operand 1. Darkened areas are outside the intersection and will have thier materials changed.

Fig. 3 shows how the z-buffer can be used to identify intersecting pixels. Zones A-E (indicated along the pixel axis) are used here to derive an expression for identifying pixels. The following expressions uniquely identify each zone:

Where zg, z1 and z2 are the z-values of the BaseLayer, BoolOuter and BoolInner surfaces respectively. α is the alpha value of operand 2.

The equality operator is used in expressions C and D because once z1 goes behind the camera, the camera sees the inside of that object, so it is rendered to be the same as z2. In expression E both z1 and z2 are behind the camera, so their values will both be infinity and are therefore equal.

B and C are the zones of interest. The complete expression to identify pixels of BaseLayer that are within operand 2 is:

α = 1 and (z1 < zg < z2 or zg < z2 = z1)

Fig.&nbsp;4: Nodes used as logic operators.
Fig. 4: Nodes used as logic operators.

Compositing nodes are used as logic operators for the above expression (Fig. 4). Each pixel should evaluate to 1 (white) if the expression is true or 0 (black) otherwise.

Zoom inFig.&nbsp;5: Screenshot of node setup.
Fig. 5: Screenshot of node setup.

"α = 1" is easy, because the alpha channel ranges from 0 to 1. The Z Combine node is used as a less than operator (C1 to C3), because that's exactly what it does: it chooses between two inputs based on which z-value is greater. Since the Z Combine node defaults to the first input if neither z-value is greater, and we know z2 ≥ z1, it can also be used to test equality (C4). The node setup is shown in Fig. 5.

Things to note:

Download the example scene.

Limitations

  1. ZInvert works fine for simple shapes like spheres, but complex shapes with overlapping geometry will not render correctly. A script could be written to hide all parts of the mesh that face the camera, but the expressions above may need to be modified.
  2. The z-buffer is not anti-aliased, so neither is the mixing. Actually, I'm not sure about this.

See Also

  1. Blender manual page on compositing nodes

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 2.5 Australia License.