0
\$\begingroup\$

What I want to achieve is to render to a texture what is viewed by a camera in a given specific time or frame, and then stop rendering to the texture, delete the camera and stay with that texture for the rest of the gameplay.

So, I know how to set a Camera to continuously render to a texture in Unity: it's all about 1) to create a RenderTexture; 2) to specify such RenderTexture in the "Target Texture" field of the desired camera; 3) to assign the RenderTexture to a diffuse material; 4) to apply such material to a given object, let's say a plane.

It works great: all that is viewed by the camera is being rendered to the plane surface, but continuously (i.e. on the fly). This means that if, for instance, I delete the camera, the RenderTexture becomes a solid color. How can I save in the RenderTexture what the camera is viewing in a specific moment and then stop rendering to the texture?

EDIT: currently, I managed to achieve it by using the following code.

    RenderTexture rendText = RenderTexture.active;
    RenderTexture.active = mycam.targetTexture;
    mycam.Render();
    Texture2D cameraImage = new Texture2D(mycam.targetTexture.width, realcam.targetTexture.height, TextureFormat.RGB24, false);
    cameraImage.ReadPixels(new Rect(0, 0, mycam.targetTexture.width, realcam.targetTexture.height), 0, 0);
    cameraImage.Apply();
    RenderTexture.active = rendText;

However, for a RenderTexture with 1980x1080 (i.e. far from high quality), that piece of code takes around 50ms to perform the capture of the camera into a Texture2D. If I try it with a RenderTexture of 4096x4096 resolution, it takes around 160ms.

What is the correct way to accomplish this task for a more performant solution?

\$\endgroup\$
  • 1
    \$\begingroup\$ Are you using unity? \$\endgroup\$ – dnk drone.vs.drones Apr 26 '16 at 15:49
  • \$\begingroup\$ @dnkdrone.vs.drones Yes I am! And sorry for lacking that info. In my original title, I included it, but after editing the title to be more accurate to the question, I accidentally erased the mention to Unity. Also, now I have included it in the tags and body of the question \$\endgroup\$ – Kim Shutter Apr 26 '16 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.