Search Unity

how to fast load high resolution image?

Discussion in 'Scripting' started by sTonto, Jul 27, 2017.

  1. sTonto

    sTonto

    Joined:
    Jun 7, 2017
    Posts:
    7
    hello guys

    I am trying to load very high quality images from the server. (4K image 8192 * 8192 ↑)

    The problem is that this image is just one of many content.
    I do not want the FPS to be lowered while loading this image. So, I wonder how you can increase the load speed while maintaining high quality.


    Of course, I know that lowering the resolution or lowering the quality of the image will improve the loading speed accordingly.
    But I would like to maintain very high quality.

    If have a good idea or someone who has solved this, please help me.

    Unity version - 5.6.0p3, 2017.1.0p1
    Target platform - PC, Mac & Linux Standalone
    PC - High specification
     
  2. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
    What are you using the image for? Would a coroutine work aka loading on a different thread?
     
  3. sTonto

    sTonto

    Joined:
    Jun 7, 2017
    Posts:
    7
    This is to exhibit photos uploaded to the server by the user.
     
  4. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
  5. sTonto

    sTonto

    Joined:
    Jun 7, 2017
    Posts:
    7
  6. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    If at all possible, I think English is needed on the forums.
    Translation: (Google Translate)
    "This may be helpful for me. I need to look at the code.
    thank you for helping me"
     
  7. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
    @sTonto If you find a solution it might be best to share on the forums, I am sure more people will need help with such things :D Glad it helped
     
  8. SiliconDroid

    SiliconDroid

    Joined:
    Feb 20, 2017
    Posts:
    302
    Yes, use a coroutine as @Basen1 mentioned:

    Code (CSharp):
    1.  
    2. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    3. //    GET WEB DATA COROUTINE
    4. float fProgress = 0.0f;
    5. private IEnumerator GetWebData_Coroutine(string sURL)
    6. {
    7.     //  START TIMER
    8.     float K_F_NETWORK_TIMEOUT_MILLIS = 20000f;//20 SEC TIMEOUT
    9.     Stopwatch oStopwatch = new Stopwatch();
    10.     oStopwatch.Reset();
    11.     oStopwatch.Start();
    12.     bool bTimedOut = false;
    13.  
    14.     //  START WEB REQUEST
    15.     fProgress = 0.0f;
    16.     sURL = sURL.Replace(" ", "%20");
    17.     WWW oWWW = new WWW(sURL);
    18.  
    19.     //  WAIT FOR WEB REQUEST
    20.     for (;;)
    21.     {
    22.         fProgress = oWWW.progress;
    23.         if (oWWW.isDone)
    24.         {
    25.             break;
    26.         }
    27.         if (oStopwatch.ElapsedMilliseconds > K_F_NETWORK_TIMEOUT_MILLIS)
    28.         {
    29.             bTimedOut = true;
    30.             break;
    31.         }
    32.         yield return null;
    33.     }
    34.  
    35.     //  FINISHED
    36.     if(!bTimedOut)
    37.     {
    38.         HandleReceivedWebData(oWWW);
    39.     }
    40. }
    Call it like this:

    Code (CSharp):
    1.    string sURL = "http://www.xyz.com/texture.jpg";
    2.    StartCoroutine(GetWebData_Coroutine(sURL));
    You can display a progress bar, fProgress can be used per frame, it goes 0 ... 1.
     
    Last edited: Jul 27, 2017
    Ryiah and Basen1 like this.
  9. sTonto

    sTonto

    Joined:
    Jun 7, 2017
    Posts:
    7
    Thank you for translating my words into English.:)
     
  10. sTonto

    sTonto

    Joined:
    Jun 7, 2017
    Posts:
    7
    Thank you for letting me know about a nice feature called the stopwatch.
    But what I want is the ability to load faster than the API provided by Unity......T-T
     
  11. SiliconDroid

    SiliconDroid

    Joined:
    Feb 20, 2017
    Posts:
    302
    I doubt it's the API that's slow it's probably your server. Under the hood unity www api runs on seperate thread.

    Have you timed how fast it takes in a browser to load the image? Make sure you clear the browser cache.
     
  12. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
  13. sTonto

    sTonto

    Joined:
    Jun 7, 2017
    Posts:
    7
    When testing with a 4K image, we found that it took 0.3-1.2 seconds.
     
  14. SiliconDroid

    SiliconDroid

    Joined:
    Feb 20, 2017
    Posts:
    302
    How long did the same URL take using unity?
     
  15. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
    I personally would recommend (HIGHLY RECOMMEND), using the native image compression format per platform (e.g.: DDS, PVR ).. strip their headers, and load directly to GPU via Texture2D.LoadRawTextureData

    https://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html

    the reason why it takes so long to load if you are using JPEG or PNG is because of the decoder it has to run through to "get" to the native format to load to the GPU.. there is no getting around it at all and can add 300ms per decode or more... been there, done that.

    go with native images and be happy.

    [edit]
    to add.. this should all be done outside of your download routine... just download your pre-processed images (DDS, or PVR.. whatever platform specific image format, without their headers!)... and read in their byte array.. load with the loadrawtexturedata method, check out the link above for the other steps, but I have solve this very problem. I guarantee it makes a huge difference. promise.
     
    Last edited: Aug 2, 2017
    Ryiah and SiliconDroid like this.
  16. vasupol11

    vasupol11

    Joined:
    Aug 8, 2017
    Posts:
    2
    @tswalk this sounds promising but lets say I have a JPEG file, what exactly do I have to do to the image to get rid of their headers?
     
  17. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
    Based on what you've stated, you are downloading these from a server right?

    Well.. you are going to have to do some work on the JPEG on the server side to convert it to the native image file format (DDS or PVR).

    Before loading it with Texture2D.LoadRawTextureData you must remove the header of the image from the byte[]. This can be done on either the server before sending, or on the device. You will want to do it on the server so that the client only needs to read the byte[] and load.
     
  18. Rajawat

    Rajawat

    Joined:
    Oct 1, 2016
    Posts:
    25
    @tswalk - How can I convert jpeg or png to native format ? Is there any tool or software to do it ? How to remove the header ?
     
  19. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
    sorry, that's why i get paid the big bucks.
     
    PygmyMonkey likes this.