Creating a rearview mirror

In this recipe, we will take advantage of Unity Pro's ability to render camera views into textures to make a rearview mirror.

Getting ready

In order to follow this recipe, please use the example scene mirrorPackage, available in the Unity Package format in the 0423_11_02 folder.

How to do it...

To create a rearview mirror, perform the following steps:

  1. Import mirrorPackage and open the scene named mirrorScene, from the 11_02 folder.
  2. Using the Transform tools (Move and Rotate), place the Main Camera next to the left-hand side of the car, as shown in the following screenshot. Make sure the rearview mirror is framed.
    How to do it...
  3. In the Hierarchy view, expand the car object. Then, drag the camera into the carGroup sub-object, making it a child of the carGroup:
    How to do it...
  4. In the Hierarchy view, access the Create drop-down menu to add a Camera to the scene. Change its name to mirrorCam and in the Inspector view, disable its Audio Listener property.
  5. Position and rotate the camera as if it is inside the rearview mirror object:
    How to do it...
  6. Expand the car object and then the carGroup sub-object. Make the mirrorCam a child of carGroup.
  7. Now, let's create a new material for the mirror. In the Project view, select the Create drop-down menu and choose Material. Name it as mirrorMaterial. Next, select the Create drop-down menu one more time and choose Render Texture. Rename it to mirrorTex.
  8. In the Inspector view, set the mirrorText size to 1024 x 512.
  9. Select the mirrorCam object. In the Inspector view, change the value of Target Texture field to mirrorTex.
  10. Select mirrorMaterial and change its Main Color texture to mirrorTex. This can be done by dragging the mirrorTex material from the Project view into the Main Color texture slot.
    How to do it...
  11. In the Hierarchy window, select the mirror sub-object. Then apply the mirrorMaterial by dragging it from the Project view into the Inspector view (below its current material).
  12. Now, the map must be flipped horizontally (more precisely, mirrored). In the Project view, select mirrorTex and change its Wrap Mode to Repeat.
  13. Select mirrorMaterial and change its x Tiling value to -1:
    How to do it...

How it works...

As you have learned from the recipe, the mirror's camera view is actually being rendered into the Render Texture in real time.

There's more...

The following is a list of interesting things that can be done with Render To Texture:

Adding details

You could also make your mirror material more interesting by changing its Shader from Diffuse to Decal. Then you can add an RGBA texture map to it where the alpha channel would control the opaqueness of the Decal texture. This is a great way of adding a layer of dust or stain to the mirror.

Using Render Texture as a TV screen

You could easily adapt this recipe to make a TV or monitor screen texture. The only major change would be not changing the x tiling value to -1.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.164.75