Creating an in-game surveillance camera

Although using a second viewport can be useful in many situations, there will be times when you need to output the image rendered from a camera to a texture at runtime. To illustrate this point, in this recipe, we will make use of Render Texture to create an in-game surveillance camera that transmits its video to a monitor.

Creating an in-game surveillance camera

In-game surveillance cameras

Getting ready

For this recipe, we have prepared the BasicScene Unity package, containing a scene named BasicScene, and also two FBX 3D models for the monitor and camera objects. The package is in the 1362_05_codes folder, and the 3D models are in the 1362_05_06 folder.

How to do it...

To create a picture-in-picture display, just follow these steps:

  1. Import the BasicScene package and the monitor and camera models into your Unity Project.
  2. From the Project view, open the BasicScene level. This is a basic scene featuring an animated character and some extra geometry.
  3. From the Project view, place the monitor and camera objects into the scene by dragging them into the Hierarchy panel. Their Transform settings should be (shown in the following screenshot): monitor: Position: X: 0; Y: 0.09; Z: 4. Rotation: X: 0; Y: 180; Z: 0. camera: Position: X: -3; Y: 0.06; Z: 4. Rotation: X: 0; Y: 90; Z: 0:
    How to do it...
  4. Create, from the Project view, a new Render Texture, and rename it screen. Then, from the Inspector view, change its Size to 512 x 512.
  5. Add a new Camera to the scene through the Create drop-down menu on top of the Hierarchy view (Create | Camera). Then, from the Inspector view, name it Surveillance and make it a child of the camera GameObject. Then, change its Transform settings to the following: Position: X: 0; Y: 2; Z: 0, and Rotation: X: 0; Y: 0; Z: 0.
  6. Select the Surveillance camera you have created, and, from the Inspector view, change its Clipping Planes | Near to 0.6. Also, populate the Target Texture slot with the Render Texture screen and disable the camera's Audio Listener component, as shown in the following screenshot:
    How to do it...
  7. From the Hierarchy view, expand the monitor object and select its screen child. Then, from the Inspector, find its material (named Desert), and, from the Shader drop-down menu, change itto Unlit/Texture. Finally, set the screen texture as its base texture, as shown in the following screenshot:
    How to do it...
  8. Now it's time to add some post-processing to the texture. From the main menu, import the Effects package (Assets | Import Package | Effects).
  9. From the Hierarchy view, select the Surveillance camera. Then, from the main menu, add the Grayscale image effect component (Component | Image Effects | Color Adjustments | Grayscale). Also, add the Noise And Grain image effect (Component | Image Effects | Noise | Noise and Grain (Filmic)). Finally, from the Inspector view, set the Intensity Multiplier of the Noise And Grain to 4.
  10. Play your scene. You should be able to see your actions in real time on the monitor's screen, as shown here:
    How to do it...

How it works...

We achieved the final result by using the surveillance camera as source for the Render Texture applied to the screen. The camera was made a child of the camera's 3D model for easier relocation. Also, its Near Clipping plane was readjusted in order to avoid displaying parts of the camera's 3D model geometry, and its Audio Source component was disabled so that it wouldn't clash with the main camera's component.

In addition to setting up the surveillance camera, two Image Effects were added to it: Noise And Grain and Greyscale. Together, these effects should make Render Texture look more like a cheap monitor's screen.

Finally, our screen render texture was applied to the screen's 3D object's material (which had its shader changed to Unlit/texture so it could be seen in low/no light conditions, like a real monitor).

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

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