Skip to content

Commit

Permalink
Add head rotation (fixes #15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sauceke committed Nov 6, 2023
1 parent ae2e5b6 commit bd51a01
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
11 changes: 7 additions & 4 deletions KK_SexFaces/FacialExpression.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using HarmonyLib;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using UnityEngine;

namespace SexFaces
Expand All @@ -21,14 +21,15 @@ public partial class FacialExpression
public float LeftEyeScaleY { get; set; } = 1f;
public float RightEyeScaleX { get; set; } = 1f;
public float RightEyeScaleY { get; set; } = 1f;
public Quaternion? NeckRot { get; set; }

public static FacialExpression Capture(ChaControl chaControl)
{
var eyeTexW = Mathf.Lerp(1.8f, -0.2f, chaControl.fileFace.pupilWidth);
var eyeTexH = Mathf.Lerp(1.8f, -0.2f, chaControl.fileFace.pupilHeight);
var leftEyeMatCtrl = chaControl.eyeLookMatCtrl[0];
var rightEyeMatCtrl = chaControl.eyeLookMatCtrl[1];
var expression = new FacialExpression()
var expression = new FacialExpression
{
EyebrowExpression = DictToString(GetExpression(chaControl.eyebrowCtrl)),
EyebrowOpenMax = chaControl.GetEyebrowOpenMax(),
Expand All @@ -41,7 +42,8 @@ public static FacialExpression Capture(ChaControl chaControl)
LeftEyeScaleX = leftEyeMatCtrl.GetEyeTexScale().x / eyeTexW,
LeftEyeScaleY = leftEyeMatCtrl.GetEyeTexScale().y / eyeTexH,
RightEyeScaleX = rightEyeMatCtrl.GetEyeTexScale().x / eyeTexW,
RightEyeScaleY = rightEyeMatCtrl.GetEyeTexScale().y / eyeTexH
RightEyeScaleY = rightEyeMatCtrl.GetEyeTexScale().y / eyeTexH,
NeckRot = Hooks.NeckLookCalcHooks.GetNeckRotation(chaControl)
};
if (IsLookingAtFixedPosition(chaControl))
{
Expand Down Expand Up @@ -80,6 +82,7 @@ public void Apply(ChaControl chaControl)
}
chaControl.mouthCtrl.ChangeFace(StringToDict(MouthExpression), true);
chaControl.ChangeMouthOpenMax(MouthOpenMax);
Hooks.NeckLookCalcHooks.SetNeckRotation(chaControl, NeckRot ?? Quaternion.identity);
}

private static bool IsLookingAtFixedPosition(ChaControl chaControl)
Expand Down
34 changes: 34 additions & 0 deletions KK_SexFaces/Hooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using KKAPI.Studio;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace SexFaces
{
Expand All @@ -13,6 +14,7 @@ public static void InstallHooks()
{
Harmony.CreateAndPatchAll(typeof(FacialExpressionLock));
Harmony.CreateAndPatchAll(typeof(EyeDirectionLock));
Harmony.CreateAndPatchAll(typeof(NeckLookCalcHooks));
}
}

Expand Down Expand Up @@ -57,5 +59,37 @@ public static class EyeDirectionLock
private static bool CanChange(ChaControl __instance) =>
!lockedControls.Contains(__instance);
}

internal static class NeckLookCalcHooks
{
private static readonly Dictionary<NeckLookCalcVer2, Quaternion> angleOffsets =
new Dictionary<NeckLookCalcVer2, Quaternion>();

public static Quaternion GetNeckRotation(ChaControl chaControl) =>
angleOffsets.TryGetValue(chaControl.neckLookCtrl.neckLookScript, out var rotation)
? rotation
: Quaternion.identity;

public static void SetNeckRotation(ChaControl chaControl, Quaternion rotation) =>
angleOffsets[chaControl.neckLookCtrl.neckLookScript] = rotation;

public static void ResetNeckRotation(ChaControl chaControl) =>
angleOffsets.Remove(chaControl.neckLookCtrl.neckLookScript);

[HarmonyPostfix]
[HarmonyPatch(typeof(NeckLookCalcVer2), nameof(NeckLookCalcVer2.NeckUpdateCalc))]
private static void NeckUpdateCalc(NeckLookCalcVer2 __instance)
{
if (!angleOffsets.TryGetValue(__instance, out var offset))
{
return;
}
foreach (var bone in __instance.aBones)
{
bone.neckBone.rotation *= offset;
bone.fixAngle = bone.neckBone.localRotation;
}
}
}
}
}
24 changes: 23 additions & 1 deletion KK_SexFaces/SexFacesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,33 @@ private void ChangeIrisScale(int index, float scale)
ChaControl.eyeLookMatCtrl[index].SetEyeTexScaleX(eyeTexW);
ChaControl.eyeLookMatCtrl[index].SetEyeTexScaleY(eyeTexH);
}

private void ResetIrisScales()
{
ChangeLeftIrisScale(1f);
ChangeRightIrisScale(1f);
}

internal void ChangeHeadPitch(float degrees)
{
var eulerAngles = Hooks.NeckLookCalcHooks.GetNeckRotation(ChaControl).eulerAngles;
eulerAngles.x = degrees;
Hooks.NeckLookCalcHooks.SetNeckRotation(ChaControl, Quaternion.Euler(eulerAngles));
}

internal void ChangeHeadYaw(float degrees)
{
var eulerAngles = Hooks.NeckLookCalcHooks.GetNeckRotation(ChaControl).eulerAngles;
eulerAngles.y = degrees;
Hooks.NeckLookCalcHooks.SetNeckRotation(ChaControl, Quaternion.Euler(eulerAngles));
}

internal void ChangeHeadRoll(float degrees)
{
var eulerAngles = Hooks.NeckLookCalcHooks.GetNeckRotation(ChaControl).eulerAngles;
eulerAngles.z = degrees;
Hooks.NeckLookCalcHooks.SetNeckRotation(ChaControl, Quaternion.Euler(eulerAngles));
}

internal void ApplyEyebrowExpression(Dictionary<int, float> expression, float openness)
{
Expand Down Expand Up @@ -284,6 +305,7 @@ private void ApplyRandomSexFace(Trigger trigger,
if (!facePool.Any())
{
ResetIrisScales();
Hooks.NeckLookCalcHooks.ResetNeckRotation(ChaControl);
return;
}
var index = random.Next(facePool.Count());
Expand Down
6 changes: 6 additions & 0 deletions KK_SexFaces/SexFacesGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ private void RegisterMakerControls(SexFacesPlugin plugin, RegisterSubCategoriesE
.ValueChanged.Subscribe(Controller.ChangeLeftIrisScale);
e.AddControl(new MakerSlider(cat, "Right Iris Scale", 0f, 2f, 1f, plugin))
.ValueChanged.Subscribe(Controller.ChangeRightIrisScale);
e.AddControl(new MakerSlider(cat, "Head Pitch", -45f, 45f, 0f, plugin))
.ValueChanged.Subscribe(Controller.ChangeHeadPitch);
e.AddControl(new MakerSlider(cat, "Head Yaw", -45f, 45f, 0f, plugin))
.ValueChanged.Subscribe(Controller.ChangeHeadYaw);
e.AddControl(new MakerSlider(cat, "Head Roll", -45f, 45f, 0f, plugin))
.ValueChanged.Subscribe(Controller.ChangeHeadRoll);
}

private void AddPatternMixer(RegisterSubCategoriesEvent e, MakerCategory cat,
Expand Down

0 comments on commit bd51a01

Please sign in to comment.