Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why does lightmapping stop dynamic batching from working?

Discussion in 'iOS and tvOS' started by invulse, Feb 20, 2011.

  1. invulse

    invulse

    Joined:
    Nov 29, 2010
    Posts:
    128
    Maybe I just don't understand how lightmapping and batching work entirely, but in my scene before I lightmap I have 29 draw calls with 156 batched. As soon as I do lightmapping I get 158 draw calls with 0 batched.

    I'm not sure if this is because lightmapping does something to materials to stop them from sharing the same one if they were before, BUT when I clear the lightmap I still get 158 draw calls with 0 batched. There appears to be no way of undoing a lightmap and allowing dynamic batching to work again unless I delete all of the objects in my scene and re-add them.

    Am I doing something wrong or is this a bug?
     
  2. invulse

    invulse

    Joined:
    Nov 29, 2010
    Posts:
    128
    I hate to bump my own thread, but the second issue of not being able get back to having dynamic batching after lightmapping and clearing the lightmap is killing me. The scene I am working on took much to long to setup for me to just delete the level geometry and re-add it. Is there a way to clear a lightmap correctly so I dynamic batching works again?
     
  3. neo

    neo

    Joined:
    Apr 22, 2009
    Posts:
    167
    Not in a position to try and repo your issue at the moment, but have you tried deleting and reimporting the assets? Maybe make a new project and re-import everything? Clearly some setting was set somewhere on these game objects that is telling the engine they are lightmapped.
     
  4. invulse

    invulse

    Joined:
    Nov 29, 2010
    Posts:
    128
    This is what I would like to understand. Lightmapping obviously leaves something behind on objects that from what I have seen is not undo-able. This is really annoying because if I lightmap a scene then decide to only lightmap the floors and walls and not props, then I would have to completely recreate the scene again to do this. There must be some way to undo what lightmapping does to objects. I would really like to hear from devs in this situation as there must be something you can do to undo lightmapping.
     
  5. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,623
    Seems like you are mixing terms: when you lightmap something it should be marked as static in that case static batching works. Do you have that enabled?
    As for a second part of your question (about why deleting lightmaps don't get things back to normal):
    go to Lightmapping window -> Object tab
    here are lightmap settings for selected objects - you can see Atlas here - if you objects had lightmap it will be filled in with some numbers. Alas there seem to be the bug with unity - we don;t clear that values after, so you can change it manually (or from script) to default Tiling X\Y = 1 Offset X\Y = 0
     
  6. invulse

    invulse

    Joined:
    Nov 29, 2010
    Posts:
    128
    I actually only have iOS basic so static batching is currently not an option for me, which is why I am trying to get my scene to correctly work with Dynamic batching. I will try changing the atlas options, but I noticed the objects that have never been lightmapped do not have these options, so I must assume this is a bug.
     
  7. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,623
    it won't because you have different shader values (this Atlas thingie)
    no, they have Atlas options - they are just greyed out and filled with default params
     
  8. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    I'm having the same problem

    This is a bug.

    What is the workaround ?
     
  9. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,623
    read higher
     
  10. Eyeofgod

    Eyeofgod

    Joined:
    Jun 25, 2010
    Posts:
    126
    I just find myself with the same problem but I can throw in more information. Im using static batching, all meshes are marked as static and all have the same material. Now the tricky part: If the lightmapper only generates one texture for all the objects they are batched correctly. BUT, if it generates more than one, the batching don't work as spected. This are the numbers of 100 objects (just planes) with one directional light and diffuse shader:
    Without lightmap: 1 Draw, 100 batched
    With 1 lightmap: 1 Draw 100 batched
    With 2 lightmaps: 18 draws, 86 batched
    With 3 lightmaps: 26 draws, 82 batched

    To remove the lightmap I have tried the commented method but it just don't work. I select all objects (its the same prefab) and pressed clear. But do I have to clear manually all offset manually?

    Can anybody help me
     
  11. macdude2

    macdude2

    Joined:
    Sep 22, 2010
    Posts:
    686
    ^ I do believe they said you had to go through and reset each one manually or you could make a script that could do it for you. However, I have no idea how you could go around making this script. I also got the exact same results as you with light mapping. I have no idea what causes this. At the most, there should be an extra call per map, but why the number off raw calls and batched objects changes with each new lightmap, I have no idea. My work around was just to use one 2048 map with low resolution lightmaps. Although it would be nice to hear some input from the admin though. I wonder if they have any answer about this weird lightmapping problem.
     
  12. Jackrabbit82

    Jackrabbit82

    Joined:
    Mar 13, 2013
    Posts:
    16
    I'm running into the same issue with Unity 4, all my level is made with tiles and when i lightmap it the draw calls are going crazy. I don't understand how reseting the tiling and offset in the lightmapping pannel could help in anyway to avoid this since the tiling and offset cannot be 1 and 0. It's not working at all since this is remapping all the assets in a wierd manner which apply the whole lightmap to objects. This was working perfectly in Unity 3.5 and I've never had to play with those values since that is what beast is doing remapping assets on the lightmap... Can someone explain how this is supposed to be a workaround and how it works ?
     
  13. Meceka

    Meceka

    Joined:
    Dec 23, 2013
    Posts:
    423
    Sorry for replying on this old topic, but i have been searching for a solution to this since about 3 hours, trying changing settings and testing. But dynamic batching is definitely gone when i enable lightmapping.

    I have just one lightmap for all objects, but it still breaks lightmapping.

    Is there a solution or workaround now? Is that bug still not fixed?

    Thanks.
     
  14. Meceka

    Meceka

    Joined:
    Dec 23, 2013
    Posts:
    423
    Please, I need help with this. Thanks.
     
  15. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    I'm working on this right now for a client. It has to do with the fact that Unity stores some 'hidden' data in the MeshRenderer which acts as parameters to the shader functionality. Basically you can't use light maps and have things batched because the shader needs to be set-up for each object (it's the same as having multiple materials).

    I am writing a script that bakes the light map data into the UVs and then using a custom shader. I believe that will solve the issues.
     
    Arkade likes this.
  16. Meceka

    Meceka

    Joined:
    Dec 23, 2013
    Posts:
    423
    Sounds great Jodon, thanks.
     
  17. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    Just to let you know, this was a success. I don't own the code so I can't post it. Here are the steps:

    Renderer.lightmapTililingOffset
    to grab the UV scaling and offset into the Lightmap. Then you grab its mesh and rewrite the UV2 so that it takes that tiling offset into account.

    Next you want to disable Unity's lightmapping on that object. To do this, set the lightmapTilingOffset to (1,1,0,0) and the lightmapIndex to -1.

    Finally, write a custom shader that takes the UV2 channel and uses it as a look-up into your own texture. Use the level's lightmap as that texture.

    This will ensure your prefab can batch and you are using the lightmapping functionality to achieve better, baked lighting.
     
    Arkade likes this.
  18. Meceka

    Meceka

    Joined:
    Dec 23, 2013
    Posts:
    423
    Can you please share the link that you have found the code and explanation? I don't believe I can do this myself. I didn't write any shaders at all And I have thousands of objects in my scene can I automate this?

    Thanks.
     
  19. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    Unfortunately there's no link, I had found this all out through experimentation. The link that explains Dynamic Batching breaking is: DrawCallBatching. I will ask my company if I can post relevant code. It took around 4 hours to get it working correctly.

    Cheers.
     
  20. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    654
    Sorry for awaking a sleeping thread but for future readers, there's a script that seems to do some of this in this old thread. I'm just trying to apply Jodon's advice from above, coupled with asset duplication to create new meshes (rather than modifying the original ones). Hoping to make something that will allow multiple applications (or updating). Do you think that's something people might find helpful? (perhaps as an asset?) Or is something already available?
     
  21. Meceka

    Meceka

    Joined:
    Dec 23, 2013
    Posts:
    423
    I didn't understand your explanation, but if you can make an asset that will enable batching on lightmapped objects, I need it.
     
    Arkade likes this.
  22. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    654
    Ok, thanks for the feedback. I'll see about crafting an asset. I wonder how large the market for it is...? (and how to explain why it is useful to those who do not already need!)
     
  23. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    654