Skip to content

Commit

Permalink
chore(automaticHandSetup): Option to generate bone anchors as parent …
Browse files Browse the repository at this point in the history
…or children
  • Loading branch information
jorgejgnz committed Oct 26, 2020
1 parent c41ee97 commit 3277be8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1,837 deletions.
72 changes: 53 additions & 19 deletions Runtime/Components/AutomaticHandSetup.cs
Expand Up @@ -10,6 +10,13 @@
using UnityEditor;
#endif

public enum Anchors
{
None = 0,
AsChildren = 1,
AsParents = 2
}

public class AutomaticHandSetup : MonoBehaviour
{
public Side handType;
Expand All @@ -27,11 +34,11 @@ public class AutomaticHandSetup : MonoBehaviour
GameObject objects;
GameObject modules;

int thumbSiblingIndex;
int indexSiblingIndex;
int middleSiblingIndex;
int ringSiblingIndex;
int pinkySiblingIndex;
int thumbSiblingIndex = -1;
int indexSiblingIndex = -1;
int middleSiblingIndex = -1;
int ringSiblingIndex = -1;
int pinkySiblingIndex = -1;

GameObject masterRootObject;
GameObject masterWrist;
Expand Down Expand Up @@ -66,7 +73,7 @@ public class AutomaticHandSetup : MonoBehaviour
public PhysicMaterial skinPhysMat;

[Header("Control")]
public bool generateArmatureAnchors = false;
public Anchors generateArmatureAnchors = Anchors.None;
public bool generateMasterOffset = true;
public bool generateRays = true;
public bool generateSlave = true;
Expand Down Expand Up @@ -105,9 +112,9 @@ public void Setup()
// Get sibilng index for index and thumb
thumbSiblingIndex = thumbRootBone.GetSiblingIndex();
indexSiblingIndex = indexRootBone.GetSiblingIndex();
middleSiblingIndex = middleRootBone.GetSiblingIndex();
ringSiblingIndex = ringRootBone.GetSiblingIndex();
pinkySiblingIndex = pinkyRootBone.GetSiblingIndex();
if (middleRootBone) middleSiblingIndex = middleRootBone.GetSiblingIndex();
if (ringRootBone) ringSiblingIndex = ringRootBone.GetSiblingIndex();
if (pinkyRootBone) pinkySiblingIndex = pinkyRootBone.GetSiblingIndex();

// Initialize phModel, masterhandModel and slaveHandModel
SetupProxyHandModule();
Expand Down Expand Up @@ -177,16 +184,21 @@ void SetupMasterObjects()
}

// Armature wrist anchor
if (generateArmatureAnchors)
if (generateArmatureAnchors > Anchors.None)
{
wristAnchor = new GameObject().transform;
wristAnchor.name = wrist.name + ".Anchor";
wristAnchor.position = wrist.position;

if (wrist.parent)
if (generateArmatureAnchors == Anchors.AsParents)
{
wristAnchor.parent = wrist.parent;

wrist.parent = wristAnchor;
wrist.parent = wristAnchor;
}
else if (generateArmatureAnchors == Anchors.AsChildren)
{
wristAnchor.parent = wrist;
}
}

// Create bones
Expand All @@ -198,7 +210,12 @@ void SetupMasterObjects()
for (int b = 0; b < _fingers[f].Length; b++)
{
Transform bone = BasicHelpers.InstantiateEmptyChild(masterFingersRoot).transform;
bone.name = "Finger" + f + ".Bone" + b;

if (b==0 && _fingers[f][b].childCount == 0)
bone.name = "Extra.Bone";
else
bone.name = "Finger" + f + ".Bone" + b;

bone.position = _fingers[f][b].position;

// Parenting
Expand All @@ -210,14 +227,27 @@ void SetupMasterObjects()
// Not rotation needed

//Armature anchor
if (generateArmatureAnchors)
if (generateArmatureAnchors > Anchors.None)
{
Transform anchor = new GameObject().transform;
anchor.name = _fingers[f][b].name + ".Anchor";
anchor.position = _fingers[f][b].position;

anchor.parent = _fingers[f][b].parent;
_fingers[f][b].parent = anchor;
if (_fingers[f][b].childCount > 0)
_fingers[f][b].GetChild(0).parent = anchor;

if (generateArmatureAnchors == Anchors.AsParents)
{
anchor.parent = _fingers[f][b].parent;
_fingers[f][b].parent = anchor;
}
else if (generateArmatureAnchors == Anchors.AsChildren)
{
anchor.parent = _fingers[f][b];

if (b == 0)
_fingers[f][b].parent = wristAnchor;
}

_fingerAnchors.Add(anchor);
}
Expand Down Expand Up @@ -391,7 +421,7 @@ void SetupMasterHandModel(MasterHandModel handModel, Transform masterWrist, Game
*/
masterBone.armatureBone = _fingers[f][b];

if (_anchors.Count >= f - 1 && _anchors[f].Length >= b - 1) masterBone.armatureAnchor = _anchors[f][b];
if (generateArmatureAnchors > Anchors.None && _anchors.Count >= f - 1 && _anchors[f].Length >= b - 1) masterBone.armatureAnchor = _anchors[f][b];

masterBone.initialArmatureBoneLocalRot = _localRots[f][b];
masterBone.relativeToOriginalArmatureLocal = Quaternion.Inverse(fingerTransforms[b].localRotation) * _localRots[f][b];
Expand Down Expand Up @@ -507,7 +537,11 @@ void SetupSlaveHandModel(SlaveHandModel handModel, Transform slaveWrist)
*/

// Simple automatic rig mapping
if (handModel.proxyHand.master.fingers[f] != null && handModel.proxyHand.master.fingers[f].bones[b] != null)
if (f > handModel.proxyHand.master.fingers.Length - 1)
Debug.LogError("Trying to access a non-existing finger!");
else if (b > handModel.proxyHand.master.fingers[f].bones.Length - 1)
Debug.LogError("Trying to access a non-existing bone!");
else if (handModel.proxyHand.master.fingers[f] != null && handModel.proxyHand.master.fingers[f].bones[b] != null)
slaveBone.masterBone = handModel.proxyHand.master.fingers[f].bones[b] as MasterBoneModel;

/*
Expand Down

0 comments on commit 3277be8

Please sign in to comment.