Search Unity

How to replace PNG image display processing with DOTS

Discussion in 'Entity Component System' started by liveralmask, Jun 2, 2019.

  1. liveralmask

    liveralmask

    Joined:
    May 27, 2019
    Posts:
    34
    I would like to replace the following code with DOTS and execute it on a worker thread, but is there a good way?

    Code (CSharp):
    1. byte[] bytes = new byte[ 0 ]; // TODO
    2. Texture2D texture = new Texture2D( 0, 0 );
    3. ImageConversion.LoadImage( texture, bytes, false );
    4. Sprite.Create( texture, new Rect( 0, 0, texture.width, texture.height ), Vector2.zero );
     
  2. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,052
    I'm also interested, we load a lot of textures dinamically on our projects.
     
  3. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Maybe https://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html might help. Most processing of textures and other data unity has formats for, need at least some work done on the main thread. At a minimum the data has to be written/synchronized on the main thread before it can be used from the main thread.. I would think LoadRawTextureData is pretty fast, and there might be other handy api's for this also I'm not aware of.

    To load files async you can use https://docs.unity3d.com/ScriptReference/Unity.IO.LowLevel.Unsafe.AsyncReadManager.html.

    I think the tools are mostly there even if fairly low level at this point.
     
    cultureulterior likes this.
  4. liveralmask

    liveralmask

    Joined:
    May 27, 2019
    Posts:
    34
    Validates the speed of LoadRawTextureData.

    By the way, is it difficult with DOTS?
     
  5. liveralmask

    liveralmask

    Joined:
    May 27, 2019
    Posts:
    34
    You need to know the width and height before running LoadRawTextureData. Also, it is not practical because strange errors occur.
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    It’s works without errors. We using that in our game in many places for checking and updating mask textures (where compute shaders not required)
     
    MNNoxMortem likes this.
  7. liveralmask

    liveralmask

    Joined:
    May 27, 2019
    Posts:
    34
    Is it likely that I've made a mistake in using LoadRawTextureData, so is it possible to have sample code for displaying PNG images ready?
     
  8. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
     

    Attached Files:

  9. liveralmask

    liveralmask

    Joined:
    May 27, 2019
    Posts:
    34
    Thank you very much. I understand how to use LoadRawTextureData correctly.

    However, I do not want to adopt it for my project because it is troublesome to convert the binary data of PNG image to the format that LoadRawTextureData can handle.

    I want to know how to run on a worker thread as easily as LoadImage.
     
  10. liveralmask

    liveralmask

    Joined:
    May 27, 2019
    Posts:
    34
  11. liveralmask

    liveralmask

    Joined:
    May 27, 2019
    Posts:
    34
    I was able to do something other than creating Texture2D in the rendering thread.
    However, because Texture2D constructor is heavy, it was useless.
     
  12. liveralmask

    liveralmask

    Joined:
    May 27, 2019
    Posts:
    34
    It turned out that this is not possible at the moment.