A Unity ID allows you to buy and/or subscribe to Unity products and services, shop in the Asset Store and participate
in the Unity community.
Discussion in 'Global Illumination' started by GSdev, May 8, 2015.
Still no terrain support?
What do you mean? We have lightmaps on our terrain and it works pretty much in a similar way. So: terrain is supported.
Can anyone provide some more instructions for using this PrefabLightmapData script? I followed the instructions at the start of this thread but I can't get it to work. Should it work with directional lights and terrains? And does it work in Unity 5.3? Thanks
See my previous reply.
The fixes on the last few pages are for 5.3.x.
HI guys I am excited to announce that I have made editor utility for Prefabmap lightmap,
With regards to existing features ,You can now select texture size and format for generated lightmaps ,
It also has LOD options.
Also plz feel free to customize it more and share with community.
Let me know your review and suggestions.
It is not actually my work, it is excellent work of @Centripetal , when we were having Unity bug (You can read about it from page 2) .I just added LOD groups and GUI layer above it.
Fortunately after bug case to Unity, later version of Unity did solve that bug.
Now,I believe original script by @Joachim_Ante do work.
Check if that solves your problem, here is modified version of that script by @WillemKokke ,
Hi guys, I've been trying to sort out the lightmapping problems for the past couple of days, with no success. The story is:
- I have this scene that takes ages for the lights to bake (around 6 hours)
- When the baking is done, even if I adjust just one light (from so many), or even if I DON*T CHANGE ANYTHING, when I click "Build" under Lighting to bake the lights (whoever thought that it would be a good idea to leave that to AUTO by default... well, just to say that it wasn't a good idea), the whole process starts all over again. Yep, 6 hours...
- Searching for a smarter alternative, I came upon this forum thread and I though: "God, I'm saved". But not so fast!
- I'm not that good at coding, so finding how to implement the C# code was a pain in the a..e, but I got it eventually (note to people posting here: maybe it would help adding some instructions on how to actually implement your stuff, please)
- Only to find that it's not actually working the way I way expecting. Or maybe I'm that stupid that I don't know how to do it right. Anyway, below is an explanatory video:
I would be more that happy to find a way that actually works in order to:
- being able to make minor adjustment in the scene without having to wait for the WHOLE scene to be light baked again.
- being able to save the lightmaps that will give the expected results, and so being able to work on different part of the scene without affecting the whole lightmaps
Here is small tutorial I made,plz check it out and let me know if it helps,
Thank for your reply. I will try to follow your tutorial and let you know of the outcome (please, please work!!!! )
Something is wrong. I make a prefab, I hit Build and bake the scene (directional specular), and everything is fine.
Then I add the Prefab Lightmap Data script to the root of the prefab, I click on Bake Prefab Lightmaps.
Stuff happens but my prefab lightmap ambient suddenly becomes black or very dark. I look in the Lightmaps tab and see 2 copies of the same lightmap stacked in the list.
If I try to open a new scene, disable Auto bake, set the atlas size etc., drag the prefab in, and apply the lightmap, it's still black (AND it does NOT have 2 copies of the same lightmap in the Lightmaps tab).
I tried a bunch of different ways, and sometimes hitting "Bake Prefab Lightmaps" will even assign an extra copy of the PrefabLightmapData script onto the prefab but will apply the prefab with only the first script instance. (the copy is in bold on the prefab in the inspector)
Tried a bunch of versions of your script, including @idurvesh 's version with the custom editor (and followed his video) and the same happened.
Tried unity 5.3.3, 5.3.4 and 5.4.0b10
I created a new empty project, brought the scripts in, and they worked.
Then I started baking a "real" prefab, it worked. Then I tried another prefab but it went back to black ambients. And it won't go back to working.
Now I'm making a new project each time I need to bake a prefab...
Note: I noticed you have to make sure you baked your Reflection Source or ambient source / probe whatever, in your target main scene, or else the lightmapped prefab you bring in will have black speculars. But this still only works for me if/when I make a fresh new project for each prefab :C
i thought this script does not work with directional specular?
correct me if am wrong (oh please let me be wrong ^^)
When it works at all, it also works with directional specular. Make sure you bake with dir spec, and when you load the prefab in, you also have dir spec turned on in that scene, and that you have baked your main scene with your Reflection Source before you bring in the lightmapped prefab.
WE NEED TO POST A GUIDE AND THE LATEST CODE IN THE TOP POST. (also would be nice if unity took care of this since it's their work we're doing here)
Can you share screenshot? I just tried here in my system, it worked fine.
Having the same problem:
- I bake the scene and everything looks fine
- using the script to bake the prefab lightmap will turn the scene to black
- updating the prefab lightmap will turn off baking on the previously baked scene, and that lightmap will be completely lost, i.e. I have to start baking again.
I thought I was the only one having this issue by doing something wrong, but apparently not. By the way, I followed idurvesh's video. I'm not saying that it's not working at all, only it's not working for some people.
just watched that video for the first time
i do it differently
(the only time i get black prefabs/tiles is when i dont set them to static)
so first i set the tile to lightmap static, disable rotation
lightining is set to baked gi
general gi is set to directional specular, default-high resolution
auto build is always disabled
then i just click on backe prefab lightmaps under the asset tab
I dont bake with the lightining tab
i dont assign lightmaps manually at all
i dont have the prefab lightmap data script on my prefabs
so tried it like its done in the video
and it still works
cant help you here .. :|
I'm getting this sort of thing:
Baked from lighting tab:
After saving the baked lightmaps to prefab, and also after loading the lightmaps from prefab, looks like this:
This doesn't always happen. If I start a new project it always works.
Then I do stuff and suddenly it doesn't work. (couldn't identify yet what)
Also, if I copy a project to a new computer it stops working.
I'm thinking somehow something in the Library or Project Settings folders gets screwed up after multiple uses of the prefab lightmaps script with different settings on different bakes.
<-- I cannot remove this image insert lol
hmm did you remove the "old lightmap script" when you do a new bake?
When i assign one to my tiles there is a new script for every bake
usually i dont assign any scripts it all and it still works magically ^^
and i am using DunGen for my runtime tests
Adding to what @Arganth already asked.... Have you set Lightning Data Asset for new scene to which "Baked scene" created?
Also make sure you select proper file format while baking ...Try with "RGB_ETC 4bit"
That might be what causes the issue. Maybe I should be careful to always remove the old lightmap script, and maybe do a scene bake with all assets turned off to "cleanse" the cached lightmap assets, before I do a new proper bake.
But I just tried that now on a project I know got screwed up a few days ago, and it's still screwed up, even if I do the above. Still might help prevent it though.
Baking goes fine:
Assigning to prefabs not so fine:
I think in that case the custom editor button will trigger a bake, but if there's no script on the prefab then the prefab won't store its lightmap indexes and references and stuff.
oh i did not know that was possible ^^
seems i have been lucky that the folder was the right none (or it gets created automatically)
could you upload this scene file?
i really would like to test it for myself (later when i am actually home ^^)
Yes plz upload scene files or you can PM me your "Team viewer" details...I will connect you remotely
@Arganth & @idurvesh OK. I added 2 small projects. One in Unity 5.3.4f1 and one in 5.4.0b10.
After cleaning up some baked lightmap folders in the 5.4.0b10 project, I'm now actually getting an error in the console in that one (when I try to bake into prefab), about index mismatch with the LightmapSettings (even though the prefabs have a fresh script component on them with no references). Haven't had time to read the code.
Either way, both projects should give you dark lightmaps, and at least in one of them there are examples of already baked dark lightmaps. Check the scenes (there are 2 or 3 in each) and the prefabs.
The projects are here: https://drive.google.com/open?id=0B50afmg1TY-eNnZfNkZxSDhCTlE
so after I discovered the other script with the editor window lol
I tried it now on my laptop (without dungen) just the test scene
and it still worked AFTER i set the textureimporterformat to argb32
the default alpha8 just blacked out for me
ok i can test them later thouroughly ^^
i just gave it a quick test on my laptop
and i get the same errors you mentioned
Are you guys getting error only on 5.4 or 5.3 too?
could you try out this project:
and see if this works for you
maybe i am totally wrong but it seems to work for me
(but unity lightmapping kills me ^^)
I guess I found a way around this problem. I guess... No scripts required.
I create a new scene. In Lighting - Scene I set Directional mode to Non-Directional, and uncheck the Auto option (no auto light baking).
I then create an Empty object, and use it as parent for all the objects in the scene. Lights will be set to Baked, with Hard / Soft Shadows. Then I set the Empty object to static, and bake the lights (Lighting - Build). Save the scene.
I then create a second scene, simillar to above. When baking on the second scene is done, from the Project window I drag the first scene into Hierarchy window. And voila, the first scene is there, with baked lights.
MAKE SURE THAT AUTO OPTION UNDER LIGHTING IS UNCHECKED, if don't want your scene to be baked again.
The error is in the 5.4 project only, but it's not because it's 5.4. It happened because in that project, to clean it up, I renamed a lightmapped scene and its lightmaps manually (in unity, and the meta files should have been cool with that). It used to "work" identically to the 5.3 project before I did that. My "mistake" I guess.
Yes, that project works. So it's strange that when I moved a project from one pc to another it stopped working. Maybe it's some weird unity settings on the other computer. But there's also (for an as of yet unknown reason) the problem with projects eventually breaking on the same machine after baking multiple things.
Oh, so you mean without using the PrefabLightmapData script in this thread?
Ok, I guess in that case, when you load scenes additively, unity might use the ambient light from the main scene onto the other scenes as well. But if you bake without directionalSpecular in your secondary scene, I don't think it will get specularity from your active scene.
NO, WAIT, I found the problem!
Open this project, then go to Player settings, set it to Deferred rendering and Linear color. Then bake lightmap and it's fine. THEN, "bake prefab lightmap" and it goes black!
So this S*** doesn't work in deferred? That's a pretty big issue.
The reason it worked "in new projects" was because all new projects are Forward by default. Has nobody here used this script in deferred mode, srsly?
[EDIT] I can confirm that creating a new unity project, setting it to deferred and linear, baking lights, then baking lightmaps to prefab, will result in black ambients. So it's not the fact that you switch from having baked in forward to baking in deferred that breaks it.
[EDIT2] Ambients retain 0 information, it's not that they're just darker. Tried baking with max ambient intensity, max indirect intensity max bounce boost, and after baking to prefab, the whole scene is almost white except for the shadows / ambient light regions which are solid black.
[EDIT3] I was wrong: it's only the Color Space: Linear that breaks it. Deferred + Gamma works. Though gamma is not an acceptable color space for my project(s).
@Joachim_Ante do you have any idea why lightmaps don't work in linear color space?
hmm have to try that later
just had the problem that i could not get my scenes "black" again....
have to test with ambience on
usually I set it to 0 for all my scenes
You are correct, Linear color space mess up "Direction Specular" lightmaps BTW "Directional" works quite well...Seems like reflection causing problem with Linear space..... @Centripetal please have look in...
Going back to the drawing board :-(
Baking the scenes separately, without the script, works fine. Creating a new scene and dragging all necessary the scenes into hierarchy works fine as well. The problem is when I want to Build the project, and that's when only one scene from the Hierarchy (the Active one) will be exported, and not all of them as I was expecting.
In the Hierarchy, dragging the scene content from one scene to another - in order to have all the objects within one Active scene - will break the lightmaps.
So I guess I'll be trying to find a solution with the scripts method again. Arrrrrrrrrrghhhh.... One thing is for sure: after finishing with my school project in 6 weeks, I won't get near Unity again for a couple of years, until some basic problems would have been (hopefully) properly dealt with.
I'm having a problem with this script when I build the game on Android. Here's a screenshot:
And here's how it's supposed to look (This is the PC build):
Does anyone know what the problem is? Thanks!
not sure but could be a gamma/linear thing
What type of shaders you use? try disabling directional light and check.....
I was using the Standard shader, but I just tried with Mobile/Diffuse. I also tried disabling the directional light. There's no difference
Has anyone here had any issues with baking Reflection Probes?
I have a main scene with some baked stuff, then I bring in some lightmapped prefabs, and I Build Reflection Probes.
The reflection probes are kinda sorta up-side down but not really:
How could this possibly have screwed up in such an inconsistent way?
It's actually not up side down, it's rotated around the world X axis by 180. DA FUQ
Is it related to when and how I bake my main scene and whether I have updated the scene with prefab lightmaps?
[UPDATE] It wasn't due to the baked lightmap to prefab workflow. It happens when you turn off HDR on the probe (still weird as hell).
I'm having a problem with my lightmaps when reassigning them and maybe some of you had also that problem:
When assigning the lightmaps back from our prefabs, we get a weird second shadow projection onto all assets.
This is before assigning lightmaps loaded scene:
This is after:
You can see the strange shadow on the front of the tent, which is actually a shadow by the tent on itself in a smaller version. We can't figure out why the shadow is duplicated or where it comes from as we only have one light in the scene.
Every asset in the scene is downloaded from an assetbundle and then instantiated as prefab. Works fine except for the second shadow.
Did anyone else saw such a problem?
Thanks for help!
I have been trying the editor tool idurvesh provided with varying levels of success, but one issue I have is I don't know what texture import format I should be using. How do I know this?
Hey guys good news, I got mail from Unity bug team, they have solved issue with " LightmapSettings.lightmaps.lightmapFar " which caused us to find alternate hard solution ...
Now I suppose script made by @Joachim_Ante shall work fine and all this duplicate/hectic work can be saved in 5.3.4p4
Thanks buddy..Your solution is working perfectly!
Thanks much... can this solution work with bundles also ?
Yes, I can confirm that.
I'm having a problem with shadows, and I have a feeling that it has to do with the baked lighting. If anyone wants to help out, that'd be greatly appreciated. Here's the thread:
Thanks much ajay and pahe...
One more question.... number of pixel lights on baking... I have ambient, one point, and 2 spots on a statue... in real time it lokos great, but when baked it ignores one of the spot lights. why ? I have number of pixel lights set to 8 (lots for the hell of it) but I would have thought you can have as many pixel lights as you want when baking.. (but limited for real time) but it seems the opposite... many ok in real time, but only a couple when baked.
any idea what can cause this ?
How / What are you bundling to make this work? I have a scene with empty object as a parent to everythign in the scene. This empty obejct, is my prefab. I bake lighting and export this prefab as a bundle... but when loading from server into app, it is all dark, and has no light mapping. After following AjayTaker3's instructions above... what prefab would you bundle to make that work please ? help appreciated, thanks.
actually, I just tried AjayTaker3 technique, and I just ended up with a 2 color level.. just white and black only. no textures, no lightmap. (looks like a 2 color display image) so I guess that doesn;t work for me anyway.
@JonnyHilly The lightmaps are existing in the assetbundle? So they are found and not null when you reassign them?
If they are not in the bundle, make sure that you don't reference the generated lightmaps from Unity in the prefab, but instead your own copies! The generated lightmaps will not be copied into the assetbundles, as they are not linked to the prefab, but the scene instance.
OK got it working with a bundled prefab, plus automatically working after downloading and instantiating bundled prefab (or a regular non bundled prefab) ... uses a modified version of the script posted above.
There are instructions in the manager script, but here they are again...
Make empty object... put everything inside it, make that your prefab. Put the LightMapManager script onto this object.
Note:- if your scene is static, and you dont need the lights.. you can put them outside of your main prefab. (but in the same scene, they will create the lightmap, but not be included in the prefab this way.
Bake your lightmap, then hit the checkboxes on the script, which copies the lightmaps into the array on that prefab, and it add scripts to all other objects with renderers in that scene ( and store the lightmap settings per rendered object)
Once done, just click the checkbox that says READY_TO_GO and click apply on the prefab. Now when you load the prefab, it auto sets up the lightmap textures, and applies the stored settings to each render object. Tested in various scenes, and with bundle uploaded to server, and downloaded into another project.
These can probably be cleaned up more.... maybe make sure there is no update() running in the LightMapObject script.... but anyway, here they are... LightMapManager.cs and LightMapObject.cs
//1. Put this script on root prefab that contains all your light mapped object to be bundled
//2. then render your lightmaps
//3. then click the m_getLightmapArray checkbox, (make sure scene tightmap textures were copied into the array)
//4. then the m_initRenderObjects checkbox. (make sure each object with renderer now has a LightMapObject component, with saved lightmap values)
//5. then turn on the READY_TO_GO checkbox.
//6. click apply to your prefab. (prefab is ready to use , and works as a prefab, or as a bundled prefab)
//7. Note:- LightMapObject has an Update() for test purposes... suggest you remove this for performance in your final version.
public class LightMapManager : MonoBehaviour
public bool READY_TO_GO = false; //set this tru, once you have grabbed
public bool m_getLightmapArray = false;
public bool m_setLightmapArray = false;
public bool m_initRenderObjects = false;
public Texture2D m_lightmapArray;
public void Start()
public void getSceneLightMapTextures()
if (LightmapSettings.lightmaps.Length > 0)
int num = LightmapSettings.lightmaps.Length;
Debug.Log("GET LIGHT MAP ARRAY, NUM =" + num);
m_lightmapArray = new Texture2D[num];
for (int i = 0; i < num; i++)
m_lightmapArray[i] = LightmapSettings.lightmaps[i].lightmapFar;
Debug.Log("GET LIGHT MAP ARRAY, NUM Lightmaps = 0");
public void setSceneLightMapTextures()
if (m_lightmapArray.Length > 0)
Debug.Log("SET LIGHT MAP ARRAY, NUM =" + m_lightmapArray.Length);
LightmapData lightmapData = new LightmapData[m_lightmapArray.Length];
for (int i = 0; i < lightmapData.Length; i++)
lightmapData[i] = new LightmapData();
lightmapData[i].lightmapFar = m_lightmapArray[i];
LightmapSettings.lightmaps = lightmapData;
Debug.Log("SET LIGHT MAP ARRAY, NUM =0");
//add LightMapObject script to all objects with renderes, then run it to save lightmap settings to them all
public void setupLightMappedRenderObjects()
Renderer objs = gameObject.GetComponentsInChildren<Renderer>();
foreach(Renderer rend in objs)
LightMapObject lmo = rend.gameObject.GetComponent<LightMapObject>(); //check for existing
lmo = rend.gameObject.AddComponent < LightMapObject>(); //if none, add the component
//get the object ready to render after loading bundle, and pre saved data
public void readyLightMappedRenderObjects()
Renderer objs = gameObject.GetComponentsInChildren<Renderer>();
foreach (Renderer rend in objs)
LightMapObject lmo = rend.gameObject.GetComponent<LightMapObject>(); //check for lightmap values
if (lmo != null)
void Update ()
if(m_getLightmapArray) //copy lightmaps from unity into this prefab
m_getLightmapArray = false;
m_setLightmapArray = false;
m_initRenderObjects = false;
//used in conjunction with LightMapManager.cs
//added automatically to objects with renderers in scene to be lightmapped and bundled (manager will add it automatically if not there)
public class LightMapObject : MonoBehaviour
public bool m_setLightmapValues; //for test
public bool m_getLightmapValues; //for test
public int m_lightmapIndex = 255;
public float m_lightmapTilingX = 1f;
public float m_lightmapTilingY = 1f;
public float m_lightmapOffsetX = 0f;
public float m_lightmapOffsetY = 0f;
public void setValues()
if (r = GetComponent<Renderer>())
Debug.Log("SET LIGHT MAP VALUES");
r.lightmapIndex = m_lightmapIndex;
r.lightmapScaleOffset = new Vector4(m_lightmapTilingX, m_lightmapTilingY, m_lightmapOffsetX, m_lightmapOffsetY);
Debug.Log("SET LIGHT MAP VALUES, can't find renderer");
public void getValues()
if (r = GetComponent<Renderer>())
Debug.Log("GET LIGHT MAP VALUES");
m_lightmapIndex = r.lightmapIndex;
m_lightmapTilingX = r.lightmapScaleOffset.x;
m_lightmapTilingY = r.lightmapScaleOffset.y;
m_lightmapOffsetX = r.lightmapScaleOffset.z;
m_lightmapOffsetY = r.lightmapScaleOffset.w;
Debug.Log("GET LIGHT MAP VALUES, can't find renderer");
//this function is for debug... if this works for you... just comment out this update function for performance reasons
void Update ()
m_setLightmapValues = false;
m_getLightmapValues = false;
Thanks for your prefab assetbundle code. I'm using Unity 5.3.4p6 and even it looks to work in Editor, I'm not able to make it work using the assetbundle on the Android device. In my case it is the map in the game which is lightmapped, and I get it dark. Any guess?