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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Request: Custom Lightmapping Model Example

Discussion in 'Shaders' started by sgoodrow, Apr 4, 2015.

  1. sgoodrow

    sgoodrow

    Joined:
    Sep 28, 2012
    Posts:
    150
    The legacy documentation explains that the deferred rendering pipeline applies Dual Lightmaps with custom lighting functions of the following form:
    1. half4 Lighting<Name>_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade); This is used for light models that are not view direction dependent (e.g. diffuse).

    2. half4 Lighting<Name>_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade, half3 viewDir); This is used for light models that are view direction dependent.
    http://docs.unity3d.com/Manual/SL-SurfaceShaderLighting.html

    I need to implement a custom lightmapping model that has a very slight deviation from the built-in result. Though I know how to replace the model, I don't know what the built-in model actually is (how the inputs are combined and what is returned). Could someone from Unity please reply with an example of the 4.6 built-in DualLightmap function?

    Thank you.

    TL;DR: What is the implementation of Unity 4.6's LightingBlinnPhong_DualLightmap function? It is lacking from the Lighting.cginc file and is not published as far as I can tell.
     
  2. akutruff

    akutruff

    Joined:
    Jul 24, 2009
    Posts:
    44
    I'm having trouble getting any custom lighting model to work for surface shaders in 5.0. Unless I'm screwing something up, nothing is working for me.
     
  3. akutruff

    akutruff

    Joined:
    Jul 24, 2009
    Posts:
    44
  4. akutruff

    akutruff

    Joined:
    Jul 24, 2009
    Posts:
    44
    I spent a few days on this and with some help from Unity, I was able to rework things. In a nutshell, I had to basically define my own _GI function.


    Start by getting the debug code from a built in shader, copy paste this function found in UnityGlobalIllumination.cginc

    inline UnityGI UnityGlobalIllumination (UnityGIInput data, half occlusion, half oneMinusRoughness, half3 normalWorld, bool reflections)


    into your shader and give it a new name. That's where you want to start making mods. Then, any function, like lightmap decoding and calcs, that you need to customize, do the same: Find unity's version in the shaders, cut/paste then rename the function. Make sure you call your customized lightmap functions from your version of the UnityGlobalIllumination function, then repeat as necessary. It is really tough though and you have to make a small change, test and repeat or you'll quickly get lost due to minimal error reporting in the editor. It's just tedious.

    I was able to add specular highlights to the Directional lightmaps without having to use Directiona Specular bakes to gain some performance. (albeit with some artifacts in certain spots that aren't noticeable in our game.)