小虾米资讯
AD1android:[求助]Android往绑定unity texture 的frambuffer 里画图无法显示
2022-07-21 20:44:46 / 我要吐槽AD2
用的是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_8888Bitmap 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, mUnityTextureIDGLUtils.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//framebufferint 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 <Int32> ("GetUnityTexturePtr");Debug.Log("texture pointer? " + texPtr);int texWidth=mCurrentActivity.Call<int>("GetTextureWidth");int texHeight=mCurrentActivity.Call<int>("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<Renderer>().material.mainTexture = textureid;在update 方法中(或者onPostRender 中,都试过),每一帧都重新画图,并调用GL.InvalidateState// Update is called once per framevoid Update () {if (initobject) {mCurrentActivity.Call ("updateTexture");GL.InvalidateState(); }}结果死活显示不了,百思不得其解,求大神带带搞定了,没人理,果然还是得靠自己
应该是线程间,纹理共享,以及上下文 等问题吧。
楼主怎么解决的?我这边也遇到同样问题。获取到textureId,感觉Android这边没啥问题,但是在unity那边操作完显示的是原始场景。想要的图片没有显示出来。
在吗 请问你本地能拿到fbo,unity那个问题是怎么解决的
Powered By © 小虾米资讯 2015
(本站部分文章来源于网络或网友爆料,不代表本站观点,如有侵权请联系及时删除 )