Search Unity

Bake Sprites Lightmaps

Discussion in '2D' started by lassade, Dec 17, 2016.

  1. lassade

    lassade

    Joined:
    Jan 27, 2013
    Posts:
    127
    Hey guys I was trying to get lightmaps for my sprites when I find out that Unity does not bake lights for SpriteRenderer so I made a very simple editor script that trick Unity into bake the lightmaps for all sprites in the scene (if they are static).

    Here is the git hub page:
    https://github.com/lassade/Sprite2DBakedLighting/tree/master

    I'm using the 5.4.1f1 so it tiny bit old, so let me know if there is a newer version that can do bake sprites lighting.

    Tell me what your thoughts. What your guys think?

    Edit:
    [ALREADY FIXED] Looks like the script only works in editor :( in release builds the lights is disconsidered.

    Edit:
    The unity was not saving the lightmap data for the SpriteRenderer compoment so I added a new component to do save and the setup. :)
     
    Last edited: Dec 20, 2016
    Samacvuk and ameo_game like this.
  2. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,526
  3. Coresi7

    Coresi7

    Joined:
    Mar 10, 2017
    Posts:
    13
    I really hope this will be in offical unity release !
    Awesome!
     
  4. Coresi7

    Coresi7

    Joined:
    Mar 10, 2017
    Posts:
    13
    The unity shader has been changed for latest Unity builds (such as Unity 2017.1) and if you use default sprites/diffuse, there'll be no lightmap support.
    Please add lightmap support in your shader. I've wrote one for you.
    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. Shader "Custom/Sprites-Diffuse"
    4. {
    5.     Properties
    6.     {
    7.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    8.         _Color ("Tint", Color) = (1,1,1,1)
    9.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    10.         [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
    11.         [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
    12.         [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
    13.         [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
    14.     }
    15.  
    16.     SubShader
    17.     {
    18.         Tags
    19.         {
    20.             "Queue"="Transparent"
    21.             "IgnoreProjector"="True"
    22.             "RenderType"="Transparent"
    23.             "PreviewType"="Plane"
    24.             "CanUseSpriteAtlas"="True"
    25.         }
    26.  
    27.         Cull Off
    28.         Lighting Off
    29.         ZWrite Off
    30.         Blend One OneMinusSrcAlpha
    31.  
    32.         CGPROGRAM
    33.         #pragma surface surf Lambert vertex:vert keepalpha
    34.         #pragma multi_compile _ PIXELSNAP_ON
    35.         #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
    36.         #include "UnitySprites.cginc"
    37.  
    38.         struct Input
    39.         {
    40.             float2 uv_MainTex;
    41.             fixed4 color;
    42.         };
    43.  
    44.         void vert (inout appdata_full v, out Input o)
    45.         {
    46.             v.vertex.xy *= _Flip.xy;
    47.  
    48.             #if defined(PIXELSNAP_ON)
    49.             v.vertex = UnityPixelSnap (v.vertex);
    50.             #endif
    51.  
    52.             UNITY_INITIALIZE_OUTPUT(Input, o);
    53.             o.color = v.color * _Color * _RendererColor;
    54.         }
    55.  
    56.         void surf (Input IN, inout SurfaceOutput o)
    57.         {
    58.             fixed4 c = SampleSpriteTexture (IN.uv_MainTex) * IN.color;
    59.             o.Albedo = c.rgb * c.a;
    60.             o.Alpha = c.a;
    61.         }
    62.         ENDCG
    63.     }
    64.  
    65.     Fallback "Transparent/VertexLit"
    66. }
    67.  
     
    Last edited: Oct 10, 2017
    ktk-kumamoto and lassade like this.
  5. lassade

    lassade

    Joined:
    Jan 27, 2013
    Posts:
    127
    Thanks @Coresi7 for pointing that out. I just updated the repo with your shader.
     
  6. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
    We're this close using your solution for claws of furry but I wonder whether it'll work in consoles. Only one way to find out :)
     
    lassade likes this.
  7. lassade

    lassade

    Joined:
    Jan 27, 2013
    Posts:
    127
    Wow @Nihil688 your game looks awesome! I'm also very curious to see if the lightmaps will work on consoles.

    Your project is the frist full game I heard that uses my scripts. Do you have any comments or suggetions to make about the my solution?
     
  8. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
    Hey @lassade Thank you very much! We're using a lot of lights in our game to make each level look different and give a unique feeling but I'm not sure how much that affects performance yet. It could be the shaders we're using with Spine. Once we finish up the last chapter of the game we'll start investigating ( xbox one is running 20 fps ) but your solution for 2D lightmaps is the only one I've found so far. I'll let you know when we do!
     
  9. DrDrimixar

    DrDrimixar

    Joined:
    Aug 13, 2014
    Posts:
    5
    Hey @lassade! We're also developing a 2D platformer and would really like to use your solution, but there is a problem. I noticed that Baking lightmaps on packed sprites give a rather weird result. All the light is moved by an offset from the origin point.

    You can observe the same effect occurring in your demoscene as well if you pack background and foreground sprites.

    EDITOR_LIGHT.png PLAYMODE_LIGHT.png
     

    Attached Files:

  10. lassade

    lassade

    Joined:
    Jan 27, 2013
    Posts:
    127
    No idea what's happening, same issue here. My guess is the lightmap scale and offset is relative to the sprites UV when they change, the lights gets mess up.

    I think its possible to fix, but no sure if you can allow rotation on atlas. I will give a closer look at it.
     
  11. lassade

    lassade

    Joined:
    Jan 27, 2013
    Posts:
    127
    Hey @DrDrimixar I pushed a new version of the script that supports SpriteAtlas but is not an ideal solution, give a look at the README to see the limitations.
     
  12. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
    Hey @lassade it seems that your solution gives us about 10 extra fps in our game, especially for console development so I have to say gj!

    I am looking into somehow optimizing the fact that it creates 17 lightmaps for a tiny scene which in turn gets an extra 200mb of size in our game.
     
  13. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
    I think we rushed a bit and I think we're getting the same issue with @DrDrimixar basically some squares are showing up weirdly, ours is more divided into smaller boxes, did you guys solve it somehow?
     
    Last edited: Feb 10, 2018
  14. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
  15. lassade

    lassade

    Joined:
    Jan 27, 2013
    Posts:
    127
    No idea whats happening.
     
  16. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
    It was the atlas
     
  17. lassade

    lassade

    Joined:
    Jan 27, 2013
    Posts:
    127
    A while ago I submited an experimental feature to handle altases in github page check the section
    Sprite Atlas Support (Beta) in the README.md see if it works.
     
  18. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    503
    Yeah I saw that but we didn't have time to implement it unfortunately as it would change quite a lot at this point.
    Also testing this in a few situations I think pre-merging all the meshes could end up with a better result, unity's default merging isn't helping I guess because this isn't taking place in runtime?
    So it's generating a lot of lightmaps and if you have a 3x3 tiling system for a building say it creates a few issues.

    But yes, your implementation is definitely in our game which will soon be out:
    http://store.steampowered.com/app/656580/Claws_of_Furry/
     
    lassade likes this.