Search Unity

Custom lightmap Surface Shader

Discussion in 'Shaders' started by n0mad, Jun 7, 2011.

  1. n0mad

    n0mad

    Joined:
    Jan 27, 2009
    Posts:
    3,732
    Hey there,

    After couple hours of research and testing, I found the perfect Surface Shader traduction of my old 1.1 Lightmap Shader.
    Before, when I used traditional passes, it would be written :

    Code (csharp):
    1.  
    2. SetTexture [_Selfillum] {
    3.       combine texture +  primary
    4.  }
    5.  
    6. SetTexture [_MainTex] {
    7.       combine texture * previous , texture
    8.  }
    9.  
    where _MainTex is your main texture, and _SelfIllum your custom black white lightmap. The more white a zone is, the more self-lit it is, without burning the color or overshooting its lighting (those were the 2 reasons why I didn't use Emission parameter instead).

    So now that Surface Shaders are in, here is how it would be perfectly traduced.
    Everything necessary is contained in the surf function :
    Code (csharp):
    1.  
    2.  void surf (Input IN, inout SurfaceOutput o) {
    3.  
    4.       half4 _lightmap = tex2D (_Selfillum, IN.uv_MainTex);
    5.       half4 tex = tex2D(_MainTex, IN.uv_MainTex);
    6.  
    7.           o.Albedo = tex.rgb*(1-_lightmap.rgb);
    8.  
    9.           o.Emission = tex.rgb*_lightmap.rgb;
    10.  
    11.           o.Alpha = tex.a;
    12.  
    13.  }
    14.  
    It's basically masking the Albedo zones with the Lightmap, as the Emission channel is added over it.

    Hope it helps !

    edit : I forgot to mention that this Surface Shader should not interfere with Beast lightmaps.
     
    Last edited: Jun 7, 2011
  2. GlitchInTheMatrix

    GlitchInTheMatrix

    Joined:
    Apr 12, 2010
    Posts:
    285
    thanks! can you post some pics showing the difference between the old a new surface shader?
     
  3. n0mad

    n0mad

    Joined:
    Jan 27, 2009
    Posts:
    3,732
    Sure ^^

    Here, a comparison on a lightly self-lit level :



    It's not very visible here, because the colorburn overshoot effect really expresses itself on highly emissive zones, and when hit by a strong light. The only difference seems to be a smoother feeling overall at the bottom.


    Here is a nice proof :



    Even dramatically lowering the Emission does no justice, and even messes up the mesh borders which have the same color the same self-light (like the circle on the upper right). It's because of scene light not reflecting toward the same direction.
    Emission is just a color addition to the base lit texture. So basically, putting this (1-_lightmap.rgb) in the base lit layer evens out this addition, creating the correct overall exposure balance.

    Hope it helps.
     

    Attached Files:

  4. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    thanks for sharing!