Unity插件不工作在OpenGL 4.1,工作与OpenGL 2.1

本文关键字:工作 OpenGL 插件 Unity | 更新日期: 2023-09-27 18:17:22

我有一个关于我正在编写的小Unity插件的问题。

插件需要一个Unity3D Texture2D,并试图更新它,编译的插件工作时运行Unity3D与-force-opengl(运行在OpenGL 2.1),但它在正常模式下运行时不工作(OpenGL 4.1)。我还遗漏了什么吗?

插件应该更新内部OpenGL纹理并设置它的数据。插件不会崩溃游戏或任何东西,但纹理更新时运行在2.1,但只是显示一个灰色纹理在4.1

在Unity自己的示例中,他们使用GLEW初始化OpenGL上下文(https://bitbucket.org/Unity-Technologies/graphicsdemos/src/548c5251ddbe82129b2584992a0f50caa4c34c6c/NativeRenderingPlugin/PluginSource/source/RenderAPI_OpenGLCoreES.cpp?at=default&fileviewer=file-view-default)但是OpenGL上下文不应该已经存在于插件中吗?关于这一点,OpenGL版本之间有什么不同吗?

我正在运行OSX El Capitan。

以下是相关的代码片段:

C插件代码

#include <stdio.h>
#include <inttypes.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include "unity/IUnityInterface.h"
#include "debug.c"
void RenderTexture (void* texId, int time) {
    GLuint gltex = (GLuint)(size_t)(texId);
    int id, i, j;
    GLubyte img[64 * 64 * 4];
    for (i = 0; i < 64; i++) {
        for (j = 0; j < 64; j++) {
            id = (4 * j) + (4 * 64 * i);
            img[id] = (GLubyte) time % 255;
            img[id + 1] = (GLubyte) time % 255;
            img[id + 2] = (GLubyte) (255 - time) % 255;
            img[id + 3] = (GLubyte) (255 - time) % 255;
        }
    }
    glBindTexture(GL_TEXTURE_2D, gltex);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 64, 64, GL_RGBA, GL_UNSIGNED_BYTE, img);
}
c# Unity代码
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System;
using System.IO;
public class TestBehaviour : MonoBehaviour {
    [DllImport("main")]
    private static extern void RenderTexture (IntPtr ptrTexture, int time);
    Texture2D tex;
    void Start () {
        tex = new Texture2D (64, 64, TextureFormat.ARGB32, false);
        tex.Apply ();
        GetComponent<Renderer> ().material.SetTexture ("_MainTex", tex);
    }
    void Update () {
        RenderTexture (tex.GetNativeTexturePtr (), (int)Time.realtimeSinceStartup * 10);
    }
}

Unity插件不工作在OpenGL 4.1,工作与OpenGL 2.1

看看文档,似乎使用-force-opengl与Unity使用传统的OpenGL。用-force-glcore试试,看看是否得到不同的结果。

如果将该命令放在条件语句中,如

if (version <= 2.1) then       -force-opengl
因此,它不会在new OpenGL
中执行

我不会假装真正的帮助,但我不能评论,所以。

首先:你在c#代码中使用ARGB格式,在C代码中使用RGBA格式,这真的很奇怪。错了,还是我弄错了?

第二件事,你甚至没有试图去理解到底是哪里出了问题。
使用GLenum error = glGetError();获取OpenGL状态机中发生的最新可能错误,然后查看glTexSubimage2d文档以查看可能错误及其原因的列表。遵循这些简单的步骤可能会帮助你了解你做错了什么,如果你没有做错任何事情,那么这是Unity方面的问题。

事实上,这个问题有很多可能的原因,没有glError信息只会让您没有进一步调查的希望。