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. Dismiss Notice

Question How can I set the ambient light for each triangle in URP?

Discussion in 'Shaders' started by tcymtz, Sep 29, 2023.

  1. tcymtz

    tcymtz

    Joined:
    Oct 7, 2019
    Posts:
    2
    Hello,

    I am trying to create a procedurally generated 3D voxel game similar to minecraft. I want to gradually darken the ambient lighting on triangles as you go deeper into a cave. My program knows what percentage of ambient light the triangles should receive, and I am trying to figure out how to implement that in the shader.

    So far, I have set my program up to use URP and I have created a custom shader which is an exact copy of the URP Lit Shader.

    In my program, I set the desired ambient light in an update loop with
    Code (CSharp):
    1. ambient_light = new Color(0.455f, 0.455f, 0.455f, 1f);
    2. RenderSettings.ambientLight = ambient_light;
    In the shader, I have added the COLOR attribute so it can read in how much light should be received per vertex:

    float4 vertex_color : COLOR;


    I am passing into the shader the percentage of light each triangle should receive through the vertex color channel. But I am having a difficult time understanding:

    A. How the URP Lit Shader receives the ambient light color from the program in general (I do not see a variable).
    B. And most importantly, how can I adjust the ambient lighting for each triangle using the vertex_color which I have passed in. I am using full PBR materials with shadows, because I want the sun and moons to cast shadows, but I just need direct control over ambient light per triangle.

    Any help would be appreciated. Thanks!
     
  2. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    73
    unless i'm not understanding your question correctly, if you're passing your ambient light through the vertex color you don't need to change anything in the lighting function, just combine the color however you would like in the fragment; i'd add or multiply the final color with the vertex color interpolator