I'm working on a project that involves players taking screenshots with an in-game camera.
After some digging, the approach I decided to go with involves using a render texture, reading it's pixels and saving the result as a PNG. The issue I'm having is that the images all end up darker or more washed out than what I see when I run the project.
My first guess was that it had to do with the color format, but both the render texture and texture format for the Texture2D are ARGB32.
public IEnumerator SaveCameraView()
yield return new WaitForEndOfFrame();
RenderTexture rendText= RenderTexture.active;
RenderTexture.active = screenShotCam.targetTexture;
Texture2D cameraImage= new Texture2D(screenShotCam.targetTexture.width, screenShotCam.targetTexture.height, TextureFormat.ARGB32, false);
cameraImage.ReadPixels(new Rect(0, 0, screenShotCam.targetTexture.width, screenShotCam.targetTexture.height), 0, 0);
RenderTexture.active = rendText;
// store the texture into a .PNG file
byte bytes = cameraImage.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/Screenshots/shot"+imageNum+".png" , bytes);
I'm thinking it might have something to do with the alpha channel, since the images look quite different depending on what I use to open them, but I'm not sure how to proceed.
Any help would be hugely appreciated. Thanks!
The screen does have it's own light (via a culling mask) with an intensity of 1 that might account for some of the difference, so I figure it's worth noting, but in any case, I'm wondering how to essentially capture what I see on that screen.
It's probably related to HDR, which I don't think gets encoded in the rendertextures. If you disable HDR on your camera and project, does that fix the problem?
Unfortunately no. I did have Tonemapping on the camera as a crude way of adjusting exposure, but even when I disable that and the HDR, I still get inconsistent images. Though now they are more contrasty.
The Tonemapping exposure adjustment seemed to be washing things out, so losing that at least resolved that issue, though I'll have to come up with something else to allow exposure adjustments.
Looks very much like a gamma/sRGB vs linear issue. Check `RenderTextureReadWrite`.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
4 People are following this question.
How to save a RenderTexture to a png from a different camera?
Wierd Screen Shot Problem, texture of screen shot looks different in editor and in system.
Save rendertexture to image in file
Problem with RenderTexture in Unity trial
Two cameras rendering to same viewport rect sometimes causes lower camera to render incorrectly