- Home /
GetPixels of RenderTexture
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 ($$anonymous$$aterial.SetTexture). You seem to want to get the pixels of the render of the screen - for that you would use Texture2D.ReadPixels.
Answer by PaulUsul · Jan 04, 2011 at 12:48 PM
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.
That rather depends on whether you want the pixels from the camera currently rendering the scene right? $$anonymous$$any reasons to need info from a different camera, or a camera with replaced shaders etc etc
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.
Camera has cullingmask which helps you remove some of the objects that you don't want to read pixels of.
So if you only want to read screen (i.e. screenshot) then use RenderTexture.GetTemporary() otherwise the above mentioned approach.
Answer by normalmur · Jul 09, 2018 at 09:11 PM
public Texture2D texture;
public RenderTexture renderTexture;
int count_x = 8*10;
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
}
Answer by samf1111 · Jun 24, 2021 at 07:47 PM
Directly from the docs: (link 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;
}
Your answer
Follow this Question
Related Questions
Setting pixels on a texture? 2 Answers
ReadPixels returns RGBA(0,0,0,0) 0 Answers
How to get ARGB values from Texture object? 1 Answer