- Home /
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/
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.
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.
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/
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!
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/
Your answer
Follow this Question
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