Search Unity

access ambient color value from a legacy shader

Discussion in 'Shaders' started by michal_gjk, Aug 23, 2016.

  1. michal_gjk

    michal_gjk

    Joined:
    Aug 13, 2016
    Posts:
    69
    Can I access the ambient color value (the one set in the lighting panel) from a legacy shader?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Define what you mean by "legacy shader". If you mean a surface shader using a Lambert or BlinnPhong shading model (like the legacy shaders use) then there's a couple of ways to get access to the ambient color. They're the same ways you get access to it from a vertex / fragment shader.

    But there's a lot of different ways to get the ambient color though so it depends on exactly what you want.

    There's the easiest way, which is to get UNITY_LIGHTMODEL_AMBIENT which is going to be a single color value, which is going to be the ambient color or equator color depending on the mode.
    Then there's unity_AmbientSky, unity_AmbientEquator, and unity_AmbientGround which you can lerp between using the world normal, if you're in constant color mode I think they're all set to the same color.
    Lastly there's a couple of different methods for accessing the spherical harmonic lighting which is per-object. The most common method is using ShadeSH9(worldNormal).
     
  3. michal_gjk

    michal_gjk

    Joined:
    Aug 13, 2016
    Posts:
    69
    I guess what I mean by legacy is a shader using SurfaceOutput (in other words not standard).

    Let's say I select Skybox as a source of ambient. Does that mean unity generates a gradient from the skybox somehow or does it derive one color from the whole skybox.

    I've noticed a few things. With standard shader highly metallic materials don't react to ambient even if they are very rough. Wonder if it's by design.
    Another thing. Standard shader materials don't seem to react to gradient and fixed color ambient and if they do it must be to a very limited degree probably because those two types of ambient don't have intensity.

    What's the correct way to mix the SH ? Should I just multiply it with albedo so that it will be ignored by metallic materials?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    You can have a surface shader that uses the standard shading model btw. If you create a new surface shader in Unity it makes one using the standard shading model.

    Also if you're using a surface shader it should handle the ambient for you using the ShadeSH9 or light maps for static objects.

    As for the proper use, you should add the direct lighting and the ambient lighting together then multiply the albedo color with that.

    And yes, a purely metallic standard surface will effectively ignore the ambient light because they have no albedo, regardless of their roughness. Metal objects use the specular reflection probes for diffuse ambience.
     
  5. michal_gjk

    michal_gjk

    Joined:
    Aug 13, 2016
    Posts:
    69
    1. I know I can have standard surface shaders. Am I right in thinking they maybe a bit heavy for mobile, though?

    2. Previously I wrote gradient or fixed color ambient don't work with the standard shader. After further investigation it turns out they only don't work in the material preview they do however work in the scene.

    So I finally have a working ambient which looks exactly the same as the one rendered by the standard shader.

    Thanks a lot!