Unity android Texture Opengl ES 数据通信

 
 

这段代码非常有启发意义

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;
} 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值