Skip to content

Commit

Permalink
Merge branch 'grip-fitting' into experimental (resolving conflicts wi…
Browse files Browse the repository at this point in the history
…th AvatarTailor)
  • Loading branch information
Ibodan committed Dec 22, 2018
2 parents f1617de + 341efdc commit 80e2b49
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
117 changes: 117 additions & 0 deletions CustomAvatar/AvatarTailor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class AvatarTailor
private const string _kPlayerArmLengthKey = "CustomAvatar.Tailoring.PlayerArmLength";
private const string _kResizePolicyKey = "CustomAvatar.Tailoring.ResizePolicy";
private const string _kFloorMovePolicyKey = "CustomAvatar.Tailoring.FloorMovePolicy";
private const string _kPlayerGripAngleKey = "AvatarAutoFitting.PlayerGripAngle";
private const string _kPlayerGripAngleYKey = "AvatarAutoFitting.PlayerGripAngleY";
private const string _kPlayerGripOffsetZKey = "AvatarAutoFitting.PlayerGripOffsetZ";

public enum ResizePolicyType
{
Expand Down Expand Up @@ -128,6 +131,92 @@ private IEnumerator FloorMendingWithDelay(SpawnedAvatar avatar, Animator animato
}
}

public void GripFittingPlayerAvatar(GameObject avatarGameObject)
{
var animator = FindAvatarAnimator(avatarGameObject);
if (animator == null) return;

void fixGrip(HumanBodyBones indexFingerBoneName, Transform handObject, float gripAngleY, float gripAngleZ, float gripOffsetZ)
{
if (handObject == null) return;
var handTarget = handObject.GetChild(0);
if (handTarget == null) return;

var indexFinger = animator.GetBoneTransform(indexFingerBoneName);

var basePoint = indexFinger.Find("_baseGripPoint");
if (basePoint == null) return;

handTarget.parent = null;
handObject.rotation = Quaternion.AngleAxis(gripAngleY, basePoint.up) * basePoint.rotation;
handObject.rotation = Quaternion.AngleAxis(gripAngleZ, handObject.forward) * handObject.rotation;
handObject.position = basePoint.position + (handObject.forward * gripOffsetZ);
handTarget.parent = handObject;

Plugin.Log("Grip alignment applied. " + gripAngleY + "," + gripAngleZ + "," + gripOffsetZ);
}

var rotZ = PlayerPrefs.GetFloat(_kPlayerGripAngleKey, 80.0f);
var rotY = PlayerPrefs.GetFloat(_kPlayerGripAngleYKey, 15.0f);
var offsetZ = PlayerPrefs.GetFloat(_kPlayerGripOffsetZKey, 0.06f);

fixGrip(HumanBodyBones.RightIndexProximal, avatarGameObject.transform.Find("RightHand"), rotY, rotZ, offsetZ);
fixGrip(HumanBodyBones.LeftIndexProximal, avatarGameObject.transform.Find("LeftHand"), rotY, 180 - rotZ, offsetZ);
}

public void PrepareGripFitting(GameObject avatarGameObject)
{
var animator = FindAvatarAnimator(avatarGameObject);
if (animator == null) return;

Vector3 nearestPointOfLines(Vector3 mainPoint1, Vector3 mainPoint2, Vector3 subPoint1, Vector3 subPoint2)
{
Vector3 vMain = mainPoint2 - mainPoint1;
Vector3 vSub = subPoint2 - subPoint1;
Vector3 vMainNorm = vMain.normalized;
Vector3 vSubNorm = vSub.normalized;
float dot = Vector3.Dot(vMainNorm, vSubNorm);
if (dot == 1.0f) return mainPoint1; // parallel lines - avoid deviding by zero
Vector3 lineToLine = subPoint1 - mainPoint1;
float delta = (Vector3.Dot(lineToLine, vMainNorm) - dot * Vector3.Dot(lineToLine, vSubNorm)) / (1.0f - dot * dot);
return mainPoint1 + delta * vMainNorm;
}

void fixGrip(Transform handObject, HumanBodyBones wristBoneName, HumanBodyBones indexFingerBoneName, HumanBodyBones indexFinger2BoneName)
{
if (handObject == null) return;
var handTarget = handObject.GetChild(0);
if (handTarget == null) return;

var wrist = animator.GetBoneTransform(wristBoneName);
var indexFinger = animator.GetBoneTransform(indexFingerBoneName);
var indexFinger2 = animator.GetBoneTransform(indexFinger2BoneName);

var origParent = handObject.parent;
handTarget.parent = origParent;
handObject.parent = handTarget;
handTarget.rotation = wrist.rotation;
handTarget.position = wrist.position;
handObject.parent = origParent;
handTarget.parent = handObject;

Transform baseGripPoint = indexFinger.Find("_baseGripPoint");
if (baseGripPoint == null)
{
baseGripPoint = new GameObject("_baseGripPoint").transform;
baseGripPoint.parent = indexFinger;
}
// nearest point of initial handObject vector for a line by index finger vector
baseGripPoint.position = nearestPointOfLines(handObject.position, handObject.position + handObject.forward, indexFinger.position, indexFinger2.position);
var handYAxis = Vector3.Cross(handObject.forward, handObject.position - wrist.position);
handYAxis.z = 0f;
baseGripPoint.rotation = Quaternion.LookRotation(Vector3.forward, handYAxis);
}

fixGrip(avatarGameObject.transform.Find("RightHand"), HumanBodyBones.RightHand, HumanBodyBones.RightIndexProximal, HumanBodyBones.RightIndexIntermediate);
fixGrip(avatarGameObject.transform.Find("LeftHand"), HumanBodyBones.LeftHand, HumanBodyBones.LeftIndexProximal, HumanBodyBones.LeftIndexIntermediate);
}

public void MeasurePlayerArmLength(Action<float> onProgress, Action<float> onFinished)
{
var active = SceneManager.GetActiveScene().GetRootGameObjects()[0].GetComponent<PlayerArmLengthMeasurement>();
Expand Down Expand Up @@ -180,5 +269,33 @@ void OnDestroy()
CancelInvoke();
}
}

public void IncrementPlayerGripAngle(int step, GameObject avatarGameObject)
{
var v = PlayerPrefs.GetFloat(_kPlayerGripAngleKey, 80.0f);
v += 5.0f * step;
PlayerPrefs.SetFloat(_kPlayerGripAngleKey, v);
PlayerPrefs.Save();
GripFittingPlayerAvatar(avatarGameObject);
}

public void IncrementPlayerGripAngleY(int step, GameObject avatarGameObject)
{
var v = PlayerPrefs.GetFloat(_kPlayerGripAngleYKey, 15.0f);
v += 5.0f * step;
PlayerPrefs.SetFloat(_kPlayerGripAngleYKey, v);
PlayerPrefs.Save();
GripFittingPlayerAvatar(avatarGameObject);
}

public void IncrementPlayerGripOffsetZ(int step, GameObject avatarGameObject)
{
var v = PlayerPrefs.GetFloat(_kPlayerGripOffsetZKey, 0.06f);
v += 0.01f * step;
PlayerPrefs.SetFloat(_kPlayerGripOffsetZKey, v);
PlayerPrefs.Save();
GripFittingPlayerAvatar(avatarGameObject);
}

}
}
25 changes: 25 additions & 0 deletions CustomAvatar/PlayerAvatarManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ private void CustomAvatarLoaded(CustomAvatar loadedAvatar, AvatarLoadResult resu

_avatarTailor.OnAvatarLoaded(_currentSpawnedPlayerAvatar);
ResizePlayerAvatar();
_avatarTailor.PrepareGripFitting(_currentSpawnedPlayerAvatar.GameObject);
GripFittingPlayerAvatar();
OnFirstPersonEnabledChanged(Plugin.Instance.FirstPersonEnabled);
}

Expand Down Expand Up @@ -165,5 +167,28 @@ public void ResizePlayerAvatar()

_avatarTailor.ResizeAvatar(_currentSpawnedPlayerAvatar);
}

public void GripFittingPlayerAvatar()
{
if (_currentSpawnedPlayerAvatar?.GameObject == null) return;

_avatarTailor.GripFittingPlayerAvatar(_currentSpawnedPlayerAvatar.GameObject);
}

public void IncrementPlayerGripAngle(int step)
{
_avatarTailor.IncrementPlayerGripAngle(step, _currentSpawnedPlayerAvatar.GameObject);
}

public void IncrementPlayerGripAngleY(int step)
{
_avatarTailor.IncrementPlayerGripAngleY(step, _currentSpawnedPlayerAvatar.GameObject);
}

public void IncrementPlayerGripOffsetZ(int step)
{
_avatarTailor.IncrementPlayerGripOffsetZ(step, _currentSpawnedPlayerAvatar.GameObject);
}

}
}
24 changes: 24 additions & 0 deletions CustomAvatar/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,30 @@ public void OnUpdate()
{
FirstPersonEnabled = !FirstPersonEnabled;
}
else if (Input.GetKeyDown(KeyCode.M))
{
PlayerAvatarManager.IncrementPlayerGripAngle(1);
}
else if (Input.GetKeyDown(KeyCode.N))
{
PlayerAvatarManager.IncrementPlayerGripAngle(-1);
}
else if (Input.GetKeyDown(KeyCode.J))
{
PlayerAvatarManager.IncrementPlayerGripAngleY(1);
}
else if (Input.GetKeyDown(KeyCode.H))
{
PlayerAvatarManager.IncrementPlayerGripAngleY(-1);
}
else if (Input.GetKeyDown(KeyCode.L))
{
PlayerAvatarManager.IncrementPlayerGripOffsetZ(1);
}
else if (Input.GetKeyDown(KeyCode.K))
{
PlayerAvatarManager.IncrementPlayerGripOffsetZ(-1);
}
}

private void SetCameraCullingMask(Camera camera)
Expand Down

0 comments on commit 80e2b49

Please sign in to comment.