Within the editor, I’m trying to instantiate a Camera, capture it’s frame to a premade render texture, blit these onto a temporary texture, then send bytes from the temporary texture to a png file. Ultimately, I’m trying to get a specialized screenshot of objects when I press a certian button in edit mode. Using Unity 2021.3.6f1 fwiw
Where I’m running into issues seems to be when I actually call tex.EncodeToPNG()
The reason I believe this is that I’m storing the intermediate texture as a public serialized variable, and inspecting it, and it looks OK. The png produced is really really dark, though colors are still distinguishable.
I’ve included pictures of the produced png and the intermediate texture.
I’ve tried using several other graphics formats for my render texture (while keeping them in sync to the Texture2D), none of them seem to make a difference.
I suspect this has to do with colorspace/sRGB, but MAN does Unity make this hard to understand.
There’s RenderTextureFormat, DefaultFormat, GraphicsFormat and TextureFormat, all of which contain enum names which aren’t consistent or clearly intercompatible.
but it’s not at all clear to me which RenderTextureFormat I should use and how to build a Texture2D which is compatible with my selected RenderTextureFormat… Why can’t these just share one enum???
RenderTextures are objects that live in GPU video memory. They are not compressed and cannot be serialized to disk. Texture2D however lives in CPU memory and can be compressed, so they have a very different set of formats available. These are what RenderTextureFormat and TextureFormat are, respectively.
DefaultFormat returns the default platform format for a specific kind of texture content (HDR, LDR, stencil, depth, etc).
GraphicsFormat is relatively new, and I suspect it’s supposed to eventually replace both RenderTextureFormat and TextureFormat.
Your graphics format is GraphicsFormat.R8G8B8A8_UNorm, which is a normalized format with 8 bit precision for each channel. Instead of using this to create your Texture2D, try using rTex.graphicsFormat.
I am running into the same issue as you have, Gwelkind. Would it be possible to speak more on how you have managed to create a Texture2D with graphicsFormat rather than with a TextureFormat?
I had some success with creating a CustomRenderTexture that has the SRGB bool set to true. It has the interesting effect of displaying an image in editor that is too light, but encoding a PNG that is visually correct.