首页 > 解决方案 > unity原生OpenGL纹理显示四次

问题描述

我目前面临一个我根本不明白的问题。我使用 ARCore 进行由内而外的跟踪任务。因为我需要做一些额外的图像处理,所以我使用 Unitys 功能来加载原生 c++ 插件。在每一帧的最后,我将 YUV_420_888 格式的图像作为原始字节数组传递给我的原生插件。

纹理句柄在组件初始化开始时创建:

private void CreateTextureAndPassToPlugin()
{

    Texture2D tex = new Texture2D(640, 480, TextureFormat.RGBA32, false);

    tex.filterMode = FilterMode.Point;
    tex.Apply();
    debug_screen_.GetComponent<Renderer>().material.mainTexture = tex;

    // Pass texture pointer to the plugin
    SetTextureFromUnity(tex.GetNativeTexturePtr(), tex.width, tex.height);
}

由于我只需要灰度图像,我基本上忽略了图像的 UV 部分,只使用如下所示的 y 坐标:

uchar *p_out;
int channels = 4;
for (int r = 0; r < image_matrix->rows; r++) {
    p_out = image_matrix->ptr<uchar>(r);
    for (int c = 0; c < image_matrix->cols * channels; c++) {
        unsigned int idx = r * y_row_stride + c;
        p_out[c] = static_cast<uchar>(image_data[idx]);
        p_out[c + 1] = static_cast<uchar>(image_data[idx]);
        p_out[c + 2] = static_cast<uchar>(image_data[idx]);
        p_out[c + 3] = static_cast<uchar>(255);
    }
}

然后将每一帧图像数据放入一个 GL 纹理中:

GLuint gltex = (GLuint)(size_t)(g_TextureHandle);
glBindTexture(GL_TEXTURE_2D, gltex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 640, 480, GL_RGBA, GL_UNSIGNED_BYTE, current_image.data);

我知道我通过创建纹理并将其作为 RGBA 传递使用了太多内存,但由于 OpenGL ES3 不支持 GL_R8 并且 GL_ALPHA 总是导致内部 OpenGL 错误,我只是将灰度值传递给每个颜色分量。

然而最终纹理被渲染,如下图所示:

图片

起初我认为,造成这种情况的原因可能在于其他通道具有相同的值,但是将除第一个通道之外的所有其他通道设置为任何值都不会产生任何影响。

我是否错过了一些明智的 OpenGL 纹理创建?

标签: androidc++unity3dopengl-es

解决方案


YUV_420_888 是一个多平面纹理,其中亮度平面每个像素只包含一个通道。

for (int c = 0; c < image_matrix->cols * channels; c++) {
    unsigned int idx = r * y_row_stride + c;

您的循环边界假设c是 4 个通道的倍数,这对于输出表面是正确的,但是在计算输入表面索引时也可以使用它。您使用的输入表面平面仅包含一个通道,因此idx是错误的。

通常,您还会多次写入相同的内存 - 循环c每次迭代都会递增 1,但您随后会写入cc+1c+2c+3因此会覆盖您上次写入的三个值。

更简短的答案 - 您的 OpenGL ES 代码很好,但我认为您正在用错误的数据填充纹理。

未经测试,但我认为您需要:

for (int c = 0; c < image_matrix->cols * channels; c += channels) {
    unsigned int idx = (r * y_row_stride) + (c / channels);

推荐阅读