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?
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?
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
public Texture2D texture;
public RenderTexture renderTexture;
int count_x = 810;
int count_y = 810;
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;
}