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

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 a list of all the things that can get imported. Then select Import

If you cannot find the Effects package to import, you may not have installed Standard Assets when you installed Unity. To get them now, you will need to run the UnityDownloadAssistant again as described at the beginning of this chapter (and it may already be in your Downloads folder).

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

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

The default Grid Projector is facing downward (Rotation value of = 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 a light color. 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 Projector/Light to Projector/Multiply.

It now paints the white gridlines 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 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 (flattened) 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 Gizmo 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.

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

Now we have a one-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: