Search Unity

Consistent lighting for double-sided shader (foliage)

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

  1. ChadrickEvans

    ChadrickEvans

    Joined:
    Mar 14, 2015
    Posts:
    46
    Before I get into the question, I'll say that I am not very experienced in coding shaders. I experiment and learn by changing things around so I may not explain things in their proper way.

    I've been tweaking a shader I threw together using examples so that I can make trees in my project. My main issue is getting the lighting to stay consistent all around the mesh. This is what I was looking at originally:



    I turned culling off but bright directional lights don't seem to affect backfaces at all.

    So, using an example, I made a custom lighting code with the intention of making the backfaces/frontfaces consistent... Well, now directional lights have a clearly visible effect but it looks entirely too flat.



    I'll include a snippet of the custom lighting I added to the shader below.

    It looks really bad and out of place compared to the billboard version I have in the background despite using the same lighting.




    Code (CSharp):
    1. half4 LightingCustomLambert (SurfaceOutput s, half3 lightDir, half atten)
    2.         {
    3.             fixed4 c;
    4.             c.rgb = (s.Albedo*0.5f) * _LightColor0.rgb * (atten);
    5.  
    6.             c.a = s.Alpha;
    7.             return c;
    8.         }

    What could I be doing wrong?
     
  2. JackieDevelops

    JackieDevelops

    Joined:
    May 11, 2017
    Posts:
    27
    I can tell you that the reason why it's flat is because you are no longer considering the normals of the mesh when calculating light. You are treating the interaction of light as if ever face on the tree is facing the sun directly.

    You may wonder why shadows aren't handling this. Well shadows don't typically get cast in the back face of a quad, that is to say that a leaf isn't going to self shadow it's backface. There may be a setting in the objects shadow drop down to fix that but I don't remember.

    You could just fake translucency to patch up your original tree. I'm writing on my phone at the moment so I can't provide pseudo code, but basically just do normal lighting with the equation you have and dot(normal, lightDir) so that the tree isn't flat. Then add a second lighting equation with dot(normal, -lightDir) but make the intensity less. That would be the simplest but also visually least pleasing solution.
     
  3. Balikk

    Balikk

    Joined:
    Oct 23, 2014
    Posts:
    49
    Hi,

    It's more a 3D trick, but you can edit your vertex normal on your foliage planes.
    Search after " normal thieves foliage 3d " you should find some orientation to do that.
    You should be able to use a standard cutout. If you want it two sided, duplicate your planes and flip the normals in your 3D software, it's the same has made a double sided shader with two passes.

    For you custom lighting, you miss the line " half NdotL = dot (s.Normal, lightDir); "
    In unity examples you have that (from here) :
    Code (CSharp):
    1.          
    2. half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten) {
    3.               // check the face direction with the light direction
    4.               half NdotL = dot (s.Normal, lightDir);
    5.  
    6.               half4 c;
    7.               c.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten);
    8.               c.a = s.Alpha;
    9.               return c;
    10.           }
     
    Last edited: Feb 14, 2018