Get Ambient Color in custom shader

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:

UNITY_LIGHTMODEL_AMBIENT
unity_AmbientSky
unity_AmbientGround
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!

1 Like

EDIT: obsolete, see @BattleAngelAlita 's answer below.

1 Like

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);

5 Likes

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 :roll_eyes:

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?

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.

6758011--780109--image_2021-01-23_231643.png

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.

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!

half3 ambient = _GlossyEnvironmentColor.xyz;

2 Likes

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