Material Masking in Cycles

[box color=”gray”]Normals and Material Masking Series – Part 1 | Part 2 | Part 3[/box]

QR-Baking-Normals-from-Bump_Heading-Image

In this third and final installment of the series, I will demonstrate how to use a series of mask images to apply several different shader setups to different areas of an object in a single material. To recap, in the first part of this series, using a simple cube as an example, I created a greyscale bump map image and used it to bake a normal map using Blender Internal. In the second part I discussed the proper way to apply a normal map in both Blender Internal and Cycles.

Material-Masing-In-Cycles_Bump-and-Normal
Bump and normal maps

Now, using the original bump map image as a starting point, I will create a series of simple black and white images to use as masks for applying several types of materials to various areas of the cube, following the details of the normal map. If you were smart ahead of time and saved a layered GIMP (xcf) or Photoshop (psd) image of the bump map, it’s simply a mater of isolating the elements, turning them white, and making the background black.

The Base Material

The base material is a grey metal texture. To achieve this look, I first add a [tooltip gravity=”n” title=”Input -> Texture Coordinate”]Texture Coordinate[/tooltip] node and an
[tooltip gravity=”n” title=”Texture -> Image Texture”]Image Texture[/tooltip] node then plug the “UV” output socket of the Texture Coordinate node into the “Vector” input socket of the Image Texture node. Then, load in the metal texture image (this texture is from CGTextures.com and can be found here).

Material-Masing-In-Cycles_Metal-Texture-Nodes-1
Base metal image texture

Now, I want to mix two shader types for this material: Diffuse and Glossy. First, add the [tooltip gravity=”n” title=”Shader -> Diffuse”]Diffuse[/tooltip] shader node and plug the “Color” output of the Image Texture node into the “Color” input of the Diffuse Shader node. This will apply the metal texture image as the diffuse color of the cube.

For the Glossy shader, I want to color it with the same texture but lighten and de-saturate it first. This is easily achieved by adding a [tooltip gravity=”n” title=”Color -> Mix”]Mix[/tooltip] node, keeping the default type as “Mix” and plugging the “Color” output socket of the Image Texture node into the top “Color1” input socket of the Mix node. Then, set the bottom “Color2” to a gray color (R:0.5, G:0.5, B:0.5) and plug the “Color” output socket of the Mix node into the “Color” input socket of the Glossy node.

Material-Masing-In-Cycles_Metal-Texture-Nodes-2
Ligthen and de-saturate the image texture for glossy color

To mix these two shader nodes I will add a [tooltip gravity=”n” title=”Shader -> Mix Shader”]Mix Shader[/tooltip] node then plug the “BSDF” output socket of the Glossy node into the top “Shader” input socket of the Mix Shader node and the “BSDF” output socket of the Diffuse node into the bottom “Shader” input socket. I will also use the metal texture image to control the mix factor of these two shader nodes, but I want to have some control over the mixing. For that I will add a [tooltip gravity=”n” title=”Converter -> Color Ramp”]Color Ramp[/tooltip] node then plug the “Color” output of the Image Texture node into the “Fac” input of the Color Ramp node and plug the “Color” output of the Color Ramp node into the “Fac” input of the Mix Shader node (this is mixing data types but for this purpose it will work fine). Finally, plug the “Shader” output of the Mix Shader node into the “Surface” input of the Material Output node.

Note: the normal map is also applied here, for this process see Part 2 of this series. Make sure to plug the “Vector” output socket of the Normal map node into the “Vector” input sockets of all subsequent shader nodes added.

Material-Masing-In-Cycles_Metal-Texture-Nodes-3
Mixing the diffuse and glossy shaders (click for larger view)
Material-Masing-In-Cycles_Base-Material-Render
Render with base metal texture

Altering the Color of the Base Material with a Mask Image

The first area of detail on the cube in which I will alter the material is the rough square area on the right-hand side of the cube. For this part, I will simply alter the color of the metal texture in this area. To do this I first create the mask image, using my original bump map as a guide, where the area of the square is pure white and the remaining area is pure black.

Material-Masing-In-Cycles_Metal-Rough-Square-Mask
Mask image created using bump map as a guide

Now I will add another Image Texture node, plug the “UV” output socket of the Texture Coordinate node into the “Vector” input socket of the Image Texture node and load in the mask image. Since all I want to do here is tint the metal texture image in this particular are of the cube, there is no need to add a new shader. I can simply alter the image before it plugs into the Diffuse node. To do this I will add a Mix node with the mode set to “Overlay” and drop it in between the “Color” output socket of the Image Texture node and the “Color” input socket of the Diffuse node. Making sure the “Color” output from the Image Texture node is running into the top “Color1” input of the Mix node, I can then change the “Color2” input to whatever color I want to tint the texture.

Material-Masing-In-Cycles_Tint-Nodes
Tinting an area of the image texture with a mix node set to overlay (click for larger view)
Material-Masing-In-Cycles_Tinted-Render
Render with tinting applied

Adding Emission to the Engraved Lines

The next area of detail I want to focus on is the engraved lines that run along the top and bottom edges of the cube and around the circular details on the left-hand side. I will make these emit a cool sci-fi green light. Again, the first step is to create a mask image to isolate this area of the cube. Using the original bump image this is a simple task.

Material-Masing-In-Cycles_Metal-Engraved-Mask
Mask image created using bump map as a guide

Since I want this area to be glowing with light, I’ll first add and [tooltip gravity=”n” title=”Shader -> Emission”]Emission[/tooltip] shader node and set the color to a yellow-green and the strength to 1. Also, for the sake of organization, I will group the nodes comprising the metal material by shift-selecting them all and hitting Ctrl+G. Then press Tab to close the Group Node. Next, to mix the node tree to this point with my new Emission node, I’ll add a Mix Shader node and plug the “Shader” output socket of the Group Node into the top “Shader” input of the Mix Shader node and the “Emission” output of the Emission node into the bottom “Shader” input.

Note: there is no “Vector” input on an Emission shader node, so I cannot apply the normal map to this shader.

Material-Masing-In-Cycles_Emit-Nodes-1
Group the previous material setup and add the emission node

Now, I want to control the mix factor of my base material and the Emission shader with the mask image. So, I’ll add another Image Texture node, load in the mask image and make sure the “UV” output of the Texture Coordinate node is plugged into the “Vector” input of the new Image Texture node. Then, simply plug the “Color” output socket of the Image Texture node into the “Fac” input socket of the Mix Shader node. (This is again mixing data types, but since the mask image is only black and white there will be no loss of data here.)

Material-Masing-In-Cycles_Emit-Nodes-2
Use mask image to control the mix factor of emission shader

Render with emission shader mix
Render with emission shader mix

Darkening the Vent Holes

I want to fake some depth in the vent on the left-hand side of the cube and I will do that by simply coloring in the holes with pure black. For this and the next part, I will use the same technique that was used above and “daisy-chain” onto the end of the node tree. I will also again group the previous material nodes. First, of course, create the mask image.

Material-Masing-In-Cycles_Metal-Vent-Mask
Mask image created using bump map as a guide

Then add a Diffuse shader node (make sure the Normal Map “Vector” is plugged into the Diffuse node “Vector”) and set the color to pure black, add a Mix Shader node and plug in just like before. And, finally, add another Image Texture node, load in the mask image (make sure the “UV” from the Texture Coordinate node is plugged into the “Vector” of the new Image Texture node) and plug the “Color” output socket of the Image Texture node into the “Fac” input of the Mix Shader node.

Material-Masing-In-Cycles_Vent-Nodes
Adding black shader to vent holes (click for larger view)

Making the Text Glass

Lastly, I want to make the text on the top of the cube glass and have light coming from behind it. I’m sure you get the routine by now, so I won’t repeat myself. Just add a Glass shader node and apply the same technique, using a mask image for the Text.

Material-Masing-In-Cycles_Metal-Text-Mask
Mask image created using bump map as a guide
Material-Masing-In-Cycles_Text-Glass-Nodes
Adding glass shader to text (click for larger view)

Now, to add the light behind the glass, I will add a plane to the scene and position it inside the cube just under the top side. For visual interest, I prefer the light not be even across the plane, but brighter in the center and fade out to the edges. To do this, UV unwrap the plane, then in GIMP or Photoshop create a grayscale image of a radial gradient – fairly simple.

Material-Masing-In-Cycles_Emit-Plane
An emission plane inside the cube, to shine light through the glass, and gradient texture to control emission intensity

Then use the following node setup, loading the gradient image into the Image Texture node. The Mix node is used to control the overall brightness without having to edit the image.

Material-Masing-In-Cycles_Emit-Plane-Nodes
Node setup for emitter plane

The final result:

Material-Masing-In-Cycles_Final-Render
Final render

This technique works well to mix materials following the influence of a normal or bump map. But it can be used in a wide variety of circumstances, anytime you need part of an object to be one material and another part a different material. That’s it for the Normals and Material Masking series, I hope it was helpful and informative.

<- Part 2: Using Normal Maps in BI and Cycles

Some textures used in creating models appearing in imagery on this post are from CGTextures.com, a most excellent source for photo textures.

Quick Reference: Using Normal Maps in BI and Cycles

[box color=”gray”]Normals and Material Masking Series – Part 1 | Part 2 | Part 3[/box]

QR-Baking-Normals-from-Bump_Heading-Image

In Part 1 of this series I covered how to bake a normal map from a bump map using Blender Internal’s texture baking abilities. This part demonstrates how to properly apply a normal map to a material in both Blender Internal and Cycles.

To quickly review, I have a simple subdivided cube, with some holding loops, unwrapped and the UVs arranged to maximize the sides of the cube facing the camera. I have also loaded the normal map image into the UV/Image Editor workspace.

QR-Using-Normal-Maps-In-BI-And-Cycles_Cube-And-UVs
Cube UV layout and normal map

Using a Normal Map in Blender Internal

To apply a normal map to a material in Blender Internal, first add a material to the cube then add a new texture to the material. Next, select “Image or Movie” and load in the normal map image. Under Image Sampling check the box next to Normal Map. It’s also a good idea to check Minimum Filter Size under Filter and set the Filter Size to 0.10 (the lowest setting possible). This will give a better quality result, especially if the normal map image is not particularly large. Set the Mapping to UV and, under Influence, check Normal and set the value to 1.

QR-Using-Normal-Maps-In-BI-And-Cycles_BI-Texture-Settings
Texture settings for normal map in Blender Internal (Click for larger view)

Using a Normal Map in Cycles

To apply the normal map to the cube in Cycles, first, add a material to the cube. Now bring up the Node Editor workspace (I generally split the window vertically with the Node Editor on the top and the 3D Viewport on the bottom.) By default, you will have a Diffuse BSDF shader node plugged into the “Surface” socket of a Material Output node. We’ll just use the diffuse shader, but give it a sexy color.

QR-Using-Normal-Maps-In-BI-And-Cycles_Default-Nodes
Default Cycles material nodes

Now we need to get the normal map image into the mix and we’ll do that with two nodes: Texture Coordinate and Image Texture. First, hit SHIFT+A to add a node and select Input -> Texture Coordinate, we’ll use this to map the image to the UV coordinates of the cube. Then add an Image Texture node (Texture -> Image Texture). Plug the “UV” output socket of the Texture Coordinate node into the “Vector” input socket of the Image Texture node. Also, change the drop-down box below the image file on the Image Texture node from “Color” to “Non-Color Data.”

QR-Using-Normal-Maps-In-BI-And-Cycles_TexCoord-ImageTex-Nodes
Texture Coordinate and Image Texture nodes (Click for larger view)

Now at this point, there are two things I’ve seen people do that are not the best way to apply a normal map:

[list icon=”busy”]

  •  Plugging the “Color” output socket of the Image Texture Node into the “Displacement” input socket of the Material Output node.
  •  Adding a Normal node, plugging the “Color” output socket of the Image Texture node into the “Normal” input socket of the Normal Node and the “Dot” output socket into the “Displacement” input socket of the Material Output node.

[/list]

Doing either of these is mismatching data types, which are indicated by the color of the sockets. Though in some circumstances this works fine, it is usually best practice to avoid plugging a socket into a socket of a different color. In this case, though you will get some displacement as a result, you are essentially turning the normal map back into a bump map by limiting the range of data being used. (For more info on data types, etc. keep an eye out for an upcoming post: Understanding the Node Interface.)

QR-Using-Normal-Maps-In-BI-And-Cycles_Incorrect-Nodes
How NOT to set up a normal map (Click for larger view)

The proper way to apply the normal map is to add – no surprise here – a Normal Map node (Vector -> Normal Map). Then plug the “Color” output from the Image Texture node into the “Color” input of the Normal Map node and the “Vector” output into the “Normal” input of the Diffuse BSDF node. This converts the color data from the image (yellow socket) into vector data (purple socket) and applies it to the normal of the shader node. Make sure the drop-down box is set to “Tangent Space” and the UV box is set to “UV Map” (or whatever the name is of your object’s UV map to which the normal map is aligned.) I’m not sure why this UV option is here; the Image Texture node vector is already set to UV, but best to do both.

QR-Using-Normal-Maps-In-BI-And-Cycles_Proper-Nodes
Using the Normal Map node (Click for larger view)
QR-Using-Normal-Maps-In-BI-And-Cycles_Cycles-Render
Cube render with normal map in Cycles

If you are building a material that mixes multiple shader nodes, simply plug the Normal Map “Vector” output socket into the “Normal” input socket of all the shader nodes. (Not all shader nodes have a “Normal” input, such as Emitter shader nodes.) As an example, I’ve mixed a Glossy shader with the Diffuse shader and applied the normal map to both. (I’ve collapsed some nodes in the image below to save space.)

QR-Using-Normal-Maps-In-BI-And-Cycles_Mixed-Shader-Niodes
Using a normal map in a material with multiple shaders (Click for larger view)
QR-Using-Normal-Maps-In-BI-And-Cycles_Cycles-Render-Glossy
Cube render in Cycles with mixed shader material

The next part of this series, Material Masking in Cycles, will demonstrate how to use mask images derived from the original bump map to mix several shaders, producing the result below with one material.

QR-Baking-Normals-from-Bump_Final-Box

<- Part 1: Baking Normals from a Bump MapPart 3: Material Masking in Cycles ->

Some textures used in creating models appearing in imagery on this post are from CGTextures.com, a most excellent source for photo textures.

Quick Reference: Baking Normals from a Bump Map

[box color=”gray”]Normals and Material Masking Series – Part 1 | Part 2 | Part 3[/box]

QR-Baking-Normals-from-Bump_Heading-Image

Brief Overview of Bump and Normal Maps

Normal Maps and Bump Maps are textures used to fake detail on a 3D mesh using data from 2D images. A bump map uses a single channel image (grayscale) in which 50% gray represents the baseline surface of the face. Values toward white indicate raised features and values towards black indicate recessed features. A normal map is a kind of three dimensional bump map that uses the RGB channels of an image to represent perturbations in the X, Y and Z axes.

QR-Baking-Normals-from-Bump_Bump-Normal-Example
Examples of a bump and normal map

The most common way of generating a normal map is by “baking” it onto the UVs of a mesh. There are several ways of doing this. The most common are:

[list icon=”sign-in”]

  • Using the geometry of a high-poly detailed mesh to bake normals onto a lower-poly version (this technique is used extensively in video games.)
  • Using Blender’s Multiresoultion modifier, which allows you to sculpt a subdivided version of a mesh while maintaining the lower-resolution base mesh, you can bake normals from the subdivided version onto the base mesh.
  • Modeling separate objects just above the surface of another object, this geometry can be baked by projecting onto the underlying faces.

[/list]

But there is another way to generate a normal map in Blender, by baking from a bump map.

Baking Normals from a Bump Map

In this example, I’ve simply added a Subdivision Surface modifier to a cube and added in some edge loops to sharpen the edges. I’ve then marked seams and UV unwrapped the cube, arranging the UVs so that the sides facing the camera are isolated and enlarged and those facing away are scaled down. In the UV/Image editor I’ve created a new image (at the bottom of the UV/Image Editor workspace: click New, an image options dialogue box will apear, name it and enter a size – I’ve used the default 1024×1024) and exported the UV Layout (at the bottom of the UV/Image editor workspace: UVs -> Export UV Layout).

QR-Baking-Normals-from-Bump_Cube-And-UVs
UV-Unwrapping the cube (click for larger view)

With the UV Layout image as a guide in GIMP I created this bump map, using 50% gray as the background. When saving out the final bump map image, make sure the UV Layout is not visible.

QR-Baking-Normals-from-Bump_Cube-Bump-Map-wUVs
Bump map drawn in GIMP – the UV lines are not visible in the final bump map image (Click for larger view)

Currently, texture baking can only be done in the Blender Internal render engine. The texture generated, however, can be used in either BI or Cycles (or any other rendering engine, provided normal maps are interpreted the same way – some renderers may apply the coordinates differently.) To create the normal map, first add a material to the cube (the material settings are not important for this task.) Then add a texture, set to “Image or Movie” and open the bump map texture. Make sure the mapping is set to UV and the influence is set to Normal with a value of 1. Also, under Bump Mapping Method select Best Quality. Note: if you are baking normals from a bump map derived from a photographic texture, changine the Bump Mapping Method will alter the level/frequency of detail baked into the normal map from the bump map.

QR-Baking-Normals-from-Bump_Material-And-Texture-Settings-BI
Material and texture settings for the bump map

Next, under the render panel there is an area labeled Bake. Set the Bake Mode to “Normals” and hit Bake. (The other bake settings are the defaults.) As long as the bump map texture is active on the cube material this will bake a normal map based on the influence of the bump map. Note: make sure the cube is selected and there is an image active for the UVs (this was done before exporting the UV Layout). Otherwise you will get an error. Once it bakes, you can save out the normal map from the UV/Image Editor.

QR-Baking-Normals-from-Bump_Bake-Settings
The normal map bake and baking settings

So, why go to all this trouble to create a normal map? A normal map will inherently shade and respond to lighting better than a bump map, simply because it uses more data and alters the surface normal on all three axes. (Although, sometimes a bump map is more appropriate.) Below is an example of two cubes, with a simple metal texture, rendered in Cycles. One has just the bump map applied and the other just the normal map. You can see the details look deeper and cripser with the normal map, though you may not want such heavy effects – in which case you can use the bump. By baking the normal map, you add to your options and, in most cases, can get better results than bump mapping alone.

QR-Baking-Normals-from-Bump_Bump-and-Normal-Comparison
Comparison of the influences of a bump and normal map (Click for larger view)

In Part 2 of this series, I will cover how to properly use normal maps in both Blender Internal and Cycles. Part 3 will cover material masking, a technique I used to achieve this final result.

QR-Baking-Normals-from-Bump_Final-Box

Part 2: Using Normal Maps in BI and Cycles ->

Some textures used in creating models appearing in imagery on this post are from CGTextures.com, a most excellent source for photo textures.

Quick Reference: Using “Clamp” in Cycles

Being that my current machine isn’t nearly as meaty when it comes to processing as I’d like – and doesn’t have a graphics card that is compatible with Cycles GPU rendering – I’m always looking for ways to improve performance when rendering in Cycles. Blender Guru has a post worth checking out, 4 Easy Ways to Speed Up Cycles, but I’d like to mention a simple technique I’ve just recently discovered, using the Clamp setting.

RenderPanel_Clamp

Using Clamp

This example is still a work in progress, but is suitable for demonstrating the use of Clamp. (You can view the final image here in my portfolio.) The scene is lit by two emitter planes and a HDRI map. The bounces are already set to pretty conservative levels, but it’s till firefly city. Here’s a render after 2642 samples with Clamp set to 0, using progressive refine so the whole image renders at once (I won’t tell you how long it took on my computer, it’s a little embarassing.)

Banner-01_Test-06_2642-Samples_15h24m
2642 Samples, Clamp set to 0

In the Render panel, under Sampling, there is an option called Clamp (more on the particulars of what this does below.)  By setting the “Clamp” to 2, this is the result after 1363 samples.

1363 Samples, Clamp set to 2
1363 Samples, Clamp set to 2

You’ll notice that the image is a little dimmer and duller, the colors are less vibrant. That is easily fixed in the compositor with a simple Color Balance node. I essentially want to increase the contrast and brighten the image. Also, might as well do a little color correction while we’re at it.

With Color Balance node, reduce the value of the Lift and increase the value of the Gain
With Color Balance node, reduce the value of the Lift and increase the value of the Gain

With a little post-processing, an image that is less muddy. I’ve reversed the dimming effect of Clamp and added some flair to the image that I would have done anyway, at the same time. A bit more refinement of the image is a good idea, but this illustrates the basic point.

1363 Samples, Clamp set to 2, Color Balance adjusted
1363 Samples, Clamp set to 2, Color Balance adjusted

So, utilizing the Clamp setting, I was able to get a render that is virtually noise-free in about half the render time. You’ll have to experiment to find a Clamp value that balances render time and image quality for each scene. Remember that using clamp will dim the image (particularly the highlights) but that is usually fixed pretty easily with some post-processing. (You’ll notice the paper on the mini notebook has blown out a bit, this can be fixed by making the texture darker.)

Why Use Clamp

For more detail on Clamp, as well as other noise reduction techniques, explanations on how they work and what causes noise in the first place, I suggest checking out this page in the Blender Wiki. Briefly, Clamp caps the value of each sample, easing fluctuations from pixel to pixel. This obviously reduces the physical accuracy of the final image and dulls the brighter highlights, but these issues are usually either negligable or easily corrected.

I chose to use Clamp over using Filter Glossy or other techniques to reduce noise in this render for three main reasons:[list icon=”plus”]

  • It’s faster, easier and more effective at noise reduction over the entire image
  • Given the amount of glass and liquid as well as their prominence in the image, I wanted to preserve the accuracy of those light interactions as much as possible
  • It doesn’t involve making adjustments to individual elements or shaders

[/list]

Disabling caustics removes entirely a real physical effect of light (though it often doesn’t appreciably diminish the quality of the image and is a good idea to use in conjunction with setting a Clamp value), while Filter Glossy forces a reduction in sharpness. With Clamp, the inaccuracy is mostly in the intensity of highlights.