• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by daomic · Apr 13, 2017 at 07:48 PM · camerarendertexturepng2dtexture

EndcodeToPNG resulting in dark image

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.

game view captured Screenshot

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;
 
         screenShotCam.Render();
 
         
         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);
         cameraImage.Apply();
         RenderTexture.active = rendText;
 
         // store the texture into a .PNG file
         byte[] bytes = cameraImage.EncodeToPNG();
 


         imageNum++;
         print (Application.dataPath);
         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!

ref1.png (75.0 kB)
shot2.png (49.6 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image daomic · Apr 13, 2017 at 07:46 PM 0
Share

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.

avatar image FortisVenaliter · Apr 13, 2017 at 07:50 PM 0
Share

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?

avatar image daomic FortisVenaliter · Apr 13, 2017 at 08:12 PM 0
Share

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.

avatar image jobigoud · Apr 19, 2017 at 05:58 PM 0
Share

Looks very much like a gamma/sRGB vs linear issue. Check `RenderTextureReadWrite`.

1 Reply

· Add your reply
  • Sort: 
avatar image
5

Answer by SpaceManDan · May 24, 2017 at 07:46 AM

I have been struggling with this same exact issue all night. Just figured it out.

  1. Create a RenderTexture in the Editor

  2. Check the box [sRGB (Color RenderTexture)]

Fixed. :D

Now use this render texture in your script and the color/gamma values should be good to go.alt text


colorrendertexture.png (436.7 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NightElfik · Feb 08, 2021 at 02:51 AM 0
Share

How to do this in code without editor?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to save a RenderTexture to a png from a different camera? 1 Answer

Save rendertexture to image in file 2 Answers

Wierd Screen Shot Problem, texture of screen shot looks different in editor and in system. 1 Answer

Projecting camera view on to a right angled surface 1 Answer

How to get pixeldata from graphics.drawmesh? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges