我目前正在为Unity制作一个Android播放器插件。基本思想是,我将在 Android 上通过MediaPlayer
播放视频,它提供了一个setSurface
API,接收SurfaceTexture
作为构造函数参数,并最终与 OpenGL-ES 纹理绑定。在大多数其他情况下,例如显示图像,我们可以将此纹理以指针/ID的形式发送到Unity,在那里调用Texture2D.CreateExternalTexture
以生成Texture2D
对象并将其设置为UIGameObject
以渲染图片。但是,在显示视频帧时,情况略有不同,因为在Android上播放的视频需要GL_TEXTURE_EXTERNAL_OES
类型的纹理,而Unity仅支持通用类型GL_TEXTURE_2D
。
为了解决这个问题,我已经用谷歌搜索了一段时间,知道我应该采用一种叫做"渲染到纹理">的技术。更清楚的说法是,我应该生成 2 个纹理,一个用于 Android 中的MediaPlayer
和SurfaceTexture
以接收视频帧,另一个用于 Unity,其中也应该包含图片数据。第一个应该是GL_TEXTURE_EXTERNAL_OES
类型(简称OES纹理),第二个应该是GL_TEXTURE_2D
类型(我们称之为2D纹理)。这两个生成的纹理在开始时都是空的。当与MediaPlayer
绑定时,OES纹理会在视频播放过程中更新,然后我们可以使用FrameBuffer
在2D纹理上绘制OES纹理的内容。
我已经编写了此过程的纯Android版本,当我最终在屏幕上绘制2D纹理时,它运行良好。但是,当我将其发布为Unity Android插件并在Unity上运行相同的代码时,不会显示任何图片。相反,它只显示来自glClearColor
的预设颜色,这意味着两件事:
FrameBuffer
-> 2D 纹理的- 传输过程已完成,Unity 会收到最终的 2D 纹理。因为只有当我们绘制OES纹理的内容
FrameBuffer
时,才会调用glClearColor
。 glClearColor
之后在绘图过程中发生了一些错误,因为我们看不到视频帧图片。事实上,我也在绘制之后和与FrameBuffer
解除绑定之前调用glReadPixels
,这将从我们绑定的FrameBuffer
读取数据。它返回的单色值与我们在glClearColor
中设置的颜色相同。
为了简化我应该在这里提供的代码,我将通过FrameBuffer
绘制一个三角形到 2D 纹理。如果我们能找出哪个部分是错误的,那么我们就可以轻松解决绘制视频帧的类似问题。
该函数将在 Unity 上调用:
public int displayTriangle() {
Texture2D texture = new Texture2D(UnityPlayer.currentActivity);
texture.init();
Triangle triangle = new Triangle(UnityPlayer.currentActivity);
triangle.init();
TextureTransfer textureTransfer = new TextureTransfer();
textureTransfer.tryToCreateFBO();
mTextureWidth = 960;
mTextureHeight = 960;
textureTransfer.tryToInitTempTexture2D(texture.getTextureID(), mTextureWidth, mTextureHeight);
textureTransfer.fboStart();
triangle.draw();
textureTransfer.fboEnd();
// Unity needs a native texture id to create its own Texture2D object
return texture.getTextureID();
}
2D 纹理初始化:
protected void initTexture() {
int[] idContainer = new int[1];
GLES30.glGenTextures(1, idContainer, 0);
textureId = idContainer[0];
Log.i(TAG, "texture2D generated: " + textureId);
// texture.getTextureID() will return this textureId
bindTexture();
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D,
GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D,
GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D,
GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D,
GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
unbindTexture();
}
public void bindTexture() {
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureId);
}
public void unbindTexture() {
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
}
Triangle
draw()
:
public void draw() {
float[] vertexData = new float[] {
0.0f, 0.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f
};
vertexBuffer = ByteBuffer.allocateDirect(vertexData.length * 4)
.order(ByteOrder.nativeOrder())
.asFloatBuffer()
.put(vertexData);
vertexBuffer.position(0);
GLES30.glClearColor(0.0f, 0.0f, 0.9f, 1.0f);
GLES30.glClear(GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
GLES30.glUseProgram(mProgramId);
vertexBuffer.position(0);
GLES30.glEnableVertexAttribArray(aPosHandle);
GLES30.glVertexAttribPointer(
aPosHandle, 3, GLES30.GL_FLOAT, false, 12, vertexBuffer);
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 3);
}
Triangle
的顶点着色器:
attribute vec4 aPosition;
void main() {
gl_Position = aPosition;
}
Triangle
的片段着色器:
precision mediump float;
void main() {
gl_FragColor = vec4(0.9, 0.0, 0.0, 1.0);
}
TextureTransfer
的关键代码:
public void tryToInitTempTexture2D(int texture2DId, int textureWidth, int textureHeight) {
if (mTexture2DId != -1) {
return;
}
mTexture2DId = texture2DId;
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mTexture2DId);
Log.i(TAG, "glBindTexture " + mTexture2DId + " to init for FBO");
// make 2D texture empty
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, textureWidth, textureHeight, 0,
GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
Log.i(TAG, "glTexImage2D, textureWidth: " + textureWidth + ", textureHeight: " + textureHeight);
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
fboStart();
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0,
GLES30.GL_TEXTURE_2D, mTexture2DId, 0);
Log.i(TAG, "glFramebufferTexture2D");
int fboStatus = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER);
Log.i(TAG, "fbo status: " + fboStatus);
if (fboStatus != GLES30.GL_FRAMEBUFFER_COMPLETE) {
throw new RuntimeException("framebuffer " + mFBOId + " incomplete!");
}
fboEnd();
}
public void fboStart() {
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOId);
}
public void fboEnd() {
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
}
最后是 Unity 端的一些代码:
int textureId = plugin.Call<int>("displayTriangle");
Debug.Log("native textureId: " + textureId);
Texture2D triangleTexture = Texture2D.CreateExternalTexture(
960, 960, TextureFormat.RGBA32, false, true, (IntPtr) textureId);
triangleTexture.UpdateExternalTexture(triangleTexture.GetNativeTexturePtr());
rawImage.texture = triangleTexture;
rawImage.color = Color.white;
好吧,上面的代码不会显示预期的三角形,而只会显示蓝色背景。我在几乎每次 OpenGL 函数调用后都会添加glGetError
,同时不会抛出任何错误。
我的 Unity 版本是 2017.2.1。对于 Android 构建,我关闭了实验性多线程渲染,其他设置都是默认的(没有纹理压缩,不使用开发构建等)。我的应用的最低 API 级别为 5.0 棒棒糖,目标 API 级别为 9.0 Pie。
我真的需要一些帮助,提前感谢!
现在我找到了答案:如果你想在你的插件中做任何绘图工作,你应该在原生层做。所以如果你想做一个Android插件,你应该在JNI
而不是Java端调用OpenGL-ES API。原因是 Unity 只允许在其渲染线程上绘制图形。如果你只是像我在Java端所做的那样调用OpenGL-ES API,它们实际上将在Unity主线程上运行,而不是渲染线程。Unity 提供了一种方法,GL.IssuePluginEvent
,用于在渲染线程上调用您自己的函数,但它需要本机编码,因为此函数需要函数指针作为其回调。下面是一个简单的使用它的示例:
JNI
边:
// you can copy these headers from https://github.com/googlevr/gvr-unity-sdk/tree/master/native_libs/video_plugin/src/main/jni/Unity
#include "IUnityInterface.h"
#include "UnityGraphics.h"
static void on_render_event(int event_type) {
// do all of your jobs related to rendering, including initializing the context,
// linking shaders, creating program, finding handles, drawing and so on
}
// UnityRenderingEvent is an alias of void(*)(int) defined in UnityGraphics.h
UnityRenderingEvent get_render_event_function() {
UnityRenderingEvent ptr = on_render_event;
return ptr;
}
// notice you should return a long value to Java side
extern "C" JNIEXPORT jlong JNICALL
Java_com_abc_xyz_YourPluginClass_getNativeRenderFunctionPointer(JNIEnv *env, jobject instance) {
UnityRenderingEvent ptr = get_render_event_function();
return (long) ptr;
}
在Android Java端:
class YourPluginClass {
...
public native long getNativeRenderFunctionPointer();
...
}
在团结方面:
private void IssuePluginEvent(int pluginEventType) {
long nativeRenderFuncPtr = Call_getNativeRenderFunctionPointer(); // call through plugin class
IntPtr ptr = (IntPtr) nativeRenderFuncPtr;
GL.IssuePluginEvent(ptr, pluginEventType); // pluginEventType is related to native function parameter event_type
}
void Start() {
IssuePluginEvent(1); // let's assume 1 stands for initializing everything
// get your texture2D id from plugin, create Texture2D object from it,
// attach that to a GameObject, and start playing for the first time
}
void Update() {
// call SurfaceTexture.updateTexImage in plugin
IssuePluginEvent(2); // let's assume 2 stands for transferring TEXTURE_EXTERNAL_OES to TEXTURE_2D through FrameBuffer
// call Texture2D.UpdateExternalTexture to update GameObject's appearance
}
您仍然需要转移纹理,并且有关它的所有内容都应该在JNI
层进行。但别担心,它们与我在问题描述中所做的几乎相同,但只是使用与 Java 不同的语言,并且有很多关于此过程的材料,因此您肯定可以做到。
最后让我再次解决解决这个问题的关键:在原生层做你的原生东西,不要沉迷于纯 Java......我非常惊讶没有博客/答案/维基告诉我们只是用C++编写代码。虽然有一些开源实现,如谷歌的gvr-unity-sdk,它们提供了一个完整的参考,但你仍然会怀疑也许你可以在不编写任何C++代码的情况下完成任务。现在我们知道我们不能。但是,说实话,我认为Unity有能力使这一进展变得更加容易。