Hello, I am trying to get the data from the RenderTexture
, encode it to jpg and send it over the network.
I tried using ReadPixels()
- it worked, but it was performance expensive, so I tried CopyTexture()
. It copies the texture with no problem, but after encoding it to JPG it is just a gray image.
sourceTexture
is my RenderTexture
Texture2D newTexture = new Texture2D(
sourceTexture.width, sourceTexture.height,
TextureFormat.ARGB32,
false
);
CopyTextureSupport copyTextureSupport = SystemInfo.copyTextureSupport;
if(copyTextureSupport.HasFlag(CopyTextureSupport.RTToTexture))
{
Graphics.CopyTexture(sourceTexture, newTexture);
//This works
//TestRawImage.texture = newTexture;
}
else
{
RenderTexture.active = sourceTexture;
newTexture.ReadPixels(
new Rect(0, 0, RenderTexture.active.width, RenderTexture.active.height),
0, 0
);
}
My network message
ImageMessage imageMessage = new ImageMessage()
{
ID = currentID,
JPGBytes = newTexture.EncodeToJPG()
};
This displays gray image
newTexture.LoadImage(imageMessage.JPGBytes);
TestRawImage.texture = newTexture;