Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Async Texture Loading

Discussion in '5.2 Beta' started by Asylum69, Jul 8, 2015.

  1. Asylum69

    Asylum69

    Joined:
    Jul 2, 2015
    Posts:
    1
    Hi,

    I'm not sure if it is in the pipe or somewhat related to the new WebResquest, but one developer told me on the forum that you planed to do async texture loading.

    Right now Texture2D.LoadImage, is not async and when you want to do an app that is heavily UI based and you stream image from the web, it can be very frustrating to have the main/ui thread blocked.

    So, is this something that is planed ?
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,649
    http://unity3d.com/unity/roadmap

    "Graphics: Async Texture Upload" is under 5.3.

    That said, that feature only deals with the CPU->GPU upload of texture data; it does not deal with, for example, decompressing image files themselves. That's something you should be able to do on a background thread yourself, though.
     
  3. Ziboo

    Ziboo

    Joined:
    Aug 30, 2011
    Posts:
    356
    Thanks for the reply,

    How can I do this in a background thread since I need to call a UnityEngine function somewhere in the code to create the texture2D ?
     
  4. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,649
    You'd decompress the texture on a background thread into an array of Color32 elements (plus width and height), and then actually create the texture object on the main thread afterwards.
     
  5. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    Welcome to the wonderful world of multithreading ;-)

    I guess the keyword is synchronisation: The background thread cannot access UnityEngine, of course, but you can check the state of the work of the background thread from the main thread in regular intervals, and when you figured out that it's done with its work, you can do that final step from the main thread.
     
  6. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,461
    So, we download a ton of PNG's from the web, and the main issue we have is the time it takes to decompress the png into a Texture2D. One of our engineers played with moving this to a background thread, but the c# library for doing this work was many times slower that Unity's built in one, making the delay much worse for the user. Is the only option here to make platform specific plugins for each platform and include libpng or something to do the decompression? It would be REALLY nice if unity's internal PNG loader had this option, as writing a plugin for every platform we support is going to take a long time..
     
  7. davebuchhofer

    davebuchhofer

    Joined:
    Nov 9, 2007
    Posts:
    126
    There are a couple options, none of them terribly appetizing.

    www.texture is considerably faster than LoadImage(bytes)
    www.textureNonReadable is also considerably faster than LoadImage(bytes)
    LoadRawImageData() is considerably faster than either of the above, but requires you to have a previously compressed texture, which is either a massive filesize difference, or separately compressed/decompressed using say, lzma.. (IE: a dds, or etc texture that has been offline compressed).

    For the webgl build, where background decompression is not an option, I've got something where we load the texture data into a gltexture using native webgl, which is mirrored with a unity Texture2D using the nativetextureid, allowing the browser to cache/render the jpg's. It 'works' but boy is that a lot of hassle just to work around the fact that there is no async loading of textures.
     
    skullthug and VReel like this.