Search Unity

  1. Dismiss Notice
  2. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice

Async Texture Loading with new Jobs system?

Discussion in 'Scripting' started by twfarro, May 8, 2018.

  1. twfarro

    twfarro

    Joined:
    Nov 23, 2013
    Posts:
    21
    Hello,

    I got really excited about the new Jobs system, hoping it would finally let me take UnityEngine objects and methods off the main thread. Specifically, I wanted to load JPG files into Texture2D objects asynchronously, so as not to cause frame halting.

    However, much to my dismay, since Unity Objects are non-blittable, they cannot be used off the main thread, making me question the usefulness of this Jobs system altogether.

    Has anyone found a suitable way to load Texture2D objects off the main thread?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    7,590
    Why does something not doing what you'd like it to do, make you question its usefulness altogether?

    "This car can't fly, I question its usefulness altogether!"

    ...

    As for loading textures.

    In what manner are you doing it?

    Have you attempted using the WWW to load asynchronously?

    Also if you really need a thread, have you tried using the .net framework to load up the data for the texture. And then jumping back to the main thread in the end and creating the texture based on what you loaded off thread.
     
    twfarro likes this.
  3. twfarro

    twfarro

    Joined:
    Nov 23, 2013
    Posts:
    21
    From my understanding, the job system is just a more limited threading library, since it can only operate on blittable datatypes. I think I would need to understand the scheduling or batch aspects better before I saw the advantages over traditional threading. Poor tone on my part, apologies.

    As for what I'm using, I am getting a byte array from file and using Texture2D.LoadImage(...). Based on profiling/less sophisticated debugging, loading the byte data from the source is not the expensive operation. It is Texture2D.LoadImage(...) that causes the half-second or more of holding the main thread. WWW load texture does not appear to present a noticeable performance difference.
     
    Last edited: May 8, 2018
  4. geertm

    geertm

    Joined:
    May 3, 2018
    Posts:
    5
  5. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    647
    The Job Systems' main purpose is to parallel equal tasks in one frame, it is not about threading at all. It is completely useless indeed if you don't have such abstract tasks that simply scale by number, such as 10k units moving around.

    Everythign that spans over more than one frame you still need coroutines or threads for all the work that can be done outside of Unity classes. Your problem is discussed here. If nothing helps you might be able to use SetPixels32() of fractions of the texture each frame, it will be much slower but will not freeze the game.
     
  6. WisockiJr

    WisockiJr

    Joined:
    Jul 5, 2014
    Posts:
    30
    That is the same problem I have now, we cannot build VR applications that load images, because loading textures from file still hang the applications for half a second. Thats unacceptable for VR application, and it is yet a problem for games that need to load images that are not build into the app. There is two main problems here, first one: you need to load the file from the disk on a Thread, decode it on a Thread. Unity will not help you with that. Second problem here: You need to upload it to the GPU. You cannot do it in a single frame, you need to split it into several frames, according to the size of the buffer. Unity will not help you with that. Thats it, Unity do not want you to create a VR app, thats unbelievable, how can I create a VR app that cannot load a JPG from my pendrive? WWW async LoadImageIntoTexture also DO NOT DO THAT. In conclusion, they still dont care, Unity3D is the most popular, the main development engine for building VR applications for the Industry, the leading, the awesome, and yet you cannot build an VR application that load a texture from disk. Please, Unity3D, we need it so much, Load Texture from my pendrive without HANGING my application!!!!
     
  7. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    830
    Hello, have you solved this??? I have a lot of textures on my web server that i have to load into my Unity VR app, but when i use any of the Unity API (www, WebReqest, and the rest of them) all do it on the main thread and causes the whole game engine to pause.

    I have been trying to solve this problem for a long time and i cant seem to solve it. in VR this is a HUGE problem!

    I cant believe Unity cant load a texture from the internet using a background thread rather than the main thread which messed up the main thread.

    Has anybody solved it?
     
  8. coldwarrior5

    coldwarrior5

    Joined:
    Nov 9, 2016
    Posts:
    12
    Yes, I too would need a fast asynchronous EncodeToPNG or EncodeToJPG as to not the hog the main thread.
     
  9. Kandera

    Kandera

    Joined:
    May 14, 2019
    Posts:
    1
    I just need a new Texture2D object out of main thread and unity says you can't do that. I can't believe it really happened.
     
unityunity