private static void JoinOn(PrefabBaker prefab, LightmapData newData)
        {
            var hashCode = prefab.GetLightMapHashCode();

            if (string.IsNullOrEmpty(hashCode))
            {
                return;
            }
            if (!prefabToLightmap.TryGetValue(hashCode, out LightMapPrefabStorage storage))
            {
                storage = new LightMapPrefabStorage
                {
                    lightData = new List <LightmapData>(4)
                };
            }
            bool addedNew            = true;
            var  hashCodeOfLightData = GetHashCodeCustom(newData);

            foreach (var item in storage.HashCodes)
            {
                if (item == hashCodeOfLightData)
                {
                    addedNew = false;
                    break;
                }
            }
            if (addedNew)
            {
                storage.lightData.Add(newData);
                storage.HashCodes.Add(hashCodeOfLightData);
                prefabToLightmap[hashCode] = storage;
            }
        }
Ejemplo n.º 2
0
        public static void Reset(PrefabBaker prefab)
        {
            var flags = (StaticEditorFlags)0;

            GameObjectUtility.SetStaticEditorFlags(prefab.gameObject, flags);

            foreach (var t in prefab.gameObject.GetComponentsInChildren <Transform>())
            {
                GameObjectUtility.SetStaticEditorFlags(t.gameObject, flags);
            }

            foreach (var light in prefab.gameObject.GetComponentsInChildren <Light>())
            {
                light.lightmapBakeType = LightmapBakeType.Realtime;
            }

            foreach (var r in prefab.gameObject.GetComponentsInChildren <MeshRenderer>())
            {
                r.lightmapIndex         = -1;
                r.realtimeLightmapIndex = -1;
            }

            List <LightmapData> lmds = new List <LightmapData>();

            foreach (var lmd in LightmapSettings.lightmaps)
            {
                bool found = false;

                if (prefab.texturesColor?.Contains(lmd.lightmapColor) ?? false)
                {
                    found = true;
                }
                else if (prefab.texturesDir?.Contains(lmd.lightmapDir) ?? false)
                {
                    found = true;
                }
                else if (prefab.texturesShadow?.Contains(lmd.shadowMask) ?? false)
                {
                    found = true;
                }

                if (!found)
                {
                    lmds.Add(lmd);
                }
            }

            prefab.texturesColor                = null;
            prefab.texturesDir                  = null;
            prefab.texturesShadow               = null;
            prefab.lights                       = null;
            prefab.renderers                    = null;
            prefab.renderersLightmapIndex       = null;
            prefab.renderersLightmapOffsetScale = null;

            LightmapSettings.lightmaps = lmds.ToArray();

            Lightmapping.giWorkflowMode = Lightmapping.GIWorkflowMode.Iterative;
        }
Ejemplo n.º 3
0
        public static void UpdateLightmaps(PrefabBaker prefab, List <MeshRenderer> renderers, List <SceneLightmap> lightmaps)
        {
            List <Texture2D> listColor     = new List <Texture2D>();
            List <Texture2D> listDir       = new List <Texture2D>();
            List <Texture2D> listShadow    = new List <Texture2D>();
            List <Renderer>  listRenderers = new List <Renderer>();
            List <int>       listIndexes   = new List <int>();
            List <Vector4>   listScales    = new List <Vector4>();

            for (var i = 0; i < renderers.Count; ++i)
            {
                // Scan current list of static lightmaps inside baker and compare thier indexes to the current renderer target lightmap index

                var slm = Baker.GetSceneLightmapFromRendererIndex(renderers[i].lightmapIndex);

                // Only if lightmap index wasn't found, save textures reference in the prefab

                int rendererLightmapIndex = listColor.IndexOf(slm.texColor);

                if (rendererLightmapIndex == -1)
                {
                    // Set index to the size of the current array

                    rendererLightmapIndex = listColor.Count;

                    listColor.Add(slm.texColor);

                    // Optional textures are checked for null

                    if (slm.texDir != null)
                    {
                        listDir.Add(slm.texDir);
                    }
                    if (slm.texShadow != null)
                    {
                        listShadow.Add(slm.texShadow);
                    }
                }

                MeshRenderer renderer = renderers[i];

                // For each renderer add its reference, lightmap index and scale offset by default

                listRenderers.Add(renderer);
                listIndexes.Add(rendererLightmapIndex);
                listScales.Add(renderer.lightmapScaleOffset);
            }

            // Convert data to match prefab format for proper serialization

            prefab.texturesColor                = listColor.ToArray();
            prefab.texturesDir                  = listDir.ToArray();
            prefab.texturesShadow               = listShadow.ToArray();
            prefab.renderers                    = listRenderers.ToArray();
            prefab.renderersLightmapIndex       = listIndexes.ToArray();
            prefab.renderersLightmapOffsetScale = listScales.ToArray();
        }
Ejemplo n.º 4
0
 public static void AddInstance(PrefabBaker instance)
 {
     if (!AddOrRemove.TryGetValue(instance, out _))
     {
         AddOrRemove.TryAdd(instance, true);
     }
     AddOrRemove[instance] = true;
     RunCoroutine();
 }
Ejemplo n.º 5
0
        public static bool Apply(PrefabBaker prefab)
        {
            if (prefab.renderers == null || prefab.renderers.Length == 0)
            {
                return(false);
            }

            int[] lightmapArrayOffsetIndex;

            var sceneLightmaps = LightmapSettings.lightmaps;

            var added_lightmaps = new List <LightmapData>();

            lightmapArrayOffsetIndex = new int[prefab.texturesColor.Length];

            for (int i = 0; i < prefab.texturesColor.Length; i++)
            {
                bool found = false;

                for (int j = 0; j < sceneLightmaps.Length; j++)
                {
                    if (prefab.texturesColor[i] == sceneLightmaps[j].lightmapColor)
                    {
                        lightmapArrayOffsetIndex[i] = j;

                        found = true;
                    }
                }

                if (!found)
                {
                    lightmapArrayOffsetIndex[i] = added_lightmaps.Count + sceneLightmaps.Length;

                    var newLightmapData = new LightmapData();

                    newLightmapData.lightmapColor = GetElement(prefab.texturesColor, i);
                    newLightmapData.lightmapDir   = GetElement(prefab.texturesDir, i);
                    newLightmapData.shadowMask    = GetElement(prefab.texturesShadow, i);

                    added_lightmaps.Add(newLightmapData);
                }
            }

            bool combined = false;

            if (added_lightmaps.Count > 0)
            {
                CombineLightmaps(added_lightmaps);

                combined = true;
            }

            UpdateLightmaps(prefab, lightmapArrayOffsetIndex);

            return(combined);
        }
Ejemplo n.º 6
0
        public static void UpdateLights(PrefabBaker component)
        {
            var lights = component.gameObject.GetComponentsInChildren <Light>(true);

            component.lights = lights.Select(light => new LightInfo
            {
                light             = light,
                lightmapBaketype  = (int)light.lightmapBakeType,
                mixedLightingMode = (int)LightmapEditorSettings.mixedBakeMode
            }).ToArray();
        }
        public static void RemoveInstanceRef(PrefabBaker prefab)
        {
            var hashCode = prefab.GetLightMapHashCode();

            if (prefabToLightmap.TryGetValue(hashCode, out LightMapPrefabStorage storage))
            {
                storage.referenceCount--;
                if (storage.referenceCount <= 0)
                {
                    storage.referenceCount = 0;
                }
            }
        }
Ejemplo n.º 8
0
        public static void RemoveInstance(PrefabBaker instance)
        {
            if (!AddOrRemove.TryGetValue(instance, out _))
            {
                AddOrRemove.TryAdd(instance, false);
            }
            AddOrRemove[instance] = false;
            var lightMapHasCode = instance.GetLightMapHashCode();

            if (!AddOrRemoveToCheck.TryGetValue(lightMapHasCode, out _))
            {
                AddOrRemoveToCheck.TryAdd(lightMapHasCode, instance.name);
            }
            RunCoroutine();
        }
        public static bool CheckInstance(PrefabBaker prefab)
        {
            bool fullyCleaned = false;
            var  hashCode     = prefab.GetLightMapHashCode();

            if (prefabToLightmap.TryGetValue(hashCode, out LightMapPrefabStorage storage))
            {
                if (storage.referenceCount <= 0)
                {
                    storage.referenceCount = 0;
                    fullyCleaned           = true;
                    RemoveEmpty(prefab, storage);
                    prefabToLightmap.Remove(hashCode);
                }
            }
            return(fullyCleaned);
        }
        public static void AddInstanceRef(PrefabBaker prefab)
        {
            var hashCode = prefab.GetLightMapHashCode();

            if (!prefabToLightmap.TryGetValue(hashCode, out _))
            {
                if (!prefab.BakeApplied)
                {
                    int max = Mathf.Max(prefab.texturesColor.Length, prefab.texturesDir.Length);
                    max = Mathf.Max(max, prefab.texturesShadow.Length);
                    for (int i = 0; i < max; i++)
                    {
                        var newLightmapData = new LightmapData
                        {
                            lightmapColor = GetElement(prefab.texturesColor, i),
                            lightmapDir   = GetElement(prefab.texturesDir, i),
                            shadowMask    = GetElement(prefab.texturesShadow, i)
                        };
                        JoinOn(prefab, newLightmapData);
                    }
                }
            }
            prefabToLightmap[hashCode].referenceCount++;
        }
        private static void RemoveEmpty(PrefabBaker prefab, LightMapPrefabStorage toRemoveData)
        {
            var sceneLightmaps = CurrentFrameLightData;
            int count          = sceneLightmaps.Count;

            for (int j = 0; j < count; j++)
            {
                int hash = GetHashCodeCustom(sceneLightmaps[j]);
                foreach (var item in toRemoveData.lightData)
                {
                    if (hash == GetHashCodeCustom(item))
                    {
                        sceneLightmaps[j] = new LightmapData();
                    }
                }
            }
            if (prefab != null)
            {
                actionsToPerform.Add(new ActionStruct {
                    prefab = prefab, AddOrRemove = false
                });
            }
            IsLightMapsChanged = true;
        }
Ejemplo n.º 12
0
 private void OnEnable()
 {
     instance = (PrefabBaker)target;
 }
Ejemplo n.º 13
0
        public override void OnInspectorGUI( )
        {
            PrefabBaker instance = ( PrefabBaker )target;

            GUILayout.Space(15);

            if (Window.instance == null)
            {
                if (GUILayout.Button("Open Baker", GUILayout.Height(30)))
                {
                    Window.OpenWindow( );
                }

                GUILayout.Space(5);
            }

            bool hasData = instance.HasBakeData;

            if (hasData)
            {
                using (new GUILayout.HorizontalScope())
                {
                    EditorGUI.BeginDisabledGroup(instance.BakeApplied);

                    if (GUILayout.Button("Apply", GUILayout.Height(25)))
                    {
                        instance.BakeApply( );
                    }

                    EditorGUI.EndDisabledGroup( );

                    GUILayout.Space(5);

                    GUI.color = new Color(1f, 0.5f, 0.5f);

                    if (GUILayout.Button("Clear", GUILayout.Height(25), GUILayout.Width(70)))
                    {
                        EditorUtils.Reset(instance);
                    }

                    GUI.color = Color.white;
                }

                GUILayout.Space(5);

                GUILayout.Label("", GUI.skin.horizontalSlider);
            }

            GUILayout.Space(5);

            EditorUtils.BoxGUI(() =>
            {
                using (new GUILayout.HorizontalScope( ))
                {
                    GUILayout.Space(10);
                    EditComponents = EditorGUILayout.Foldout(EditComponents, " Override Nested Components", true);
                }

                //EditComponents = EditorGUILayout.ToggleLeft( " Override Nested Components", EditComponents );

                if (!EditComponents)
                {
                    return;
                }

                GUILayout.Space(5);

                PanelComponentsOverride.Draw( );

                GUILayout.Space(5);

                if (GUILayout.Button("Apply"))
                {
                    PanelComponentsOverride.Apply(instance.gameObject);
                }
            });

            GUILayout.Space(5);

            if (!hasData)
            {
                return;
            }

            PreviewData = EditorGUILayout.Foldout(PreviewData, "Show data", true);

            GUILayout.Space(5);

            if (!PreviewData)
            {
                return;                  //base.OnInspectorGUI( );
            }
            var ln = instance.lights?.Length;

            string info = $"{ instance.lights?.Length ?? 0 } Lights | " +
                          $" {instance.renderers?.Length ?? 0} Renderers\n" +
                          $"{instance.texturesColor?.Length ?? 0} Color Textures";

            if ((instance.texturesDir?.Length ?? 0) > 0)
            {
                info += $"\n{ instance.texturesDir?.Length ?? 0} Directional Textures";
            }

            if ((instance.texturesShadow?.Length ?? 0) > 0)
            {
                info += $"\n{ instance.texturesShadow?.Length ?? 0} Shadow Textures";
            }

            GUILayout.Label("Info", EditorStyles.boldLabel);
            EditorGUILayout.HelpBox(info, MessageType.None);

            GUILayout.Space(5);

            GUILayout.Label("Textures", EditorStyles.boldLabel);

            DrawTextures(instance.texturesColor, ref scroll_color);
            DrawTextures(instance.texturesDir, ref scroll_dir);
            DrawTextures(instance.texturesShadow, ref scroll_shadow);
        }
        public static bool InitializeInstance(PrefabBaker prefab)
        {
            if (prefab.renderers == null || prefab.renderers.Length == 0)
            {
                return(false);
            }

            var sceneLightmapsRef = CurrentFrameLightData;

            added_lightmaps.Clear();
            changed_lightmaps.Clear();

            int max = Mathf.Max(prefab.texturesColor.Length, prefab.texturesDir.Length);

            max = Mathf.Max(max, prefab.texturesShadow.Length);

            int[] lightmapArrayOffsetIndex = new int[max];
            emptySlots.Clear();
            int count = CurrentFrameLightData.Count;

            for (int i = 0; i < max; i++)
            {
                bool found = false;
                for (int j = 0; j < count && !found; j++)
                {
                    if (sceneLightmapsRef[j].lightmapColor == null &&
                        sceneLightmapsRef[j].lightmapDir == null &&
                        sceneLightmapsRef[j].shadowMask == null)
                    {
                        if (!emptySlots.ContainsKey(j))
                        {
                            emptySlots.Add(j, true);
                        }
                        continue;
                    }
                    found |= prefab.texturesColor.Length > i && prefab.texturesColor[i] != null && prefab.texturesColor[i] == sceneLightmapsRef[j].lightmapColor;
                    found |= prefab.texturesDir.Length > i && prefab.texturesDir[i] != null && prefab.texturesDir[i] == sceneLightmapsRef[j].lightmapDir;
                    found |= prefab.texturesShadow.Length > i && prefab.texturesShadow[i] != null && prefab.texturesShadow[i] == sceneLightmapsRef[j].shadowMask;

                    if (found)
                    {
                        lightmapArrayOffsetIndex[i] = j;
                        break;
                    }
                }

                if (!found)
                {
                    var newLightmapData = new LightmapData
                    {
                        lightmapColor = GetElement(prefab.texturesColor, i),
                        lightmapDir   = GetElement(prefab.texturesDir, i),
                        shadowMask    = GetElement(prefab.texturesShadow, i)
                    };

                    if (emptySlots.Keys.Count > 0)
                    {
                        int indexToGet = -1;
                        foreach (var item in emptySlots.Keys)
                        {
                            indexToGet = item;
                            break;
                        }
                        lightmapArrayOffsetIndex[i] = indexToGet;
                        changed_lightmaps.Add(new LightmapWithIndex
                        {
                            lightData = newLightmapData,
                            index     = lightmapArrayOffsetIndex[i]
                        });
                    }
                    else
                    {
                        lightmapArrayOffsetIndex[i] = added_lightmaps.Count + count;
                        added_lightmaps.Add(newLightmapData);
                    }
                    JoinOn(prefab, newLightmapData);
                }
            }

            bool combined = false;

            if (added_lightmaps.Count > 0 || changed_lightmaps.Count > 0)
            {
                IsLightMapsChanged = true;
                CombineLightmaps(added_lightmaps, changed_lightmaps);
                combined = true;
            }

            // Required for each instance once!
            UpdateLightmaps(prefab, lightmapArrayOffsetIndex);
            return(combined);
        }