Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Screenshot encoding very dark in Linear Color space

Discussion in 'Editor & General Support' started by Volkerku, Apr 11, 2020.

  1. Volkerku

    Volkerku

    Joined:
    Nov 23, 2016
    Posts:
    108
    I'm having an issue with my 360 screenshots in a VR app (Oculus Quest). The code works well, but the images come out extremly dark. Guess it might have to do with linear color space converstion.
    Thries everything in regards to rendertexture and Texture2D formats but no luck. The image in the rendertexture appear correct, but once they are saved come out almost black.

    Any ideas? Code below.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5.  
    6. public class CapturePanorama : MonoBehaviour
    7. {
    8.     public bool stereoscopic = false;
    9.     public Camera targetCam;
    10.     public Camera camLft;
    11.     public Camera camRgt;
    12.     public RenderTexture cubeMapLft;
    13.     public RenderTexture cubeMapRgt;
    14.     public RenderTexture equiRendTex;
    15.  
    16.  
    17.  
    18.     void Update() {
    19.         if (Input.GetKeyDown(KeyCode.Space)) {
    20.             Capture();
    21.         }
    22.     }
    23.  
    24.     public void Capture() {
    25.  
    26.         if ( !stereoscopic) {
    27.             targetCam.RenderToCubemap(cubeMapLft);
    28.             cubeMapLft.ConvertToEquirect(equiRendTex);
    29.         } else {
    30.             camLft.RenderToCubemap(cubeMapLft);
    31.             cubeMapLft.ConvertToEquirect(equiRendTex,Camera.MonoOrStereoscopicEye.Right);
    32.  
    33.             camRgt.RenderToCubemap(cubeMapRgt);
    34.             cubeMapRgt.ConvertToEquirect(equiRendTex,Camera.MonoOrStereoscopicEye.Left);
    35.         }
    36.  
    37.         Save(equiRendTex);
    38.     }
    39.  
    40.     public void Save(RenderTexture rt) {
    41.  
    42.         fileNameJPG = Application.dataPath.Replace("/Assets", "") + "/Exports/" + "pano360.JPG";
    43.  
    44.         Texture2D tex = new Texture2D(rt.width, rt.height,TextureFormat.RGB24, false,false);
    45.         //Texture2D(int width, int height, TextureFormat textureFormat = TextureFormat.RGBA32, bool mipChain = true, bool linear = false);
    46.         RenderTexture.active = rt;
    47.         tex.ReadPixels(new Rect(0,0,rt.width,rt.height), 0, 0);
    48.         RenderTexture.active = null;
    49.         byte[] bytes = tex.EncodeToJPG(85);
    50.  
    51.         System.IO.File.WriteAllBytes(fileNameJPG, bytes);
    52.  
    53.     }
    54. }
     
  2. jeremedia

    jeremedia

    Joined:
    Apr 21, 2015
    Posts:
    62
    Any luck with a solution? Also having the same problem: linear project produces dark and overly saturated 360 renders.
     
  3. akiyamaexe

    akiyamaexe

    Joined:
    Mar 3, 2020
    Posts:
    12
    I'm facing a similar problem.

    Doing Application.CaptureScreenshot in Linear mode produces a dark image.

    unity version is 2019.4.
     
  4. noemis

    noemis

    Joined:
    Jan 27, 2014
    Posts:
    75
    #metoo
     
  5. Leniaal

    Leniaal

    Joined:
    Nov 7, 2012
    Posts:
    119
    Found a solution for this issue.

    Code (CSharp):
    1.         var texture2D = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false, true);
    2.         texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    3.  
    4.         Color[] pixels = texture2D.GetPixels();
    5.         for (int p = 0; p < pixels.Length; p++)
    6.         {
    7.             pixels[p] = pixels[p].gamma;
    8.         }
    9.         texture2D.SetPixels(pixels);
    10.  
    11.         texture2D.Apply();
    Kudos to Christian Kauppert
     
    henners999, chealin, scyoon and 6 others like this.
  6. ShivaFooL

    ShivaFooL

    Joined:
    Jul 3, 2010
    Posts:
    16
    Thank you so much, Leniaal ! That did indeed solve the problem.
     
  7. akareactor

    akareactor

    Joined:
    Apr 6, 2015
    Posts:
    104
    It works for me too! U2018 + multicam render, however. Mega thanks!
     
  8. Murray_Zutari

    Murray_Zutari

    Joined:
    Jun 1, 2017
    Posts:
    45
    Thanks @Leniaal this works for me too. Bit slow in VR but will try offload to another thread
     
  9. ptr_ilya

    ptr_ilya

    Joined:
    Mar 21, 2014
    Posts:
    2
    Thank you so much!
     
  10. ManjuYadav

    ManjuYadav

    Joined:
    Dec 21, 2019
    Posts:
    2
    Thank you so much Leniaal it worked.
     
  11. FunSTW

    FunSTW

    Joined:
    Jun 3, 2018
    Posts:
    7
    If you use TextureFormat.RGBA32 for Gamma correction, it can cause color distortion.
    You can use TextureFormat.RGBA64 instead to avoid this issue.

    Code (CSharp):
    1. var texture2D = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGBA64, false, true);

     
    Last edited: Jan 7, 2023