Friday, November 28, 2014

Controlling Texture Tiling in Cycles

A couple of years ago, I wrote a small tutorial on how to apply one or more textures as decals to one single mesh in Cycles. The technique back then took advantage of using several UV maps fed through the Attribute node. Today, I am revisiting this topic because it is perfectly possible to apply one or more images to mesh in Cycles with total control over scale, rotation, position—and more importantly, tiling. Let's see how this is done.

Open up a new Blender file, get rid of everything, and add a plane. Press 7 to switch to top view and Shift-C to center the view on the plane. I like to work on Orthogonal view, but that's me. Create a material for the plane. Split the window to have access to the Node Editor. In the 3D Viewport, switch to Rendered shading mode. Now, with the plane selected, go into Edit mode, select all, and press U to UV unwrap the plane with the option Project from View (bounds).

Now, make sure you have the Node Wrangler addon activated. In the Node Editor window's Properties panel, click on the Add Texture Setup button. This adds a Texture Coordinate, Mapping, and Image Texture nodes and connects them to the existing Diffuse node (Figure 1).

Figure 1
Now you need to choose an image texture in the Image Texture node. I am using a free image of an earth I found on Pixabay. The image I used is a PNG with a transparent background (Figure 2).

Figure 2

Now, I'd like to scale this image down to 25% and place one single instance of it—that is, without tiling it. However, when you change the scale in the Mapping node, the image becomes tiled! This is not what we want. Notice I am using the Point option in the Mapping node, which alters the projection itself, not the texture. To scale down the image to 25% size, you need to enter a 4 (Figure 3).

Figure 3

So, how do we stop the image from tiling? It took me a while to discover, but you do it by checking the Min and Max options, and entering the appropriate values. What does Min and Max mean? Min and Max control how a texture tiles over the XYZ axes, so the Min and Max value refer to dimension units in the texture coordinate. Check these two options and see what happens! The image texture stops tiling at the maximum coordinate value assigned under Max—which is one, so the tiling stops at 1 (Figure 4). If you enter non-integer values here, you'll get a bit of a tiling stretching over the remaining geometry in that axis. Blender simply extends the pixels at the texture's edge (Figure 5).


Figure 4
Figure 5
I'd like to scale the earth to 50% its original size, and place it in the center. There are several ways to do this with the mapping node. I find that using a 2 scale in Point mode, and changing the X and Y location to -0.5, as seen in Figure 6, works well enough.

Figure 6

OK, so we can stop an image texture from tiling over the whole UV projected mesh. This is actually great news for decal professionals in Blender. What about adding a second image texture to the same mesh without using a second UV map? Piece of cake. Image 7 shows the noodle I am using. I am simply using a different Mapping node for the second image (the moon). For this example, I am mixing the two textures with the Add mode. Also, it is important to combine colors of different image textures before plugging the color info into a shader node. Combining color info at the shader level becomes more restrictive, because we lack certain mixing modes for shaders.

Figure 7

Let's take this one step further. I want the plane to be dark blue, and I want to place the earth and the moon on top of it as if they were bumper stickers. For this, I will use the alpha information from the texture images themselves, I will combine them into one grayscale image, and this will control the mix between two shaders—one with the earth and moon, the other, dark blue. Image 8 shows the final node setup.

Figure 8
Figure 9

Wednesday, November 26, 2014

Aligning Geometry to World Axes in Blender

Oftentimes, I have been puzzled by Blender's apparent lack of capabilities regarding work planes (this is not entirely true—see Update at the bottom of the page). A work plane, as defined in other programs such as modo, lets us change alignment of the global XYZ axes momentarily in order to edit a mesh in a more comfortable way. In this post I am sharing my own workaround in Blender to align any object to any work plane, with one condition—the work plane needs to be represented by some mesh element. An example will make this more clear.

Open up a new Blender file and delete everything. Press 5 to go into Orthogonal view. Add a Sphere. Now, go into Edit mode, switch to Face mode, and select a random face (Figure 1). We will realign the whole sphere so this face ends up squarely aligned to the world axes.

Figure 1
Blender has a set of view alignment commands that align the view to any selection within a mesh. The alignment can be to the top, bottom, front, back, right, and left of a selection (Figure 2). The keyboard shortcuts for these alignment commands are the same as the ones used for standard views, but with the Shift key added. So, if Numpad 7 is used to switch to top view, Shift-Numpad 7 is used to align the view to the top of a selection. Press Shift-Numpad 7 now to align the 3D Viewport to the top of the selected polygon (Figure 3). Press Shift-S and choose Cursor to Selected to place the 3D cursor on the selected face.

Figure 2
Figure 3
Press Tab to go into Object mode. Making sure not to change the 3D View, press Shift-A to add a Cube. In the Tool Shelf options for the newly created cube, enter 0.1 for Radius, and check the Align to View option (Figure 4). The cube is placed exactly in the precise location where the selected face sits. You can rotate the view to check this is so (Figure 5).

Figure 4
Figure 5
If you check the cube's Transform values in the Properties panel, you'll see it has the same location and rotation as the selected face (Figure 6).

Figure 6
Now, deselect everything. Select the sphere and then the cube. Press Control-J to join them into one mesh. The selection order is important, because we are joining the sphere to the cube, not the other way around. This way, we are preserving the cube's funky rotation and position. All you need to do now is press Option-G and Option-R to clear the object's rotation and location. The sphere-cum-cube is aligned perfectly so that the selected face is facing squarely up in the Z axis (Figure 7). 

Figure 7
Finally, go into Edit mode, select the cube, and delete it. Go back into Object mode, press Shift-Control-Option-C, choose Origin to Center of Mass, and press Option-G again to clear its position and place it on the world origin (Figure 8). We are done!

Figure 8
Update: Right after I published this post, Eriandor left a comment with  a link on creating custom transform orientations. I did not know, or remember, this feature existed. I think now custom transform orientations might offer a better, less cumbersome workflow in many cases. Thanks Eriandor! Here is the link.