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.
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): // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) Shader "Custom/Sprites-Diffuse" { Properties { [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} _Color ("Tint", Color) = (1,1,1,1) [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0 [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1) [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1) [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {} [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0 } SubShader { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" "CanUseSpriteAtlas"="True" } Cull Off Lighting Off ZWrite Off Blend One OneMinusSrcAlpha CGPROGRAM #pragma surface surf Lambert vertex:vert keepalpha #pragma multi_compile _ PIXELSNAP_ON #pragma multi_compile _ ETC1_EXTERNAL_ALPHA #include "UnitySprites.cginc" struct Input { float2 uv_MainTex; fixed4 color; }; void vert (inout appdata_full v, out Input o) { v.vertex.xy *= _Flip.xy; #if defined(PIXELSNAP_ON) v.vertex = UnityPixelSnap (v.vertex); #endif UNITY_INITIALIZE_OUTPUT(Input, o); o.color = v.color * _Color * _RendererColor; } void surf (Input IN, inout SurfaceOutput o) { fixed4 c = SampleSpriteTexture (IN.uv_MainTex) * IN.color; o.Albedo = c.rgb * c.a; o.Alpha = c.a; } ENDCG } Fallback "Transparent/VertexLit" }
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
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?
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!
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.
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.
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.
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.
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?
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.
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/