1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

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:
    14
    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:
    14
    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:
    14
    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 !