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

Writting to rendertexture comes out darker

Discussion in 'Shaders' started by geroppo, Aug 25, 2016.

  1. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    Hi, I was trying to render the encoded depth and normals of the screen to a RenderTexture, but for some reason the results in the render texture are a bit more darker that the original ( see pic below). Screenshot_1.png

    I was wondering if it could be something related to gamma correction.
    Also note that I'm using the exact same shader to render to the screen and to the render texture. The color format of the render texture is ARGB32, and to render the encoded depth and normals I'm using the texture that unity provides me ( _CameraDepthNormalsTexture).
    Any Ideas?
     
    Last edited: Aug 26, 2016
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    I had this issue as well, and while I haven't put in the time to find the root cause, my solution was to not use a pre-made render-texture, but instead generate it in code, which for some reason gives me a proper color space RenderTexture.
    I had first tried fixing it by doing a pow(color, 1 / 2.2); to gamma correct in the shader, but when I sampled the colors in photoshop, they were still a bit off from my input colors.

    Code (CSharp):
    1. RenderTexture outputMap = new RenderTexture(textureSize, textureSize, 32);
    2. outputMap.name = "Whatever";
    3. outputMap.enableRandomWrite = true;
    4. outputMap.Create();
    5. //Put the above stuff in Awake()  if you need to update this every frame...
    6. RenderTexture.active = outputMap;
    7. GL.Clear(true, true, Color.black);
    8. Graphics.Blit(mainTexture, outputMap, rtMat);
     
    Last edited: Aug 25, 2016
    aikkala, yaoling1997 and TeamDefiant like this.
  3. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    I did the exact same thing lol. Thanks for your answer, I've already spent an entire day trying to figure this out.

    Btw, is the GL.Clear necessary? If I understood correctly, with that you are clearing the current active render texture, which you just created so it should be empty, right?.
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    Nah it's probably not necessary, just haven't done a bake yet to test without it.
     
  5. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    Alright, thanks!
     
  6. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    Scratch that, at least in my case, without the GL.Clear, I just end up with a blank, transparent texture.
     
  7. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    Oh wow, thank you for letting me know
     
  8. pmaloo

    pmaloo

    Joined:
    Jan 25, 2015
    Posts:
    35
    Shouldn't you be using _CameraDepthNormalsTexture instead of _CameraDepthTexture for normals?
     
  9. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    Yes, I'm using that, I just noticed that I wrote the other one :p
     
  10. pmaloo

    pmaloo

    Joined:
    Jan 25, 2015
    Posts:
    35
    Were the screenshots above taken during run time? If so, the render texture will always be darker because Unity darkens everything except the Game preview area.

    I did a quick test exporting the Render Texture as well as screen content to a png fie. They both turned out to be exactly the same.
     
  11. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    Unfortunately I'm not at my computer at this moment but I believe that yes, it was taken during runtime. Will test it when I get home, thanks!
     
  12. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    Well now I feel dumb, you were right :p I can't believe I didn't notice it.

    Btw is it possible that the _CameraDepthNormalsTexture is not generated when you do a camera.Render() in the editor?, because it seems to render gray, but in runtime works fine.
     
  13. pmaloo

    pmaloo

    Joined:
    Jan 25, 2015
    Posts:
    35
    Happens to all of us. Just a day ago, I was wondering why the camera.Render() was messing up my depth. Turned out I forgot to set the depthbuffer on RenderTexture.

    _CameraDepthNormalsTexture should be generated in editor mode as well. I was just playing around with it a few days ago.
     
  14. Fragsteel

    Fragsteel

    Joined:
    Nov 8, 2016
    Posts:
    6
    Years late, but for anyone else reading, this can be an issue between linear and gamma space. If your project is in gamma, this'll happen. You can use a pre-made RenderTexture, but in the Inspector options for that texture, try enabling sRGB (Color RenderTexture).
     
  15. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,873
    In Unity 2018.3+, I get a darkening due to what seems to be a bug in Unity's handling of writing alpha values to the RT. If you're in ARGB32 (as OP was), writing alpha values are rendered correctly in the Texture-Preview window (they blend), but it breaks Unity's rendering of the resulting image in any Canvas. Short term solution is to switch down to RGB565 (RGB24 doesn't exist as an option, which is depressing).
     
    jiraphatK likes this.
  16. cr4y

    cr4y

    Joined:
    Jul 12, 2012
    Posts:
    44
    For me, the source of the issue was in writing from RenderTexture to Texture using ReadPixels.
    When you create Texture2D, there is an additional parameter: linear. Set it to true to avoid additional Gamma Correction.


    Code (CSharp):
    1. Texture2D tex = new Texture2D(rTex.width, rTex.height, TextureFormat.RGB24, false, true); //last parameter
    2. RenderTexture.active = rTex;
    3. tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
    4. tex.Apply();
     
  17. TeamDefiant

    TeamDefiant

    Joined:
    Mar 29, 2017
    Posts:
    50
    This did it for me. :)
     
  18. yaoling1997

    yaoling1997

    Joined:
    Dec 19, 2018
    Posts:
    3
    It works! You are such a genius!
     
  19. kevin-masson

    kevin-masson

    Joined:
    Sep 24, 2018
    Posts:
    71
    I did a minor adjustment to check if the texture is linear or not.
    This should adapt to any texture format.

    Code (CSharp):
    1. bool isLinear = !GraphicsFormatUtility.IsSRGBFormat(rTex.graphicsFormat);
    2.  
    3. Texture2D tex = new Texture2D(
    4.     rTex.width,
    5.     rTex.height,
    6.     TextureFormat.RGB24,
    7.     false,
    8.     isLinear );
    9. RenderTexture.active = rTex;
    10. tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
    11. tex.Apply();
     
  20. jiraphatK

    jiraphatK

    Joined:
    Sep 29, 2018
    Posts:
    251
    Thanks. this did it for me.
     
    Last edited: Dec 17, 2021
  21. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,873
    Is this still broken in 2019, 2020? I think I submitted a bug report 2 years ago, but ... if you are still seeing it today then it's worth submitting a new one now using your situation. Stuff like this really should be fixed!
     
    luzcamacho likes this.
  22. Ensortainment

    Ensortainment

    Joined:
    Dec 19, 2020
    Posts:
    14
    Bless you, this did it. Many thanks!
     
  23. VeryBadPenny

    VeryBadPenny

    Joined:
    Jun 3, 2018
    Posts:
    34
    In Unity 2021 these seem to have different names, but the default was Color Format = R8G8B8A_UNORM for me and I got the darkening problem, resolved (by trial and error) by switching to R8G8B8A8_SRGB.
     
  24. Wothanar

    Wothanar

    Joined:
    Sep 2, 2016
    Posts:
    122
    have the problem in urp 2020.3.17f1 nothing of here seems to work about render textures so i will try with out render textures since seems to be broken
     
  25. Wothanar

    Wothanar

    Joined:
    Sep 2, 2016
    Posts:
    122

    https://gyazo.com/2b930454acb6f30d116992f02e3a06cb There is no srgb option in inspecto Color render texture? can you explain what you mean with that plz?
     
  26. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    Color format defines whether it is sRGB or not (among other possibilities). Right now you have it set as UNORM which is a linear format. Switch to R8G8B8A8_SRGB.
     
  27. Braza

    Braza

    Joined:
    Oct 11, 2013
    Posts:
    126
    For me, it was a matter of delaying rendering till the end of the frame, for some reason. But I was rendering a single shot and noticed that the picture becomes OK in the case of continuous rendering.
    Code (CSharp):
    1.     public void Refresh()
    2.     {
    3.         RenderTexture = shipRenderer.RenderTexture;
    4.  
    5.         StartCoroutine(DelayedRenderShip());
    6.     }
    7.  
    8.     IEnumerator DelayedRenderShip()
    9.     {
    10.         yield return new WaitForEndOfFrame();
    11.         RenderTexture = shipRenderer.RenderTexture;
    12.     }
     
  28. colinsauce00

    colinsauce00

    Joined:
    Jul 13, 2021
    Posts:
    7
    Hello all, years late but thread looks to still be active. I spent hours and hours on this doing all above solutions and many of my own such as playing with shader properties and camera properties etc.

    My solution: Go to an unlit shader (shadows do not affect render), and turn OFF post processing on the camera that renders your object. My post processing was being applied twice, once where the camera was looking at the object I wanted, another when it displayed.

    Before you try above methods check. the post. processing.
     
    somesome2314 likes this.
  29. Fredjack99

    Fredjack99

    Joined:
    Aug 4, 2018
    Posts:
    2
    In my case, it turns out I was drawing to a raw image that had a color set to dark blue. This shaded everything which was drawn from RenderTexture. I had to switch to pure white to fix the image. So, yet another way to screw things up :)
     
  30. unity_F349637BE9870150A007

    unity_F349637BE9870150A007

    Joined:
    May 19, 2023
    Posts:
    2
    set depth stencil format as none it will work properly .
     
  31. unity_F349637BE9870150A007

    unity_F349637BE9870150A007

    Joined:
    May 19, 2023
    Posts:
    2
    In render texture setting there is a parameter depth stencil change its value to none
     
  32. restush96

    restush96

    Joined:
    May 28, 2019
    Posts:
    120
    For me change to SRGB fix the issue. No need to delay or anything.

    upload_2023-9-2_17-39-13.png