From 0186f5ee23706092ea54083b4a6942cb8ba0e16b Mon Sep 17 00:00:00 2001 From: Indigocoder1 Date: Tue, 24 Dec 2024 13:15:31 -0800 Subject: [PATCH 1/3] Added scene loaded event to reload materials --- Nautilus/Utility/MaterialUtils.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Nautilus/Utility/MaterialUtils.cs b/Nautilus/Utility/MaterialUtils.cs index 7dbd9e8e..8c146ac8 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,10 +12,16 @@ 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() @@ -22,7 +29,7 @@ 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(LoadReferences()); + } } From 8cbe4cc485f920dabf9617953446c8619ed1631c Mon Sep 17 00:00:00 2001 From: Indigocoder1 Date: Tue, 24 Dec 2024 15:45:09 -0800 Subject: [PATCH 2/3] Old mat now destroyed before making copy --- Nautilus/Utility/MaterialUtils_Subnautica.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Nautilus/Utility/MaterialUtils_Subnautica.cs b/Nautilus/Utility/MaterialUtils_Subnautica.cs index a39bba9e..c1bc7d46 100644 --- a/Nautilus/Utility/MaterialUtils_Subnautica.cs +++ b/Nautilus/Utility/MaterialUtils_Subnautica.cs @@ -184,6 +184,7 @@ private static void OnCyclopsLoaded(GameObject cyclops) var holoMat = cyclops.transform.Find("HelmHUD/HelmHUDVisuals/Canvas_LeftHUD/EngineOnUI/EngineOff_Button") .GetComponent().material; + Object.Destroy(HolographicUIMaterial); HolographicUIMaterial = new Material(holoMat); _cyclopsLoaded = true; } From 70d62766fb6f3daeb5e81b6f495c8eb51134f2fb Mon Sep 17 00:00:00 2001 From: Indigocoder1 Date: Tue, 24 Dec 2024 16:21:39 -0800 Subject: [PATCH 3/3] Moved reference reloading to partial classes --- Nautilus/Utility/MaterialUtils.cs | 4 ++-- Nautilus/Utility/MaterialUtils_BelowZero.cs | 10 ++++++++++ Nautilus/Utility/MaterialUtils_Subnautica.cs | 13 ++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Nautilus/Utility/MaterialUtils.cs b/Nautilus/Utility/MaterialUtils.cs index 8c146ac8..90d06048 100644 --- a/Nautilus/Utility/MaterialUtils.cs +++ b/Nautilus/Utility/MaterialUtils.cs @@ -23,7 +23,7 @@ internal static void Patch() _sceneEventAdded = true; } } - + private static IEnumerator LoadReferences() { yield return PatchInternal(); @@ -337,6 +337,6 @@ private static void OnSceneLoaded(Scene scene, LoadSceneMode mode) { if (scene.name != "MenuEnvironment") return; - CoroutineHost.StartCoroutine(LoadReferences()); + 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 c1bc7d46..b2bf1031 100644 --- a/Nautilus/Utility/MaterialUtils_Subnautica.cs +++ b/Nautilus/Utility/MaterialUtils_Subnautica.cs @@ -184,9 +184,20 @@ private static void OnCyclopsLoaded(GameObject cyclops) var holoMat = cyclops.transform.Find("HelmHUD/HelmHUDVisuals/Canvas_LeftHUD/EngineOnUI/EngineOff_Button") .GetComponent().material; - Object.Destroy(HolographicUIMaterial); 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