mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-02 14:29:25 +00:00
VisualCloneFix: Sped up FindExclusionVertList by 100x
This commit is contained in:
parent
9e3edf9241
commit
472e5a0b63
3 changed files with 57 additions and 46 deletions
|
@ -1,4 +1,5 @@
|
||||||
using MelonLoader;
|
using System;
|
||||||
|
using MelonLoader;
|
||||||
|
|
||||||
namespace NAK.VisualCloneFix;
|
namespace NAK.VisualCloneFix;
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
using ABI_RC.Core.Player.LocalClone;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using ABI_RC.Core.Player.LocalClone;
|
||||||
using ABI_RC.Core.Player.TransformHider;
|
using ABI_RC.Core.Player.TransformHider;
|
||||||
using ABI.CCK.Components;
|
using ABI.CCK.Components;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using Debug = UnityEngine.Debug;
|
||||||
|
|
||||||
namespace NAK.VisualCloneFix;
|
namespace NAK.VisualCloneFix;
|
||||||
|
|
||||||
|
@ -36,28 +40,20 @@ public static class Patches
|
||||||
MeshHiderExclusion headExclusionBehaviour = new();
|
MeshHiderExclusion headExclusionBehaviour = new();
|
||||||
headExclusion.behaviour = headExclusionBehaviour;
|
headExclusion.behaviour = headExclusionBehaviour;
|
||||||
headExclusionBehaviour.id = 1; // head bone is always 1
|
headExclusionBehaviour.id = 1; // head bone is always 1
|
||||||
|
|
||||||
// body is 0, ensure it is not masked away
|
|
||||||
//LocalCloneManager.cullingMask &= ~(1 << 0);
|
|
||||||
|
|
||||||
// get all FPRExclusions
|
// get all FPRExclusions
|
||||||
var fprExclusions = root.GetComponentsInChildren<FPRExclusion>(true).ToList();
|
var fprExclusions = root.GetComponentsInChildren<FPRExclusion>(true);
|
||||||
|
|
||||||
// get all valid exclusion targets, and destroy invalid exclusions
|
// get all valid exclusion targets, and destroy invalid exclusions
|
||||||
Dictionary<Transform, FPRExclusion> exclusionTargets = new();
|
Dictionary<Transform, FPRExclusion> exclusionTargets = new();
|
||||||
|
|
||||||
int nextId = 2;
|
int nextId = 2;
|
||||||
for (int i = fprExclusions.Count - 1; i >= 0; i--)
|
foreach (FPRExclusion exclusion in fprExclusions)
|
||||||
{
|
{
|
||||||
FPRExclusion exclusion = fprExclusions[i];
|
|
||||||
if (exclusion.target == null
|
if (exclusion.target == null
|
||||||
|| exclusionTargets.ContainsKey(exclusion.target)
|
|| exclusionTargets.ContainsKey(exclusion.target)
|
||||||
|| !exclusion.target.gameObject.scene.IsValid())
|
|| !exclusion.target.gameObject.scene.IsValid())
|
||||||
{
|
continue; // invalid exclusion
|
||||||
UnityEngine.Object.Destroy(exclusion);
|
|
||||||
fprExclusions.RemoveAt(i);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (exclusion.behaviour == null) // head exclusion is already created
|
if (exclusion.behaviour == null) // head exclusion is already created
|
||||||
{
|
{
|
||||||
|
@ -71,25 +67,23 @@ public static class Patches
|
||||||
}
|
}
|
||||||
|
|
||||||
// process each FPRExclusion (recursive)
|
// process each FPRExclusion (recursive)
|
||||||
foreach (FPRExclusion exclusion in fprExclusions)
|
int exclusionCount = exclusionTargets.Values.Count;
|
||||||
|
for (var index = 0; index < exclusionCount; index++)
|
||||||
{
|
{
|
||||||
|
FPRExclusion exclusion = exclusionTargets.Values.ElementAt(index);
|
||||||
ProcessExclusion(exclusion, exclusion.target);
|
ProcessExclusion(exclusion, exclusion.target);
|
||||||
exclusion.UpdateExclusions(); // initial state
|
exclusion.UpdateExclusions(); // initial state
|
||||||
}
|
}
|
||||||
|
|
||||||
// log totals
|
|
||||||
//LocalCloneMod.Logger.Msg($"Exclusions: {fprExclusions.Count}");
|
|
||||||
__result = exclusionTargets;
|
__result = exclusionTargets;
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
void ProcessExclusion(FPRExclusion exclusion, Transform transform)
|
void ProcessExclusion(FPRExclusion exclusion, Transform transform)
|
||||||
{
|
{
|
||||||
if (exclusionTargets.ContainsKey(transform)
|
if (exclusionTargets.ContainsKey(transform)
|
||||||
&& exclusionTargets[transform] != exclusion) return; // found other exclusion root
|
&& exclusionTargets[transform] != exclusion) return; // found other exclusion root
|
||||||
|
|
||||||
//exclusion.affectedChildren.Add(transform); // associate with the exclusion
|
|
||||||
exclusionTargets.TryAdd(transform, exclusion); // add to the dictionary (yes its wasteful)
|
exclusionTargets.TryAdd(transform, exclusion); // add to the dictionary (yes its wasteful)
|
||||||
|
|
||||||
foreach (Transform child in transform)
|
foreach (Transform child in transform)
|
||||||
ProcessExclusion(exclusion, child); // process children
|
ProcessExclusion(exclusion, child); // process children
|
||||||
}
|
}
|
||||||
|
@ -97,39 +91,55 @@ public static class Patches
|
||||||
|
|
||||||
[HarmonyPrefix]
|
[HarmonyPrefix]
|
||||||
[HarmonyPatch(typeof(SkinnedLocalClone), nameof(SkinnedLocalClone.FindExclusionVertList))]
|
[HarmonyPatch(typeof(SkinnedLocalClone), nameof(SkinnedLocalClone.FindExclusionVertList))]
|
||||||
public static bool FindExclusionVertList(
|
private static bool FindExclusionVertList(
|
||||||
SkinnedMeshRenderer renderer, IReadOnlyDictionary<Transform, FPRExclusion> exclusions,
|
SkinnedMeshRenderer renderer, IReadOnlyDictionary<Transform, FPRExclusion> exclusions,
|
||||||
ref int[] __result)
|
ref int[] __result)
|
||||||
{
|
{
|
||||||
Mesh sharedMesh = renderer.sharedMesh;
|
// Start the stopwatch
|
||||||
var boneWeights = sharedMesh.boneWeights;
|
Stopwatch stopwatch = new();
|
||||||
int[] vertexIndices = new int[sharedMesh.vertexCount];
|
stopwatch.Start();
|
||||||
|
|
||||||
// Pre-map bone transforms to their exclusion ids if applicable
|
var boneWeights = renderer.sharedMesh.boneWeights;
|
||||||
var bones = renderer.bones;
|
var bones = renderer.bones;
|
||||||
int[] boneIndexToExclusionId = new int[bones.Length];
|
int boneCount = bones.Length;
|
||||||
for (int i = 0; i < bones.Length; i++)
|
|
||||||
{
|
|
||||||
Transform bone = bones[i];
|
|
||||||
if (bone != null && exclusions.TryGetValue(bone, out FPRExclusion exclusion))
|
|
||||||
boneIndexToExclusionId[i] = ((MeshHiderExclusion)(exclusion.behaviour)).id;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
bool[] boneHasExclusion = new bool[boneCount];
|
||||||
|
|
||||||
|
// Populate the weights array
|
||||||
|
for (int i = 0; i < boneCount; i++)
|
||||||
|
if (exclusions.ContainsKey(bones[i]))
|
||||||
|
boneHasExclusion[i] = true;
|
||||||
|
|
||||||
const float minWeightThreshold = 0.2f;
|
const float minWeightThreshold = 0.2f;
|
||||||
for (int i = 0; i < boneWeights.Length; i++)
|
|
||||||
|
int[] vertexIndices = new int[renderer.sharedMesh.vertexCount];
|
||||||
|
|
||||||
|
// Check bone weights and add vertex to exclusion list if needed
|
||||||
|
for (int i = 0; i < boneWeights.Length; i++)
|
||||||
{
|
{
|
||||||
BoneWeight weight = boneWeights[i];
|
BoneWeight weight = boneWeights[i];
|
||||||
|
Transform bone;
|
||||||
if (weight.weight0 > minWeightThreshold)
|
|
||||||
vertexIndices[i] = boneIndexToExclusionId[weight.boneIndex0];
|
if (boneHasExclusion[weight.boneIndex0] && weight.weight0 > minWeightThreshold)
|
||||||
else if (weight.weight1 > minWeightThreshold)
|
bone = bones[weight.boneIndex0];
|
||||||
vertexIndices[i] = boneIndexToExclusionId[weight.boneIndex1];
|
else if (boneHasExclusion[weight.boneIndex1] && weight.weight1 > minWeightThreshold)
|
||||||
else if (weight.weight2 > minWeightThreshold)
|
bone = bones[weight.boneIndex1];
|
||||||
vertexIndices[i] = boneIndexToExclusionId[weight.boneIndex2];
|
else if (boneHasExclusion[weight.boneIndex2] && weight.weight2 > minWeightThreshold)
|
||||||
else if (weight.weight3 > minWeightThreshold)
|
bone = bones[weight.boneIndex2];
|
||||||
vertexIndices[i] = boneIndexToExclusionId[weight.boneIndex3];
|
else if (boneHasExclusion[weight.boneIndex3] && weight.weight3 > minWeightThreshold)
|
||||||
|
bone = bones[weight.boneIndex3];
|
||||||
|
else continue;
|
||||||
|
|
||||||
|
if (exclusions.TryGetValue(bone, out FPRExclusion exclusion))
|
||||||
|
vertexIndices[i] = ((MeshHiderExclusion)(exclusion.behaviour)).id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stop the stopwatch
|
||||||
|
stopwatch.Stop();
|
||||||
|
|
||||||
|
// Log the execution time
|
||||||
|
Debug.Log($"FindExclusionVertList execution time: {stopwatch.ElapsedMilliseconds} ms");
|
||||||
|
|
||||||
__result = vertexIndices;
|
__result = vertexIndices;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,6 @@
|
||||||
],
|
],
|
||||||
"downloadlink": "https://github.com/NotAKidoS/NAK_CVR_Mods/releases/download/r36/VisualCloneFix.dll",
|
"downloadlink": "https://github.com/NotAKidoS/NAK_CVR_Mods/releases/download/r36/VisualCloneFix.dll",
|
||||||
"sourcelink": "https://github.com/NotAKidoS/NAK_CVR_Mods/tree/main/VisualCloneFix/",
|
"sourcelink": "https://github.com/NotAKidoS/NAK_CVR_Mods/tree/main/VisualCloneFix/",
|
||||||
"changelog": "- Fixed FPRExclusions IsShown state being inverted when toggled.\n- Fixed head FPRExclusion generation not checking for existing exclusion.",
|
"changelog": "- Fixed FPRExclusions IsShown state being inverted when toggled.\n- Fixed head FPRExclusion generation not checking for existing exclusion.\n- Sped up FindExclusionVertList by 100x by not being an idiot. This heavily reduces avatar hitch with Visual Clone active.",
|
||||||
"embedcolor": "#f61963"
|
"embedcolor": "#f61963"
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue