Search Unity

Custom Surface Shader Baked Light Maps -Please Help 5.3.1

Discussion in 'Shaders' started by ruj, Jan 21, 2016.

  1. ruj

    ruj

    Joined:
    Feb 28, 2013
    Posts:
    113
    EDIT: See follow up post for what appears to be happening, I would love anyone's ideas or help with this.

    I have a custom lighting model that worked fine in 4x, but in 5.3.1 the baked light maps are completely ignored in the Forward render path, I only get realtime lighting. Could someone help me understand how I can get the baked lightmaps to work? They work fine if I use Legacy Deferred and the _prepass portion of the shader, but I need this to use the forward path.

    Thanks for any help! I searched all over, and can only find docs that deal with 4x for this problem.

    Here is the shader, in case anyone has ideas, it is super basic:
    Code (CSharp):
    1. Shader "Custom/Space/SpaceDungeon" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.  
    5.     }
    6.     SubShader {
    7.         Tags { "RenderType"="Opaque"}
    8.         LOD 200
    9.      
    10.         CGPROGRAM
    11.         #pragma surface surf Lambert fullforwardshadows
    12.         #pragma target 3.0
    13.      
    14.         sampler2D _MainTex;  
    15.         sampler2D _EmissiveTex;
    16.         float _EmissiveBrightness;
    17.         fixed4 _EmissiveColor;
    18.  
    19.         struct Input {
    20.             float2 uv_MainTex;
    21.    
    22.         };
    23.            
    24.         void surf (Input IN, inout SurfaceOutput o) {
    25.              o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    26.              half3 EmTex = tex2D (_EmissiveTex, IN.uv_MainTex).rgb;
    27.             o.Emission =  o.Albedo * EmTex * _EmissiveBrightness * _EmissiveColor;
    28.       }
    29.         ENDCG
    30.     }
    31.     FallBack "Diffuse"
    32. }
    33.  
     
    Last edited: Jan 21, 2016
  2. ruj

    ruj

    Joined:
    Feb 28, 2013
    Posts:
    113
    Some more info here, somehow, the shadows appear to be applied ON TOP OF the baked lighting, so it is present, but the shadows are multiplying it down for some reason. Has anyone seen anything like this in the forward render path?
    upload_2016-1-21_13-26-53.png

    And for reference, this is what happens when I turn the realtime directional light off:
    upload_2016-1-21_14-9-17.png
     
    Last edited: Jan 21, 2016
  3. ruj

    ruj

    Joined:
    Feb 28, 2013
    Posts:
    113
    Still hoping someone who knows how this all works will chime in, but I am guessing the problem lies somewhere in here in UnityGlobalIllumination.cginc. I very well may be wrong, but it appears to want to darken a shadowed baked object with a realtime shadow, but I can't understand why it would just NOT APPLY THE LIGHT where it is shadowed. Why does it need to darken it further?


    Code (CSharp):
    1. inline half3 MixLightmapWithRealtimeAttenuation (half3 lightmapContribution, half attenuation, fixed4 bakedColorTex)
    2. {
    3.     // Let's try to make realtime shadows work on a surface, which already contains
    4.     // baked lighting and shadowing from the current light.
    5.     // Generally do min(lightmap,shadow), with "shadow" taking overall lightmap tint into account.
    6.     half3 shadowLightmapColor = bakedColorTex.rgb * attenuation;
    7.     half3 darkerColor = min(lightmapContribution, shadowLightmapColor);
    8.  
    9.     // However this can darken overbright lightmaps, since "shadow color" will
    10.     // never be overbright. So take a max of that color with attenuated lightmap color.
    11.     return max(darkerColor, lightmapContribution * attenuation);
    12. }
    Anyone smarter than me have any ideas?
     
  4. ruj

    ruj

    Joined:
    Feb 28, 2013
    Posts:
    113
    Going to continue this in case anyone is interested. I verified that this WAS the cause by making an alternate version of the standard shader that looks into a different version of this function that just returns the lightmapContribution. That gave me a result that made sense visually. However, I cannot figure out how to get there in my own shader, specifically how to write a surface shader that bypasses that portion of the standard shading model.