Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by rich2020 · Jan 20, 2018 at 04:51 PM · readpixels

Faster way to get raw texture data than Texture2D.ReadPixels?

I want to capture the screen the user is seen and send that data as a byte array to a remote machine, running another Unity3D application.

I am using Texture2D.GetRawTextureData() at present, but after timing it, I see that it takes up to 12 milliseconds:

 private byte[] FrameData;
 public IEnumerator SetFrame()
 {
     // Can take up to 12 ms!
     yield return new WaitForEndOfFrame();
 
     int width = Screen.width;
     int height = Screen.height;
 
     Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

     var sw = new System.Diagnostics.Stopwatch();
     sw.Start();
     tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
     Debug.Log(sw.ElapsedMilliseconds.ToString());

     FrameData = tex.GetRawTextureData();
     Destroy(tex);
 }

On this other machine, I do the following to display the image to the user:

 void DisplayFrame(byte[] frame)
 {
     //3ms at most
     Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
     texture.LoadRawTextureData(frame);
     texture.Apply();
     GetComponent<Renderer>().material.mainTexture = texture;
 }

Is there something faster than ReadPixels?

(PS how the hell do I insert a new line between sentences? This editor just ignores them!)

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 landon912 · Mar 08, 2018 at 09:13 PM 0
Share

I’m away from my desktop, but I will look into this question more in depth when back.

However, is your problem ReadPixels() or GetRawTextureData()? In your code sample, you only test the speed of ReadPixels() but your question is referring to GetRawTextureData()?

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Elarm00 · Mar 09, 2018 at 02:59 PM

If you are working on 2018 you could use ( or try at least, as its still experimental ) AsyncGPUReadback.Request, also take a look at this https://medium.com/google-developers/real-time-image-capture-in-unity-458de1364a4c basically what you want to do is find a place where triggering gpu flush is the least expensive. Good Luck!

Comment
Add comment · Show 1 · 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 rich2020 · Mar 09, 2018 at 10:03 PM 0
Share

Thanks, I'll look into AsyncGPUReadback.Request. I've come across that link you posted a while back, but it didn't really help me because I didn't know what they were talking about with "if the garbage collector is thread safe, maybe we can run it on a background thread and avoid blocking the main render thread.".

P.S you have included a , at they end of your link. You need to remove it, otherwise it goes to a 404 webpage.

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

75 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 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

What is the bottleneck in this iphone code? 3 Answers

ReadPixels works on device (iPad) but not in the editor? 3 Answers

Read-Write enabled sprite cant use sprite packer. 0 Answers

ReadPixels is capturing GUI, how to get a screencap without GUI 1 Answer

Why is ReadPixels on my RenderTexture creating a pure gray texture? 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