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

How to save Unity HDR renderTexture into a .exr file?

Discussion in 'General Graphics' started by moranBoar, Jan 3, 2022.

  1. moranBoar

    moranBoar

    Joined:
    Dec 14, 2018
    Posts:
    6
    [Purpose]:
    I need to save the Unity camera view into a .exr file.

    [Error]:
    Remapping between formats 74 -> 73 is not supported


    [What I've tried]:
    I tried the official solution: https://docs.unity3d.com/ScriptReference/ImageConversion.EncodeToEXR.html

    First, I set the renderTexture format as RenderTextureFormat.DefaultHDR to make sure that it stores HDR data.
    Code (CSharp):
    1. renderTex = new RenderTexture(renderWidth, renderHeight, 16, RenderTextureFormat.DefaultHDR);

    Then, I tried to save the renderTexture data into a .exr file.
    Code (CSharp):
    1. Texture2D image = new Texture2D(2, 2, TextureFormat.RGB9e5Float, false, true);
    2. image.ReadPixels(new Rect(renderWidth / 2, renderHeight / 2, 2, 2), 0, 0);
    3. image.Apply();
    4.  
    5. byte[] bytes = image.EncodeToEXR(Texture2D.EXRFlags.CompressZIP);
    6. File.WriteAllBytes(Application.dataPath + "/HDRs/"+tag+"reflectance.exr", bytes);
    The error may be related to the RenderTexture and Texture2D format. I tried to change the Texture2D format to TextureFormat.RGBAFloat. The error changed:
    Remapping between formats 74 -> 52 is not supported


    It seems that the Texture2D format cannot store the HDR RenderTexture data, and the original data gets truncated?(I guess) I've tried all the TextureFormat tagged "HDR "in page https://docs.unity3d.com/ScriptReference/TextureFormat.html. None of them works, some of them is unsupported... What can I do to save a HDR camera view into a .exr file?
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,004
    try having equivalent formats for both. ARGBFloat for the RT and RGBAFloat for the texture.
     
  3. moranBoar

    moranBoar

    Joined:
    Dec 14, 2018
    Posts:
    6
    Thanks. It works. But I'm worry about the value truncation. Is RGBFloat format large enough to hold all HDR data?
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,004
    AFAIK there isn’t a bigger format available in Unity.
     
    moranBoar likes this.