Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by RyFridge · Jan 10, 2016 at 11:25 PM · texture2drendertexturereadpixelsgetpixel

Get Colors from RenderTexture faster!

Hey there!
I need to pull Colors from my RenderTexture (8x8 pixels) and I am doing that by reading it into a Texture2d first and then using GetPixel on that.

         displayCamera.targetTexture = rt;
         displayCamera.Render ();
 
         RenderTexture.active = rt;
 
         // WAY TOO SLOW
         targetTexture.ReadPixels (new Rect (0, 0, xResolution, yResolution), 0, 0);
 
         targetTexture.Apply ();

As you can see I already pointed out the problematic bottleneck line.

It uses about 35 ms on my CPU time, even though the Texture2D is also only 8x8 pixels large. Now I know it has to do with juggling the pixels between GPU and CPU, but is there really not any other way to just get pixel colors from a RenderTexture (or in my original interest, a MovieTexture) every frame?

If you know a way with shaders or something else, please help me! There seems to be no solution...

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 RyFridge · Jan 12, 2016 at 10:04 AM 0
Share

Okay, I read a lot and got to the conclusion that OpenGL is faster, or so everybody says. I tried for past few hours, but there seems to be a major problem in my understanding of how this works. I get a color, but it is like random noise, so I must be reading the buffer wrong. Please help!

 [SerializeField] Light displayLight;
     Camera displayCamera;
     [SerializeField] ParticleSystemRenderer particles;
     [SerializeField] int xResolution = 8;
     [SerializeField] int yResolution = 8;
     [SerializeField] Renderer display$$anonymous$$odelRenderer;
     [SerializeField] bool play$$anonymous$$ovie = false;
 
     $$anonymous$$ovieTexture movie;
 
     void Awake(){
         displayCamera = GetComponent<Camera> ();
 
         if (play$$anonymous$$ovie) {
             movie = ($$anonymous$$ovieTexture)display$$anonymous$$odelRenderer.material.GetTexture ("_Emission$$anonymous$$ap");
             movie.loop = true;
             movie.Play ();
         }
 
         displayCamera.aspect = 4f / 3f;
     }
 
     [DllImport("opengl32")]
     public static extern void glReadPixels (int x, int y, int width, int height, int format, int type, IntPtr buffer);
 
     void Update () {
         displayCamera.Render();
     }
 
     const int GL_RGB          = 0x1908;
     const int GL_UNSIGNED_BYTE = 0x1401;
 
     public byte[] destination;
 
     public class ColorBuffer
     {
         int stackpointer = 0;
         float red = 0f;
         float green = 0f;
         float blue = 0f;
 
         public void save(byte input){
             int position = stackpointer % 3;
 
             switch (position) {
             case 0:
                 red += input;
                 break;
             case 1:
                 green += input;
                 break;
             case 2:
                 blue += input;
                 break;
             default:
                 break;
             }
 
             stackpointer++;
         }
 
         public Color load(){
             return new Color (
                 ((red / (stackpointer / 3f)) / 255f),
                 ((green / (stackpointer / 3f)) / 255f),
                 ((blue / (stackpointer / 3f)) / 255f)
             );
         }
     }
 
     void OnPostRender(){
         int size = xResolution * yResolution * 3;
 
         IntPtr buffer = $$anonymous$$arshal.AllocHGlobal (size);
         byte[] bytes = new byte[size];
 
         glReadPixels(0, 0, xResolution, yResolution, GL_RGB, GL_UNSIGNED_BYTE, buffer);
 
         $$anonymous$$arshal.Copy(buffer, bytes, 0, size);  
 
         ColorBuffer cb = new ColorBuffer();
 
         for (int i = 0; i < size; i++)
             cb.save(bytes[i]);
 
         Color newColor = cb.load ();
         displayLight.color = newColor;
 
         Debug.Log("Color: " + 
             newColor.r.ToString("F2") + ", " + 
             newColor.g.ToString("F2") + ", " + 
             newColor.b.ToString("F2") + ", " + 
             newColor.a.ToString("F2"));
 
         $$anonymous$$arshal.FreeHGlobal(buffer);
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gwelkind · Nov 03, 2021 at 07:52 PM

You might want a compute shader. It depends on what you're trying to do wit hit.

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

39 People are following this question.

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

Related Questions

Convert Texture to Texture2d takes a lot of time in android device. 0 Answers

Trying to Take a Snapshot of Only a Portion of the Screen In-Game 0 Answers

Rendering Camera to PNG produces completely grey image. 1 Answer

Using 3D Render Texture with camera 0 Answers

Texture2D ReadPixels for specific Display 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