- Home /
EncodeToJPG() creates gray image after using CopyTexture().
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 Encodimg it to JPG it is just a gray image.
Here is my code (sourceTexture is my RenderTexture):
Texture2D newTexture = new Texture2D(sourceTextture.width, sourceTextture.height, TextureFormat.ARGB32, false);
CopyTextureSupport copyTextureSupport = SystemInfo.copyTextureSupport;
if(copyTextureSupport.HasFlag(CopyTextureSupport.RTToTexture))
{
Graphics.CopyTexture(sourceTextture, newTexture);
//This works
//TestRawImage.texture = newTexture;
}
else
{
RenderTexture.active = sourceTextture;
newTexture.ReadPixels(new Rect(0, 0, RenderTexture.active.width, RenderTexture.active.height), 0, 0);
}
ImageMessage imageMessage = new ImageMessage()
{
ID = currentID,
JPGBytes = newTexture.EncodeToJPG()
};
//This displays gray image
newTexture.LoadImage(imageMessage.JPGBytes);
TestRawImage.texture = newTexture;
Answer by Remy_Unity · Mar 13, 2018 at 12:56 PM
Graphics.CopyTexture (or Graphics.Blit) works on GPU side, whereas ReadPixels works on CPU. That's why EncodetoJPG which is on CPU works after ReadPixels and not CopyTexture.
You will have to use this one to save your JPG to disk.
Your answer
Follow this Question
Related Questions
Texture2D to RenderTexture 1 Answer
Copy non-rectangle part of one texture to another 2 Answers
Save image from camera with post processing. 0 Answers
Graphics.DrawTexture in OnPostprocessTexture 1 Answer
GetPixels of RenderTexture 3 Answers