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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Correct way to calculate ambient lighting?

Discussion in 'Shaders' started by SUBZERO8K, Feb 13, 2018.

  1. SUBZERO8K

    SUBZERO8K

    Joined:
    Jan 15, 2013
    Posts:
    36
    Hi there, I am working on a custom shader and had a question about which way I should be calculating my ambient lighting.

    I have tried two different techniques:

    float4 ambient = UNITY_LIGHTMODEL_AMBIENT;
    and
    float ambient = ShadeSH9(float4(worldNormal, 1));

    Below I have a comparison image using the two techniques, with UNITY_LIGHTMODEL_AMBIENT on the left and ShadeSH9(float4(worldNormal, 1)) on the right.
    Screen Shot 2018-02-13 at 11.17.42 AM.png
    Comparing to the diffuse color tint I have set, it would seem that UNITY_LIGHTMODEL_AMBIENT is a bit more accurate, but I'm still not sure.
    Screen Shot 2018-02-13 at 11.17.51 AM.png

    Which technique should I be using to calculate my ambient lighting? Is one of these techniques more correct than the other? Thanks!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,248
    ShadeSH9 is the "correct" way to calculate ambient lighting in terms of it being what Unity uses internally for most of its shaders, and will correctly handle the directional lighting from light probes.

    UNITY_LIGHTMODEL_AMBIENT is just a solid color that is a constant average of the skybox's color, the ambient color, or the equator color depending on what setting you're using for ambient lighting. This can produce more favorable results depending on the use.
     
    SUBZERO8K likes this.
  3. SUBZERO8K

    SUBZERO8K

    Joined:
    Jan 15, 2013
    Posts:
    36
    Thanks for the information!