Thursday, May 24, 2012

A fine procedural wood material for Cycles—Dissected

Just yesterday, I discovered this forum thread on Blenderartists, entitled A fine procedural wood material for Cycles, where the author, wesdves, shares an incredibly complex and good looking procedural material created with Cycles nodes. The .blend file is available for download on his first post. Just taking a look at the node setup for this material is mind-boggling (figure 2). The author has groups of nodes within other groups of nodes, all interconnected, mixing together, doing weird math, and in the end you get this great, realistic-looking result:

Figure 1. I created my own scene, appended (Shift-F1) the Wood material,
assigned it to a subdivided cube, and hit Render. This is the result.

Figure 2. This is what the node setup for the Wood material looks like. This is just the tip of the iceberg :)
Click on it to see a larger version.

I decided to render the file at different node locations to see how this thing was put together. Here are some of these renders, explained. I hope not only to learn a bit more about nodes in general, but also to share this knowledge so that others can come up with similar crazy stuff.

The first thing I did was connect each one of the two Mix nodes that come out of the color nodes at the bottom of the window (figure 2), one at a time, and do a render of that (figures 3 and 4). After all, that's where the color is coming from. Notice that the color distribution on each of these two Mix nodes is controlled by the same Wood.Rings node group, which I rendered too (figure 5). This Wood.Rings node in turn gets some kind of information from two other node groups (Wood Gnarl, Grain and Stain, and Wood.Planks), originating all from the Object output of a Texture Coordinate node. Notice also that, against what I think should be standard procedure, the author has connected yellow outputs to blue outputs along the way. This is probably OK here, although I have to admit that it hurts my eyes :) 

Figure 3. Result of rendering Mix 1.

Figure 4. Result of rendering Mix 2.

Figure 5. This is what you get when you connect the Wood.Rings
group into the Color input socket of the Glossy BSDF node and render it. 

Since a mix node puts these two outputs (figures 3 and 4) together through a third input (figure 5), I figured that's the same as having two images layered in Photoshop and mixing them through an alpha channel. So that's precisely what I did next. The result is shown on figures 6 and 7.

Figure 6. The final result of compositing figures 3 and 4 in Photoshop.
I think it's very similar to the final result in Cycles.

Figure 7. The Photoshop layers setup. I'm using an inverted version
of figure 5 as a layer mask to control the mix.

I continued doing some digging, connecting node outputs into the Glossy BSDF shader node, and here are some of the results I got.

Figure 8. This is what you get if you render the Object output
from the Texture Coordinate node. The colors depend on their
orientation in the local X Y Z space of the object. I think red is
for X, blue is for Z, and green for Y (not seen here,
only the yellow hints at it).

Figure 9. The result of rendering the Wood Gnarl, Grain and
Dirt node group.

Figure 10. The result of rendering the Wood Planks node group.
These last two are almost identical.

I'm going to try to dissect one of the node groups in this whole node setup. There is a lot of math that, frankly, I don't understand completely. Some of it seems superfluous and overly convoluted, but checking every step is a feat of back-engineering by itself, outside of the scope of this dissection exercise. I have a feeling that a simpler solution might have been possible in some areas—but I don't know fr sure right now. I'm going to focus on the Wood.Planks node group and try to have an intuitive sense for what it's doing. If you click on the Wood.Planks node and hit Tab, it expands, and it shows its contents (figures 11 and 12).

Figure 11. The collapsed Wood.Planks node group.

Figure 12. The Wood.Planks node group in all its splendor. Actually, not quite. notice more node groups inside of it :)

This Wood.Planks node group starts by separating the color components from the Object output in the Texture Coordinates node. If you render each of those three components you get the images shown on figures 13 to 15. Notice that they are greyscale, because each of the channels in an RGB image is defined in a scale from 0 to 1 (or 0 to 256, or whatever). An RGB color has three dimensions—a greyscale value has only one dimension.

Figure 13. Grayscale output for Red.

Figure 14. Grayscale output for Green.

Figure 15. Grayscale output for Blue.
 Each one of these separate channels goes through a conveyor-belt-like process that changes it a bit a time. The most important step is where they go through the node group named Wood.Modulator (figures 16 and 17) by its author. You can read about modulation is on Wikipedia's article about it. In the end, the images above get converted to what you see on figures 18 to 20.

Figure 16. The collapsed Wood.Modulator node group.

Figure 17. Expanded Wood.Modulator node group. Lots of math happening here. basically, the grayscale values are
subjected to division, rounding, subtraction, and multiplying with each other and with a starting value.

Figure 18. Red value after the modulation treatment.

Figure 19. Green value after the modulation treatment.

Figure 20. Blue value after the modulation treatment.

The Wood.Modulator node group has to outputs. So far, I looked at what the Frac output does. Here is what the Int output looks like:

Figure 21. Int output result.

I'm not going to continue with the remaining node groups. Some of them incorporate procedural textures modified with math—which gives the wood grain look. Also, you probably get the idea by now of how to begin to dissect a node setup and how to go about analyzing things. Someone on the forums mentioned that it would be great if there was some kind of system for adding comments to nodes. I think it would be a great thing to have—yet another tool for learning and sharing.

No comments:

Post a Comment