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
2
Question by eeytan · Oct 23, 2014 at 11:15 PM · texture2drendertexturereadpixels

Only grey (205) when reading pixels from a RenderTexture to a Texture2D

I need to look up the color on a RenderTexture where a RayCast hits it. Of course, it's not possible to that directly. RenderTextures don't have a getPixel method. So, every frame, I save the RenderTexture to a Texture2D. On Start I call:

 public Texture2D tex = null;
 public RenderTexture myRenderTexture; // Assigned in the inspector.

 void Start () {
     RenderTexture.active = myRenderTexture;
     tex = new Texture2D(myRenderTexture.width, myRenderTexture.height, TextureFormat.ARGB32, false);
 }

Then, on update, I call this from another script:

 public IEnumerator refresh () {
      yield return new WaitForEndOfFrame();
      tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0, true);
      tex.Apply ();
 }

Then, my plan was to just use tex as the Texture2D of my RenderTexture.

But what actually happens is that tex only contains a big square of grey, specifically, in RGB, it's all (205,205,205). I can see the Texture2D in the inspector and it's all grey. And the pixel colors I gather from the RayCast are all (205,205,205).

Sometimes, by tweaking some aspects, such as the TextureFormat or changing the wrap mode on the Texture2D, I get a copy of the "Game" frame in the lower left corner of the 2D Texture, but that is not reliable and does not happen often. The Game frame does not show the RenderTexture but does show an object that uses the RenderTexture.

The RenderTexture is actually applied as expected on a plane, so I'm confident that the RenderTexture works as expected and that the problem resides in the way the data is copied to the Texture2D.

Why isn't my RenderTexture copied to the Texture2D?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by eeytan · Oct 24, 2014 at 07:58 PM

There were two things I needed to do. Here's my new texture-forwarding script:

 public Texture2D tex;
 public RenderTexture myRenderTexture;

 // Use this for initialization
 void Start () {
     tex = new Texture2D (myRenderTexture.width, myRenderTexture.height, TextureFormat.RGB24, false);
 }
 
 // Update is called once per frame
 void Update () {
     RenderTexture.active = myRenderTexture;
     tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
     tex.Apply ();
 }

So now, I'm re-assigning the RenderTexture as the main render texture every frame.

The other thing I needed to do was on the render texture asset itself: I set the anti-aliasing and and depth buffer to "None".

I think that's all it took.

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

2 People are following this question.

avatar image avatar image

Related Questions

ReadPixels returns RGBA(0,0,0,0) 0 Answers

Why is ReadPixels on my RenderTexture creating a pure gray texture? 1 Answer

Can a Texture2D be created at runtime from a snapshot of a RenderTexture? 3 Answers

Render Texture does not work on only IOS devices 0 Answers

scale squash rendertexture to different size 1 Answer


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