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.
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.
To create a picture-in-picture display, just follow these steps:
BasicScene
package and the monitor
and camera
models into your Unity Project.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
:screen
. Then, from the Inspector view, change its Size to 512
x 512
.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
.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: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
.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).
18.190.156.93