Android插件中的SurfaceTexture在Unity中不起作用

pal*_*one 5 android opengl-es unity-game-engine textureview android-unity-plugin

我无法将纹理绑定到a SurfaceTexture以在Unity中显示.

更新4:基于更新1中的管道(表面 - >外部纹理通过表面纹理 - > fbo - >纹理2d)我知道SurfaceTexture没有正确地将其表面转换为纹理.我可以通过pixelcopy从其表面获得正确的绘制图片,我可以确认我的FBO绘图到texture2d管道使用一些测试颜色.所以问题是,为什么SurfaceTexture不能将其表面转换为纹理?

Texture在Java中生成一个并将其指针传递回Unity:

public void initGLTexture()
{
    Log.d("Unity", "initGLTexture");
    int textures[] = new int[1];
    GLES20.glGenTextures(1, textures, 0);
    mTextureId = textures[0];

    GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureId);
    GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
Run Code Online (Sandbox Code Playgroud)

SurfaceTexture从id(在Java中)创建一个:

mSurfaceTexture = new SurfaceTexture(mTextureId);
mSurfaceTexture.setDefaultBufferSize(512, 512);
Run Code Online (Sandbox Code Playgroud)

我使用第三方库GeckoView渲染到SurfaceTexture的Surface上.我从Unity调用以下方法OnRenderObject()来保持所有GL渲染在同一个线程上:

mSurfaceTexture.updateTexImage();
Run Code Online (Sandbox Code Playgroud)

我知道上面的代码可以正确地绘制到表面上.

我在Unity中调用以下内容来加载纹理:

_imageTexture2D = Texture2D.CreateExternalTexture(
        512,512,TextureFormat.RGBA32,false,true,(IntPtr) mTextureId);
_rawImage.texture = _imageTexture2D;
Run Code Online (Sandbox Code Playgroud)

为什么RawImage应用纹理只显示这个看起来精灵的东西,它应该是一个网页?

质地

更新1:所以我一直在研究以下假设:使用Gecko绘制到Surface,并使用SurfaceTexture将此表面渲染为a GL_TEXTURE_EXTERNAL_OES.因为我不能在Unity上显示它(不知道为什么)我将这个纹理绘制到帧缓冲区并将帧缓冲区中的像素复制到a GL_TEXTURE_2D.我在texture_2d中获得了一个网页(在带有imageview的模拟器中glReadPixels).但是,当我将工作导入Unity以测试管道是否可以到目前为止我只是得到一个黑屏.我可以通过PixelCopyapi 获取表面图像.

这是我的FBO概述代码 - 我的渲染代码来自grafika的texture2D程序:

    // bind display buffer
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBufferId);
    GlUtil.checkGlError("glbindframebuffer");

    // unbind external texture to make sure it's fresh
    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
    GlUtil.checkGlError("glunbindexternaltex");

   // bind source texture (done in drawFrame as well )
    GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mOffscreenTextureId);
    GlUtil.checkGlError("glBindFramebuffer");

    // draw to frame buffer
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);    // again, only really need to
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);     //  clear pixels outside rect

    mFullScreen.drawFrame(mOffscreenTextureId, mIdentityMatrix);

    // unbind source texture
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,0);
    GlUtil.checkGlError("glBindTexture2d");

    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
    GlUtil.checkGlError("glunbindexternaltex");

    // make sure we're still bound to fbo
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBufferId);
    GlUtil.checkGlError("glBindTexture2d");

    // copy pixels from frame buffer to display texture
     GLES20.glCopyTexImage2D(GLES20.GL_TEXTURE_2D,0,GLES20.GL_RGBA,0,0,512,512,0);

    // read pixels from the display buffer to imageview for debugging
    BitmapDisplay.mBitmap = SavePixels(0,0,512,512);

    // unbind texture
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,0); 
Run Code Online (Sandbox Code Playgroud)

这是我的播放器设置>其他: 设置

更新2:可能尝试的管道:通过接口在C++中调用外部纹理的绘制函数到FBO(附加到Unity的texture_2d).

更新3:从本机代码调用Java函数,负责将纹理从SurfaceTexture绘制到FBO到Unity的纹理,通过GL.IssuePluginEvent产生黑色纹理,如第一次更新.它将在模拟器中显示图像,但在Unity中不显示.

小智 2

几个月前,我不得不执行类似的任务,并发现正确的管道是在 Unity 中创建纹理,在 C 中获取本机指针,最后在 Java 层中更新它。

请看一下这个示例项目,它应该会给您带来不同的视角。

https://github.com/robsondepaula/unity-android-native-camera

问候