Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by juan-jo · Sep 23, 2010 at 07:51 PM · texturetexture2drendertexturegetpixels

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image skovacs1 · Sep 23, 2010 at 08:52 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
6

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image diegzumillo · Aug 04, 2012 at 08:41 PM 0
Share

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?

avatar image whydoidoit · Jun 21, 2013 at 10:31 AM 0
Share

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

avatar image Birkeman · Jun 21, 2013 at 12:18 PM 0
Share

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.

avatar image whydoidoit Birkeman · Jun 21, 2013 at 12:20 PM 2
Share

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

avatar image SethF · Nov 04, 2013 at 07:30 PM 0
Share

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.

avatar image ratneshpatel SethF · Jan 10, 2020 at 11:44 AM 0
Share

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.

avatar image
1

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 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
     }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Raycast project to texture at runtime 1 Answer

Save image from camera with post processing. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges