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

Measurement tools

We've created a Unity scene, added a few primitive 3D objects and created a couple of basic textures, including a photograph. Along the way, we learned about positioning and transforming objects in Unity's 3D world space. The problem is that the actual size of stuff in your scene is not always obvious. You could be zoomed up or you may be using either a Perspective versus Orthographic view, or other features that affect the apparent size. Let's look at ways to deal with the scale.

Keeping a unit cube handy

I suggest keeping a unit cube handy in your Hierarchy panel. When it's not needed, just disable it (uncheck the checkbox in the top left-hand side of the Inspector panel). It can be used like a measuring stick or rather a measuring block, when needed. I use one to estimate actual world sizes of objects, distances between objects, heights and elevations, and so forth. Let's do it now.

Create a unit cube, name it Unit Cube, place it somewhere out of the way for now, such as Position (-2, 0.5, -2).

You can also add a tick-marked tape measure texture to its edges.

Leave it enabled for the time being.

Using a Grid Projector

I want to tell you about the Grid Projector, a handy tool that is used to visualize a scale in any Unity scene. It's one of the Standard Assets in the Effects package. So, you may need to import it into your project. To import, perform the following steps:

  1. Select Assets in the main menu bar and then navigate to Import Package | Effects.
  2. The Import dialog box pops up, containing the list of all the things that can get imported. If you want, just click on All and then select Import. However, we only really need the Projectors folder so that you can choose and import just that folder.

Now, we'll add a Projector to the scene, as follows:

  1. Find the Grid Projector prefab located in the Project panel, navigating to the Assets/Standard Assets/Effects/Projectors/Prefabs folder.
  2. Drag a copy of Grid Projector into your scene. Set its Y value of Position to 5 so that it's above the ground plane.

The default Grid Projector is facing downward (Rotation value of X = 90), which is usually what we want. In the Scene view, you can see the Orthographic projection rays. A Unity doc (http://docs.unity3d.com/Manual/class-Projector.html) explains a Projector as follows:

A Projector allows you to project a Material onto all objects that intersect its frustum.

This means that the objects intersected by the projection rays will receive the projected material.

In this case, as you'd expect, the Projector material (also named GridProjector) has a "Grid" texture, which simply looks like a crosshair (see for yourself, in the Assets/…/Projectors/Textures/Grid object).

By default, the Projector shines the grid pattern as a light on the surface that it illuminates. In our scene, the GroundPlane plane is light. So the grid may not show up. Now follow the following steps:

With Grid Projector selected in the Hierarchy panel, locate the GridProjector Material component in the Inspector panel and change its Shader from Hidden/Projector Light to Hidden/Projector Multiply.

It now paints white gridlines on black. To get a better feel of what's going on, change the scene view to a Top view orientation, as follows:

  1. Click the green Y cone on the Scene view gizmo in the upper right-hand side of the View panel.
  2. Also, click the little cube at the center of the gizmo to change from the Perspective to the Orthographic (non-distortion) view.

You should now be looking straight down onto the ground plane. With the Grid Projector selected (make sure that the Translate tool is active, which is the second icon in the top left icon toolbar), you can grab the Translate gizmo attached to the Projector and move it from side-to-side. The grid line will move accordingly. You might leave it at Position (-2.5, 5, -0.5) and avoid the projector blocking the directional light.

At this point, the built-in view reference grid might be confusing. So, turn it off in the following way:

  1. In the Scene view panel, click on Gizmos (the menu with this name, which has options to control your gizmos) and uncheck Show Grid.

    OK, so what does this get us? We can see that the default grid size measures half the edge of the unit cube. In Inspector, the Projector component's Orthographic size value is 0.25.

  2. Change the Projector's Orthographic Size value from 0.25 to 0.5.
  3. Save the scene and the project.

Now, we have a 1-unit grid that can be turned on and projected onto the scene any time it is needed.

Let's leave it on for now because it looks kind of cool, as you can see in the following screenshot:

Using a Grid Projector

Measuring the Ethan character

How big is an avatar? Unity comes with a third-person character named Ethan. Let's add him to our scene. He's one of the Standard Assets in the Characters package. So, you may need to import that into your project.

To import, perform the following steps:

  1. Select Assets in the main menu bar and then navigate to Import Package | Characters.
  2. The Import dialog box pops up, containing the list of all the things that can get imported. Click All and then Import. The ThirdPersonController is a prefab (pre-built asset) located in the Project panel. This can be found by navigating to the Assets/Standard Assets/Characters/ThirdPersonCharacter/Prefabs folder.
  3. Drag a copy of ThirdPersonController into your scene. The exact X and Z positions don't matter, but set the Y to 0 so that the character named Ethan is standing on GroundPlane. I left mine at (2.2, 0, 0.75).

Let's try it out:

  1. Click on the Play icon at the top of the Unity window in the center, to start your game. Use the W, A, S, and D keys to move him around. Run, Ethan! Run!
  2. Click on the Play icon again to stop the game and return to the edit mode.

So, how big is Ethan? According to Google search, the average height of a human male is 5' 6" or 1.68 meters (in USA, the average adult male is more like 5' 10" or 1.77 meters tall). Let's see how tall Ethan is when compared to these:

  • Slide the unit cube next to Ethan using the Translate gizmo

    Alright, he's about 1.6 times its height

  • Scale the unit cube's height (Y) to 1.6 and adjust its Y position to 0.8

Look again. As illustrated in the following screenshot, he's not quite 1.6. So Ethan is a little shorter than the average male (unless you include his pointy hairdo). Swinging my view around, I'm looking at Ethan right in the face, and by further adjusting the cube the eye level is about 1.4 meters. Make a note of this:

  1. Restore the unit cube's Scale (1,1,1) and Position (-2, 0.5, -2).
  2. Save the scene and the project.

The following screenshot shows the comparison of a cube 1.6 units tall and Ethan:

Measuring the Ethan character