Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

LoadImage vs. CreateExternalTexture

Discussion in 'Android' started by Koru, Jul 3, 2017.

  1. Koru

    Koru

    Joined:
    Feb 14, 2012
    Posts:
    42
    Hi,
    currently I am working on a Unity's client that is receiving camera frames by network using sockets. I checked LoadImage at the beginning and I wasn't satisfied with the results, as there are spikes every time client decodes frame and I cannot use LoadImage from other thread.

    I've read that creating textures in native code should be more efficient but my native Android skills are not that high and I ended up with a solution that is twice slower than LoadImage. I tried also LoadRawTextureData to move texture loading to another thread, but it was total fail. I could not find proper TextureFormat I guess, everytime I had an error "not enough data provided".

    I am using opencv to prepare frames encoded in jpg format. Here is my Java code for creating native textures, I noticed that decodeByteArray method is responsible for low performance:
    Code (CSharp):
    1. public class NativeImage {
    2.  
    3.     private static Bitmap gBitmap;
    4.     private static BitmapFactory.Options gOpts = new BitmapFactory.Options();
    5.  
    6.     public static int NativeImageTexturePtr(byte[] data) {
    7.         gOpts.inSampleSize = 1;
    8.         int textures[] = new int[1];
    9.         GLES20.glGenTextures(1, textures, 0);
    10.         if(textures[0] != 0) {
    11.             if(gBitmap != null) {
    12.                 gBitmap.recycle();
    13.             }
    14.             gBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    15.             gOpts.inBitmap = gBitmap;
    16.             GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
    17.             GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    18.             GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    19.             GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
    20.             GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
    21.             GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, gBitmap, 0);
    22.         }
    23.         return textures[0];
    24.     }
    25. }
    26.  
    Could someone advice me how to speed up native texture creation? Does Unity use external libraries that gets better results than my method? I could live with same performance as LoadImage, but I would like to load textures asynchronous to avoid spikes that make my client a bit laggy.
     
  2. CastryGames

    CastryGames

    Joined:
    Oct 1, 2014
    Posts:
    77
    amigo, ¿podrías resolver algo? Estoy en el mismo problema que tú, ¿qué me aconsejas?
     
  3. CastryGames

    CastryGames

    Joined:
    Oct 1, 2014
    Posts:
    77
    friend, could you solve something, I'm in the same problem as you, what do you advise me?