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. Dismiss Notice

Question Saved HDR is 8 bit?

Discussion in 'High Definition Render Pipeline' started by TOES, Mar 28, 2023.

  1. TOES

    TOES

    Joined:
    Jun 23, 2017
    Posts:
    134
    I am trying to save an HDR rendered image as EXR. Using the default 100.000 LUX light simulating the sun, so without any exposure compensation the whole scene is rendered as white.

    The idea is to do post processing, like exposure, on the saved EXR file. But it is simply all white, and decreasing the exposure simply makes it black. So, all information seems to have been washed out.

    I tried a lower sun intensity, and then I seem to get some HDR, but intensity maxed out at about 1000 before the information got lost (so it is not 8 bit at least, but also not the expected float). However, we do need the full range that Unity uses internally for the post processing tools.

    Any idea why this happens?

    The code is here:

    Code (CSharp):
    1.     public static void SaveRenderTextureToEXR(RenderTexture rt, string filePath)
    2.     {
    3.         //rt is  RenderTextureFormat.ARGBFloat, and is attached to the camera targetTexture
    4.         Texture2D tex = new Texture2D(rt.width, rt.height, TextureFormat.RGBAFloat, false);
    5.         RenderTexture.active = rt;
    6.         tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
    7.         RenderTexture.active = null;
    8.      
    9.         byte[] exrData = tex.EncodeToEXR(Texture2D.EXRFlags.OutputAsFloat);
    10.         File.WriteAllBytes(filePath, exrData);
    11.  
    12.         Object.DestroyImmediate(tex);
    13.     }
     
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    551
    Maybe the camera has tonemapping enabled?

    I'd run this code every frame and then take a RenderDoc capture to see if you get the values before or after the tonemapping pass.
     
  3. TOES

    TOES

    Joined:
    Jun 23, 2017
    Posts:
    134
    I found the way, I had to inject this as a volume processing step, inserted before regular post processing. After post processing the HDR information is truncated.
     
    c0d3_m0nk3y likes this.