使用GL_TEXTURE_EXTERNAL_OES在Unity纹理上渲染Android Camera Preview

时间:2016-12-23 21:21:37

标签: c# android unity3d opengl-es camera

我正在尝试从Unity-Texture上的java android插件渲染相机预览。 Android插件在后台运行,因此没有视图或活动。在没有视图的情况下获取Cameradata的唯一方法是使用SurfaceTexture(请参阅this Question)。

由于性能原因,我的计划是使用OpenGL直接在Unity-Texture上呈现Android代码中的SurfaceTexture。我没有得到任何编译/运行时错误,但Unity内部的纹理只是保持白/黑。

这是我的代码:

Android-Library(CameraFeed.java):

public class CameraFeed implements SurfaceTexture.OnFrameAvailableListener {

private SurfaceTexture mTexture;
private Camera mCamera;
private int unityTextureID;

public CameraFeed() {

    int textures[] = new int[1];
    // reserve an ID for the GL-Texture..
    GLES20.glGenTextures(1, textures, 0);
    unityTextureID = textures[0];

    // Bind texture to EXTERNAL_OES
    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, unityTextureID);

    // Create a AndroidSurface Texture with the Unity Texture ID
    mTexture = new SurfaceTexture(unityTextureID);

    // Create Camera and open it
    mCamera = Camera.open();
    mCamera.startPreview();
    try {
        // Bind the Unity Texture to the Camera-Frame
        mCamera.setPreviewTexture(mTexture);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // onFrameAvailable of this Class will be called when frame is ready..
    mTexture.setOnFrameAvailableListener(this);
}

// Offer the Handle to the texture to Unity-Scripts
public int getUnityTextureID() {
    return unityTextureID;
}

@Override
public void onFrameAvailable(SurfaceTexture surfaceTexture) {
    // update when new frame is here..
    surfaceTexture.updateTexImage();
}
}

Unity-C#脚本(CameraFeed.cs):

public class CameraFeed : MonoBehaviour {
private Texture2D nativeTexture;
private AndroidJavaObject javaCameraFeed;

void Start () {
    javaCameraFeed = new AndroidJavaObject("com.oberloestern.grabhuegel.arlibs3.CameraFeed");

    Int32 texPtr = javaCameraFeed.Call <Int32> ("getUnityTextureID");
    Debug.Log("texture pointer? " + texPtr);

    nativeTexture = Texture2D.CreateExternalTexture (128, 128, TextureFormat.RGBA32 , false, false, new System.IntPtr(texPtr));
}

void Update () {

}
void OnGUI() {
    // Draw the Texture from Android in the middle of the screen
    GUI.DrawTexture(new Rect(50, 50, Screen.width - 100, Screen.height - 100), nativeTexture);
}
}

我已设法使用GLES20.GL_TEXTURE_2D向Unity发送位图纹理,但SurfaceTexture必须通过GLES11Ext.GL_TEXTURE_EXTERNAL_OES发送...

有人知道如何让这个工作吗?

0 个答案:

没有答案