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 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;

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.

Hello~ I meet a similar problem recently, and I found this post. But I don’t understand clearly “You will have to use this one to save your JPG to disk.” could anyone explain in detail~