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 OMA_Fox · Nov 23, 2016 at 02:13 PM · androidperformancetexture2dthreadingconstructor

Non-blocking construction (loading and copying) of large Texture2D's in C#

I'm building a VR app for Android which deals with loading a lot of large textures dynamically (all images are over 6MB in size as png's). These textures can either come from an Amazon S3 server, in which case they arrive as a stream, or from the user's device itself.

In both cases I'm able to get hold of the raw data or texture asynchronously without a problem. In the first I query the server and get a callback with the stream of data, and in the second I use the WWW class to get hold of the texture making use of the "file://" protocol.

The problem happens as soon as I want to actually construct a Texture2D from this data so that I can make use of it.

With the stream I convert it into a byte[] and try calling LoadImage(), and with the WWW class I simply try copying it with myTexture = www.texture. Both times I get a massive frame out as the Texture2D object is created. I want to eradicate this frame out because the app is simply un-shippable with it.

   using (var stream = responseStream)
   {
      byte[] myBinary = ToByteArray(stream);
      m_myTexture.LoadImage(myBinary);  // Commenting this line removes frame out
   }

   ...

   WWW www = new WWW("file://" + filePath);
   yield return www;
   m_myTexture = www.texture;  // Commenting this line removes frame out

Unfortunately Unity doesn't seem to like running these operations on a separate thread from the main thread and throws an exception when I try, saying that construction can only be done on the main thread.

Is there some way to perform construction operation on a seperate thread anyway? Or perhaps chunk up construction, so that I can spread it over multiple frames? Or maybe do some sort of fast memcopy operation that won't stall the main thread?

Thanks in advance!

PS: I asked pretty much the exact same question on Stack Overflow earlier this month, and have yet to get a working answer: goo.gl/UfSOtT

PPS: I've created a working example of the problem in the following repo: https://github.com/NeoSouldier/Texture2DTest/

Comment
Add comment · Show 4
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 christoph_r · Nov 23, 2016 at 09:12 PM 0
Share

So, judging from your stack overflow comments using Texture2D's SetPixels() method is too slow? And you apparently tried time slicing your method already, too? Giving us a short overview of what you've tried so far (and why it didn't work out) would be useful.

avatar image OMA_Fox christoph_r · Nov 24, 2016 at 12:41 PM 0
Share

Hey @christoph_r, thanks for the reply! The problem with the SetPixel() method is that you still need to do myTexture = www.texture in order to even get hold of the Texture2D you want to copy over, and as soon as run that line of code you frame out. None of the 4 other things suggested by @Programmer worked either unfortunately.

avatar image hexagonius · Nov 23, 2016 at 09:16 PM 1
Share

I found this interesting and found this of which the last post might be of a little help, although I see what you mean

https://forum.unity3d.com/threads/async-texture-loading.339312/

avatar image OMA_Fox hexagonius · Nov 24, 2016 at 12:41 PM 0
Share

Hey @hexagonius, thanks for the replies! That link is certainly relevant, in fact its pretty much asking the same question as I am. But they don't come up with a better solution than what I've found, and its over 1 year old so I'm hoping the tech has moved forward since then!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by OMA_Fox · Jan 17, 2017 at 09:21 AM

Eventually this problem was solved by creating a C++ Plugin (built through Android Studio 2.2) that makes use of "stb_image.h" for loading the image, and OpenGL to generate textures and map a set of scanlines onto the texture over multiple frames. The texture is then handed over to Unity through Texture2D.CreateExternalTexture().

This method does not make the work asynchronous but spreads the loading cost over multiple frames removing the synchronous block and subsequent frame out.

I wasn't able to make the texture creation asynchronous because in order for the OpenGL functions to work you are required to be running the code from Unity's main Render Thread, so functions must be called through GL.IssuePluginEvent() - Unity's docs use the following project to explain how to make use of this functionality: https://bitbucket.org/Unity-Technologies/graphicsdemos/

I've cleaned up the test repo I was working on and written instructions in the README to make it as easy as possible to understand the final solution I came to. I hope that it will be of use to someone at some point and that they won't have to spend as long as I've done to solve this problem! https://github.com/NeoSouldier/Texture2DTest/

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

7 People are following this question.

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

Related Questions

Unity to android (game very laggy) 1 Answer

Android Tablet vs iOS iPad performance difference 0 Answers

Render Android device camera from native pointer 0 Answers

Spikes with Physics.Simulate() in empty scene? 0 Answers

Mobile performance tips 3 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