Skip to content

Commit

Permalink
Added Resize Character resize method. Improvements to Breathing bone …
Browse files Browse the repository at this point in the history
…effect perf/memory.
  • Loading branch information
OrangeSpork committed Dec 3, 2021
1 parent 76ece5d commit 057aa60
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 30 deletions.
62 changes: 52 additions & 10 deletions AdvIKPlugin/AdvIKGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ internal class AdvIKGUI
private static Toggle resizeCentroid_ElbowCenter;
private static Toggle resizeCentroid_ElbowLeft;
private static Toggle resizeCentroid_ElbowRight;
private static Toggle resizeCentroid_Resize;

private static Toggle resizeChainMode_LeftArm;
private static Toggle resizeChainMode_RightArm;
private static Toggle resizeChainMode_LeftLeg;
private static Toggle resizeChainMode_RightLeg;
private static Toggle resizeChainMode_RightLeg;

private static Button resizeButton;
private static TextMeshProUGUI resizeButtonText;
Expand Down Expand Up @@ -283,6 +284,9 @@ internal static void UpdateUI(OCIChar _char)
case Algos.IKResizeCentroid.ELBOW_RIGHT:
resizeCentroid_ElbowRight.isOn = true;
break;
case Algos.IKResizeCentroid.RESIZE:
resizeCentroid_Resize.isOn = true;
break;

}
Algos.IKResizeChainAdjustment leftArmAdjustment;
Expand Down Expand Up @@ -1466,16 +1470,50 @@ public static void SetupResizeControls()
updatingResizeCentroidControls = false;
});

// Full Chara Resize
Text resizeCentroidResize = SetupText("ResizeCentroid", -460, "Rescale Chara:", ResizePanel);
resizeCentroidResize.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 190);
resizeCentroidResize.fontSize = 16;
resizeCentroid_Resize = SetupToggle("ResizeCentroid", -460, resizeCentroidResize.transform, 105, ResizePanel);
resizeCentroid_Resize.onValueChanged.AddListener(delegate (bool value)
{
if (updatingResizeCentroidControls)
{
return;
}
updatingResizeCentroidControls = true;
ClearResizeCentroidControls();
if (selectedChar != null && value)
{
resizeCentroid_Resize.isOn = true;
foreach (AdvIKCharaController controller in StudioAPI.GetSelectedControllers<AdvIKCharaController>())
{
controller.IKResizeController.Centroid = Algos.IKResizeCentroid.RESIZE;
}

}
else if (selectedChar != null)
{
resizeCentroid_Auto.isOn = true;
foreach (AdvIKCharaController controller in StudioAPI.GetSelectedControllers<AdvIKCharaController>())
{
controller.IKResizeController.Centroid = Algos.IKResizeCentroid.AUTO;
}
}
updatingResizeCentroidControls = false;
});


// Left Arm Chain Mode

Text resizeLeftArmChainText = SetupText("ResizeLAChainDesc", -460, "Turn On/Off Ind. Limbs", ResizePanel);
Text resizeLeftArmChainText = SetupText("ResizeLAChainDesc", -485, "Turn On/Off Ind. Limbs", ResizePanel);
resizeLeftArmChainText.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 190);
resizeLeftArmChainText.fontSize = 16;

Text resizeLeftArmChainOffText = SetupText("ResizeCentroid", -485, "Left Arm:", ResizePanel);
Text resizeLeftArmChainOffText = SetupText("ResizeCentroid", -510, "Left Arm:", ResizePanel);
resizeLeftArmChainOffText.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 150);
resizeLeftArmChainOffText.fontSize = 16;
resizeChainMode_LeftArm = SetupToggle("ResizeCentroid", -485, resizeLeftArmChainOffText.transform, 65, ResizePanel);
resizeChainMode_LeftArm = SetupToggle("ResizeCentroid", -510, resizeLeftArmChainOffText.transform, 65, ResizePanel);
resizeChainMode_LeftArm.onValueChanged.AddListener(delegate (bool value)
{
if (updatingResizeCentroidControls)
Expand Down Expand Up @@ -1504,10 +1542,10 @@ public static void SetupResizeControls()
updatingResizeCentroidControls = false;
});

Text resizeRightArmChainOffText = SetupText("ResizeCentroid", -485, 90, "Right Arm:", ResizePanel);
Text resizeRightArmChainOffText = SetupText("ResizeCentroid", -510, 90, "Right Arm:", ResizePanel);
resizeRightArmChainOffText.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 150);
resizeRightArmChainOffText.fontSize = 16;
resizeChainMode_RightArm = SetupToggle("ResizeCentroid", -485, resizeRightArmChainOffText.transform, 75, ResizePanel);
resizeChainMode_RightArm = SetupToggle("ResizeCentroid", -510, resizeRightArmChainOffText.transform, 75, ResizePanel);
resizeChainMode_RightArm.onValueChanged.AddListener(delegate (bool value)
{
if (updatingResizeCentroidControls)
Expand Down Expand Up @@ -1536,10 +1574,10 @@ public static void SetupResizeControls()
updatingResizeCentroidControls = false;
});

Text resizeLeftLegChainOffText = SetupText("ResizeCentroid", -510, "Left Leg:", ResizePanel);
Text resizeLeftLegChainOffText = SetupText("ResizeCentroid", -535, "Left Leg:", ResizePanel);
resizeLeftLegChainOffText.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 150);
resizeLeftLegChainOffText.fontSize = 16;
resizeChainMode_LeftLeg = SetupToggle("ResizeCentroid", -510, resizeLeftLegChainOffText.transform, 65, ResizePanel);
resizeChainMode_LeftLeg = SetupToggle("ResizeCentroid", -535, resizeLeftLegChainOffText.transform, 65, ResizePanel);
resizeChainMode_LeftLeg.onValueChanged.AddListener(delegate (bool value)
{
if (updatingResizeCentroidControls)
Expand Down Expand Up @@ -1568,10 +1606,10 @@ public static void SetupResizeControls()
updatingResizeCentroidControls = false;
});

Text resizeRightLegChainOffText = SetupText("ResizeCentroid", -510, 90, "Right Leg:", ResizePanel);
Text resizeRightLegChainOffText = SetupText("ResizeCentroid", -535, 90, "Right Leg:", ResizePanel);
resizeRightLegChainOffText.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 150);
resizeRightLegChainOffText.fontSize = 16;
resizeChainMode_RightLeg = SetupToggle("ResizeCentroid", -510, resizeRightLegChainOffText.transform, 75, ResizePanel);
resizeChainMode_RightLeg = SetupToggle("ResizeCentroid", -535, resizeRightLegChainOffText.transform, 75, ResizePanel);
resizeChainMode_RightLeg.onValueChanged.AddListener(delegate (bool value)
{
if (updatingResizeCentroidControls)
Expand Down Expand Up @@ -1660,6 +1698,8 @@ public static void SetupResizeControls()
&& child.gameObject != resizeCentroidElbowCntrText.gameObject && child.gameObject != resizeCentroidElbowLeftText.gameObject && child.gameObject != resizeCentroidElbowRgtText.gameObject && child.gameObject != resizeElbowCentroidText.gameObject
&& child.gameObject != resizeCentroid_ElbowCenter.gameObject && child.gameObject != resizeCentroid_ElbowLeft.gameObject && child.gameObject != resizeCentroid_ElbowRight.gameObject

&& child.gameObject != resizeCentroidResize.gameObject && child.gameObject != resizeCentroid_Resize.gameObject

&& child.gameObject != resizeLeftArmChainOffText.gameObject && child.gameObject != resizeLeftArmChainText.gameObject
&& child.gameObject != resizeChainMode_LeftArm.gameObject

Expand Down Expand Up @@ -1709,6 +1749,8 @@ private static void ClearResizeCentroidControls()
resizeCentroid_ElbowCenter.isOn = false;
resizeCentroid_ElbowLeft.isOn = false;
resizeCentroid_ElbowRight.isOn = false;

resizeCentroid_Resize.isOn = false;
}

public static void SetupBreathControls()
Expand Down
1 change: 1 addition & 0 deletions AdvIKPlugin/AdvIKPlugin.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Compile Include="$(MSBuildThisFileDirectory)AdvIKShoulderRotator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Algos\BreathingBoneEffect.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Algos\IKResizeAdjustment.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Algos\ResizeBoneEffect.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ConfigurationManagerAttributes.cs" />
<Compile Include="$(MSBuildThisFileDirectory)OverriddenTransform.cs" />
</ItemGroup>
Expand Down
43 changes: 28 additions & 15 deletions AdvIKPlugin/Algos/BreathingBoneEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public override BoneModifierData GetEffect(string bone, BoneController origin, C
newUpperChestPos.z = ((1 + newUpperChestPos.z) * appliedUpperBreathScale.z) - ((1 + newUpperChestPos.z));
newUpperChestPos.y = ((1 + newUpperChestPos.y) * appliedUpperBreathScale.y) - ((1 + newUpperChestPos.y));

Vector3 newNeckPos = new Vector3(0, newUpperChestPos.y + (newUpperChestPos.y * NeckMotionDampeningFactor), newUpperChestPos.z + (newUpperChestPos.z * NeckMotionDampeningFactor));
Vector3 newNeckPos = new Vector3(0, newUpperChestPos.y + (newUpperChestPos.y * NeckMotionDampeningFactor), 0);

// Same with lower chest
Vector3 newLowerChestPos = new Vector3(0, 0, 0);
Expand Down Expand Up @@ -228,71 +228,84 @@ public override BoneModifierData GetEffect(string bone, BoneController origin, C
if (bone.Equals(UpperChest))
{
#if KOIKATSU || KKS
return new BoneModifierData(FrameEffects.UpperChestScale, 1f, Vector3.zero, Vector3.zero);
return UpdateBoneModifier(bone, FrameEffects.UpperChestScale, Vector3.zero);
#else
return new BoneModifierData(FrameEffects.UpperChestScale, 1f, FrameEffects.UpperChestPos, Vector3.zero);
return UpdateBoneModifier(bone, FrameEffects.UpperChestScale, FrameEffects.UpperChestPos);
#endif
}
else if (bone.Equals(LowerChest))
{
#if KOIKATSU || KKS
return new BoneModifierData(FrameEffects.LowerChestScale, 1f, Vector3.zero, Vector3.zero);
return UpdateBoneModifier(bone, FrameEffects.LowerChestScale, Vector3.zero);
#else
return new BoneModifierData(FrameEffects.LowerChestScale, 1f, FrameEffects.LowerChestPos, Vector3.zero);
return UpdateBoneModifier(bone, FrameEffects.LowerChestScale, FrameEffects.LowerChestPos);
#endif
}
else if (bone.Equals(Abdomen))
{
#if KOIKATSU || KKS
return new BoneModifierData(FrameEffects.AbdomenScale, 1f, Vector3.zero, Vector3.zero);
return UpdateBoneModifier(bone, FrameEffects.AbdomenScale, Vector3.zero);
#else
return new BoneModifierData(FrameEffects.AbdomenScale, 1f, FrameEffects.AbdomenPos, Vector3.zero);
return UpdateBoneModifier(bone, FrameEffects.AbdomenScale, FrameEffects.AbdomenPos);
#endif
}
else if (bone.Equals(Breasts))
{
#if KOIKATSU || KKS
return null;
#else
return new BoneModifierData(Vector3.one, 1f, FrameEffects.BreastAdj, Vector3.zero);
return UpdateBoneModifier(bone, Vector3.one, FrameEffects.BreastAdj);
#endif
}
#if KOIKATSU || KKS
else if (bone.Equals(LeftBreast))
{
return new BoneModifierData(Vector3.one, 1f, FrameEffects.BreastAdj, Vector3.zero);
return UpdateBoneModifier(bone, Vector3.one, FrameEffects.BreastAdj);
}
else if (bone.Equals(RightBreast))
{
return new BoneModifierData(Vector3.one, 1f, FrameEffects.BreastAdj, Vector3.zero);
return UpdateBoneModifier(bone, Vector3.one, FrameEffects.BreastAdj);
}
#endif
else if (bone.Equals(LeftShoulder))
{
#if KOIKATSU || KKS
return new BoneModifierData(Vector3.one, 1f, new Vector3(FrameEffects.LeftShoulderAdj.x / 2, 0, 0), Vector3.zero);
return UpdateBoneModifier(bone, Vector3.one, new Vector3(FrameEffects.LeftShoulderAdj.x / 2, 0, 0));
#else
return new BoneModifierData(Vector3.one, 1f, FrameEffects.LeftShoulderAdj, Vector3.zero);
return UpdateBoneModifier(bone, Vector3.one, FrameEffects.LeftShoulderAdj);
#endif
}
else if (bone.Equals(RightShoulder))
{
#if KOIKATSU || KKS
return new BoneModifierData(Vector3.one, 1f, new Vector3(FrameEffects.RightShoulderAdj.x / 2, 0, 0), Vector3.zero);
return UpdateBoneModifier(bone, Vector3.one, new Vector3(FrameEffects.RightShoulderAdj.x / 2, 0, 0));
#else
return new BoneModifierData(Vector3.one, 1f, FrameEffects.RightShoulderAdj, Vector3.zero);
return UpdateBoneModifier(bone, Vector3.one, FrameEffects.RightShoulderAdj);
#endif
}
else if (bone.Equals(Neck))
{
return new BoneModifierData(Vector3.one, 1f, FrameEffects.NeckAdj, Vector3.zero);
return UpdateBoneModifier(bone, Vector3.one, FrameEffects.NeckAdj);
}
else
{
return null;
}
}

private Dictionary<string, BoneModifierData> boneModifierCache = new Dictionary<string, BoneModifierData>();
private BoneModifierData UpdateBoneModifier(string bone, Vector3 scale, Vector3 position)
{
if (!boneModifierCache.TryGetValue(bone, out BoneModifierData boneModifier))
{
boneModifier = new BoneModifierData();
boneModifierCache[bone] = boneModifier;
}
boneModifier.ScaleModifier = scale;
boneModifier.PositionModifier = position;
return boneModifier;
}

public void RestoreDefaults()
{
IntakePause = defaultIntakePause;
Expand Down
50 changes: 45 additions & 5 deletions AdvIKPlugin/Algos/IKResizeAdjustment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Linq;
using System;
using System.Collections.Specialized;
using System.Collections;
using KKABMX.Core;
#if !KOIKATSU && !KKS
using AIChara;
#endif
Expand All @@ -17,6 +19,9 @@ public class IKResizeAdjustment

public OCIChar Self { get; set; }

private ResizeBoneEffect _resize;


private IKResizeCentroid centroid;
public IKResizeCentroid Centroid
{
Expand All @@ -41,6 +46,8 @@ public IKResizeCentroid Centroid
private IKResizeCentroid AppliedCentroid { get; set; }
private Dictionary<IKChain, IKResizeChainAdjustment> AppliedChainAdjustments { get; set; }

private Vector3 OriginalCharacterScale { get; set; }

private ManualLogSource Log => AdvIKPlugin.Instance.Log;

public IKResizeAdjustment()
Expand All @@ -61,6 +68,16 @@ public IKResizeAdjustment()

public void OnReload(ChaControl chaControl)
{
if (_resize == null)
{
BoneController boneController = chaControl.GetComponent<BoneController>();
_resize = new ResizeBoneEffect();
boneController.AddBoneEffect(_resize);
#if DEBUG
Log.LogInfo($"Registering Resize Bone Effect");
#endif
}

AdjustmentApplied = false;

Self = KKAPI.Studio.StudioObjectExtensions.GetOCIChar(chaControl);
Expand All @@ -82,6 +99,17 @@ public void OnReload(ChaControl chaControl)
// Update Current Scale
PopulateCurrentScale();

// Reinitialize Rescale -- Must be after Populate Current Scale since we need the old number to reverse out the scaling adjustment as ABMX is still applying the prior number
_resize.ResizeAmount = Vector3.one;

if (OriginalCharacterScale == Vector3.zero)
{
OriginalCharacterScale = FindCurrentScale();
#if DEBUG
Log.LogInfo($"Setting Original Character Scale to: {OriginalCharacterScale}");
#endif
}

if (!AdvIKPlugin.StudioAutoApplyResize.Value)
{
return;
Expand All @@ -107,19 +135,20 @@ public void UndoAdjustment()
Dictionary<IKScale, float> tempPriorScale = new Dictionary<IKScale, float>(PriorScale);
Dictionary<IKScale, float> tempCurrentScale = new Dictionary<IKScale, float>(CurrentScale);
Dictionary<IKChain, IKResizeChainAdjustment> tempChainAdjustments = new Dictionary<IKChain, IKResizeChainAdjustment>(ChainAdjustments);
IKResizeCentroid tempCentroid = Centroid;
IKResizeCentroid tempCentroid = Centroid;

Centroid = AppliedCentroid;
ChainAdjustments = AppliedChainAdjustments;
CurrentScale = tempPriorScale;
PriorScale = tempCurrentScale;
PriorScale = tempCurrentScale;

ApplyAdjustment();

CurrentScale = new Dictionary<IKScale, float>(tempCurrentScale);
PriorScale = new Dictionary<IKScale, float>(tempPriorScale);
Centroid = tempCentroid;
ChainAdjustments = new Dictionary<IKChain, IKResizeChainAdjustment>(tempChainAdjustments);
_resize.ResizeAmount = Vector3.one;

AdjustmentApplied = false;
}
Expand Down Expand Up @@ -170,6 +199,8 @@ public void ApplyAdjustment()
{
ApplyElbowCentroidAdjustment(UseCentroid);
}
else if (UseCentroid == IKResizeCentroid.RESIZE)
ApplyFullResize(UseCentroid);


if (UseCentroid != IKResizeCentroid.NONE)
Expand All @@ -180,6 +211,14 @@ public void ApplyAdjustment()
}
}

private void ApplyFullResize(IKResizeCentroid centroid)
{
_resize.ResizeAmount = OriginalCharacterScale / CurrentScale[IKScale.BODY];
#if DEBUG
Log.LogInfo($"Applying Character Resize Scaling Adjustment of: {_resize.ResizeAmount.y} Targeting: {OriginalCharacterScale.y} From: {CurrentScale[IKScale.BODY]}");
#endif
}

private void ApplyThighCentroidAdjustment(IKResizeCentroid centroid)
{
// Thigh Adjustment works down the leg chain from thigh
Expand Down Expand Up @@ -850,9 +889,9 @@ private void PopulateCurrentScale()
private Vector3 FindCurrentScale()
{
#if KOIKATSU || KKS
return Self.charInfo.objAnim.transform.Find("cf_j_root/cf_n_height").localScale;
return Self.charInfo.objAnim.transform.Find("cf_j_root/cf_n_height").localScale / _resize.ResizeAmount.y;
#else
return Self.charInfo.objAnim.transform.Find("cf_J_Root/cf_N_height").localScale;
return Self.charInfo.objAnim.transform.Find("cf_J_Root/cf_N_height").localScale / _resize.ResizeAmount.y;
#endif
}

Expand Down Expand Up @@ -1072,6 +1111,7 @@ public enum IKResizeCentroid
KNEE_RIGHT = 17,
ELBOW_CENTER = 18,
ELBOW_LEFT = 19,
ELBOW_RIGHT = 20
ELBOW_RIGHT = 20,
RESIZE = 21
}
}

0 comments on commit 057aa60

Please sign in to comment.