Unity android Texture Opengl ES 数据通信
来源:互联网 发布:中国贸易数据库 编辑:程序博客网 时间:2024/05/16 11:53
这段代码非常有启发意义
Can someone please help me figure out what is the problem with my code? I am trying to load an image from the native side and send the texture to Unity. I am using Unity Pro 5.0.2f1.
Unity Side:
void Start () {AndroidJavaObject mImageLoader = new AndroidJavaObject("com.saeid.android.LoadTexture2D");Texture2D texture2D = new Texture2D(1920, 1080, TextureFormat.ARGB32, false);Int32 texPtr = mImageLoader.Call <Int32> ("loadImageReturnTexturePtr", "/storage/sdcard0/Images/test.jpg");Debug.Log("texture pointer? " + texPtr);Texture2D nativeTexture = Texture2D.CreateExternalTexture (1920, 1080, TextureFormat.ARGB32 , false, false, (IntPtr)texPtr);texture2D.UpdateExternalTexture(nativeTexture.GetNativeTexturePtr());gameObject.GetComponent<Renderer>().material.mainTexture = texture2D;}
Java Side:
public int loadImageReturnTexturePtr(String imagePath) {Log.d(LOGTAG, "loading image1: " + imagePath);Bitmap bitmap = BitmapFactory.decodeFile(imagePath);Log.d(LOGTAG, "Bitmap is: " + bitmap);ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());bitmap.copyPixelsToBuffer(buffer);int textures[] = new int[1];GLES20.glGenTextures(1, textures, 0);int textureId = textures[0];GLES20.glActiveTexture(GLES20.GL_TEXTURE0);GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, 1920, 1080, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);GLES20.glTexParameteri(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);Log.d(LOGTAG, "texture id returned: " + textureId);return textureId;}
=======================================================AnswerSo, I figured it out... The code is actually correct. except the texture format in both side should be the same. In my case, I haveTextureFormat.ARGB32
(in unity side) andGLES20.GL_RGBA
(in Java side) which don't match. Also somehowGLES20.glTexImage2D(...)
didn't work for me. I replaced it withGLUtils.texImage2D(GLES20.GL_TEXTURE_2D,0, bitmap,0);
and finally I noticed the same code works on same Unity versions and doesn't work on some other. for example it is not working in 5.0.2f1 but it works in 5.0.3.
1 0
- Unity android Texture Opengl ES 数据通信
- Android OpenGL ES->Compressed Texture
- OpenGL ES texture fonts
- Android ApiDemos示例解析(201):Graphics->OpenGL ES->Compressed Texture
- openGL ES 2.0 笔记 texture
- Converting FFmpeg frame to OpenGL ES texture
- opengl es 2.0 Reder to texture iOS
- Android 中使用OpenGL ES进行2D开发(纹理Texture使用)
- android OpenGL ES实现渲染到透明的纹理 render to transparent texture
- Android 中使用OpenGL ES进行2D开发(纹理Texture使用)
- OpenGL ES 帧缓冲对象(FBO):Render to texture
- android opengl es 源码
- Android NDK opengl ES
- android的opengl es
- android opengl es--立方体
- Android OpenGL ES
- android opengl es
- Android OpenGL ES
- 第7章 高级分组
- PHP PSR-3 日志接口规范
- Java 多线程 并发编程
- 土地面积计算单位及其换算单位-亩分厘
- 7.1 基本的GROUP BY用法
- Unity android Texture Opengl ES 数据通信
- 泛型
- 7.2 HAVING子句
- Android 动态关闭Manifest中的Receiver的方法
- PHP PSR-4 Autoloader 自动加载
- 7.3 GROUP BY的“新”功能
- Java 集合类详解
- linux下安装mysql的详细教程
- Jquery制作--美化下拉框