Trying to set a texture from Java plugin in Unity 4.6

筅森魡賤 提交于 2020-01-15 15:34:32

问题


What i'm trying to do:

  • Pull an image from sd-card on phone using Java Plugin.
  • Unity passes a texture ID to plugin.
  • Plugin uses opengl to assign the image to the texture in Unity through the ID.
  • Will (eventually) be used to play a video clip from the phone in Unity, for now, it's just trying to change a texture outside of unity.

My issue:

When i call the method in the plugin, passing texture.GetNativeTextureID() into it, the texture does not change. I'm currently only using a simple black 50x50 texture for testing, and the original texture is a flat white.

I'm worried that i've missed something significant, as this is my first time working with Gl calls in java. Much of the answers to similar problems involve using native C++ instead of Java, but I can't find a concrete answer saying that C++ must be used. I'd like to do my best to avoid writing another set of plugins and plugin handlers for C++, but if it's the most efficient/only way to get this working, i'll do it as i'm not unfamiliar with OpenGL and C++

Code:

The plugin method is called from OnPreRender() in a script attached to the main camera:

if (grabTex) {
    int texPtr = m_VideoTex.GetNativeTextureID();
    Debug.Log( "texPtr = " + texPtr );
    m_JVInterface.SetTex( texPtr );
}

m_VideoTex is a basic Texture2D( 50, 50 ) with all pixels set to white, attached to the diffuse shader on the quad in the scene.

The Java plugin code is as follows:

public void SetTexture(Context cont, int _texPointer) {
    if (_texPointer != 0) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;
        options.inJustDecodeBounds = false;
        final Bitmap bitmap = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/black.jpg", options);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, _texPointer);

        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

        Log.i("VideoHandler", "Recieved ID: " + _texPointer);

        bitmap.recycle();
    }
}

回答1:


This is most likely a problem with the OpenGL Context. The easiest way would be to send the texture as raw bytes to Unity and then upload as texture inside Unity.



来源:https://stackoverflow.com/questions/30642503/trying-to-set-a-texture-from-java-plugin-in-unity-4-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!