Search Unity

Question Update Ambient node at runtime?

Discussion in 'Shader Graph' started by sevensixtytwo, Jan 24, 2020.

  1. sevensixtytwo

    sevensixtytwo

    Joined:
    Apr 5, 2013
    Posts:
    27
    Hello, is there any way to force the Ambient node to update at runtime?

    I have a custom-lit shader that uses two color inputs for lit and shadowed faces respectively. I want to be able to manipulate them depending on the scene's lighting scheme without having to changing the material. I can easily plug in the main light's color value for the lit faces and thought that I could do the same with the Ambient color for the shadowed faces. Unfortunately, it doesn't work, and I read that the Ambient node only updates when entering Play Mode or when saving the project.

    For more context, I generate a level from user-selected variants so a base scene will have its directional light and ambient lighting changed when loading the scene and I want the objects using my shader to conform with that.

    Ideal result:


    Ideal graph


    I'd really like to know if there is any way I can get this to work properly. Failing that, if the team has any plans to address this soon or reasons to implement it as such.

    Thanks.
     
    NotaNaN likes this.
  2. Korimaru88

    Korimaru88

    Joined:
    Jul 24, 2015
    Posts:
    18
    Have the exact problem so I'm bumping this thread instead of making a new one.

    Edit: Worked around it by adding a property for ambient color, unchecking "exposed", and creating this script:
    Code (CSharp):
    1. [ExecuteInEditMode]
    2. public class ToonLightingUpdater : MonoBehaviour
    3. {
    4.     private int ambientId;
    5.  
    6.     private void Start()
    7.     {
    8.         ambientId = Shader.PropertyToID("ambient_color");
    9.     }
    10.  
    11.     private void Update()
    12.     {
    13.         Shader.SetGlobalColor(ambientId, RenderSettings.ambientSkyColor);
    14.     }
    15. }
     
    Last edited: Jul 19, 2020