Search Unity

Get Ambient Color in custom shader

Discussion in 'Universal Render Pipeline' started by uwdlg, Oct 26, 2020.

  1. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    150
    Hi,

    I'm trying to convert my custom shaders from built-in to URP (package version 7.5.1 in 2019.4.9f1) and can't figure out how to access the ambient color. To clarify, I'm talking about the color set in Lighting > Environment Lighting > Ambient Color when Source is set to Color. I've tried the following variables:
    Code (CSharp):
    1. UNITY_LIGHTMODEL_AMBIENT
    2. unity_AmbientSky
    3. unity_AmbientGround
    4. unity_AmbientEquator
    ...for the last three I also tried setting Source to Gradient, but I can't get any of those colors in my shader (I'm using
    return half4(*variable from above*.rgb, 1);
    in the fragment shader, everything else works fine). They all produce different shades of grey, but not any of the colors I set up in the editor. So how can I get the ambient color?

    Thanks!
     
    changtzupo likes this.
  2. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    150
    Last edited: Oct 26, 2020
    changtzupo likes this.
  3. BattleAngelAlita

    BattleAngelAlita

    Joined:
    Nov 20, 2016
    Posts:
    400
    Plain ambient light contain in the L0 coefficient of spherical harmonic. You can get it with
    half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w);
     
    Last edited: Oct 26, 2020
  4. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    150
    Thank you very much, just what I was looking for!
    I had dug my way to those coefficients, but didn't try the last components :rolleyes:
     
  5. keith_unity844

    keith_unity844

    Joined:
    Oct 31, 2019
    Posts:
    1
    I always get black when trying BattleAngelAlita's solution. Light settings are using ambient color. Anything I might be missing that seems obvious but isn't for this lowly guy?

    Edit: I figured out it was just my "Scene Lighting" that was toggled off. It's working, but I really need to be able to toggle off the Scene Lighting without my custom terrain going black. How do other shaders respect this setting in a way that just ignores the ambient color instead of using the unset values?
     
    Last edited: Nov 19, 2020
  6. pithakoteTata

    pithakoteTata

    Joined:
    Oct 18, 2019
    Posts:
    18
    Thank you thank you thank you thank you.
    That saved my URP Shader. How did you know that would help in Ambient Lighting?
    I've been digging through documentations without any success.
     

    Attached Files:

  7. FariAnderson

    FariAnderson

    Joined:
    Jan 20, 2020
    Posts:
    37
    Thank you so much, i was working in Built-In RP and when i disabled the Sun, the ambient light went black completely.
    so your solution worked.
     
  8. beatdesign

    beatdesign

    Joined:
    Apr 3, 2015
    Posts:
    137
    It is mentioned here: https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html
    Spherical harmonics coefficients (used by ambient and light probes) are set up for ForwardBase, PrePassFinal and Deferred pass types. They contain 3rd order SH to be evaluated by world space normal (see ShadeSH9 from UnityCG.cginc). The variables are all half4 type, unity_SHAr and similar names.
    but I agree with you: the documentation is not so clear!
     
  9. tylearymf_unity

    tylearymf_unity

    Joined:
    May 25, 2022
    Posts:
    12
    half3 ambient = _GlossyEnvironmentColor.xyz;
     
    marie-hmm likes this.
  10. marie-hmm

    marie-hmm

    Joined:
    Mar 25, 2016
    Posts:
    11
    OMG thank you, this is the only thing that worked for me. The unity_SHAr/g/b were just black. How did you even find it? Manual doesn't even mention it and it just hangs there in Library/PackageCache/com.unity.render-pipelines.universal@14.0.9/ShaderLibrary/Input.hlsl