GetPixels of RenderTexture (7714)

RenderTexture Inherits from Texture, as well as Texture2D, but of course the Texture2D function GetPixels can't be used on RenderTexture.

Is there a way to transfer the current frame of a given camera's RenderTexture to another texture?

What are you using this for? You can create a renderTexture asset either in the inspector or in a script and assign a material to use this renderTexture (Material.SetTexture). You seem to want to get the pixels of the render of the screen - for that you would use Texture2D.ReadPixels.

3 Answers

3

this is a summary of the code I use to get the pixels from a renderTexture

Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

// ofc you probably don't have a class that is called CameraController :P
Camera activeCamera = CameraController.getActiveCamera();

// Initialize and render
RenderTexture rt = new RenderTexture(width, height, 24);
activeCamera.targetTexture = rt;
activeCamera.Render();
RenderTexture.active = rt;

// Read pixels
tex.ReadPixels(rectReadPicture, 0, 0);

// Clean up
activeCamera.targetTexture = null;
RenderTexture.active = null; // added to avoid errors 
DestroyImmediate(rt);

it is taken from the scripts in this thread 2nd post

http://forum.unity3d.com/threads/3880-Submitting-featured-content

I don't understand this code. tex is declared and then called the readpixels from it. When is the information from render texture passed to tex?

ReadPixels reads from the currently active render texture (or the screen if no render texture is active).

There is actually no reason to create and render to a RenderTexture since Texture2D.ReadPixels() just reads from the normal framebuffer. The only thing you accomplish is to make the process take longer. As long as you do the call to ReadPixels in LateUpdate you are also sure that your game objects are positioned as you want them.

So, there's RenderTexture.GetTemporary(). You don't actually have to create a camera for it. You can just set it as the active render target and draw to it as necessary. When you're done with it, you call RenderTexture.ReleaseTemporary() and pass it the reference to the temporary render texture to properly dispose of it.

Sorry, my bad. You are right, it works only for UI Text, for 3d Text you`ll need some other solution. But it is still something. Unity native UI Outline script does not produce such clean results as the script in the link does. In my own experience the Circle outline from the link produces very nice outlines. Maybe, the script can be rewritten to suit your needs.

public Texture2D texture;
public RenderTexture renderTexture;
int count_x = 810;
int count_y = 8
10;
void Start (){
texture = new Texture2D(count_x, count_y, TextureFormat.RGB24, false);

        Rect rectReadPicture = new Rect(0, 0, count_x, count_y);

        RenderTexture.active = renderTexture;

        // Read pixels
        texture.ReadPixels(rectReadPicture, 0, 0);
        texture.Apply();

        RenderTexture.active = null; // added to avoid errors 
}

Directly from the docs: (1
static public Texture2D GetRTPixels(RenderTexture rt)
{
// Remember currently active render texture
RenderTexture currentActiveRT = RenderTexture.active;

        // Set the supplied RenderTexture as the active one
        RenderTexture.active = rt;

        // Create a new Texture2D and read the RenderTexture image into it
        Texture2D tex = new Texture2D(rt.width, rt.height);
        tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);

        // Restorie previously active render texture
        RenderTexture.active = currentActiveRT;
        return tex;
    }

This works- but for some reason my texture is much darker than the original render texture