Production camera using render textures

https://www.cnblogs.com/chenliyang/p/6558455.html

Production camera using render textures

Starting with Unity 5, the ability to render textures is available to all users (yes, including the free version)! You can use this feature to create cool effects, and in this article, I'll show you how to create a cool surveillance camera that projects the camera's view onto the screen.

What is a render texture?

A render texture is a texture that you can draw on and then use like any other sprite/texture. One of the coolest things is that you can use it as the target of the camera's view - so the camera draws what it sees onto the texture (instead of displaying it on the screen).

Also, a script to save the render texture as a .PNG file is included here, which you can use however you want.

 

Show a surveillance camera on the TV (ignore the vampires...)

The technology is similar to a surveillance camera setup. You have a camera that sees the image, then record it to a render texture (the equivalent of a video tape or hardware driver storing the image), and finally project the image onto the TV screen.

download

You can download the full Unity package (requires Unity 5+), or you can follow the tutorial.

This pack includes a demo scene showing random objects (and a vampire!). The camera is pointed at a random object and displayed on the TV screen.

There's a nice design in vampires - you choose which camera is visible or not (of course, mirrors or cameras can't see vampires).

In the sample scene, you can do the following controls:

· Arrow keys - move the surveillance camera

V - Toggles visibility of vampires

· S—Save the current view of the surveillance camera as a .PNG file.

Specific steps

If you are very familiar with Unity, you can skip this section.

For Unity newbies, most of the detailed basic operations will be covered here. If you don't understand or can't figure it out, just open the resource pack - a very simple project - and look at the objects and their settings.

The great advantage of this technique is that key functions do not require a single line of code. They are all included in Unity's editor.

This project has two scripts, but they are additional functions (one is to move the camera and the other is to save the rendered texture as a .PNG file).

New Construction

New Unity 3D project.

The resources you need are two sprites (a TV and a vampire). You can find it from the download file or use your own.

Put the TV on the screen and place some random objects elsewhere (they will be observed with the surveillance camera later). Put vampires among these random objects.

In order for the TV and vampire sprites to be imported into the scene, they need to be selected and then change the texture type to sprite.

Resize the TV to make it look the right size. A zoom ratio of (4, 4, 1) is almost just right. It will convert objects placed in the scene view to 2D.

You can directly use the Unity main menu (eg: Main Menu > GameObject > 3D Object > Cube) to create spheres, cubes, etc., or by dragging some sprites into the scene to create some random items for the camera to see.

As shown in the figure below, go to the next step.

 

scene as shown

Create a vampire

Make the vampire invisible to the camera by creating a special layer and telling the surveillance camera to ignore this layer.

Select the vampire in the Hierarchy panel, then choose Add Layer... Add a new layer in the Properties panel drop-down list, as follows:

 

Create new layer

Type "vampire" under the field for User Layer 8.

 

named 'vampire'

With the vampire selected, set its layer to vampire:

 

Setting up layers doesn't do anything right now, but after we do a few steps, it will.

Create a render texture

Right-click anywhere in the assets folder and select Create > Render Texture. Create a new render texture. Name it SecurityCameraTexture.

 

Create a render texture

 

 

Select the SecurityCameraTexture texture and set its size to 480x270 in the properties panel. Its size determines its resolution, and the resolution of the .PNG image that will be saved from the camera view.

 

Set the size of the render texture

Create materials from render textures

Create a new material using the SecurityCameraTexture you just created:

1.  

Right-click anywhere in the resources folder

2. 

Select Create > Material, name it SecurityFootage

3. 

Select the new material and change its shader properties (on the properties panel) to Mobile/Diffuse (you need to make its material accept a texture, which is not supported by default).

4. 

Drag the SecurityCameraTexture onto the texture properties of the material (the little box with the photo of the mountain in the upper right corner):

 

Create new material

At this point, we have created a material that can display theSecurityCameraTexture texture onto any object surface.

surveillance camera

Add a new camera to the scene (Main Menu > GameObject > Camera).

Rename the new camera to SecurityCamera.

Drag the SecurityCameraTexture onto the SecurityCamera's target texture field. This makes the camera view show the rendered texture instead of showing it to the screen, because the camera is now recording it instead of showing it to the game screen.

 

When the camera is selected, you can see a small preview window with the scene window visible.

When the camera is selected, its view in the preview window is the scene view. Your scene may not be the same as the picture above.

 

Move the camera to where you can see all objects and vampires. You need to make sure the camera is in front of your 3D space scene so you can see all the objects in the scene. When all objects can be seen in the camera preview window (excluding the TV screen), proceed to the next step.

filter vampires

In the preview window above (need to display the object on the TV screen, it will be fine later), you cannot see the vampire. To filter the vampires, select the SecurityCamera in the Hierarchy panel, then remove the vampirelayer from its clipping Mask (so it doesn't see anything in the vampire layer):

1. 

Select SecurityCamera in the Hierarchy panel 

2. 

In the Properties panel, click the drop-down list for Crop Mask

3. 

Uncheck the vampire layer (checked layers are visible to the camera).

 

Objects to filter vampire layers

 

 If the vampire is still visible in the camera preview, go back and make sure you have a vampire layer created for the vampire.

Create TV

Create a plane in the scene (Main Menu > GameObject > 3D Object > Plane). Rename the plane to TVScreen.

If you haven't used a plane before, it might seem odd. Because the plane is a two-dimensional plane, it may not be visible at certain angles and viewing angles. Rotate the plane to face the camera. Switching between 3D and 2D scene views helps you modify it.

Modify TV Screen to fit TV Genie's screen. Make sure it's placed right in front of the TV sprite so the main camera sees it.

Drag SecurityFootage to TV Screen's Material > Element 0 Properties (you'll need to expand the material list later on like the bottom of the screen):

Use the previously created SecurityFootage as the material for the plane

So far, the material has been associated with the TV screen. As you will recall from the previous step, the material will show what the surveillance camera sees.

If the TV picture is upside down, you need to rotate the plane to the correct position.

That's it!

 

You don't even need to run the scene to see the scene objects (except the vampire) displayed on the TV screen. Super cool!

Let's summarize our basic steps:

1. 

Create a render texture

2. 

Create a camera to draw what it sees onto a render texture

3. 

Create a material to display the render texture

4. 

Finally, create the TV screen display material.

Like a real surveillance camera, we capture the view and send it to a screen via a video recording device (a camera that renders a texture similar to a movie).

raise part

 

The sample project contains two additional codes. One script adds some user controls to move the surveillance camera and toggle the vampire show and hide; another script saves the surveillance camera view as a .PNG image file when the "S" key is pressed. The second script is as follows, with comments.

save render texture

Code written by 'Mate-O' in Unity Q&A with modifications ( http://answers.unity3d.com/questions/22954/how-to-save-a-picture-take-screenshot-from-a-camer. html )

The code is simple - read the rendered texels, encode as .PNG, and finally save as .PNG. Comments explain what each piece of code does.

 

 

[csharp]  view plain copy  
 
 print?
  1. Code example:  
  2. using UnityEngine;  
  3. using System.Collections;  
  4. using System;  
  5.    
  6. public class CameraSnapshot : MonoBehaviour  
  7. {  
  8. [SerializeField]  
  9. RenderTexture securityCameraTexture;  // drag the render texture onto this field in the Inspector  
  10. [SerializeField]  
  11. Camera securityCamera; // drag the security camera onto this field in the inspector  
  12.    
  13. void LateUpdate()  
  14. {  
  15.    
  16.     if (Input.GetKeyDown("s"))  
  17.     {  
  18.         StartCoroutine(SaveCameraView());  
  19.     }  
  20.    
  21. }  
  22.    
  23. public IEnumerator SaveCameraView()  
  24. {  
  25.     yield return new WaitForEndOfFrame();  
  26.   
  27.     // get the camera's render texture  
  28.     RenderTexture rendText = RenderTexture.active;  
  29.     RenderTexture.active = securityCamera.targetTexture;  
  30.    
  31.     // render the texture  
  32.     securityCamera.Render();  
  33.    
  34.     // create a new Texture2D with the camera's texture, using its height and width  
  35.     Texture2D cameraImage= new Texture2D(securityCamera.targetTexture.width, securityCamera.targetTexture.height, TextureFormat.RGB24, false);  
  36.     cameraImage.ReadPixels(new Rect(0, 0, securityCamera.targetTexture.width, securityCamera.targetTexture.height), 0, 0);  
  37.     cameraImage.Apply();  
  38.     RenderTexture.active = rendText;  
  39.    
  40.     // store the texture into a .PNG file  
  41.     byte[] bytes = cameraImage.EncodeToPNG();  
  42.    
  43.     // save the encoded image to a file  
  44.     System.IO.File.WriteAllBytes(Application.persistentDataPath + "/camera_image.png", bytes);  
  45. }  
  46. }  
  47.       


 

 

Once the script is added to the scene, the "S" key can be pressed to take a camera screenshot. You can access saved files.

Summarize

A render texture is a canvas to which you can draw. By associating the render texture with the output of the camera, you can draw what the camera sees onto the render texture.

Since a render texture is a texture, it can be associated with a material. Then, add the material to the object and effectively project the render texture onto the object's surface.

The surveillance display is a good metaphor for how it works. A camera is a camera, a render texture is a video recording, and a material is the screen your video is projected onto.

Outlook?

You should now understand how render textures work and be flexible about their use. .PNG output also offers many possibilities. Render texture features (among other properties) were not available to non-professional users of versions prior to Unity 5, and more and more openness makes Unity a better and better tool.

Some thoughts on using this technique:

· Use multiple sprites to form a new image and save it as .PNG to use in the game (eg: compose a face with different features).

· Infrared cameras can see creatures invisible to the naked eye.

· If you can find a way to modify the camera output (eg make the TV screen black and white or greenish like an old computer screen).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325264815&siteId=291194637