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

"Index was outside the bounds of the array" #38

Open viv-01 opened 3 years ago

viv-01 commented 3 years ago

I'm getting the following error, Can you advise?

IndexOutOfRangeException: Index was outside the bounds of the array. PrefabLightmapData.GenerateLightmapInfo (UnityEngine.GameObject root, System.Collections.Generic.List1[T] rendererInfos, System.Collections.Generic.List1[T] lightmaps, System.Collections.Generic.List1[T] lightmapsDir, System.Collections.Generic.List1[T] shadowMasks, System.Collections.Generic.List`1[T] lightsInfo) (at Assets/Scripts/PrefabLightmapData.cs:245) PrefabLightmapData.GenerateLightmapInfo () (at Assets/Scripts/PrefabLightmapData.cs:182)

Ayfel commented 3 years ago

Something is off, I'd recommend removing the script and adding it again to the prefab and rebake

1ibrium commented 3 years ago

Hi bro! A value of 0xFFFE is internally used for objects that have their scale in lightmap set to 0; they affect lightmaps, but don't have a lightmap assigned themselves.

https://docs.unity3d.com/ScriptReference/Renderer-lightmapIndex.html

Ayfel commented 3 years ago

Hi bro! A value of 0xFFFE is internally used for objects that have their scale in lightmap set to 0; they affect lightmaps, but don't have a lightmap assigned themselves.

https://docs.unity3d.com/ScriptReference/Renderer-lightmapIndex.html

I am not following, what is this in reference to?

1ibrium commented 2 years ago

I fixed this issue by follow this reference, This issue triggered by "scale in lightmap set to 0"

Ayfel commented 2 years ago

I fixed this issue by follow this reference, This issue triggered by "scale in lightmap set to 0"

Feel free to make a pull request or give us a snipet to include : )

1ibrium commented 2 years ago

OK, as soon as possible

1ibrium commented 2 years ago
                if (renderer.lightmapScaleOffset != Vector4.zero)
                {
                    //1ibrium's mod : https://docs.unity3d.com/ScriptReference/Renderer-lightmapIndex.html
                    if(renderer.lightmapIndex < 0 || renderer.lightmapIndex == 0xFFFE) continue;
                    info.lightmapOffsetScale = renderer.lightmapScaleOffset;
                    Texture2D lightmap    = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapColor;
                    Texture2D lightmapDir = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapDir;
                    Texture2D shadowMask  = LightmapSettings.lightmaps[renderer.lightmapIndex].shadowMask;
                    info.lightmapIndex = lightmaps.IndexOf(lightmap);
                    if (info.lightmapIndex == -1)
                    {
                        info.lightmapIndex = lightmaps.Count;
                        lightmaps.Add(lightmap);
                        lightmapsDir.Add(lightmapDir);
                        shadowMasks.Add(shadowMask);
                    }

                    rendererInfos.Add(info);
                }
Ayfel commented 2 years ago

Thanks! I'll push it