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

OpenGL Update texture

Discussion in 'General Graphics' started by RemyOreka, May 22, 2020.

  1. RemyOreka

    RemyOreka

    Joined:
    Nov 8, 2017
    Posts:
    2
    Hello,

    I'm looking for a quick way to update a texture without using the slow Texture2D.LoadImage();. So i have found the NativePlugin exemple and try to modify it to use an IntPtr from a Unity Texture2D and pass it to glTexSubImage2D function.

    Here is my code :

    Unity side :
    Code (CSharp):
    1.  
    2. public Texture2D textureToApply;
    3. public Texture2D textureToApply2;
    4. private void CreateTextureAndPassToPlugin()
    5.     {
    6.         Texture2D tex = new Texture2D(1024,1024,TextureFormat.RGBA32, false);
    7.         Texture2D tex2 = new Texture2D(1024,1024,TextureFormat.RGBA32, false);
    8.         tex.Apply();
    9.  
    10.         GetComponent<Renderer>().material.mainTexture = tex;
    11.  
    12.         //copy textureToApply2 pixel into tex2 to be sure to have the same TextureFormat (RGBA32)
    13.         tex2.SetPixels(textureToApply2.GetPixels());
    14.         tex2.Apply();
    15.        
    16.         GetComponent<Renderer>().material.mainTexture = tex;
    17.         //Send informations about the original texture to the DLL
    18.        SetTextureFromUnity (tex.GetNativeTexturePtr(), tex.width, tex.height);
    19.        //Send the IntPtr of the texture to apply
    20.         UpdateFramePointer(tex2.GetNativeTexturePtr());
    21.  
    22.     }
    OpenGL side :
    Code (CSharp):
    1.  
    2. static void* g_FramePointer = NULL;
    3. extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UpdateFramePointer(void* frame)
    4. {
    5.     // IntPtr of the texture to apply in OpenGL
    6.     g_FramePointer = frame;
    7. }
    8.  
    9. static void* g_TextureHandle = NULL;
    10. static int   g_TextureWidth = 0;
    11. static int   g_TextureHeight = 0;
    12.  
    13. extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API SetTextureFromUnity(void* textureHandle, int w, int h)
    14. {
    15.     // Pass the informations about the original texture.
    16.     g_TextureHandle = textureHandle;
    17.     g_TextureWidth = w;
    18.     g_TextureHeight = h;
    19.  
    20. }
    21. static void update_rgb_texture()
    22. {
    23.  
    24.     if (g_FramePointer == NULL)
    25.         return;
    26.     void* textureHandle = g_TextureHandle;
    27.     int width = g_TextureWidth;
    28.     int height = g_TextureHeight;
    29.     if (!textureHandle)
    30.         return;
    31.  
    32.     int textureRowPitch;
    33.     void* textureDataPtr = s_CurrentAPI->BeginModifyTexture(textureHandle, width, height, &textureRowPitch);
    34.     if (!textureDataPtr)
    35.         return;
    36.  
    37.     isUpdated = true;
    38.    
    39.     s_CurrentAPI->EndModifyTexture(textureHandle, width, height, textureRowPitch, g_FramePointer);
    40.  
    41.     delete[](unsigned char*)g_FramePointer;
    42. }
    I'm a bit lost, i have searched for 2 days but i can't find a way to make it work.
    If someone have an idea to solve my problem.
    It's would be nice.

    Thanks,
    Rémy