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.
Compositor nodes. Tested in Blender 2.45; should also work with versions 2.43 and 2.44.
There are two operands:
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).
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)
"α = 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:
This work is licensed under a Creative Commons Attribution-Share Alike 2.5 Australia License.