Skip to content

Commit

Permalink
Fix glitch with neck bone position + ABMX neck length modifier.
Browse files Browse the repository at this point in the history
Add resize on folder feature (VNGE support)
  • Loading branch information
OrangeSpork committed Dec 13, 2021
1 parent 16b455b commit 63a36dd
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 6 deletions.
69 changes: 68 additions & 1 deletion AdvIKPlugin/AdvIKCharaController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;

using System.Linq;

using KKAPI.Chara;
using KKAPI;
Expand Down Expand Up @@ -374,13 +374,79 @@ private IEnumerator setSpineStiffness(float spineStiffnessValue)

private bool toeAdjustmentApplied;

private void RecurseForName(TreeNodeObject tno)
{
if (tno.textName != null && tno.textName.ToUpper().StartsWith("-RESIZE"))
flagNodeObjectList.Add(tno);

foreach (TreeNodeObject child in tno.child)
{
RecurseForName(child);
}
}

private IEnumerator ScanForFlagCo()
{
yield return new WaitUntil(() => Studio.Studio.Instance != null && Studio.Studio.Instance.dicObjectCtrl != null);
while (this.enabled)
{
flagNodeObjectList.Clear();
if (AdvIKPlugin.EnableResizeOnFolder.Value)
{
OCIChar me = StudioObjectExtensions.GetOCIChar(ChaControl);
RecurseForName(me.treeNodeObject);
}
yield return new WaitForSeconds(5);
}
}


private List<TreeNodeObject> flagNodeObjectList = new List<TreeNodeObject>();

protected override void Update()
{
toeAdjustmentApplied = false;

if (_breathing != null)
_breathing.FrameEffects = null;

foreach (TreeNodeObject flagNodeObject in flagNodeObjectList)
{
if (flagNodeObject != null && flagNodeObject.visible)
{
try
{
if (flagNodeObject.textName == null || !flagNodeObject.textName.ToUpper().StartsWith("-RESIZE"))
{
flagNodeObjectList.Remove(flagNodeObject);
break;
}

string reqCentroidMode = flagNodeObject.textName.ToUpper().Substring(flagNodeObject.textName.LastIndexOf(":") + 1);
#if DEBUG
AdvIKPlugin.Instance.Log.LogInfo($"Folder Requested Adjustment via: {reqCentroidMode}");
#endif
IKResizeCentroid currentCentroid = _iKResizeAdjustment.Centroid;
IKResizeCentroid requestedCentroid = (IKResizeCentroid)Enum.Parse(typeof(IKResizeCentroid), reqCentroidMode);
_iKResizeAdjustment.Centroid = requestedCentroid;
_iKResizeAdjustment.ApplyAdjustment(true);
_iKResizeAdjustment.Centroid = currentCentroid;
flagNodeObject.SetVisible(false);
}
catch (Exception errAny)
{
#if DEBUG
AdvIKPlugin.Instance.Log.LogInfo($"Error checking resize flag: {errAny.Message}\n{errAny.StackTrace}");
#endif
try
{
flagNodeObjectList.Remove(flagNodeObject);
}
catch { }
}
}
}

base.Update();
}

Expand Down Expand Up @@ -448,6 +514,7 @@ public Vector3 FindFKRotation(Transform t)
protected override void OnEnable()
{
StartCoroutine("StartBreathing", new PluginData());
StartCoroutine(ScanForFlagCo());
}


Expand Down
5 changes: 4 additions & 1 deletion AdvIKPlugin/AdvIKPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public partial class AdvIKPlugin : BaseUnityPlugin
{
public const string GUID = "orange.spork.advikplugin";
public const string PluginName = "AdvIKPlugin";
public const string Version = "1.6.6";
public const string Version = "1.6.7";

public static AdvIKPlugin Instance { get; set; }

Expand All @@ -40,6 +40,7 @@ public partial class AdvIKPlugin : BaseUnityPlugin
public static ConfigEntry<float> MakerBreathScale { get; set; }
public static ConfigEntry<float> MakerBreathRateScale { get; set; }
public static ConfigEntry<bool> StudioAutoApplyResize { get; set; }
public static ConfigEntry<bool> EnableResizeOnFolder { get; set; }

internal BepInEx.Logging.ManualLogSource Log => Logger;

Expand All @@ -65,6 +66,8 @@ public AdvIKPlugin()

StudioAutoApplyResize = Config.Bind("Options", "Studio - Auto Resize on Reload", true, "Automatically Apply Configured IK Adjustment on Reloading Characters");

EnableResizeOnFolder = Config.Bind("Options", "Studio - Enable Resize on Folder Control", true, "Trigger resize with a folder of name -RESIZE:CENTROID_NAME - see readme for list of valid centroid names");

var harmony = Harmony.CreateAndPatchAll(typeof(Hooks));
harmony.Patch(typeof(MPCharCtrl).GetNestedType("IKInfo", AccessTools.all).GetMethod("Init"), null, new HarmonyMethod(typeof(AdvIKGUI).GetMethod(nameof(AdvIKGUI.InitUI), AccessTools.all)));
harmony.Patch(typeof(MPCharCtrl).GetNestedType("IKInfo", AccessTools.all).GetMethod("UpdateInfo"), null, new HarmonyMethod(typeof(AdvIKGUI).GetMethod(nameof(AdvIKGUI.UpdateUI), AccessTools.all)));
Expand Down
7 changes: 6 additions & 1 deletion AdvIKPlugin/Algos/BreathingBoneEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,12 @@ public override BoneModifierData GetEffect(string bone, BoneController origin, C
}
else if (bone.Equals(Neck))
{
return UpdateBoneModifier(bone, Vector3.one, FrameEffects.NeckAdj);
// Looks like applying a 0 position offset to a bone with a length adjustment causes ABMX to incorrectly revert the length adjustment
// Work around by supplying a very small position offset instead
if (FrameEffects.NeckAdj.y == 0)
return UpdateBoneModifier(bone, Vector3.one, new Vector3(0f, 0.001f, 0f));
else
return UpdateBoneModifier(bone, Vector3.one, FrameEffects.NeckAdj);
}
else
{
Expand Down
16 changes: 13 additions & 3 deletions AdvIKPlugin/Algos/IKResizeAdjustment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public IKResizeCentroid Centroid

private Dictionary<IKScale, float> CurrentScale { get; set; } = new Dictionary<IKScale, float>();
private Dictionary<IKScale, float> PriorScale { get; set; } = new Dictionary<IKScale, float>();
private Dictionary<IKScale, float> OriginalScale { get; set; } = new Dictionary<IKScale, float>();

public bool AdjustmentApplied { get; set; }
public bool PriorCharacter { get; set; }
Expand All @@ -63,8 +64,7 @@ public IKResizeAdjustment()

AdjustmentApplied = false;
PriorCharacter = false;
}

}

public void OnReload(ChaControl chaControl)
{
Expand Down Expand Up @@ -105,6 +105,7 @@ public void OnReload(ChaControl chaControl)
if (OriginalCharacterScale == Vector3.zero)
{
OriginalCharacterScale = FindCurrentScale();
OriginalScale = new Dictionary<IKScale, float>(CurrentScale);
#if DEBUG
Log.LogInfo($"Setting Original Character Scale to: {OriginalCharacterScale}");
#endif
Expand Down Expand Up @@ -153,13 +154,19 @@ public void UndoAdjustment()
AdjustmentApplied = false;
}

public void ApplyAdjustment()
public void ApplyAdjustment(bool useOriginal = false)
{
if (!PriorScale.ContainsKey(IKScale.BODY) || !PriorScale.Where(entry => CurrentScale[entry.Key] != entry.Value).Any())
{
return;
}

Dictionary<IKScale, float> priorCopy = new Dictionary<IKScale, float>(PriorScale);
if (useOriginal)
{
PriorScale = OriginalScale;
}

IKResizeCentroid UseCentroid = Centroid;
if (Centroid == IKResizeCentroid.AUTO)
{
Expand Down Expand Up @@ -209,6 +216,9 @@ public void ApplyAdjustment()
AppliedCentroid = UseCentroid;
AppliedChainAdjustments = new Dictionary<IKChain, IKResizeChainAdjustment>(ChainAdjustments);
}

if (useOriginal)
PriorScale = priorCopy;
}

private void ApplyFullResize(IKResizeCentroid centroid)
Expand Down

0 comments on commit 63a36dd

Please sign in to comment.