[求助]Android往绑定unity texture 的frambuffer 里画图无法显示
unity3d吧
全部回复
仅看楼主
level 2
printfire3 楼主
用的是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 <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 frame
void Update () {
if (initobject) {
mCurrentActivity.Call ("updateTexture");
GL.InvalidateState();
}
}
结果死活显示不了,百思不得其解,求大神带带
2017年09月12日 13点09分 1
level 2
printfire3 楼主
搞定了,没人理,果然还是得靠自己
2017年09月13日 13点09分 2
请问有完整的方法吗? 挺急的.... q:1226341090
2022年07月21日 11点07分
level 1
应该是线程间,纹理共享,以及上下文 等问题吧。
2017年10月09日 05点10分 4
level 1
楼主怎么解决的?我这边也遇到同样问题。获取到textureId,感觉Android这边没啥问题,但是在unity那边操作完显示的是原始场景。想要的图片没有显示出来。
2018年11月13日 03点11分 5
1