Screenshot encoding very dark in Linear Color space (784977)

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class CapturePanorama : MonoBehaviour
{
    public bool stereoscopic = false;
    public Camera targetCam;
    public Camera camLft;
    public Camera camRgt;
    public RenderTexture cubeMapLft;
    public RenderTexture cubeMapRgt;
    public RenderTexture equiRendTex;



    void Update() {
        if (Input.GetKeyDown(KeyCode.Space)) {
            Capture();
        }
    }

    public void Capture() {

        if ( !stereoscopic) {
            targetCam.RenderToCubemap(cubeMapLft);
            cubeMapLft.ConvertToEquirect(equiRendTex);
        } else {
            camLft.RenderToCubemap(cubeMapLft);
            cubeMapLft.ConvertToEquirect(equiRendTex,Camera.MonoOrStereoscopicEye.Right);

            camRgt.RenderToCubemap(cubeMapRgt);
            cubeMapRgt.ConvertToEquirect(equiRendTex,Camera.MonoOrStereoscopicEye.Left);
        }

        Save(equiRendTex);
    }

    public void Save(RenderTexture rt) {

        fileNameJPG = Application.dataPath.Replace("/Assets", "") + "/Exports/" + "pano360.JPG";

        Texture2D tex = new Texture2D(rt.width, rt.height,TextureFormat.RGB24, false,false);
        //Texture2D(int width, int height, TextureFormat textureFormat = TextureFormat.RGBA32, bool mipChain = true, bool linear = false);
        RenderTexture.active = rt;
        tex.ReadPixels(new Rect(0,0,rt.width,rt.height), 0, 0);
        RenderTexture.active = null;
        byte[] bytes = tex.EncodeToJPG(85);

        System.IO.File.WriteAllBytes(fileNameJPG, bytes);

    }
}

Any luck with a solution? Also having the same problem: linear project produces dark and overly saturated 360 renders.

I’m facing a similar problem.

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

unity version is 2019.4.

#metoo

Found a solution for this issue.

        var texture2D = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false, true);
        texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);

        Color[] pixels = texture2D.GetPixels();
        for (int p = 0; p < pixels.Length; p++)
        {
            pixels[p] = pixels[p].gamma;
        }
        texture2D.SetPixels(pixels);

        texture2D.Apply();

Kudos to Christian Kauppert

12 Likes

Thank you so much, Leniaal ! That did indeed solve the problem.

It works for me too! U2018 + multicam render, however. Mega thanks!

Thanks @Leniaal this works for me too. Bit slow in VR but will try offload to another thread

Thank you so much!

Thank you so much Leniaal it worked.

If you use TextureFormat.RGBA32 for Gamma correction, it can cause color distortion.
You can use TextureFormat.RGBA64 instead to avoid this issue.

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

3 Likes