Search Unity

Surface Shader - Custom lighting function not working with baked GI

Discussion in 'Shaders' started by CrazyRocksStudios, Mar 20, 2017.

  1. CrazyRocksStudios

    CrazyRocksStudios

    Joined:
    Sep 29, 2015
    Posts:
    15
    Hello,

    We have a toon surface shader with a custom lighting function:
    Code (CSharp):
    1. fixed4 LightingToon (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)

    and we use this shader on the Unity terrain but we want to bake all shadows on the terrain to have better performance so we use Baked GI.

    As soon as GI is baked - this function just stops working. (not launching, probably replaced by Lambert)

    So I did a little research and found that methods like this are obsolete now and replaced by:
    Code (CSharp):
    1. fixed4 Lighting<Name>(SurfaceOutput s, half3 viewDir, UnityGI gi)
    2. fixed4 Lighting<Name>(SurfaceOutput s, UnityGI gi)
    3. half4 Lighting<Name>_Deferred (SurfaceOutput s, half3 viewDir, UnityGI gi, out half4 outDiffuseOcclusion, out half4 outSpecSmoothness, out half4 outNormal)
    4. half4 Lighting<Name>_Deferred (SurfaceOutput s, UnityGI gi, out half4 outDiffuseOcclusion, out half4 outSpecSmoothness, out half4 outNormal)
    5. fixed4 Lighting<Name>(_PrePass (SurfaceOutput s, half4 light)
    6. void Lighting<Name>_GI(SurfaceOutput s, UnityGIInput data, inout UnityGI gi)
    When I replace LightingToon function with e.g. :
    Code (CSharp):
    1. fixed4 Lighting<Name>(SurfaceOutput s, half3 viewDir, UnityGI gi)
    - then the function works with baked GI.

    My questions are:
    1. Is it still possible to use the old type lighting function with baked GI and how ?
    2. How can I rewrite my lighting function not having necessary parameters like: atten or lightDir ?

    Thank You for help !
    Stan
     
  2. CrazyRocksStudios

    CrazyRocksStudios

    Joined:
    Sep 29, 2015
    Posts:
    15
    Update:

    I managed to rewrite my Lighting function using the new form (with GI as parameter) and combined it successfully with the existing light color. I used gi.light.dir instead of missing light dir from param and i used 1 for my attenuation since I use only directional light in this case. Everything works fine - I can bake GI and my toon terrain texture is mixed now with lightmap shadow. The only problem left is: the output differs from the original lighting function - my terrain gets less indirect light. As a test i use Precomputed GI (which was also working for the original lighting function).
    As soon as I figure out what's going on I will write new update here ;) In the meantime any of Your help is appreciated !
     
  3. CrazyRocksStudios

    CrazyRocksStudios

    Joined:
    Sep 29, 2015
    Posts:
    15
    Update:

    It turned out that the original lighting function works with baked GI ( i mean is normally called - my mistake ) but the output is wrong because it adds GI to the color multiplied by lighting 0 (light is disabled for objects with lightmap) - so the output is GI * Albedo which in my case is wrong because i have specific surface color (toon) not simple albedo - i changed this lighting function for my case to not include zero lighting in computation and it works fine !
     
  4. Ng0ns

    Ng0ns

    Joined:
    Jun 21, 2016
    Posts:
    197
    I know this is an old thread, but would it be possible to shed some light on how you made it work (perhaps code =D)?
    I'm kinda stuck attempting to do the same. While the light interact with objects, it seems masked by the GI and thus doesn't show on the whole object.

    Cheers.