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

Unity Skybox shaders don't work in HDR

Discussion in 'Shaders' started by makeshiftwings, Apr 4, 2014.

  1. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    I'm hoping someone can help me figure out how to fix the built-in Unity skybox shaders to work when HDR is enabled. Currently it seems the mobile skybox shader works great, but the other three non-mobile skybox shaders and anything based off of them does not work. Here's the current problem, which shows up most obviously when using an alpha-blended shader (like a cloud) in front of a dark sky:

    Regular Skybox Shader, No HDR (correct)
    $NoHDR.JPG

    Mobile Skybox Shader, HDR enabled (also correct)
    $Mobile-HDR.JPG

    Regular Skybox Shader, HDR enabled (definitely not correct)
    $HDR.JPG


    I'm guessing the mobile one works because it just does a simple "combine texture" and that's it, while the non-mobile ones do this:
    Code (csharp):
    1.  
    2. fixed4 skybox_frag (v2f i, sampler2D smp)
    3. {
    4.     fixed4 tex = tex2D (smp, i.texcoord);
    5.     fixed4 col;
    6.     col.rgb = tex.rgb + _Tint.rgb - unity_ColorSpaceGrey;
    7.     col.a = tex.a * _Tint.a;
    8.     return col;
    9. }
    10.  
    Something about that unity_ColorSpaceGrey seems suspicious but I don't really understand what's going on. Anyone have any insight?
     
  2. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Following up on this, I changed that suspicious colorspacegrey line to this:

    Code (csharp):
    1.  
    2. col.rgb = tex.rgb * _Tint.rgb;
    3.  
    And it seems to work great. Can someone explain to me why the original shader doesn't use a simple multiply for the tint, and what ColorSpaceGrey is? I'm guessing that maybe in non-HDR the col.rgb gets clamped to 0,0,0, but in HDR it goes negative?
     
  3. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    I think I answered my own question, as this also fixes the problem:

    Code (csharp):
    1.  
    2. col.rgb = clamp(tex.rgb + _Tint.rgb - unity_ColorSpaceGrey, 0, 100);
    3.  
    So in HDR, colors can go negative, which means any shader that relies on subtracting without remembering to clamp to zero is going to fail. Am I right that this is a bug? I can't imagine that a dark sky is "supposed to" become a negative color in HDR; it looks like they just forgot to clamp.
     
  4. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,446
    Having this issue too now, saving the files as a .jpeg instead of HDR helped in our case we aren't using HDR for a mobile VR project