Skip to content

Commit

Permalink
arm stretching mode
Browse files Browse the repository at this point in the history
  • Loading branch information
JLPM22 committed Oct 4, 2022
1 parent 701ee96 commit 90ddf2b
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 16 deletions.
51 changes: 51 additions & 0 deletions AvatarGo/Assets/AvatarGo/Scripts/Avatar/ArmsStretch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ArmsStretch : MonoBehaviour
{
private Transform RightEndEffector;
private Transform LeftEndEffector;

private Animator Animator;
private Transform RLowerArm;
private Transform RHand;
private Transform LLowerArm;
private Transform LHand;

private void Awake()
{
Animator = GetComponent<Animator>();
Debug.Assert(Animator != null, "Animator is null");
RLowerArm = Animator.GetBoneTransform(HumanBodyBones.RightLowerArm);
RHand = Animator.GetBoneTransform(HumanBodyBones.RightHand);
LLowerArm = Animator.GetBoneTransform(HumanBodyBones.LeftLowerArm);
LHand = Animator.GetBoneTransform(HumanBodyBones.LeftHand);
}

public void DoUpdate()
{
if (RightEndEffector != null)
{
Vector3 dir = (RHand.position - RLowerArm.position).normalized;
float dist = Vector3.Distance(RHand.position, RightEndEffector.position);
Debug.Log(dist);
RLowerArm.position += dir * dist;
}
if (LeftEndEffector != null)
{
Vector3 dir = (LHand.position - LLowerArm.position).normalized;
float dist = Vector3.Distance(LHand.position, LeftEndEffector.position);
LLowerArm.position += dir * dist;
}
}

public void SetRightEndEffector(Transform t)
{
RightEndEffector = t;
}
public void SetLeftEndEffector(Transform t)
{
LeftEndEffector = t;
}
}
11 changes: 11 additions & 0 deletions AvatarGo/Assets/AvatarGo/Scripts/Avatar/ArmsStretch.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 54 additions & 5 deletions AvatarGo/Assets/AvatarGo/Scripts/AvatarGo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public enum ControllersStyle
OpenHandFreeControllers, // No Finger IK... controllers are free to move
ClosedHandFreeControllers, // Finger IK... controllers are free to move
ClosedHandAttachedControllers, // Finger IK... controllers remain attached to the hand
OpenHandHiddenControllers // No Finger IK... controllers are not displayed
OpenHandHiddenControllers, // No Finger IK... controllers are not displayed
CloseHandStretchArm, // Finger IK... arm is stretch to reach the controller
OpenHandStretchArm // No Finger IK... controllers are not displayer... arm is stretch to reach the controller
}
public ControllersStyle controllersStyle = ControllersStyle.ClosedHandAttachedControllers;

Expand Down Expand Up @@ -63,6 +65,7 @@ public void resetControllersStyle()
controllerUnity.SetControllersAttached(false);
controllerUnity.SetOpenFingers();
controllerUnity.SetClampControllers(false);
controllerUnity.SetArmStretch(false);
}
}
else if (controllersStyle == ControllersStyle.ClosedHandFreeControllers)
Expand All @@ -74,8 +77,9 @@ public void resetControllersStyle()
{
AvatarController_UnityIK controllerUnity = (AvatarController_UnityIK)controller;
controllerUnity.SetControllersAttached(true);
controllerUnity.SetAutomaticFingers(handRight.GetComponentInChildren<ControllerFingers>(), handLeft.GetComponentInChildren<ControllerFingers>());
controllerUnity.SetAutomaticFingers(handRight.GetComponentInChildren<ControllerFingers>(true), handLeft.GetComponentInChildren<ControllerFingers>(true));
controllerUnity.SetClampControllers(false);
controllerUnity.SetArmStretch(false);
}
}
else if (controllersStyle == ControllersStyle.ClosedHandAttachedControllers)
Expand All @@ -87,8 +91,9 @@ public void resetControllersStyle()
{
AvatarController_UnityIK controllerUnity = (AvatarController_UnityIK)controller;
controllerUnity.SetControllersAttached(true);
controllerUnity.SetAutomaticFingers(handRight.GetComponentInChildren<ControllerFingers>(), handLeft.GetComponentInChildren<ControllerFingers>());
controllerUnity.SetAutomaticFingers(handRight.GetComponentInChildren<ControllerFingers>(true), handLeft.GetComponentInChildren<ControllerFingers>(true));
controllerUnity.SetClampControllers(true);
controllerUnity.SetArmStretch(false);
}
}
else if (controllersStyle == ControllersStyle.OpenHandHiddenControllers)
Expand All @@ -102,6 +107,35 @@ public void resetControllersStyle()
controllerUnity.SetControllersAttached(false);
controllerUnity.SetUniformFingers(0.1f);
controllerUnity.SetClampControllers(false);
controllerUnity.SetArmStretch(false);
}
}
else if (controllersStyle == ControllersStyle.CloseHandStretchArm)
{
active = singleInput == null || singleInput.stage != PipelineUtils.Stage.DONE;
activeControllers = true;

if (controller != null && controller is AvatarController_UnityIK)
{
AvatarController_UnityIK controllerUnity = (AvatarController_UnityIK)controller;
controllerUnity.SetControllersAttached(true);
controllerUnity.SetAutomaticFingers(handRight.GetComponentInChildren<ControllerFingers>(true), handLeft.GetComponentInChildren<ControllerFingers>(true));
controllerUnity.SetClampControllers(true);
controllerUnity.SetArmStretch(true);
}
}
else if (controllersStyle == ControllersStyle.OpenHandStretchArm)
{
active = false;
activeControllers = false;

if (controller != null && controller is AvatarController_UnityIK)
{
AvatarController_UnityIK controllerUnity = (AvatarController_UnityIK)controller;
controllerUnity.SetControllersAttached(false);
controllerUnity.SetUniformFingers(0.1f);
controllerUnity.SetClampControllers(false);
controllerUnity.SetArmStretch(true);
}
}

Expand Down Expand Up @@ -130,7 +164,6 @@ public void resetControllersStyle()
private GameObject inputSystem;
[HideInInspector] public SingleInput singleInput;
[HideInInspector] public ContinuousInput continuousInput;
private AvatarProperties avatarProperties;

// Scene elements
private GameObject displayMirrorObj;
Expand Down Expand Up @@ -298,7 +331,13 @@ public bool loadAvatar()
controller.driver = driver;
controller.body = body;
(controller as AvatarController_UnityIK).Desc = humanDescription;
avatarProperties = avatar.GetComponent<AvatarProperties>();

// Arm Stretch
//AvatarProperties avatarProperties = avatar.GetComponent<AvatarProperties>();
ArmsStretch armsStretch = avatar.AddComponent<ArmsStretch>();
armsStretch.SetLeftEndEffector(body.jointWristLeft.transform);
armsStretch.SetRightEndEffector(body.jointWristRight.transform);
((AvatarController_UnityIK)controller).SetArmStretch(armsStretch);

setIKActive(true);

Expand Down Expand Up @@ -366,6 +405,16 @@ public void clearAvatar()
}
}

#if UNITY_EDITOR
private void OnValidate()
{
if (UnityEditor.EditorApplication.isPlaying)
{
resetControllersStyle();
}
}
#endif

/************************* UI ****************************/

private void setDisplayMirrorVisible(bool flag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public bool CalibrationFinished

private bool ControllersAttached = false;
private bool ClampControllers = false;

private bool ArmsStretchFlag = false;

private int Flag = 0;

Expand All @@ -32,6 +32,8 @@ public bool CalibrationFinished
private Quaternion LastLeftHandWorldRotation;
private Quaternion LastRightHandWorldRotation;

private ArmsStretch ArmsStretch;

// Fingers
private BendFingersType FingersType;
private float UniformBendT;
Expand Down Expand Up @@ -213,6 +215,12 @@ private void LateUpdate()
Animator.GetBoneTransform(HumanBodyBones.LeftHand).localRotation = Animator.GetBoneTransform(HumanBodyBones.LeftHand).localRotation * Quaternion.Euler(AvatarProperties.LeftHandRotation);
}

// Arm Stretch
if (ArmsStretchFlag)
{
ArmsStretch.DoUpdate();
}

// Fingers
if (FingersType == BendFingersType.Automatic)
{
Expand Down Expand Up @@ -251,6 +259,16 @@ private void LateUpdate()
}
}

public void SetArmStretch(ArmsStretch armsStretch)
{
ArmsStretch = armsStretch;
}

public void SetArmStretch(bool e)
{
ArmsStretchFlag = e;
}

public void SetControllersAttached(bool e)
{
ControllersAttached = e;
Expand Down
2 changes: 1 addition & 1 deletion AvatarGo/ProjectSettings/EditorBuildSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ EditorBuildSettings:
serializedVersion: 2
m_Scenes: []
m_configObjects:
Unity.XR.OpenVR.Settings: {fileID: 11400000, guid: 3da6e5a8de53f2a49865b7c713ce3ced,
Unity.XR.OpenVR.Settings: {fileID: 11400000, guid: 08b8fabde19e30c4286268565d5dfbef,
type: 2}
com.unity.xr.management.loader_settings: {fileID: 11400000, guid: 6891c9216432d7a498f3b1cdae302043,
type: 2}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Some dependencies might not be installed yet. Press continue and open the projec

Probably the TMP Essential Resources are not imported. Go to: 'Window/TextMeshPro/Import TMP Essential Resources' and import all files.

**Q: I can only see the mirror with one eye:**

Go to 'Edit/Project Settings/OpenVR' and change 'Stereo Rendering Mode' from 'Single Pass Instanced' to 'Multi Pass'.



## License
Expand Down
2 changes: 2 additions & 0 deletions doc/howtouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* ClosedHandFreeControllers: Show controllers in their real position but with Finger IK
* ClosedHandAttachedControllers: Show controllers attached to the hands with Finger IK
* OpenHandHiddenControllers: No controllers
* CloseHandStretchArm: Show controllers attached to the hands with Finger IK and stretch the arm to reach the end effector
* OpenHandStretchArm: No controllers and stretch the arm to reach the end effector
- **OnCalibrationFinished**: Scripts can subscribe to this event; it will be called once the calibration is completed and the user is inside the avatar
- Add a ground (e.g., a plane) with position y=0 and with a collider so the avatar can be correctly placed on top
- Press Play!
Expand Down
23 changes: 14 additions & 9 deletions doc/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ AvatarGo has been tested in **Unity 2020.3 LTS** and **2021.2**
We **recommend using 2020.3+** since previous Unity version may have different dependencies/packages manager and AvatarGo might be more difficult to install. However, if you manage to install all dependencies (SteamVR, Netcode for GameObjects and TextMeshPro) it should work in older Unity versions.

## Installation
There are two ways to install AvatarGo: using an existing project and adding AvatarGo as a package or creating a new project by cloning this repository.
### First option: Package
There are two ways to install AvatarGo: creating a new project by cloning this repository or using an existing project and adding AvatarGo as a package.
We recommend using the first option since it will contain the most updated version:
### First option: Clone
- Clone this repository with git or download as a zip
- Open the project with Unity (it will warn you that there are errors in the project, press continue and ignore)
- When the project is cloned the Package Manager dependencies should be handled automatically by Unity, if that is not the case, please follow the steps for installing dependencies in the Second option section
- Install SteamVR from the Asset Store: https://assetstore.unity.com/packages/tools/integration/steamvr-plugin-32647
(When installing SteamVR, Unity should automatically set up the project for its use in VR and install OpenVR)
- Delete all MainCameras in the scene (or change their MainCamera tag)
### Second option: Package
- Download the last version of the .unitypackage from the [Release page](https://github.com/UPC-ViRVIG/AvatarGo/releases)
- Open the package or go to the Unity menu ‘Assets/Import Package/Custom Package’ and select the package
- At this point some errors will appear until all dependencies are installed:
Expand All @@ -17,14 +25,11 @@ There are two ways to install AvatarGo: using an existing project and adding Ava
- Netcode for GameObjects:
- Click the + button
- Add package from git URL: com.unity.netcode.gameobjects and click Add
- Once the package is installed, click the left arrow to unfold all versions
- Click 'See other versions'
- Select 1.0.0-pre.3 and click the 'Update' button (newer versions may not work).
- TextMeshPro (from Unity Registry) (after installation make sure the TMP Essential Resources are imported from the TextMeshPro window)
- Install SteamVR from the Asset Store: https://assetstore.unity.com/packages/tools/integration/steamvr-plugin-32647
(When installing SteamVR, Unity should automatically set up the project for its use in VR and install OpenVR)
- Delete all MainCameras in the scene (or change their MainCamera tag)
### Second option: Clone
- Clone this repository with git
- Open the project with Unity (it will warn you that there are errors in the project, press continue and ignore)
- When the project is cloned the Package Manager dependencies should be handled automatically by Unity, if that is not the case, please follow the steps for installing dependencies in the First option
- Install SteamVR from the Asset Store: https://assetstore.unity.com/packages/tools/integration/steamvr-plugin-32647
(When installing SteamVR, Unity should automatically set up the project for its use in VR and install OpenVR)
- Delete all MainCameras in the scene (or change their MainCamera tag)

0 comments on commit 90ddf2b

Please sign in to comment.