Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to use CreateExternalTexture?

Discussion in 'Android' started by diamond2004, Oct 24, 2015.

  1. diamond2004

    diamond2004

    Joined:
    Jan 24, 2014
    Posts:
    8
    Hi, Can someone please help me figure out what is the problem with my code? I am trying to load an image from the native side and send the texture to Unity. I am using Unity Pro 5.0.2f1.

    Unity Side:
    void Start () {
    AndroidJavaObject mImageLoader = new AndroidJavaObject("com.saeid.android.LoadTexture2D");
    Texture2D texture2D = new Texture2D(1920, 1080, TextureFormat.ARGB32, false);

    Int32 texPtr = mImageLoader.Call <Int32> ("loadImageReturnTexturePtr", "/storage/sdcard0/Images/test.jpg");
    Debug.Log("texture pointer? " + texPtr);
    Texture2D nativeTexture = Texture2D.CreateExternalTexture (1920, 1080, TextureFormat.ARGB32 , false, false, (IntPtr)texPtr);
    texture2D.UpdateExternalTexture(nativeTexture.GetNativeTexturePtr());
    gameObject.GetComponent<Renderer>().material.mainTexture = texture2D;
    }


    Java Side:
    public int loadImageReturnTexturePtr(String imagePath) {
    Log.d(LOGTAG, "loading image1: " + imagePath);

    Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
    Log.d(LOGTAG, "Bitmap is: " + bitmap);

    ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
    bitmap.copyPixelsToBuffer(buffer);

    int textures[] = new int[1];
    GLES20.glGenTextures(1, textures, 0);
    int textureId = textures[0];

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, 1920, 1080, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
    GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
    GLES20.GL_CLAMP_TO_EDGE);
    Log.d(LOGTAG, "texture id returned: " + textureId);

    return textureId;
    }
     
  2. diamond2004

    diamond2004

    Joined:
    Jan 24, 2014
    Posts:
    8
    So, I figured it out... The code is actually correct. except the texture format in both side should be the same.
    In my case, I have ARGB32 (in unity side) and RGBA (in Java side) which don't match. Also somehow GLES20.glTexImage2D(...) didn't work for me. I replaced it with
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D,0, bitmap,0); and finally I noticed the same code works on same Unity versions and doesn't work on some other. for example it is not working in 5.0.2f1 but it works in 5.0.3.
     
    vvqboy likes this.
  3. vvqboy

    vvqboy

    Joined:
    Sep 19, 2016
    Posts:
    1
    Thank you very much for sharing.
    I used the code above, but get an exception below:
    call to OpenGL ES API with no current context (logged once per thread)
    I try to call java side code both in Start() or Update() method, but the result is the same.
    The app is running on android platform.