diff --git a/Nautilus/Utility/MaterialUtils.cs b/Nautilus/Utility/MaterialUtils.cs index 7dbd9e8e..90d06048 100644 --- a/Nautilus/Utility/MaterialUtils.cs +++ b/Nautilus/Utility/MaterialUtils.cs @@ -1,6 +1,7 @@ using Nautilus.Utility.MaterialModifiers; using System.Collections; using UnityEngine; +using UnityEngine.SceneManagement; using UWE; namespace Nautilus.Utility; @@ -11,18 +12,24 @@ namespace Nautilus.Utility; public static partial class MaterialUtils { private static readonly int _emissionMap = Shader.PropertyToID("_EmissionMap"); + private static bool _sceneEventAdded; internal static void Patch() { CoroutineHost.StartCoroutine(LoadReferences()); + if (!_sceneEventAdded) + { + SceneManager.sceneLoaded += OnSceneLoaded; + _sceneEventAdded = true; + } } - + private static IEnumerator LoadReferences() { yield return PatchInternal(); IsReady = true; - + yield break; } @@ -249,7 +256,7 @@ public static void ApplyUBERShader(Material material, float shininess, float spe { material.EnableKeyword("MARMO_NORMALMAP"); } - + material.enableInstancing = true; material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack | MaterialGlobalIlluminationFlags.RealtimeEmissive; @@ -325,4 +332,11 @@ public static void SetMaterialCutout(Material material, bool cutout) material.DisableKeyword("MARMO_ALPHA_CLIP"); } } + + private static void OnSceneLoaded(Scene scene, LoadSceneMode mode) + { + if (scene.name != "MenuEnvironment") return; + + CoroutineHost.StartCoroutine(ReloadStaleReferences()); + } } diff --git a/Nautilus/Utility/MaterialUtils_BelowZero.cs b/Nautilus/Utility/MaterialUtils_BelowZero.cs index a126260a..ce8bd8c1 100644 --- a/Nautilus/Utility/MaterialUtils_BelowZero.cs +++ b/Nautilus/Utility/MaterialUtils_BelowZero.cs @@ -42,6 +42,16 @@ private static IEnumerator LoadGlassMaterials() InteriorGlassMaterial.SetFloat("_Shininess", 6f); InteriorGlassMaterial.SetFloat("_Fresnel", 0.88f); } + + private static IEnumerator ReloadStaleReferences() + { + Object.Destroy(GlassMaterial); + Object.Destroy(ExteriorGlassMaterial); + Object.Destroy(ShinyGlassMaterial); + Object.Destroy(InteriorGlassMaterial); + + yield return LoadGlassMaterials(); + } } #endif \ No newline at end of file diff --git a/Nautilus/Utility/MaterialUtils_Subnautica.cs b/Nautilus/Utility/MaterialUtils_Subnautica.cs index a39bba9e..b2bf1031 100644 --- a/Nautilus/Utility/MaterialUtils_Subnautica.cs +++ b/Nautilus/Utility/MaterialUtils_Subnautica.cs @@ -187,5 +187,17 @@ private static void OnCyclopsLoaded(GameObject cyclops) HolographicUIMaterial = new Material(holoMat); _cyclopsLoaded = true; } + + private static IEnumerator ReloadStaleReferences() + { + Object.Destroy(GlassMaterial); + Object.Destroy(ExteriorGlassMaterial); + Object.Destroy(ShinyGlassMaterial); + Object.Destroy(InteriorGlassMaterial); + Object.Destroy(HolographicUIMaterial); + + yield return LoadGlassMaterials(); + yield return LoadUIMaterial(); + } } #endif \ No newline at end of file