该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

用的是Unity2017 版本,

Android生成一个texture2D 纹理,然后传给unity 的模型,然后绑定到framebuffer ,往frambuffer 里画图,unity的模型无法显示,折腾了两周了,还是找不到原因,求大神解惑,万分感谢。

Android code:

生成一个2D 的mUnityTextureID , 给它初始化一张图片的纹理,并绑定到Framebuffer 上,

public void GenUnityTexturePtr(Context context)

{

Log.d(TAG, "GenUnityTexturePtr");

int id=R.drawable.img3d;

BitmapFactory.Options options=new BitmapFactory.Options();

options.inPreferredConfig= Bitmap.Config.ARGB_8888

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),id,options);

int textures[] = new int[1];

GLES20.glGenTextures(1, textures, 0);

mUnityTextureID = textures[0];

GLES20.glActiveTexture(GLES20.GL_TEXTURE1)

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mUnityTextureID

GLUtils.texImage2D(GLES20.GL_TEXTURE_2D,0,bitmap,0);

GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);

GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);

GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE)

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

bitmap.recycle()

Log.d(TAG, "texture id returned: " + mUnityTextureID)

生成framebuffer 并绑定纹理I D mUnityTextureID

//framebuffer

int buffers[] = new int[1];

GLES20.glGenFramebuffers(1, buffers, 0);

mIdFBO = buffers[0];

GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mIdFBO);

GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, mUnityTextureID, 0

}

然后使用shader 画图,比如画一个红色的四边形,shader 画图的代码没有问题,直接画到GLSurfaceView 上是能成功显示的。并且这时候,将纹理id mUnityTextureID使用shader 采样后画到GLSurfaceView 上也是能显示的。

但Unity 这边拿到mUnityTextureID纹理给到模型上,就无法显示

Unity code

Int32 texPtr = mCurrentActivity.Call ("GetUnityTexturePtr");

Debug.Log("texture pointer? " + texPtr);

int texWidth=mCurrentActivity.Call("GetTextureWidth");

int texHeight=mCurrentActivity.Call("GetTextureHeight");

Debug.Log("CreateExternalTexture texture width:" + texWidth+"height:"+texHeight);

Texture2D textureid = new Texture2D (texWidth,texHeight, TextureFormat.ARGB32, false);

Texture2D nativeTexture = Texture2D.CreateExternalTexture (texWidth, texHeight, TextureFormat.ARGB32 , false, false, new System.IntPtr(texPtr));

Debug.Log("CreateExternalTexture textureid:" + nativeTexture);

textureid.UpdateExternalTexture (nativeTexture.GetNativeTexturePtr());

GetComponent().material.mainTexture = textureid;

在update 方法中(或者onPostRender 中,都试过),每一帧都重新画图,并调用GL.InvalidateState

// Update is called once per frame

void Update () {

if (initobject) {

mCurrentActivity.Call ("updateTexture");

GL.InvalidateState();

}

}

结果死活显示不了,百思不得其解,求大神带带

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐