Ayfel / PrefabLightmapping

Script for saving lightmapping data to prefabs. Used through the Assets tab in Unity. Place your prefbas in the scene with this script at the root. Set up your lighting and in the editor go to Assets->Bake Prefab Lightmaps. After is processed you can now spawn your prefabs in different scenes and they will use the lightmapping from the original scene.
655 stars 91 forks source link

Inconsistent application of lightmaps on rotated prefabs instantiated by script #41

Closed michaeloverton closed 3 years ago

michaeloverton commented 3 years ago

Thank you so much for your work on this. This is really great stuff. I am having an issue with the lightmaps sometimes not being applied to my prefabs (happens about 30% of the time).

I am trying to build an "infinite office space". I make prefab rooms, and then as the player traverses the rooms, more are generated on the fly, and old ones are destroyed. To generate the lightmaps, I have put each room in its own scene, and I then use your "Bake Prefab Lightmaps" option. This works, and populates the Prefab Lightmap Data on each prefab room. Within each room's scene, I can also drag in copies of that room, and the lighting works on each one.

For the actual "game", I Instantiate() the prefabs using a script. Most of the time, the lighting is correct, but sometimes, the lighting looks very strange. Do you have any ideas why this would be happening?

Here are some relevant images: https://imgur.com/a/KBlkGAb

When I baked the lighting, I set everything in the prefab as Static. After baking, I tried making the prefabs NOT static. This did not fix the problem. I also tried keeping them static and had the same problem.

The inconsistency is very confusing to me. Any help would be amazing. Thank you so so much.

michaeloverton commented 3 years ago

note: in the images, i made a typo - below the third image, it should say dragging PREFABS into scene gives correct lighting

Ayfel commented 3 years ago

Can you share your lighting settings, what type of lights you use (mixed, baked, realtime, etc) and how this lighting is setup? Also what render pipeline you use and lighting settings of the scenes where you instantiate? It is strange that you get different results dragging vs instantiating, which would make me suspect something in that process but hard to say

Ayfel commented 3 years ago

Also you said sometimes the lighting is wrong but sometimes is fine, does it happen consistently with the same prefabs that the lighting is not working? Do you get the same result in a build vs the editor?

michaeloverton commented 3 years ago

Thanks for your response.

  1. The lighting issues happen with all prefabs. It does NOT happen with just some prefabs.
  2. I get the same results in the editor and both production and development builds.
  3. In an attempt to fix the problem, I made ALL lights be baked. They are mostly baked point lights and some baked area lights. One weirdness I noticed is that sometimes even if I delete the prefab lighting data, and even though the point lights are set to "baked", I end up seeing light in the prefab (in this case, shouldn't the prefab be dark, because I have baked no light?)
  4. I'm using the standard render pipeline.

Could this be a side effect of the materials that are being used on the assets? I noticed on some assets, the material being used has Emission Global Illumination set to Realtime. Would this make a difference?

Another thing I notice is that the walls of my prefab rooms are actually 3D Planes (as opposed to solid boxes). Could this cause problems?

It is confusing to me because in the case where the lighting is bad, the lightmap is actually still applied, but the lightmap appears to be darkened. I know the lightmap is applied, because if it weren't the asset would simply be nearly black, right?

Here are some relevant images and descriptions.

https://imgur.com/a/ZC4JKAC

Thank you so much for your help. I'm not a lighting expert - I am learning about this stuff as I go.

michaeloverton commented 3 years ago

Also, I am using Unity 2019.4.13f1 if that helps...

Ayfel commented 3 years ago

It shouldn't be because of materials at all. It also looks like the prefab has lightmaps applied as all the shadows show there in the same way. An easy way to see this is to disable all lights in the scene and see if they have still light/shadows. It feels like there is some extra light when it looks fine that's why I was asking. Is there a way you could make a reproducible project for me to check or similar. I am not seeing anything obvious wrong here and the setup is basically the same I mostly use, Unity 2019 and Standard render pipeline with regular lighting settings. I recommend setting the Bake Lighting Mode to "Bake Indirect" instead though so it will bake everything. Let me know after you test that.

zcwaa22 commented 3 years ago

Hi @Ayfel and @michaeloverton Sorry to hijack this thread but I thought I'd share what I've found in relation to this issue.

I'm also creating a "infinite" space, a corridor that is instantiated as you move along it. My root "block" prefabs contain, walls and door and its these "block" prefabs which I first add the PrefabLighmapData script to, bake and then instantiate at runtime.

What I've been finding is that instantiated "blocks" which have a rotation of 0 on the Y axis look perfect but as soon as I instantiate them at 90, 180 or 270, or indeed change the transform's value of Y rotation while the engine is running the lightmaps get dark and blotchy.

I'm new to lighting so maybe this is an error on my part elsewhere but if not it might help shed some light on what's happening?

michaeloverton commented 3 years ago

@zcwaa22 Oh, excellent observation! I haven't tested that. Does it happen when you manually place the prefabs into a new scene (by hand as opposed to script instantiation) and rotate them? Any ideas about this @Ayfel ? I will test this on my end soon.

zcwaa22 commented 3 years ago

@zcwaa22 Oh, excellent observation! I haven't tested that. Does it happen when you manually place the prefabs into a new scene (by hand as opposed to script instantiation) and rotate them? Any ideas about this @Ayfel ? I will test this on my end soon.

Yep. Both dragging a baked lightmapped prefab and rotating it when the scene isn't running and when the scene is running produces the same undesired result for me..

EDIT:> Selecting "Direction Mode - Non-Directional" in Lightmapper Settings just fixed the issue for me - hope it does for you :)

Ayfel commented 3 years ago

Oh so its the Directional mode that was causing it for you? I'll need to test that out

michaeloverton commented 3 years ago

I've confirmed that this is indeed the case for me as well. The issue is the worst when the rotation is 180 degrees, but it is also incorrect at 90 and 270.

This does make me think of a potential workaround, though - make separate prefabs with separate lightmaps for each rotation, and swap them in as necessary. This would really not be ideal, however...

Here is an example: https://imgur.com/a/hIJfNJp

zcwaa22 commented 3 years ago

Hi @michaeloverton have you tried changing the directional mode in lightmapper settings- this is what fixed the issue for me

michaeloverton commented 3 years ago

That fixed it for me too! Thank you so much. So if I'm understanding things correctly, this is because:

When you bake a Directional lightmap, Unity generates two lightmap textures. One texture stores information about the intensity and color of the lighting received across the target surface. This is identical to the Non-Directional lightmap. The other texture stores the dominant light direction, along with a factor describing how much of the total light received comes from that dominant direction.

The "second texture" about the dominant light direction is what makes the lightmap incorrect upon rotation in our case. Makes sense.

Thank you so much, both of you. Will close the issue.

michaeloverton commented 3 years ago

@Ayfel thanks again. It might be a good idea to put something in the FAQ about not using directional lightmaps if the prefabs will be rotated. Up to you, though, as I know you're probably busy.

Ayfel commented 3 years ago

Yes I agree, I want to explore the reason behind this because initially I had disabled directional lightmapping and defaulting everything to non-directional and later on re-enabled the option because everything seemed to work fine. I want to understand how Unity store this directional information.