Unity Virtual Reality Projects
上QQ阅读APP看书,第一时间看更新

An introduction to Blender

Open the Blender application. Dismiss the opening splash screen. You will be in the Blender editor, which is similar to what's shown in the following screenshot:

An introduction to Blender

Like Unity, Blender consists of a number of non-overlapping windows, and their layout can be customized to suit your needs. However, the Blender interface can be more daunting, in part because it integrates a number of different editors that can be opened at the same time, in their own panels.

It's helpful to realize that the default view, as shown in the preceding screenshot, contains five different editors!

The most obvious editor is the large 3D View, which I highlighted with a (red) rectangle. This is where you can view, move, and organize the objects in your Blender scene.

The following are the other editors that are opened:

  • The Info editor, which can be seen along the top edge of the app, has global menus and information about the application
  • The Timeline editor, which is present along the bottom edge of the app, is for animations
  • The Outliner editor, in the upper right-hand side, has a hierarchical view of the objects in your scene
  • The Properties editor, which can be seen to the right below the Outliner, is a powerful panel that lets you see and modify many properties of the objects in the scene

Each editor can have multiple panes. Let's consider the 3D View editor:

  • The large area in the middle is the 3D Viewport, where you can view, move, and organize the objects in your Blender scene.
  • Just below the 3D Viewport is the editor Header, which is called so although it's at the bottom in this case. The Header is a row of menus and tools that provide great control over the editor, including view selectors, edit modes, transform manipulators, and layer management.
  • On the left-hand side is the Tool Shelf containing various editing tools that can be applied to the currently selected object, which can be organized into tabbed groups. The Tool Shelf can be toggled open or closed by grabbing and sliding its edge or by pressing the key T.
  • The 3D Viewport also has a Properties pane, which may be hidden by default and can be toggled open or closed by pressing the key N. It provides the property settings for the currently selected object.

In the upcoming instructions, we will ask you to change the Interaction Mode of the 3D View editor, say between the Edit Mode and Texture Paint mode. This is selected in the Header, as shown in the following screenshot:

An introduction to Blender

The other editors also have the Header panes. The Info editor (at the top of the app) is only a Header! The Outliner and Properties editors (on the right) have their Headers at the top of their panel rather than at the bottom.

Once you recognize this layout, it doesn't look so crowded and confusing.

The Properties editor Header has a wide set of icons, which act like tabs, to select the group of properties presented in the rest of the panel. Hovering your mouse over the icon (like any of the UI widgets here) will show a tooltip with a better hint as regards what it's for. It's pictured in the following images (in a couple of pages) when we get to using it.

The Blender layout is very flexible. You can even change a panel from one editor to another. At the far left of each Header is the Editor Type selector. When you click on it, you can see all the options.

In addition to the plethora of things that you can click on in the Blender interface, you can use just about any command using a keyboard shortcut. If you forget where to find a selection, press the space bar and type in your best guess of the command name that you're looking for. It just might pop up!

The following is the screenshot showing the Editor Type selector available in Blender:

An introduction to Blender

A unit cube

Now, let's build a unit cube in Blender.

The default scene may already have objects, including a cube, camera, and a light source, as shown earlier in the default Blender window. (Your start up settings may be different, since that can be configured).

If your startup scene does not contain a unit cube, create one, as follows:

  1. Make sure that the scene is empty by deleting whatever is there in it (right-click to select, X key on the keyboard to delete).
  2. Set the 3D cursor to the origin (0,0,0) using Shift + S (opens the Snap options list) | Cursor To Center.
  3. In the left Tool Shelf panel, choose the Create tab and under Mesh select Cube to add a cube.

OK, now we're all on the same page.

Note that in Blender, the reference grid extends in the x and y axes, and z is up (unlike Unity, where the y-axis is up).

Furthermore, note that the default cube in Blender has the size of (2, 2, 2). We want a unit cube sitting on the ground plane at the origin. For this, follow the following steps:

  1. Open the Properties pane with the keyboard N key.
  2. Navigate to Transform | Scale and set X, Y, Z to (0.5, 0.5, 0.5).
  3. Navigate to Transform | Location and set Z to 0.5.
  4. Press the N key again to hide the pane.
  5. You can zoom in using the scroll-wheel of the mouse.

For our purposes, also ensure that the current renderer is Blender Render (on the drop-down selector on the Info editor at the top of the app window in the center).

A UV Texture image

Next, we will create a UV Texture image, as follows:

  1. Go into Edit Mode using the Interaction Mode selector in the bottom Header bar.
  2. Select all ( press the A key on keyboard twice) to make sure that all the faces are selected.
  3. In the left Tool Shelf panel, select the Shading/UVs tab.
  4. Under UV Mapping click on Unwrap, select Smart UV Project from the drop-down list, accept the default values, and click on OK (the result, shown in the following screenshot, also shows what the unwrapped cube looks like).
  5. Now, go into the Texture Paint mode using the Interaction Mode selector in the bottom Header bar again.
  6. We need to define a paint slot for our material. Click on Add Paint Slot, select Diffuse Color, name it CubeFaces, and press OK.

We can now start painting directly on the cube. Paint the front face first, as follows:

  1. Make a smaller brush. In the left Tool Shelf panel, in the Tools tab, navigate to Brush | Radius and enter 8 px.
  2. It may be easier to work in an orthographic view. From the menu bar at the bottom, navigate to View | View Persp/Ortho.
  3. Then, navigate to View | Front.
  4. You can zoom in or out using the mouse scroll-wheel, if needed.
  5. With your best handwriting, write the word Front using the left-click of the mouse and draw.
  6. Now, we'll do the same for the back face.
  7. From the menu bar at the bottom, navigate to View | Back and select this face with a right-click.
  8. With your best handwriting, write Back.

Repeat the above process for the left, right, top, and bottom faces. If at some point it's not painting, make sure that there's a current face selected. Try right-clicking on the face to reselect it. The result should look something like this (shown side-by-side both in the 3D View editor with an orthographic perspective and the UV/Image Editor):

A UV Texture image

Now, we need to save the texture image and set up its properties, as follows:

  1. Change the current Editor Type to UV/Image Editor using the selector on the far left of the Header at the bottom of the 3D View editor.
  2. Click on the Browse Image to be linked selector icon (just towards the left of + icon) and choose CubeFaces from the list.
  3. The Image menu item on the menu bar at the bottom now has an asterisk (Image*) indicating that there's an unsaved image. Click on it, select Save As Image, and save it as CubeFaces.png. Use a folder outside the Unity project.
  4. On the right-hand side, in the Properties editor panel, find the long row of icons in its Header and select the Texture one (third from the last). It may be hidden if the panel isn't wide enough; you can scroll down with your mouse to show it, as shown in the following screenshot:
    A UV Texture image
  5. Within the Texture properties, change Type to Image or Movie.
  6. Then, in the Image group of properties, click on the Browse Image to be Linked selector icon (as shown in the following screenshot) and choose CubeFaces:
    A UV Texture image
  7. You should see the labeled faces texture image in the Preview window.

Good! Let's save the Blender model, as follows:

  1. Select File from the top main menu bar in the Info editor and click on Save (or press Ctrl + S).
  2. Use the same folder as the one where you saved the texture image.
  3. Name it UprightCube.blend and click on Save Blender File.

We should now have two files in a folder, UprightCube.blend and CubeFaces.png. I use a folder named Blender/ in the root of my Unity project. Note that alternatively, you can export to other standard formats, such as FBX (short for Filmbox).

Wow, that was a lot. Don't worry if you didn't get it all. Blender can be daunting. However, Unity needs models. You can always download someone else's models from the Unity Asset Store and other 3D model sharing sites. Don't be a wimp, learn to make your own. Ha ha! Seriously, it's a good thing to start to learn.

Importing into Unity

Back in Unity, we now want to import both the files, UprightCube.blend and CubeFaces.png, one at a time, as follows:

  1. In the Project panel, select the top-level Assets folder, navigate to Create | Folder, and rename the folder to Models.
  2. An easy way to import files into Unity is to just drag and drop the .blend file from the Windows Explorer (or Mac Finder) window into the Project panel Assets/Models folder and drag and drop the .png file into the Assets/Textures folder (or you can use the Assets | Import New Asset... from the main menu bar).
  3. Add UprightCube to the scene by dragging it from the Assets/Models folder where it was just imported into the Scene view.
  4. Set its position so that it's away from the other objects. I left mine at Position (2.6, 2.2, -3).
  5. Drag the CubeFaces texture from the Assets/Textures folder into the Scene view, hovering over the just added UprightCube so that it receives the texture, and drop the texture onto the cube.

The scene should now look something like this:

Importing into Unity

A few observations

The back of the cube is facing us. Is that a mistake? Actually, it makes sense since the current viewpoint is looking forward. So we should see the back of the cube. If you didn't notice already, the same goes for Ethan. It also seems like the cube has a one-unit dimension.

However, on closer examination, in the cube's Inspector panel, you'll see it imported with the scale that we gave it in Blender—(0.5, 0.5, 0.5). Also, it has an X-rotation of -90 (minus 90). Thus, if we reset the transform, that is, the scale to (1,1,1), it'll be 2 units in our world space and tipped over (so, don't reset it).

There's not much that we can do to compensate for the rotational adjustment without going back to Blender.

Tip

Blender's default up direction is Z, while Unity's is Y. Importing with a -90 X-rotation adjusts for that. An imported scale can be adjusted in the object's Inspector panel's Import Settings.

The scaling issue can be fixed with further tweaking, as follows:

  1. In the Project panel, select UprightCube that we imported. The Inspector panel shows its Import Settings.
  2. Change Scale Factor from 1 to 0.5.
  3. Click on Apply.

    The cube in the scene can now have Scale (1, 1, 1) and be a unit cube, like we'd expected.

  4. With UprightCube selected in Hierarchy, change its Transform value of Scale to (1, 1, 1).

Before ending the preceding process, select UprightCube from the Hierarchy panel and drag it into the Project panel's Assets folder. (You may consider making an Assets/Prefabs subfolder and placing it into that). This makes it a reusable prefab, texture image and all.

There are some important lessons in this exercise (other than having learned a little bit of Blender) that apply to any 3D Unity project, including the VR ones. Normally, you will be importing models that are much more complex than a cube. You will likely run into issues related to data conversion, scale, orientation, and UV texture images that might be confusing at best. If this happens, try to break the problem into smaller, more isolated scenarios. Do little tests to gain insight on how applications exchange data and help you understand which parameter tweaks might be necessary.