Skip to content

Commit

Permalink
[ME] Disable Cache and Cache-clear methods (#202)
Browse files Browse the repository at this point in the history
Adds a plugin config entry for disabling the cache and public methods to clear the entire or a specific game object from the Renderer cache.
This aims to solve issues that are related to the caching, like the multi mesh issue with ObjImport.
Users can disable the cache to see if it fixes whatever issue they are having, or call the cache clear method with RUE, modders can specifically clear the cache for their GameObjects if they change the renderers.
  • Loading branch information
Njaecha authored Nov 21, 2023
1 parent 83655a1 commit a27d168
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/MaterialEditor.Core/Core.MaterialEditor.Hooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ private static void GetBodyRendererList(GameObject gameObject, List<Renderer> re
}

private static readonly Dictionary<GameObject, List<Renderer>> _RendererLookup = new Dictionary<GameObject, List<Renderer>>();

internal static void ClearCache(GameObject gameObject = null)
{
if (gameObject == null) _RendererLookup.Clear();
else _RendererLookup.Remove(gameObject);
}

[HarmonyPrefix, HarmonyPatch(typeof(MaterialEditorAPI.MaterialAPI), nameof(MaterialEditorAPI.MaterialAPI.GetRendererList))]
private static bool MaterialAPI_GetRendererList(ref IEnumerable<Renderer> __result, GameObject gameObject)
{
if (gameObject == null)
if (!MaterialEditorPlugin.RendererCachingEnabled.Value || gameObject == null)
return true;

if (_RendererLookup.TryGetValue(gameObject, out var cached))
Expand Down
22 changes: 21 additions & 1 deletion src/MaterialEditor.Core/Core.MaterialEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public partial class MaterialEditorPlugin : MaterialEditorAPI.MaterialEditorPlug
/// <summary>
/// MaterialEditor plugin version
/// </summary>
public const string PluginVersion = "3.3.0";
public const string PluginVersion = "3.4.0";

/// <summary>
/// Material which is used in normal map conversion
Expand All @@ -81,6 +81,8 @@ public partial class MaterialEditorPlugin : MaterialEditorAPI.MaterialEditorPlug
internal static ConfigEntry<KeyboardShortcut> ResetReceiveShadows { get; private set; }
internal static ConfigEntry<KeyboardShortcut> PasteEditsHotkey { get; private set; }

internal static ConfigEntry<bool> RendererCachingEnabled { get; private set; }

/// <summary>
/// Parts of the body
/// </summary>
Expand Down Expand Up @@ -151,6 +153,7 @@ public override void Awake()
//Disable ShaderOptimization since it doesn't work properly
ShaderOptimization.Value = false;
#endif
RendererCachingEnabled = Config.Bind("Config", "Renderer Cache", true, "Turning this off will fix cache related issues but may have a negative impact on performance.");
}

internal void Main()
Expand Down Expand Up @@ -998,5 +1001,22 @@ public override bool ConvertNormalMap(ref Texture tex, string propertyName)
/// <param name="chaControl"></param>
/// <returns>KKAPI character controller</returns>
public static MaterialEditorCharaController GetCharaController(ChaControl chaControl) => chaControl == null ? null : chaControl.gameObject.GetComponent<MaterialEditorCharaController>();

/// <summary>
/// Clears all GameObjects from the Renderer Cache.
/// </summary>
public static void ClearCache()
{
Hooks.ClearCache();
}

/// <summary>
/// Clears a specific GameObject from the RendererCache.
/// </summary>
/// <param name="gameObject"></param>
public static void ClearCache(GameObject gameObject)
{
Hooks.ClearCache(gameObject);
}
}
}

0 comments on commit a27d168

Please sign in to comment.