Search code examples
openglunity-game-enginetextures

Converting an opengl screen to bytes and back to Texture in unity


I have a opengl software which genertes a window and then sends its bytes through a socket to unity. The opengl software retrieves the data:

glreadPixels(0,0,width,height,GL_RGB,GL_UNSIGNED_BYTE, buffer);

where 'buffer' is byte[] buffer which is being sent to the unity app. There I try to convert it back to an image by doing:

Texture2D t = new Texture(width,hight,Textureformat.ETC2_RGB,false);
t.LoadRawTextureData(buffer);
t.Applay();

However the texture apears as black screen with colorful stains which are moving and disapearing cyclically. I chose the 'ETC2_RGB' format because it looked relevant, but I also tried some more formats from TextureFormat, and all of them looked white\black with colorful dots\pink, etc. What are the right convertions that I should choose?


Solution

  • You're using the wrong texture format. ETC2_RGB is a compressed format with 4 bits per pixel. What you query from glReadPixel is a uncompressed format with 8 bits per channel. The format for this is Textureformat.RGB24.