Wednesday, September 23, 2009

Tutorial for sectioning a mesh with nodes

This is from the Blender wiki:

In this tutorial we will create a section passing through a simple object using material nodes. We'll then have a look at various ways of combining several sections into more complex cut-outs, useful for, for example, architectural sections.

This node set-up was originally described in this thread on the Blender Artists forums. The Cross Section script might also be of interest.

Simple set-up

Add a cube

Adding a cube

In top view (Num7), add a cube (Space>Add>Mesh>Cube).

Just to make this simple object slightly more interesting, let's rescale it twice. Rotate (MMB drag) your view to see what you are doing. Under the 3D view, make sure "Median point" is selected as pivot and "Move object centers only" next to it is disabled.

Still in edit mode with everything selected, scale along the X axis (S>X>3), and along the Y axis (S>Y>2).

Selecting a pivot

Create the section texture

Selecting a texture type

Tab into object mode. Go to the material menu (F5) and click "Add new". Accept the default material and name.

  1. Then go to the texture menu (F6), click "Add new" and rename the texture "Section". Select Blend as material type and in the Color tab activate Colorband.
  2. The colorband we are going to create will serve as a mask that we are going to map onto the cube later. To get a crisp sharp section through the cube, we just need to move the two default colors to the center of the colorband in order to get a black transparent half and a white opaque half. So, move the left color to Pos 0.5. leave it black. Select the right color (change Cur:0 to Cur:1). move it to Pos 0.5 and change the color to White.
Setting up the colorband

Three axes for visualisation

Adding a cylinder for the Z axis

To make it easier to orient ourselves later, we'll add three cylinders representing the three coordinate axes in the render outputs.

In the example below all three cylinders are made a single object. This is not necessary of course. If you prefer to make them three separate objects, just tab into object mode before copy-rotating them. Then follow the instructions below. If you make them three separate objects you can also skip the vertex group part below; just assign materials to each separate object.

  1. In object mode and in top view, add a cylinder (Space>Add>Mesh>Cylinder) with, say, 16 vertices. As above, rescale it (S, Shift+Z, 0.1) (S, Z, 5).
  2. Copy-rotate it to create an X axis (Shift+D, R, Y, 90).
  3. Repeat to create a Y axis (Shift+D, R, Z, 90).
Colors setup for all three cylinders.

Let's create three vertex groups for these cylinders and add three simple materials to them.

  1. Go to the edit menu (F9). Still in edit mode, deselect everything (A). Place the mouse over the first Z-cylinder and hit L to select all linked vertices. In the Link and materials submenu, add a new vertex group, name it "Z", and hit Assign. Add a new material index, change its color to blue, and hit Assign.
  2. Deselect everything and repeat as above for the X and Y cylinders. Make them red and green respectively.
  3. Go to the material menu again (F5). In the Links and pipeline submenu, use the "3 mat 3" button to select the newly created materials and rename them Blue, Red, and Green respectively.

Node set-up

Initial node setp
Adding nodes

Now let's set up the nodes.

  1. First lets open the node editor. Assuming you are using the default Blender layout, in the top menu select the Material screen (or simply Ctrl+). Open the node editor in one of the subwindows using the button in the lower left corner. All you will see at this time is a grey area, but don't worry. Open the material menu (F5) if not open already.
  2. Select the cube. In the Links and Pipeline subwindow click Nodes to activate nodes for this material and ZTransp to activate transparency.
  3. Drag (G) the Material and Output nodes and place them on the right part of the area. In the material node, select the material.
  4. On the left side, add a geometry input node (Space>Add>Input>Geometry).
  5. Add Mapping node (Space>Add>Vector>Mapping). It should automatically be connected to the Global connector of the Geometry node.
  6. Add a Texture node (Space>Add>Input>Texture). Use the tiny box to select the "Section" texture.
  7. All nodes are set up now. Connect the texture node's Value output to the Output node's Alpha input to complete the node system.
Final node setup

Moving the section around

Section passing along the Y axis with the default mapping
Section rotated about the Z axis

Press F12 to render the scene. With the default settings in the mapping node, the section is passing along the Y axis. You can easily change this in the mapping node. Move the section along the axes by changing the value in the three top boxes. Rotate the section about the axes using the middle row. In my case I had to change the Z rot value to -135 to make the section face the camera.

Increasing the X value
moves the section along the X axis.

Note: Keep in mind that the mapping moves the section before it rotates it. So, if you change the rotation first, moving the section can then lead to non-intuitive results. Additionally, geometrically our section is an endless plane, so rescaling it or moving it in some directions, is not going affect the result.

[edit] Improving the setup

Trashadow enabled

As you can see the inside of the cube is dark, as are the parts of the cylinders passing through the cube. Let's change this.

  1. With the cube still selected, in the shaders submenu enable TraShadow. This will allow transparent shadows to be cast on the inside of the cube.
  2. Select the cylinder object, and for each material enable TraShadow.
  3. If you want to make the cylinders all bright, disable Shadow for each material.

Adding more than one section

Two sections applied to the same material

To add several sections,

  1. Copy the mapping and texture nodes. Plug the Global output of the geometry node into the second mapping node.
  2. Change the orientation of the second series of nodes. In the example given, the section was rotated 90 degrees about the Z axis.
  3. Add a mix node, and plug both texture nodes' color output into it.
  4. Add the value output of the first texture node into the Fac input of the mix node.
  5. Finally plug the mix node into the Alpha input of the ouput node.
Node setup for more than one section

Using different mix modes

Sections along the X axis...
...and Y axis

More complex sections can be achieved by using combinations of different mix modes.

Here are two simple sections through a building. The first is passing along the X axis and the second along the Y axis.

These two simple sections can be combined into more interesting outputs using several methods.

Result using multiply mode
Result using difference mode

With Multiply the values of both sections get multiplied. If one or both sections are black/transparent the resulting output is zero or transparent. Therefore, only the part of the building shared by both sections remains visible.

With Difference the values from both sections are subtracted from each other and the absolute value is returned. The resulting output is the parts of the building unique to either of the sections.

The two mix outputs combined into a single complex section
Three different sections combined into a single cutout

Finally, by plugging these two outputs into a third mix node set to Add, both outputs are combined into a single section.

To make the cut-out even more interesting keep adding sections using different mixing modes.

Here the previous output is combined with a third horizontal section. By choosing Multiply the transparency of this third sections cancels out the previous result making the top of the building transparent.

No comments: