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.
618 stars 88 forks source link

Transparency in Build #64

Closed Baltzs closed 6 months ago

Baltzs commented 7 months ago

Hello there,

First of all, your addon is a really helpfull and powerfull feature that should be in unity by default, so thank you very much for your work.

I'm writing to you as I don't think this issue has been mentionned before.

I'm working with Unity 2021.3.23, on HDRP.

Our game is a rogue like where each room is being instanciated in a specific scene as a prefab.

In theses rooms, specific walls are made of a single mesh with multiple materials, for the walls and windows. (2 opaque 1 transparent for the window). When baking theses, in the editor everything works fine, window's transparency is being correctly displayed. However when building the project, every single object that was baked looses it's transparency, (I think decals are also affected by this).

I've solved this issued by not baking any transparent materials, however that would mean we would have to split all of our transparent meshes.

Is this somehting that we absolutely have to do, or is it possible to have the build working like in the editor?

I don't fully understand how the lightmaps are read in the build, and might be missing something, and I hoped you could enlight me about a thing or two.

Hope you find the time to help us out! Thanks again!

image_2023-12-11_114202197

Ayfel commented 7 months ago

Hi,

If you bake a regular transparent object and build a scene with that object (without instantiation, to rule out the prefab light mapping) does it work properly? I am not sure why this would be happening so let's dig a bit on how things are setup. Are you loading your prefabs from disk, from assetbundles?

Baltzs commented 7 months ago

Hey, thanks for the quick response,

So were not using assetbundle, were loading prefab directly from the disk.

I did a test of creating a new scene, two cube with 2 transparent mtl (the one were using on our windows, and a new one I did.)

Both worked correctly in the build.

I did another test of building the exact same scene but with the prefab of one of our room loaded inside (nothing else but the player), and the issue was up again, our window was opaque.

To be extra sure, i did another build with unpacking the prefab and deleting the lightmap prefab component, and this time it worked.

So my guess is that the issue come from the light map prefab, might be because of the way lightmaps are merged with the base color of the elements. For opaque objects you can just multiply the lightmap to the base color of the material, but for material with alpha it dosen't work the same since unity is reading the image alpha as the alpha of the material.

Now why it dosen't work in the build while it works well in the editor, i'm not sure, once again I don't exactly know how all of this works!

pbsdles2 pbsdles3 pbsdles4

Ayfel commented 7 months ago

Can you take a look at your graphics settings in project settings, it might look different in HDRP but you should take a look at shader stripping etc to make sure all modes you need are included

image

Baltzs commented 7 months ago

Yes, sorry i should have mentionned i did the thing you suggested on other posts before submitting.

Here is it under HDRP, does it looks okay to you?

image

Ayfel commented 7 months ago

Thanks, can you change the instancing variants to not strip?

image

Baltzs commented 7 months ago

I set it up to "keep all", built the same scene again, stil did not work, but it did took some time to build (~20 min).

image

image

Ayfel commented 7 months ago

can you make a small repo project for me to check? just same version of unity, hdrp and settings with a simple model/material and prefab for me to check. I suspect something in the HDRP shaders is making it do this, but its probably version specific too

Baltzs commented 7 months ago

Okay, sorry for the delay.

I've created a specific repo based on our main one, I've sent you an invitation.

I've set up three windows: one which has a prefab lightmap, another one that shares the same material as the prefab but is not a prefab, and a last one which has its own material and not a prefab.

The prefab lightmap is opaque again, and the interesting thing is that the material on the other window that is not supposed to have the lightmap prefab is also opaque.

This means that if a mesh shares the same transparent material as a prefab lightmap, it also loses its transparency

Hope you can check that and understand something about it! Thanks again!

image

Ayfel commented 6 months ago

So when the system applies the lightmaps it reassigns shaders, as this was needed in certain cases and certain materials. For these HDRP shaders it seems like that is causing the "transparent" keyword to be lost. So I added a new bool to the script, you can uncheck this in your prefabs and it seems to work well.

image

image

Download the latest version from master to access that. I left it true by default but you can uncheck it.

Baltzs commented 6 months ago

Thank you so much for your quick answer and reaction!!

Just made another build and it works perfectly! This is awsome and a great load off of our shoulders!

Hope you have a wonderfull day, thanks again !!

image

Ayfel commented 6 months ago

Happy to help : )

Nattuhan commented 5 months ago

@Ayfel Hi, thank you for the great source code. I faced the same problem and came across this issue and solution. If possible, I would like to know what the purpose of the few lines of processing that are skipped when the bool is set to false are originally written for ("release shaders" are needed in specific cases, for optimization, etc...). ). Thanks.

Ayfel commented 5 months ago

So that line just reassigns the shaders to the materials, and it is (was) necessary in certain cases where the lightmap wouldn't show up unless the shader was reassigned/reset. I have found that different versions of Unity behave differently in some cases and this is a regression that happens in some cases when loading from assetbundles etc. Technically it shouldn't be necessary but sometimes it is unfortunately.