Using texture and material stencils
Stencil is one of the most useful tip when you want to control finely the way the shader changes from one texture to another, on the same object, and with the use of a single material index.
For Blender: 2.50Alpha 2 and better
The way it works is quite simple, since it relies on the texture slots list. The textures within the lists simply stack on top of the others. Basically, the first texture in the list is applied as if it was the very first layer of texture. The second texture in the list is applied over the previous one ; if it is 100% opaque, then the first texture would be completely hidden by the first one. The same applies for the third, the fourth and so on, for each of the textures in the slots list.
Of course, you can make use of transparent or semi-transparent textures, in the upper layers of textures, so that they would show at least parts of the underlying textures. But dealing this with Alpha properties of textures can get nasty and cumbersome. Here comes the Stencil.
1. Using a stencil with textures
By using stencils, you won't have to care of the transparency of any individual texture anymore. You will just have to specify a bottom texture, a top texture, and then a stencil texture that will convey all the transparency information and indicate to the renderer how the two different textures are to be mixed over the textured surface.
The process is quite simple. Let's suppose you start with a newly added plane object. In the Material menu, add a new material using the New button.
1.1 Adding the "bottom" texture, the first layer
Shift now to the Texture menu. The texture list of available slots should be available.
The first slot available is for the first layer over the surface. This means that it is the bottom texture. This may be confusing, as in the slots list, the upper texture would be the bottom one, and the more down one would be the op one. Keeping this in mind will make things less confuse.
Select the first slot (the one at the top of the list) and use the New button to assign a new texture to it. You can also use the texture browser button to select among a list of pre-existing textures already available in the scene. Let's name the texture
Ground, and in the dropdown menu Type, select Image or Movie. In this exemple, I chose a texture from the Blender Texture Disc that is now in the Public Domain.
Of course, you could use any other type of texture. In any case, be sure that the Mapping and Influence panels are set properly for your surface model, especially if it is uv-mapped.
1.2 Adding the "top" texture, the second layer
Now select the second texture slot. Do the same as previously, in order to add another texture to the material. For the example's sake, name it
Grass and set it also to the Image or Movie type of texture. Most of the time, the Mapping panel would be set the same as previously, but this is not mandatory, as you are perfectly allowed to use an uvmapped image with a procedural texture, for example.
1.3 Setting the stencil texture
You can now select the third texture slot for the last part of this tutorial, and a third texture named
Stencil, for example. The stencil works on the basis of the Grey value of any picture: black value will show the bottom texture (e.g., the first layer) while white value will show the top texture (e.g., the second layer). Obviously, intermediate colors will mix the two textures according to the grey value: dark pixels will tend to display the bottom texture predominanty, for instance, while light pixels would display the top texture.
Bitmap or procedural?
As for stencil, once again, the use of uvmapped images will provide a very powerful control over the blending of the layered texutres, but a procedural stencil would enable very quick-and-dirty but interesting results over the textured surface. It most probably depends on the distance to the camera of the textured object and its own complexity. On primitives and simple objects, procedural or even blend texture types are very convenient.
While you would have to set the Mapping panel as you see fit, a special attention should be deserved to the Influence panel.
- At first, you have to deactivate the Color influence over the Diffuse of the shader. Otherwise, you would have your grey-based texture that would override the two other textures on the surface of the object.
- You then have to convert the RGB space values of the texture to Grey space values. This is done by checking the RGB to Intensity option.
- Finally, you also have to state that this texture is to be used as a stencil. This is obviously done by checking the Stencil option.
1.4 Sorting the texture slots
Of course, if you make a render right now, you would see no stencil magic at work, only the top texture covering uniformly all the surface of the object. This is because the location of the stencil in the stencil slots does matter.
Back to the list of texture slots, select the
Stencil texture and use the up and down arrow buttons on the side of the list to relocate it between the
2. Using a stencil with node materials
In this mini-tutorial, we will see how to mix two existing materials (coming along with its own complex texture setup, perhaps also involving stencils). Once again, the Mapping of the texture is not our primary focus, so you can use any Projection type (Flat, Tube, Cube, Sphere) or any Coordinates system (UV...).
2.1 The raw materials
For the purpose of this tutorial, once again I rely on the Blender Texture Disc discussed before. I selected the two following materials:
and appended them into my test scene using . I also created a third material, named
Stencil, that would be my stencil material. Basicly, it has an Image or Movie texture type applied to it, with Generated coordinates, and Sphere projection. The image is black and white: a checker also issued from the Blender
Texture Disc, but any grey toned texture would have been suitable also. In case of a RGB texture, remember to check the RGB to Intensity option in the Influence panel.
2.2 The Material Node editor
With the object to be shaded selected, change the Screen layout to
Compositing. The Node Editor is then the main view.
- In the Material menu, click on the New button to link a brand new material to the selected object. Rename it a convenient way, like
- In the header of the Node Editor, activate the Material Nodes icon and check the Use Nodes option. The default nodes settings pop up on screen.
- Use the Add > Input entry of the menu to add a second Material node (or use to call the floating menu). Repeat one more time, in order to have 3 Material nodes available.
If you have a node selected, the newly added node will automatically be linked back to it through the most logical socket. To unlink two nodes, just click on the entrance socket of the node newly added.
- Now add, a Mix node (Add > Color > Mix).
- Sort the nodes network: put the 3 Material nodes on the far left end, the Mix node somewhere in the middle, and the original Output node of the far right end. As for the connexions, complete the network like this:
- The Color output of the first Material node to the Color1 entrance of the Mix node
- The Color output of the second Material node to the Color2 entrance of the Mix node
- The Color output of the third Material node to the Fac entrance of the Mix node
- The Color output of the Mix node to the Color entrance of the Output node
- Assign the materials:
orangematerial to the first Material node, which will provide the bottom (1st layer) material
redapplematerial to the second Material node, which will provide the top (2nd layer) material
Stencilmaterial to the third Material node, which will provide the blending of the two materials: white is opaque and will show the top material, while black is transparent and will show the bottom material
The only thing left is to do a render of your scene to enjoy stencils with Material nodes. I'm quite sure you will have quite a lot of creative ideas on how to use stencils in your scenes, from now on!
Article written on February the 27th, 2005.
Updated on July the 8th, 2010 for Blender 2.50 Alpha 2. Comments re-initialized.