From 5fe8880a3e4c1d2779bf05350b2da323a5fc97ca Mon Sep 17 00:00:00 2001 From: PureDark Date: Mon, 22 Oct 2018 06:46:20 +0800 Subject: [PATCH 1/8] Added full body tracking; Added a new way to fix knee bending. --- CustomAvatar/AvatarBehaviour.cs | 90 ++++++++++++++++---------- CustomAvatar/AvatarEventsPlayer.cs | 41 +++++++----- CustomAvatar/CustomAvatar.cs | 12 ++-- CustomAvatar/CustomAvatar.csproj | 24 +++---- CustomAvatar/IAvatarInput.cs | 4 +- CustomAvatar/PlayerAvatarInput.cs | 88 ++++++++++++++++++++++--- CustomAvatar/PlayerAvatarManager.cs | 14 ++-- CustomAvatar/Plugin.cs | 79 ++++++++++++++++++---- CustomAvatar/VRIK/IKManagerAdvanced.cs | 13 ++++ CustomAvatar/VRIK/IKSolverVR.cs | 29 +++++++-- 10 files changed, 291 insertions(+), 103 deletions(-) diff --git a/CustomAvatar/AvatarBehaviour.cs b/CustomAvatar/AvatarBehaviour.cs index e29db07..4954a07 100644 --- a/CustomAvatar/AvatarBehaviour.cs +++ b/CustomAvatar/AvatarBehaviour.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using System; +using UnityEngine; namespace CustomAvatar { @@ -10,8 +11,10 @@ public class AvatarBehaviour : MonoBehaviour private Transform _body; private Transform _left; private Transform _right; + private Transform _leftLeg; + private Transform _rightLeg; - private Vector3 _prevBodyPos; + private Vector3 _prevBodyPos; public void Init(IAvatarInput avatarInput) { @@ -21,41 +24,60 @@ public void Init(IAvatarInput avatarInput) _body = gameObject.transform.Find("Body"); _left = gameObject.transform.Find("LeftHand"); _right = gameObject.transform.Find("RightHand"); - } + _leftLeg = gameObject.transform.Find("LeftLeg"); + _rightLeg = gameObject.transform.Find("RightLeg"); + } private void LateUpdate() { - var headPosRot = _avatarInput.HeadPosRot; - var leftPosRot = _avatarInput.LeftPosRot; - var rightPosRot = _avatarInput.RightPosRot; - - _head.position = headPosRot.Position; - _head.rotation = headPosRot.Rotation; - - _left.position = leftPosRot.Position; - _left.rotation = leftPosRot.Rotation; - - _right.position = rightPosRot.Position; - _right.rotation = rightPosRot.Rotation; - - var vrPlatformHelper = PersistentSingleton.instance; - - vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_left); - vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_right); - - if (_body == null) return; - _body.position = _head.position - (_head.transform.up * 0.1f); - - var vel = new Vector3(_body.transform.localPosition.x - _prevBodyPos.x, 0.0f, - _body.localPosition.z - _prevBodyPos.z); - - var rot = Quaternion.Euler(0.0f, _head.localEulerAngles.y, 0.0f); - var tiltAxis = Vector3.Cross(gameObject.transform.up, vel); - _body.localRotation = Quaternion.Lerp(_body.localRotation, - Quaternion.AngleAxis(vel.magnitude * 1250.0f, tiltAxis) * rot, - Time.deltaTime * 10.0f); - - _prevBodyPos = _body.transform.localPosition; + try + { + var headPosRot = _avatarInput.HeadPosRot; + var leftPosRot = _avatarInput.LeftPosRot; + var rightPosRot = _avatarInput.RightPosRot; + var leftLegPosRot = _avatarInput.LeftLegPosRot; + var rightLegPosRot = _avatarInput.RightLegPosRot; + + _head.position = headPosRot.Position; + _head.rotation = headPosRot.Rotation; + + _left.position = leftPosRot.Position; + _left.rotation = leftPosRot.Rotation; + + _right.position = rightPosRot.Position; + _right.rotation = rightPosRot.Rotation; + + if (_leftLeg != null && _rightLeg != null) + { + _leftLeg.position = leftLegPosRot.Position; + _leftLeg.rotation = leftLegPosRot.Rotation; + + _rightLeg.position = rightLegPosRot.Position; + _rightLeg.rotation = rightLegPosRot.Rotation; + } + + var vrPlatformHelper = PersistentSingleton.instance; + + vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_left); + vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_right); + + if (_body == null) return; + _body.position = _head.position - (_head.transform.up * 0.1f); + + var vel = new Vector3(_body.transform.localPosition.x - _prevBodyPos.x, 0.0f, + _body.localPosition.z - _prevBodyPos.z); + + var rot = Quaternion.Euler(0.0f, _head.localEulerAngles.y, 0.0f); + var tiltAxis = Vector3.Cross(gameObject.transform.up, vel); + _body.localRotation = Quaternion.Lerp(_body.localRotation, + Quaternion.AngleAxis(vel.magnitude * 1250.0f, tiltAxis) * rot, + Time.deltaTime * 10.0f); + + _prevBodyPos = _body.transform.localPosition; + } catch(Exception e) + { + Console.WriteLine("{0}\n{1}", e.Message, e.StackTrace); + } } private Transform GetHeadTransform() diff --git a/CustomAvatar/AvatarEventsPlayer.cs b/CustomAvatar/AvatarEventsPlayer.cs index 1f04459..f9ccc24 100644 --- a/CustomAvatar/AvatarEventsPlayer.cs +++ b/CustomAvatar/AvatarEventsPlayer.cs @@ -1,5 +1,6 @@ using System.Linq; using UnityEngine; +using UnityEngine.SceneManagement; namespace CustomAvatar { @@ -13,6 +14,29 @@ public class AvatarEventsPlayer : MonoBehaviour private BeatmapObjectCallbackController _beatmapObjectCallbackController; private void Start() + { + SceneManager.sceneLoaded += SceneManagerOnSceneLoaded; + } + + private void OnDestroy() + { + SceneManager.sceneLoaded -= SceneManagerOnSceneLoaded; + if (_scoreController == null) return; + _scoreController.noteWasCutEvent -= SliceCallBack; + _scoreController.noteWasMissedEvent -= NoteMissCallBack; + _scoreController.multiplierDidChangeEvent -= MultiplierCallBack; + _scoreController.comboDidChangeEvent -= ComboChangeEvent; + + _saberCollisionManager.sparkleEffectDidStartEvent -= SaberStartCollide; + _saberCollisionManager.sparkleEffectDidEndEvent -= SaberEndCollide; + + _gameEnergyCounter.gameEnergyDidReach0Event -= FailLevelCallBack; + + + _beatmapObjectCallbackController.beatmapEventDidTriggerEvent -= OnBeatmapEventDidTriggerEvent; + } + + private void SceneManagerOnSceneLoaded(Scene newScene, LoadSceneMode mode) { _eventManager = gameObject.GetComponent(); if (_eventManager == null) @@ -44,23 +68,6 @@ private void Start() _beatmapObjectCallbackController.beatmapEventDidTriggerEvent += OnBeatmapEventDidTriggerEvent; } - private void OnDestroy() - { - if (_scoreController == null) return; - _scoreController.noteWasCutEvent -= SliceCallBack; - _scoreController.noteWasMissedEvent -= NoteMissCallBack; - _scoreController.multiplierDidChangeEvent -= MultiplierCallBack; - _scoreController.comboDidChangeEvent -= ComboChangeEvent; - - _saberCollisionManager.sparkleEffectDidStartEvent -= SaberStartCollide; - _saberCollisionManager.sparkleEffectDidEndEvent -= SaberEndCollide; - - _gameEnergyCounter.gameEnergyDidReach0Event -= FailLevelCallBack; - - - _beatmapObjectCallbackController.beatmapEventDidTriggerEvent -= OnBeatmapEventDidTriggerEvent; - } - private void SliceCallBack(NoteData noteData, NoteCutInfo noteCutInfo, int multiplier) { if (!noteCutInfo.allIsOK) diff --git a/CustomAvatar/CustomAvatar.cs b/CustomAvatar/CustomAvatar.cs index 047032a..26c9b48 100644 --- a/CustomAvatar/CustomAvatar.cs +++ b/CustomAvatar/CustomAvatar.cs @@ -81,13 +81,13 @@ public GameObject GameObject public void Load(Action loadedCallback) { - if (IsLoaded) - { - loadedCallback(this, AvatarLoadResult.Completed); - return; - } + if (IsLoaded) + { + loadedCallback(this, AvatarLoadResult.Completed); + return; + } - void Loaded(AvatarLoadResult result) + void Loaded(AvatarLoadResult result) { loadedCallback(this, result); } diff --git a/CustomAvatar/CustomAvatar.csproj b/CustomAvatar/CustomAvatar.csproj index 39f7323..3119dc7 100644 --- a/CustomAvatar/CustomAvatar.csproj +++ b/CustomAvatar/CustomAvatar.csproj @@ -51,41 +51,41 @@ - ..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Beat Saber\Beat Saber_Data\Managed\Assembly-CSharp.dll + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\Assembly-CSharp.dll - ..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Beat Saber\Beat Saber_Data\Managed\IllusionPlugin.dll + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\IllusionPlugin.dll - ..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.dll + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.dll - ..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.AnimationModule.dll + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.AnimationModule.dll - ..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.AssetBundleModule.dll + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.AssetBundleModule.dll - ..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.AudioModule.dll + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.AudioModule.dll - ..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.CoreModule.dll + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.CoreModule.dll - ..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.ParticleSystemModule.dll + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.ParticleSystemModule.dll - ..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.PhysicsModule.dll + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.PhysicsModule.dll - ..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.VRModule.dll + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.VRModule.dll - ..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.XRModule.dll + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.XRModule.dll @@ -134,6 +134,6 @@ - + \ No newline at end of file diff --git a/CustomAvatar/IAvatarInput.cs b/CustomAvatar/IAvatarInput.cs index afba6f0..bb2ef0d 100644 --- a/CustomAvatar/IAvatarInput.cs +++ b/CustomAvatar/IAvatarInput.cs @@ -5,5 +5,7 @@ public interface IAvatarInput PosRot HeadPosRot { get; } PosRot LeftPosRot { get; } PosRot RightPosRot { get; } - } + PosRot LeftLegPosRot { get; } + PosRot RightLegPosRot { get; } + } } \ No newline at end of file diff --git a/CustomAvatar/PlayerAvatarInput.cs b/CustomAvatar/PlayerAvatarInput.cs index 57be21a..1cd9a53 100644 --- a/CustomAvatar/PlayerAvatarInput.cs +++ b/CustomAvatar/PlayerAvatarInput.cs @@ -1,35 +1,105 @@ -using UnityEngine.XR; +using System; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.XR; namespace CustomAvatar { public class PlayerAvatarInput : IAvatarInput { - public PosRot HeadPosRot + public PlayerAvatarInput() + { + } + + public PosRot HeadPosRot { get { return GetXRNodeWorldPosRot(XRNode.Head); } } public PosRot LeftPosRot { - get { return GetXRNodeWorldPosRot(XRNode.LeftHand); } + get + { + if(Plugin.IsTrackerAsHand && Plugin.Trackers.Capacity >= 2) + return GetTrackerWorldPosRot(Plugin.Trackers[0]); + else + return GetXRNodeWorldPosRot(XRNode.LeftHand); + } } public PosRot RightPosRot { - get { return GetXRNodeWorldPosRot(XRNode.RightHand); } - } + get + { + if (Plugin.IsTrackerAsHand && Plugin.Trackers.Capacity >= 2) + return GetTrackerWorldPosRot(Plugin.Trackers[1]); + else + return GetXRNodeWorldPosRot(XRNode.RightHand); + } + } + + public PosRot LeftLegPosRot + { + get + { + if (Plugin.IsFullBodyTracking && Plugin.Trackers.Capacity >= 2) + return GetTrackerWorldPosRot(Plugin.Trackers[0]); + else + return new PosRot(new Vector3(), new Quaternion()); + } + } - private static PosRot GetXRNodeWorldPosRot(XRNode node) + public PosRot RightLegPosRot + { + get + { + if (Plugin.IsFullBodyTracking && Plugin.Trackers.Capacity >= 2) + return GetTrackerWorldPosRot(Plugin.Trackers[1]); + else + return new PosRot(new Vector3(), new Quaternion()); + } + } + + private static PosRot GetXRNodeWorldPosRot(XRNode node) { var pos = InputTracking.GetLocalPosition(node); var rot = InputTracking.GetLocalRotation(node); - var roomCenter = BeatSaberUtil.GetRoomCenter(); + var roomCenter = BeatSaberUtil.GetRoomCenter(); var roomRotation = BeatSaberUtil.GetRoomRotation(); pos = roomRotation * pos; pos += roomCenter; rot = roomRotation * rot; return new PosRot(pos, rot); - } - } + } + + private static PosRot GetTrackerWorldPosRot(XRNodeState tracker) + { + Vector3 pos = new Vector3(); + Quaternion rot = new Quaternion(); + try + { + var notes = new List(); + InputTracking.GetNodeStates(notes); + foreach (XRNodeState note in notes) + { + if (note.uniqueID != tracker.uniqueID) + continue; + if (note.TryGetPosition(out pos) && note.TryGetRotation(out rot)) + { + var roomCenter = BeatSaberUtil.GetRoomCenter(); + var roomRotation = BeatSaberUtil.GetRoomRotation(); + pos = roomRotation * pos; + pos += roomCenter; + rot = roomRotation * rot; + } + } + } + catch (Exception e) + { + Console.WriteLine(e.Message + "\n" + e.StackTrace); + } + return new PosRot(pos, rot); + } + } } \ No newline at end of file diff --git a/CustomAvatar/PlayerAvatarManager.cs b/CustomAvatar/PlayerAvatarManager.cs index 008dd78..a04d158 100644 --- a/CustomAvatar/PlayerAvatarManager.cs +++ b/CustomAvatar/PlayerAvatarManager.cs @@ -104,9 +104,9 @@ public CustomAvatar SwitchToPreviousAvatar() var nextAvatar = avatars[nextIndex]; CurrentPlayerAvatar = nextAvatar; return nextAvatar; - } + } - private void CustomAvatarLoaded(CustomAvatar loadedAvatar, AvatarLoadResult result) + private void CustomAvatarLoaded(CustomAvatar loadedAvatar, AvatarLoadResult result) { if (result != AvatarLoadResult.Completed) { @@ -142,12 +142,12 @@ private void OnFirstPersonEnabledChanged(bool firstPersonEnabled) } private void SceneManagerOnSceneLoaded(Scene newScene, LoadSceneMode mode) - { - ResizePlayerAvatar(); - OnFirstPersonEnabledChanged(Plugin.Instance.FirstPersonEnabled); - } + { + ResizePlayerAvatar(); + OnFirstPersonEnabledChanged(Plugin.Instance.FirstPersonEnabled); + } - private void ResizePlayerAvatar() + private void ResizePlayerAvatar() { if (_currentSpawnedPlayerAvatar?.GameObject == null) return; if (!_currentSpawnedPlayerAvatar.CustomAvatar.AllowHeightCalibration) return; diff --git a/CustomAvatar/Plugin.cs b/CustomAvatar/Plugin.cs index cca2099..886a002 100644 --- a/CustomAvatar/Plugin.cs +++ b/CustomAvatar/Plugin.cs @@ -6,6 +6,7 @@ using IllusionPlugin; using UnityEngine; using UnityEngine.SceneManagement; +using UnityEngine.XR; namespace CustomAvatar { @@ -17,15 +18,60 @@ public class Plugin : IPlugin private bool _init; private bool _firstPersonEnabled; - - private WaitForSecondsRealtime _sceneLoadWait = new WaitForSecondsRealtime(0.1f); + private static bool _isTrackerAsHand; + private static bool _isFullBodyTracking; + + public static List Trackers = new List(); + public static bool IsTrackerAsHand + { + get { return _isTrackerAsHand; } + set + { + _isTrackerAsHand = value; + List notes = new List(); + Trackers = new List(); + InputTracking.GetNodeStates(notes); + foreach (XRNodeState note in notes) + { + if (note.nodeType != XRNode.HardwareTracker || !InputTracking.GetNodeName(note.uniqueID).Contains("LHR-")) + continue; + Trackers.Add(note); + } + if (Trackers.Capacity == 0) + _isTrackerAsHand = false; + Console.WriteLine("IsFullBodyTracking : " + _isFullBodyTracking); + } + } + + public static bool IsFullBodyTracking + { + get { return _isFullBodyTracking; } + set + { + _isFullBodyTracking = value; + List notes = new List(); + Trackers = new List(); + InputTracking.GetNodeStates(notes); + foreach (XRNodeState note in notes) + { + if (note.nodeType != XRNode.HardwareTracker || !InputTracking.GetNodeName(note.uniqueID).Contains("LHR-")) + continue; + Trackers.Add(note); + } + if (Trackers.Capacity == 0) + _isFullBodyTracking = false; + Console.WriteLine("IsFullBodyTracking : " + _isFullBodyTracking); + } + } + + private WaitForSecondsRealtime _sceneLoadWait = new WaitForSecondsRealtime(0.1f); public Plugin() { Instance = this; - } + } - public event Action FirstPersonEnabledChanged; + public event Action FirstPersonEnabledChanged; public static Plugin Instance { get; private set; } public AvatarLoader AvatarLoader { get; private set; } @@ -83,7 +129,8 @@ public void OnApplicationStart() FirstPersonEnabled = PlayerPrefs.HasKey(FirstPersonEnabledKey); SceneManager.sceneLoaded += SceneManagerOnSceneLoaded; - } + IsFullBodyTracking = true; + } public void OnApplicationQuit() { @@ -106,7 +153,8 @@ private void AvatarsLoaded(IReadOnlyList loadedAvatars) PlayerAvatarManager = new PlayerAvatarManager(AvatarLoader, previousAvatar); PlayerAvatarManager.AvatarChanged += PlayerAvatarManagerOnAvatarChanged; - } + IsFullBodyTracking = true; + } private void SceneManagerOnSceneLoaded(Scene newScene, LoadSceneMode mode) { @@ -116,7 +164,8 @@ private void SceneManagerOnSceneLoaded(Scene newScene, LoadSceneMode mode) private void PlayerAvatarManagerOnAvatarChanged(CustomAvatar newAvatar) { PlayerPrefs.SetString(PreviousAvatarKey, newAvatar.FullPath); - } + IsFullBodyTracking = IsFullBodyTracking; + } public void OnUpdate() { @@ -133,8 +182,16 @@ public void OnUpdate() else if (Input.GetKeyDown(KeyCode.Home)) { FirstPersonEnabled = !FirstPersonEnabled; - } - } + } + else if (Input.GetKeyDown(KeyCode.F6)) + { + IsTrackerAsHand = !IsTrackerAsHand; + } + else if (Input.GetKeyDown(KeyCode.F5)) + { + IsFullBodyTracking = !IsFullBodyTracking; + } + } private IEnumerator SetCameraCullingMask() { @@ -154,7 +211,7 @@ public void OnLevelWasInitialized(int level) } public void OnLevelWasLoaded(int level) - { - } + { + } } } \ No newline at end of file diff --git a/CustomAvatar/VRIK/IKManagerAdvanced.cs b/CustomAvatar/VRIK/IKManagerAdvanced.cs index f7bb2bc..c6cc558 100644 --- a/CustomAvatar/VRIK/IKManagerAdvanced.cs +++ b/CustomAvatar/VRIK/IKManagerAdvanced.cs @@ -163,6 +163,9 @@ class IKManagerAdvanced : MonoBehaviour [Range(-180f, 180f), Tooltip("Angular offset of the knee bending direction.")] public float LeftLeg_swivelOffset; + [Range(-180f, 180f), Tooltip("Rotation of the knee bend normal value.")] + public float LeftLeg_bendRotation; + [Space(20)] @@ -188,6 +191,9 @@ class IKManagerAdvanced : MonoBehaviour [Range(-180f, 180f), Tooltip("Angular offset of the knee bending direction.")] public float RightLeg_swivelOffset; + [Range(-180f, 180f), Tooltip("Rotation of the knee bend normal value.")] + public float RightLeg_bendRotation; + [Space(20)] @@ -293,6 +299,13 @@ public void Start() } } } + if (!CustomAvatar.Plugin.IsFullBodyTracking) + { + SetProperty(_VRIK.solver.leftLeg, "positionWeight", 0); + SetProperty(_VRIK.solver.leftLeg, "rotationWeight", 0); + SetProperty(_VRIK.solver.rightLeg, "positionWeight", 0); + SetProperty(_VRIK.solver.rightLeg, "rotationWeight", 0); + } } public static void SetProperty(object obj, string fieldName, object value) diff --git a/CustomAvatar/VRIK/IKSolverVR.cs b/CustomAvatar/VRIK/IKSolverVR.cs index d2c54bc..9fdf599 100644 --- a/CustomAvatar/VRIK/IKSolverVR.cs +++ b/CustomAvatar/VRIK/IKSolverVR.cs @@ -1530,16 +1530,29 @@ public override void ApplyOffsets() Vector3 vector = Quaternion.Inverse(rotation) * point; num = Mathf.Atan2(vector.x, vector.z) * 57.29578f * this.bendGoalWeight; } - float num2 = this.swivelOffset + num; + if (bendGoal != null) + { + this.bendNormal = bendGoal.position - thigh.solverPosition; + this.bendNormal = RotateRound(this.bendNormal, new Vector3(), Vector3.up, bendRotation); + } + + float num2 = this.swivelOffset + num; if (num2 != 0f) { this.bendNormal = Quaternion.AngleAxis(num2, this.thigh.solverPosition - this.lastBone.solverPosition) * this.bendNormal; this.thigh.solverRotation = Quaternion.AngleAxis(-num2, this.thigh.solverRotation * this.thigh.axis) * this.thigh.solverRotation; } - } + } + + public static Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis, float angle) + { + Vector3 point = Quaternion.AngleAxis(angle, axis) * (position - center); + Vector3 resultVec3 = center + point; + return resultVec3; + } - // Token: 0x060003A7 RID: 935 RVA: 0x0001A624 File Offset: 0x00018A24 - private void ApplyPositionOffset(Vector3 offset, float weight) + // Token: 0x060003A7 RID: 935 RVA: 0x0001A624 File Offset: 0x00018A24 + private void ApplyPositionOffset(Vector3 offset, float weight) { if (weight <= 0f) { @@ -1632,8 +1645,12 @@ public override void ResetOffsets() [Range(-180f, 180f)] public float swivelOffset; - // Token: 0x040002E9 RID: 745 - [HideInInspector] + [Tooltip("Rotation of the knee bend normal value.")] + [Range(-180f, 180f)] + public float bendRotation; + + // Token: 0x040002E9 RID: 745 + [HideInInspector] [NonSerialized] public Vector3 IKPosition; From 8e038f1250d9eabe3bdeec44abeb171e494f302a Mon Sep 17 00:00:00 2001 From: PureDark Date: Sun, 18 Nov 2018 12:14:25 +0800 Subject: [PATCH 2/8] Full body tracking and stuff --- CustomAvatar/AvatarBehaviour.cs | 62 ++++++++++++++++++-------- CustomAvatar/CustomAvatar.csproj | 1 + CustomAvatar/IAvatarFullBodyInput.cs | 15 +++++++ CustomAvatar/IAvatarInput.cs | 2 - CustomAvatar/PlayerAvatarInput.cs | 13 +++++- CustomAvatar/Plugin.cs | 14 ++++-- CustomAvatar/VRIK/IKManagerAdvanced.cs | 21 +++++++-- 7 files changed, 100 insertions(+), 28 deletions(-) create mode 100644 CustomAvatar/IAvatarFullBodyInput.cs diff --git a/CustomAvatar/AvatarBehaviour.cs b/CustomAvatar/AvatarBehaviour.cs index 4954a07..0441973 100644 --- a/CustomAvatar/AvatarBehaviour.cs +++ b/CustomAvatar/AvatarBehaviour.cs @@ -9,23 +9,33 @@ public class AvatarBehaviour : MonoBehaviour private Transform _head; private Transform _body; - private Transform _left; - private Transform _right; + private Transform _leftHand; + private Transform _rightHand; private Transform _leftLeg; private Transform _rightLeg; + private Transform _pelvis; private Vector3 _prevBodyPos; - public void Init(IAvatarInput avatarInput) + private Vector3 _prevLeftLegPos = default(Vector3); + private Vector3 _prevRightLegPos = default(Vector3); + private Quaternion _prevLeftLegRot = default(Quaternion); + private Quaternion _prevRightLegRot = default(Quaternion); + + private Vector3 _prevPelvisPos = default(Vector3); + private Quaternion _prevPelvisRot = default(Quaternion); + + public void Init(IAvatarInput avatarInput) { _avatarInput = avatarInput; _head = GetHeadTransform(); _body = gameObject.transform.Find("Body"); - _left = gameObject.transform.Find("LeftHand"); - _right = gameObject.transform.Find("RightHand"); + _leftHand = gameObject.transform.Find("LeftHand"); + _rightHand = gameObject.transform.Find("RightHand"); _leftLeg = gameObject.transform.Find("LeftLeg"); _rightLeg = gameObject.transform.Find("RightLeg"); + _pelvis = gameObject.transform.Find("Pelvis"); } private void LateUpdate() @@ -35,31 +45,47 @@ private void LateUpdate() var headPosRot = _avatarInput.HeadPosRot; var leftPosRot = _avatarInput.LeftPosRot; var rightPosRot = _avatarInput.RightPosRot; - var leftLegPosRot = _avatarInput.LeftLegPosRot; - var rightLegPosRot = _avatarInput.RightLegPosRot; _head.position = headPosRot.Position; _head.rotation = headPosRot.Rotation; - _left.position = leftPosRot.Position; - _left.rotation = leftPosRot.Rotation; + _leftHand.position = leftPosRot.Position; + _leftHand.rotation = leftPosRot.Rotation; + + _rightHand.position = rightPosRot.Position; + _rightHand.rotation = rightPosRot.Rotation; - _right.position = rightPosRot.Position; - _right.rotation = rightPosRot.Rotation; + if (_leftLeg != null && _rightLeg != null && _avatarInput is IAvatarFullBodyInput) + { + var _fbinput = _avatarInput as IAvatarFullBodyInput; + var leftLegPosRot = _fbinput.LeftLegPosRot; + var rightLegPosRot = _fbinput.RightLegPosRot; + _prevLeftLegPos = Vector3.Lerp(_prevLeftLegPos, leftLegPosRot.Position, 15 * Time.deltaTime); + _prevLeftLegRot = Quaternion.Slerp(_prevLeftLegRot, leftLegPosRot.Rotation, 10 * Time.deltaTime); + _leftLeg.position = _prevLeftLegPos; + _leftLeg.rotation = _prevLeftLegRot; + + _prevRightLegPos = Vector3.Lerp(_prevRightLegPos, rightLegPosRot.Position, 15 * Time.deltaTime); + _prevRightLegRot = Quaternion.Slerp(_prevRightLegRot, rightLegPosRot.Rotation, 10 * Time.deltaTime); + _rightLeg.position = _prevRightLegPos; + _rightLeg.rotation = _prevRightLegRot; + } - if (_leftLeg != null && _rightLeg != null) + if(_pelvis != null && _avatarInput is IAvatarFullBodyInput) { - _leftLeg.position = leftLegPosRot.Position; - _leftLeg.rotation = leftLegPosRot.Rotation; + var _fbinput = _avatarInput as IAvatarFullBodyInput; + var pelvisPosRot = _fbinput.PelvisPosRot; - _rightLeg.position = rightLegPosRot.Position; - _rightLeg.rotation = rightLegPosRot.Rotation; + _prevPelvisPos = Vector3.Lerp(_prevPelvisPos, pelvisPosRot.Position, 17 * Time.deltaTime); + _prevPelvisRot = Quaternion.Slerp(_prevPelvisRot, pelvisPosRot.Rotation, 13 * Time.deltaTime); + _pelvis.position = _prevPelvisPos; + _pelvis.rotation = _prevPelvisRot; } var vrPlatformHelper = PersistentSingleton.instance; - vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_left); - vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_right); + vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_leftHand); + vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_rightHand); if (_body == null) return; _body.position = _head.position - (_head.transform.up * 0.1f); diff --git a/CustomAvatar/CustomAvatar.csproj b/CustomAvatar/CustomAvatar.csproj index 3119dc7..a16a615 100644 --- a/CustomAvatar/CustomAvatar.csproj +++ b/CustomAvatar/CustomAvatar.csproj @@ -103,6 +103,7 @@ + diff --git a/CustomAvatar/IAvatarFullBodyInput.cs b/CustomAvatar/IAvatarFullBodyInput.cs new file mode 100644 index 0000000..15b3096 --- /dev/null +++ b/CustomAvatar/IAvatarFullBodyInput.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CustomAvatar +{ + interface IAvatarFullBodyInput:IAvatarInput + { + PosRot LeftLegPosRot { get; } + PosRot RightLegPosRot { get; } + PosRot PelvisPosRot { get; } + } +} diff --git a/CustomAvatar/IAvatarInput.cs b/CustomAvatar/IAvatarInput.cs index bb2ef0d..e1ac7c3 100644 --- a/CustomAvatar/IAvatarInput.cs +++ b/CustomAvatar/IAvatarInput.cs @@ -5,7 +5,5 @@ public interface IAvatarInput PosRot HeadPosRot { get; } PosRot LeftPosRot { get; } PosRot RightPosRot { get; } - PosRot LeftLegPosRot { get; } - PosRot RightLegPosRot { get; } } } \ No newline at end of file diff --git a/CustomAvatar/PlayerAvatarInput.cs b/CustomAvatar/PlayerAvatarInput.cs index 1cd9a53..9d1b4d5 100644 --- a/CustomAvatar/PlayerAvatarInput.cs +++ b/CustomAvatar/PlayerAvatarInput.cs @@ -5,7 +5,7 @@ namespace CustomAvatar { - public class PlayerAvatarInput : IAvatarInput + public class PlayerAvatarInput : IAvatarFullBodyInput { public PlayerAvatarInput() { @@ -60,6 +60,17 @@ public PosRot RightLegPosRot } } + public PosRot PelvisPosRot + { + get + { + if (Plugin.IsFullBodyTracking && Plugin.Trackers.Capacity >= 3) + return GetTrackerWorldPosRot(Plugin.Trackers[3]); + else + return new PosRot(new Vector3(), new Quaternion()); + } + } + private static PosRot GetXRNodeWorldPosRot(XRNode node) { var pos = InputTracking.GetLocalPosition(node); diff --git a/CustomAvatar/Plugin.cs b/CustomAvatar/Plugin.cs index 886a002..bbbc1f4 100644 --- a/CustomAvatar/Plugin.cs +++ b/CustomAvatar/Plugin.cs @@ -39,7 +39,7 @@ public static bool IsTrackerAsHand } if (Trackers.Capacity == 0) _isTrackerAsHand = false; - Console.WriteLine("IsFullBodyTracking : " + _isFullBodyTracking); + Console.WriteLine("IsTrackerAsHand : " + IsTrackerAsHand); } } @@ -60,6 +60,15 @@ public static bool IsFullBodyTracking } if (Trackers.Capacity == 0) _isFullBodyTracking = false; + var currentAvatar = Instance.PlayerAvatarManager.GetCurrentAvatar(); + if (currentAvatar != null) + { + var _IKManagerAdvanced = currentAvatar.GameObject.GetComponentInChildren(true); + if (_IKManagerAdvanced != null) + { + _IKManagerAdvanced.CheckFullBodyTracking(); + } + } Console.WriteLine("IsFullBodyTracking : " + _isFullBodyTracking); } } @@ -109,7 +118,7 @@ public string Name public string Version { - get { return "3.1.3-beta"; } + get { return "3.2.0-beta"; } } public static void Log(string message) @@ -153,7 +162,6 @@ private void AvatarsLoaded(IReadOnlyList loadedAvatars) PlayerAvatarManager = new PlayerAvatarManager(AvatarLoader, previousAvatar); PlayerAvatarManager.AvatarChanged += PlayerAvatarManagerOnAvatarChanged; - IsFullBodyTracking = true; } private void SceneManagerOnSceneLoaded(Scene newScene, LoadSceneMode mode) diff --git a/CustomAvatar/VRIK/IKManagerAdvanced.cs b/CustomAvatar/VRIK/IKManagerAdvanced.cs index c6cc558..1534788 100644 --- a/CustomAvatar/VRIK/IKManagerAdvanced.cs +++ b/CustomAvatar/VRIK/IKManagerAdvanced.cs @@ -298,13 +298,26 @@ public void Start() } } } + CheckFullBodyTracking(); } + } + + public void CheckFullBodyTracking() + { + VRIK _VRIK = base.gameObject.GetComponent(); if (!CustomAvatar.Plugin.IsFullBodyTracking) { - SetProperty(_VRIK.solver.leftLeg, "positionWeight", 0); - SetProperty(_VRIK.solver.leftLeg, "rotationWeight", 0); - SetProperty(_VRIK.solver.rightLeg, "positionWeight", 0); - SetProperty(_VRIK.solver.rightLeg, "rotationWeight", 0); + _VRIK.solver.leftLeg.positionWeight = 0; + _VRIK.solver.leftLeg.rotationWeight = 0; + _VRIK.solver.rightLeg.positionWeight = 0; + _VRIK.solver.rightLeg.rotationWeight = 0; + } + else + { + _VRIK.solver.leftLeg.positionWeight = LeftLeg_positionWeight; + _VRIK.solver.leftLeg.rotationWeight = LeftLeg_rotationWeight; + _VRIK.solver.rightLeg.positionWeight = RightLeg_positionWeight; + _VRIK.solver.rightLeg.rotationWeight = RightLeg_rotationWeight; } } From 9baf3e0422f169f39e4c7a2ac1db2b3840802298 Mon Sep 17 00:00:00 2001 From: Assistant Date: Wed, 26 Dec 2018 10:52:59 -0400 Subject: [PATCH 3/8] merged 5fe8880 --- CustomAvatar/AvatarBehaviour.cs | 90 ++++--- CustomAvatar/AvatarEventsPlayer.cs | 40 +-- CustomAvatar/CustomAvatar.cs | 12 +- CustomAvatar/IAvatarInput.cs | 4 +- CustomAvatar/PlayerAvatarInput.cs | 88 ++++++- CustomAvatar/PlayerAvatarManager.cs | 4 +- CustomAvatar/Plugin.cs | 75 +++++- CustomAvatar/VRIK/IKManagerAdvanced.cs | 17 +- CustomAvatar/VRIK/IKSolverVR.cs | 344 ++++++++++++++++--------- 9 files changed, 468 insertions(+), 206 deletions(-) diff --git a/CustomAvatar/AvatarBehaviour.cs b/CustomAvatar/AvatarBehaviour.cs index 29cc29d..e913360 100644 --- a/CustomAvatar/AvatarBehaviour.cs +++ b/CustomAvatar/AvatarBehaviour.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using System; +using UnityEngine; namespace CustomAvatar { @@ -10,8 +11,10 @@ public class AvatarBehaviour : MonoBehaviour private Transform _body; private Transform _left; private Transform _right; + private Transform _leftLeg; + private Transform _rightLeg; - private Vector3 _prevBodyPos; + private Vector3 _prevBodyPos; public void Init(IAvatarInput avatarInput) { @@ -21,41 +24,60 @@ public void Init(IAvatarInput avatarInput) _body = gameObject.transform.Find("Body"); _left = gameObject.transform.Find("LeftHand"); _right = gameObject.transform.Find("RightHand"); - } + _leftLeg = gameObject.transform.Find("LeftLeg"); + _rightLeg = gameObject.transform.Find("RightLeg"); + } private void LateUpdate() { - var headPosRot = _avatarInput.HeadPosRot; - var leftPosRot = _avatarInput.LeftPosRot; - var rightPosRot = _avatarInput.RightPosRot; - - _head.position = headPosRot.Position; - _head.rotation = headPosRot.Rotation; - - _left.position = leftPosRot.Position; - _left.rotation = leftPosRot.Rotation; - - _right.position = rightPosRot.Position; - _right.rotation = rightPosRot.Rotation; - - var vrPlatformHelper = PersistentSingleton.instance; - - vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_left); - vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_right); - - if (_body == null) return; - _body.position = _head.position - (_head.transform.up * 0.1f); - - var vel = new Vector3(_body.transform.localPosition.x - _prevBodyPos.x, 0.0f, - _body.localPosition.z - _prevBodyPos.z); - - var rot = Quaternion.Euler(0.0f, _head.localEulerAngles.y, 0.0f); - var tiltAxis = Vector3.Cross(gameObject.transform.up, vel); - _body.localRotation = Quaternion.Lerp(_body.localRotation, - Quaternion.AngleAxis(vel.magnitude * 1250.0f, tiltAxis) * rot, - Time.deltaTime * 10.0f); - - _prevBodyPos = _body.transform.localPosition; + try + { + var headPosRot = _avatarInput.HeadPosRot; + var leftPosRot = _avatarInput.LeftPosRot; + var rightPosRot = _avatarInput.RightPosRot; + var leftLegPosRot = _avatarInput.LeftLegPosRot; + var rightLegPosRot = _avatarInput.RightLegPosRot; + + _head.position = headPosRot.Position; + _head.rotation = headPosRot.Rotation; + + _left.position = leftPosRot.Position; + _left.rotation = leftPosRot.Rotation; + + _right.position = rightPosRot.Position; + _right.rotation = rightPosRot.Rotation; + + if (_leftLeg != null && _rightLeg != null) + { + _leftLeg.position = leftLegPosRot.Position; + _leftLeg.rotation = leftLegPosRot.Rotation; + + _rightLeg.position = rightLegPosRot.Position; + _rightLeg.rotation = rightLegPosRot.Rotation; + } + + var vrPlatformHelper = PersistentSingleton.instance; + + vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_left); + vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_right); + + if (_body == null) return; + _body.position = _head.position - (_head.transform.up * 0.1f); + + var vel = new Vector3(_body.transform.localPosition.x - _prevBodyPos.x, 0.0f, + _body.localPosition.z - _prevBodyPos.z); + + var rot = Quaternion.Euler(0.0f, _head.localEulerAngles.y, 0.0f); + var tiltAxis = Vector3.Cross(gameObject.transform.up, vel); + _body.localRotation = Quaternion.Lerp(_body.localRotation, + Quaternion.AngleAxis(vel.magnitude * 1250.0f, tiltAxis) * rot, + Time.deltaTime * 10.0f); + + _prevBodyPos = _body.transform.localPosition; + } catch(Exception e) + { + Console.WriteLine("{0}\n{1}", e.Message, e.StackTrace); + } } private Transform GetHeadTransform() diff --git a/CustomAvatar/AvatarEventsPlayer.cs b/CustomAvatar/AvatarEventsPlayer.cs index e861741..343ec7c 100644 --- a/CustomAvatar/AvatarEventsPlayer.cs +++ b/CustomAvatar/AvatarEventsPlayer.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Reflection; using UnityEngine; +using UnityEngine.SceneManagement; namespace CustomAvatar { @@ -29,6 +30,29 @@ private void _Restart() } private void Start() + { + SceneManager.sceneLoaded += SceneManagerOnSceneLoaded; + } + + private void OnDestroy() + { + if (_scoreController == null) return; + _scoreController.noteWasCutEvent -= SliceCallBack; + _scoreController.noteWasMissedEvent -= NoteMissCallBack; + _scoreController.multiplierDidChangeEvent -= MultiplierCallBack; + _scoreController.comboDidChangeEvent -= ComboChangeEvent; + + _saberCollisionManager.sparkleEffectDidStartEvent -= SaberStartCollide; + _saberCollisionManager.sparkleEffectDidEndEvent -= SaberEndCollide; + + _gameEnergyCounter.gameEnergyDidReach0Event -= FailLevelCallBack; + + + _beatmapObjectCallbackController.beatmapEventDidTriggerEvent -= OnBeatmapEventDidTriggerEvent; + _beatmapDataModel.beatmapDataDidChangeEvent -= BeatmapDataChangedCallback; + } + + private void SceneManagerOnSceneLoaded(Scene newScene, LoadSceneMode mode) { _eventManager = gameObject.GetComponent(); if (_eventManager == null) @@ -69,23 +93,7 @@ private void Start() } } - private void OnDestroy() - { - if (_scoreController == null) return; - _scoreController.noteWasCutEvent -= SliceCallBack; - _scoreController.noteWasMissedEvent -= NoteMissCallBack; - _scoreController.multiplierDidChangeEvent -= MultiplierCallBack; - _scoreController.comboDidChangeEvent -= ComboChangeEvent; - - _saberCollisionManager.sparkleEffectDidStartEvent -= SaberStartCollide; - _saberCollisionManager.sparkleEffectDidEndEvent -= SaberEndCollide; - _gameEnergyCounter.gameEnergyDidReach0Event -= FailLevelCallBack; - - - _beatmapObjectCallbackController.beatmapEventDidTriggerEvent -= OnBeatmapEventDidTriggerEvent; - _beatmapDataModel.beatmapDataDidChangeEvent -= BeatmapDataChangedCallback; - } private void BeatmapDataChangedCallback() { diff --git a/CustomAvatar/CustomAvatar.cs b/CustomAvatar/CustomAvatar.cs index 3d5ee13..5e08257 100644 --- a/CustomAvatar/CustomAvatar.cs +++ b/CustomAvatar/CustomAvatar.cs @@ -90,13 +90,13 @@ public GameObject GameObject public void Load(Action loadedCallback) { - if (IsLoaded) - { - loadedCallback(this, AvatarLoadResult.Completed); - return; - } + if (IsLoaded) + { + loadedCallback(this, AvatarLoadResult.Completed); + return; + } - void Loaded(AvatarLoadResult result) + void Loaded(AvatarLoadResult result) { loadedCallback(this, result); } diff --git a/CustomAvatar/IAvatarInput.cs b/CustomAvatar/IAvatarInput.cs index afba6f0..bb2ef0d 100644 --- a/CustomAvatar/IAvatarInput.cs +++ b/CustomAvatar/IAvatarInput.cs @@ -5,5 +5,7 @@ public interface IAvatarInput PosRot HeadPosRot { get; } PosRot LeftPosRot { get; } PosRot RightPosRot { get; } - } + PosRot LeftLegPosRot { get; } + PosRot RightLegPosRot { get; } + } } \ No newline at end of file diff --git a/CustomAvatar/PlayerAvatarInput.cs b/CustomAvatar/PlayerAvatarInput.cs index 57be21a..1cd9a53 100644 --- a/CustomAvatar/PlayerAvatarInput.cs +++ b/CustomAvatar/PlayerAvatarInput.cs @@ -1,35 +1,105 @@ -using UnityEngine.XR; +using System; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.XR; namespace CustomAvatar { public class PlayerAvatarInput : IAvatarInput { - public PosRot HeadPosRot + public PlayerAvatarInput() + { + } + + public PosRot HeadPosRot { get { return GetXRNodeWorldPosRot(XRNode.Head); } } public PosRot LeftPosRot { - get { return GetXRNodeWorldPosRot(XRNode.LeftHand); } + get + { + if(Plugin.IsTrackerAsHand && Plugin.Trackers.Capacity >= 2) + return GetTrackerWorldPosRot(Plugin.Trackers[0]); + else + return GetXRNodeWorldPosRot(XRNode.LeftHand); + } } public PosRot RightPosRot { - get { return GetXRNodeWorldPosRot(XRNode.RightHand); } - } + get + { + if (Plugin.IsTrackerAsHand && Plugin.Trackers.Capacity >= 2) + return GetTrackerWorldPosRot(Plugin.Trackers[1]); + else + return GetXRNodeWorldPosRot(XRNode.RightHand); + } + } + + public PosRot LeftLegPosRot + { + get + { + if (Plugin.IsFullBodyTracking && Plugin.Trackers.Capacity >= 2) + return GetTrackerWorldPosRot(Plugin.Trackers[0]); + else + return new PosRot(new Vector3(), new Quaternion()); + } + } - private static PosRot GetXRNodeWorldPosRot(XRNode node) + public PosRot RightLegPosRot + { + get + { + if (Plugin.IsFullBodyTracking && Plugin.Trackers.Capacity >= 2) + return GetTrackerWorldPosRot(Plugin.Trackers[1]); + else + return new PosRot(new Vector3(), new Quaternion()); + } + } + + private static PosRot GetXRNodeWorldPosRot(XRNode node) { var pos = InputTracking.GetLocalPosition(node); var rot = InputTracking.GetLocalRotation(node); - var roomCenter = BeatSaberUtil.GetRoomCenter(); + var roomCenter = BeatSaberUtil.GetRoomCenter(); var roomRotation = BeatSaberUtil.GetRoomRotation(); pos = roomRotation * pos; pos += roomCenter; rot = roomRotation * rot; return new PosRot(pos, rot); - } - } + } + + private static PosRot GetTrackerWorldPosRot(XRNodeState tracker) + { + Vector3 pos = new Vector3(); + Quaternion rot = new Quaternion(); + try + { + var notes = new List(); + InputTracking.GetNodeStates(notes); + foreach (XRNodeState note in notes) + { + if (note.uniqueID != tracker.uniqueID) + continue; + if (note.TryGetPosition(out pos) && note.TryGetRotation(out rot)) + { + var roomCenter = BeatSaberUtil.GetRoomCenter(); + var roomRotation = BeatSaberUtil.GetRoomRotation(); + pos = roomRotation * pos; + pos += roomCenter; + rot = roomRotation * rot; + } + } + } + catch (Exception e) + { + Console.WriteLine(e.Message + "\n" + e.StackTrace); + } + return new PosRot(pos, rot); + } + } } \ No newline at end of file diff --git a/CustomAvatar/PlayerAvatarManager.cs b/CustomAvatar/PlayerAvatarManager.cs index da6c2a6..45b5249 100644 --- a/CustomAvatar/PlayerAvatarManager.cs +++ b/CustomAvatar/PlayerAvatarManager.cs @@ -104,9 +104,9 @@ public CustomAvatar SwitchToPreviousAvatar() var nextAvatar = avatars[nextIndex]; CurrentPlayerAvatar = nextAvatar; return nextAvatar; - } + } - private void CustomAvatarLoaded(CustomAvatar loadedAvatar, AvatarLoadResult result) + private void CustomAvatarLoaded(CustomAvatar loadedAvatar, AvatarLoadResult result) { if (result != AvatarLoadResult.Completed) { diff --git a/CustomAvatar/Plugin.cs b/CustomAvatar/Plugin.cs index 5cbc7e9..e1394e6 100644 --- a/CustomAvatar/Plugin.cs +++ b/CustomAvatar/Plugin.cs @@ -6,6 +6,7 @@ using IllusionPlugin; using UnityEngine; using UnityEngine.SceneManagement; +using UnityEngine.XR; namespace CustomAvatar { @@ -22,13 +23,58 @@ public class Plugin : IPlugin private WaitForSecondsRealtime _sceneLoadWait = new WaitForSecondsRealtime(0.1f); private GameScenesManager _scenesManager; + private static bool _isTrackerAsHand; + private static bool _isFullBodyTracking; + + public static List Trackers = new List(); + public static bool IsTrackerAsHand + { + get { return _isTrackerAsHand; } + set + { + _isTrackerAsHand = value; + List notes = new List(); + Trackers = new List(); + InputTracking.GetNodeStates(notes); + foreach (XRNodeState note in notes) + { + if (note.nodeType != XRNode.HardwareTracker || !InputTracking.GetNodeName(note.uniqueID).Contains("LHR-")) + continue; + Trackers.Add(note); + } + if (Trackers.Capacity == 0) + _isTrackerAsHand = false; + Console.WriteLine("IsFullBodyTracking : " + _isFullBodyTracking); + } + } + + public static bool IsFullBodyTracking + { + get { return _isFullBodyTracking; } + set + { + _isFullBodyTracking = value; + List notes = new List(); + Trackers = new List(); + InputTracking.GetNodeStates(notes); + foreach (XRNodeState note in notes) + { + if (note.nodeType != XRNode.HardwareTracker || !InputTracking.GetNodeName(note.uniqueID).Contains("LHR-")) + continue; + Trackers.Add(note); + } + if (Trackers.Capacity == 0) + _isFullBodyTracking = false; + Console.WriteLine("IsFullBodyTracking : " + _isFullBodyTracking); + } + } public Plugin() { Instance = this; - } + } - public event Action FirstPersonEnabledChanged; + public event Action FirstPersonEnabledChanged; public static Plugin Instance { get; private set; } public AvatarLoader AvatarLoader { get; private set; } @@ -112,7 +158,8 @@ public void OnApplicationStart() FirstPersonEnabled = PlayerPrefs.HasKey(FirstPersonEnabledKey); RotatePreviewEnabled = PlayerPrefs.HasKey(RotatePreviewEnabledKey); SceneManager.sceneLoaded += SceneManagerOnSceneLoaded; - } + IsFullBodyTracking = true; + } public void OnApplicationQuit() { @@ -143,7 +190,8 @@ private void AvatarsLoaded(IReadOnlyList loadedAvatars) PlayerAvatarManager = new PlayerAvatarManager(AvatarLoader, AvatarTailor, previousAvatar); PlayerAvatarManager.AvatarChanged += PlayerAvatarManagerOnAvatarChanged; - } + IsFullBodyTracking = true; + } private void SceneManagerOnSceneLoaded(Scene newScene, LoadSceneMode mode) { @@ -167,7 +215,8 @@ private void SceneTransitionDidFinish() private void PlayerAvatarManagerOnAvatarChanged(CustomAvatar newAvatar) { PlayerPrefs.SetString(PreviousAvatarKey, newAvatar.FullPath); - } + IsFullBodyTracking = IsFullBodyTracking; + } public void OnUpdate() { @@ -182,8 +231,16 @@ public void OnUpdate() else if (Input.GetKeyDown(KeyCode.Home)) { FirstPersonEnabled = !FirstPersonEnabled; - } - } + } + else if (Input.GetKeyDown(KeyCode.F6)) + { + IsTrackerAsHand = !IsTrackerAsHand; + } + else if (Input.GetKeyDown(KeyCode.F5)) + { + IsFullBodyTracking = !IsFullBodyTracking; + } + } private void SetCameraCullingMask(Camera camera) { @@ -202,7 +259,7 @@ public void OnLevelWasInitialized(int level) } public void OnLevelWasLoaded(int level) - { - } + { + } } } diff --git a/CustomAvatar/VRIK/IKManagerAdvanced.cs b/CustomAvatar/VRIK/IKManagerAdvanced.cs index f7bb2bc..07a28e1 100644 --- a/CustomAvatar/VRIK/IKManagerAdvanced.cs +++ b/CustomAvatar/VRIK/IKManagerAdvanced.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -163,6 +163,9 @@ class IKManagerAdvanced : MonoBehaviour [Range(-180f, 180f), Tooltip("Angular offset of the knee bending direction.")] public float LeftLeg_swivelOffset; + [Range(-180f, 180f), Tooltip("Rotation of the knee bend normal value.")] + public float LeftLeg_bendRotation; + [Space(20)] @@ -188,6 +191,9 @@ class IKManagerAdvanced : MonoBehaviour [Range(-180f, 180f), Tooltip("Angular offset of the knee bending direction.")] public float RightLeg_swivelOffset; + [Range(-180f, 180f), Tooltip("Rotation of the knee bend normal value.")] + public float RightLeg_bendRotation; + [Space(20)] @@ -293,6 +299,15 @@ public void Start() } } } +#if PLUGIN + if (!CustomAvatar.Plugin.IsFullBodyTracking) + { + SetProperty(_VRIK.solver.leftLeg, "positionWeight", 0); + SetProperty(_VRIK.solver.leftLeg, "rotationWeight", 0); + SetProperty(_VRIK.solver.rightLeg, "positionWeight", 0); + SetProperty(_VRIK.solver.rightLeg, "rotationWeight", 0); + } +#endif } public static void SetProperty(object obj, string fieldName, object value) diff --git a/CustomAvatar/VRIK/IKSolverVR.cs b/CustomAvatar/VRIK/IKSolverVR.cs index 8d6de43..cf0b208 100644 --- a/CustomAvatar/VRIK/IKSolverVR.cs +++ b/CustomAvatar/VRIK/IKSolverVR.cs @@ -3,35 +3,39 @@ using System; using AvatarScriptPack; -namespace AvatarScriptPack { +namespace AvatarScriptPack +{ /// /// Hybrid %IK solver designed for mapping a character to a VR headset and 2 hand controllers /// [System.Serializable] - public partial class IKSolverVR: IKSolver { + public partial class IKSolverVR : IKSolver + { #region Wrapper /// /// Sets this VRIK up to the specified bone references. /// - public void SetToReferences(VRIK.References references) { - if (!references.isFilled) { + public void SetToReferences(VRIK.References references) + { + if (!references.isFilled) + { Debug.LogError("Invalid references, one or more Transforms are missing."); return; } - + solverTransforms = references.GetTransforms(); - hasChest = solverTransforms [3] != null; + hasChest = solverTransforms[3] != null; hasNeck = solverTransforms[4] != null; hasShoulders = solverTransforms[6] != null && solverTransforms[10] != null; hasToes = solverTransforms[17] != null && solverTransforms[21] != null; - + readPositions = new Vector3[solverTransforms.Length]; readRotations = new Quaternion[solverTransforms.Length]; - + DefaultAnimationCurves(); GuessHandOrientations(references, true); } @@ -39,25 +43,31 @@ public void SetToReferences(VRIK.References references) { /// /// Guesses the hand bones orientations ('Wrist To Palm Axis' and "Palm To Thumb Axis" of the arms) based on the provided references. if onlyIfZero is true, will only guess an orientation axis if it is Vector3.zero. /// - public void GuessHandOrientations(VRIK.References references, bool onlyIfZero) { - if (!references.isFilled) { + public void GuessHandOrientations(VRIK.References references, bool onlyIfZero) + { + if (!references.isFilled) + { Debug.LogWarning("VRIK References are not filled in, can not guess hand orientations. Right-click on VRIK header and slect 'Guess Hand Orientations' when you have filled in the References."); return; } - - if (leftArm.wristToPalmAxis == Vector3.zero || !onlyIfZero) { + + if (leftArm.wristToPalmAxis == Vector3.zero || !onlyIfZero) + { leftArm.wristToPalmAxis = GuessWristToPalmAxis(references.leftHand, references.leftForearm); } - - if (leftArm.palmToThumbAxis == Vector3.zero || !onlyIfZero) { + + if (leftArm.palmToThumbAxis == Vector3.zero || !onlyIfZero) + { leftArm.palmToThumbAxis = GuessPalmToThumbAxis(references.leftHand, references.leftForearm); } - - if (rightArm.wristToPalmAxis == Vector3.zero || !onlyIfZero) { + + if (rightArm.wristToPalmAxis == Vector3.zero || !onlyIfZero) + { rightArm.wristToPalmAxis = GuessWristToPalmAxis(references.rightHand, references.rightForearm); } - - if (rightArm.palmToThumbAxis == Vector3.zero || !onlyIfZero) { + + if (rightArm.palmToThumbAxis == Vector3.zero || !onlyIfZero) + { rightArm.palmToThumbAxis = GuessPalmToThumbAxis(references.rightHand, references.rightForearm); } } @@ -65,15 +75,18 @@ public void GuessHandOrientations(VRIK.References references, bool onlyIfZero) { /// /// Set default values for the animation curves if they have no keys. /// - public void DefaultAnimationCurves() { + public void DefaultAnimationCurves() + { if (locomotion.stepHeight == null) locomotion.stepHeight = new AnimationCurve(); - if (locomotion.heelHeight == null) locomotion.heelHeight = new AnimationCurve (); - - if (locomotion.stepHeight.keys.Length == 0) { + if (locomotion.heelHeight == null) locomotion.heelHeight = new AnimationCurve(); + + if (locomotion.stepHeight.keys.Length == 0) + { locomotion.stepHeight.keys = GetSineKeyframes(0.03f); } - - if (locomotion.heelHeight.keys.Length == 0) { + + if (locomotion.heelHeight.keys.Length == 0) + { locomotion.heelHeight.keys = GetSineKeyframes(0.03f); } } @@ -81,123 +94,145 @@ public void DefaultAnimationCurves() { /// /// Adds position offset to a body part. Position offsets add to the targets in VRIK. /// - public void AddPositionOffset(PositionOffset positionOffset, Vector3 value) { - switch(positionOffset) { - case PositionOffset.Pelvis: spine.pelvisPositionOffset += value; return; - case PositionOffset.Chest: spine.chestPositionOffset += value; return; - case PositionOffset.Head: spine.headPositionOffset += value; return; - case PositionOffset.LeftHand: leftArm.handPositionOffset += value; return; - case PositionOffset.RightHand: rightArm.handPositionOffset += value; return; - case PositionOffset.LeftFoot: leftLeg.footPositionOffset += value; return; - case PositionOffset.RightFoot: rightLeg.footPositionOffset += value; return; - case PositionOffset.LeftHeel: leftLeg.heelPositionOffset += value; return; - case PositionOffset.RightHeel: rightLeg.heelPositionOffset += value; return; + public void AddPositionOffset(PositionOffset positionOffset, Vector3 value) + { + switch (positionOffset) + { + case PositionOffset.Pelvis: spine.pelvisPositionOffset += value; return; + case PositionOffset.Chest: spine.chestPositionOffset += value; return; + case PositionOffset.Head: spine.headPositionOffset += value; return; + case PositionOffset.LeftHand: leftArm.handPositionOffset += value; return; + case PositionOffset.RightHand: rightArm.handPositionOffset += value; return; + case PositionOffset.LeftFoot: leftLeg.footPositionOffset += value; return; + case PositionOffset.RightFoot: rightLeg.footPositionOffset += value; return; + case PositionOffset.LeftHeel: leftLeg.heelPositionOffset += value; return; + case PositionOffset.RightHeel: rightLeg.heelPositionOffset += value; return; } } /// /// Adds rotation offset to a body part. Rotation offsets add to the targets in VRIK /// - public void AddRotationOffset(RotationOffset rotationOffset, Vector3 value) { + public void AddRotationOffset(RotationOffset rotationOffset, Vector3 value) + { AddRotationOffset(rotationOffset, Quaternion.Euler(value)); } /// /// Adds rotation offset to a body part. Rotation offsets add to the targets in VRIK /// - public void AddRotationOffset(RotationOffset rotationOffset, Quaternion value) { - switch(rotationOffset) { - case RotationOffset.Pelvis: spine.pelvisRotationOffset = value * spine.pelvisRotationOffset; return; - case RotationOffset.Chest: spine.chestRotationOffset = value * spine.chestRotationOffset; return; - case RotationOffset.Head: spine.headRotationOffset = value * spine.headRotationOffset; return; + public void AddRotationOffset(RotationOffset rotationOffset, Quaternion value) + { + switch (rotationOffset) + { + case RotationOffset.Pelvis: spine.pelvisRotationOffset = value * spine.pelvisRotationOffset; return; + case RotationOffset.Chest: spine.chestRotationOffset = value * spine.chestRotationOffset; return; + case RotationOffset.Head: spine.headRotationOffset = value * spine.headRotationOffset; return; } } /// /// Call this in each Update if your avatar is standing on a moving platform /// - public void AddPlatformMotion(Vector3 deltaPosition, Quaternion deltaRotation, Vector3 platformPivot) { - locomotion.AddDeltaPosition (deltaPosition); + public void AddPlatformMotion(Vector3 deltaPosition, Quaternion deltaRotation, Vector3 platformPivot) + { + locomotion.AddDeltaPosition(deltaPosition); raycastOriginPelvis += deltaPosition; - locomotion.AddDeltaRotation (deltaRotation, platformPivot); + locomotion.AddDeltaRotation(deltaRotation, platformPivot); spine.faceDirection = deltaRotation * spine.faceDirection; } /// /// Resets all tweens, blendings and lerps. Call this after you have teleported the character. /// - public void Reset() { + public void Reset() + { if (!initiated) return; UpdateSolverTransforms(); Read(readPositions, readRotations, hasChest, hasNeck, hasShoulders, hasToes); - + spine.faceDirection = rootBone.readRotation * Vector3.forward; locomotion.Reset(readPositions, readRotations); raycastOriginPelvis = spine.pelvis.readPosition; } - public override void StoreDefaultLocalState() { - for (int i = 1; i < solverTransforms.Length; i++) { - if (solverTransforms[i] != null) { + public override void StoreDefaultLocalState() + { + for (int i = 1; i < solverTransforms.Length; i++) + { + if (solverTransforms[i] != null) + { defaultLocalPositions[i - 1] = solverTransforms[i].localPosition; defaultLocalRotations[i - 1] = solverTransforms[i].localRotation; } } } - - public override void FixTransforms() { + + public override void FixTransforms() + { if (!initiated) return; - for (int i = 1; i < solverTransforms.Length; i++) { - if (solverTransforms[i] != null) { + for (int i = 1; i < solverTransforms.Length; i++) + { + if (solverTransforms[i] != null) + { bool isPelvis = i == 1; bool isArm = i > 5 && i < 14; - if (isPelvis || isArm) { + if (isPelvis || isArm) + { solverTransforms[i].localPosition = defaultLocalPositions[i - 1]; } solverTransforms[i].localRotation = defaultLocalRotations[i - 1]; } } } - - public override IKSolver.Point[] GetPoints() { + + public override IKSolver.Point[] GetPoints() + { Debug.LogError("GetPoints() is not applicable to IKSolverVR."); return null; } - - public override IKSolver.Point GetPoint(Transform transform) { + + public override IKSolver.Point GetPoint(Transform transform) + { Debug.LogError("GetPoint is not applicable to IKSolverVR."); return null; } - - public override bool IsValid(ref string message) { - if (solverTransforms == null || solverTransforms.Length == 0) { + + public override bool IsValid(ref string message) + { + if (solverTransforms == null || solverTransforms.Length == 0) + { message = "Trying to initiate IKSolverVR with invalid bone references."; return false; } - - if (leftArm.wristToPalmAxis == Vector3.zero) { + + if (leftArm.wristToPalmAxis == Vector3.zero) + { message = "Left arm 'Wrist To Palm Axis' needs to be set in VRIK. Please select the hand bone, set it to the axis that points from the wrist towards the palm. If the arrow points away from the palm, axis must be negative."; return false; } - - if (rightArm.wristToPalmAxis == Vector3.zero) { + + if (rightArm.wristToPalmAxis == Vector3.zero) + { message = "Right arm 'Wrist To Palm Axis' needs to be set in VRIK. Please select the hand bone, set it to the axis that points from the wrist towards the palm. If the arrow points away from the palm, axis must be negative."; return false; } - - if (leftArm.palmToThumbAxis == Vector3.zero) { + + if (leftArm.palmToThumbAxis == Vector3.zero) + { message = "Left arm 'Palm To Thumb Axis' needs to be set in VRIK. Please select the hand bone, set it to the axis that points from the palm towards the thumb. If the arrow points away from the thumb, axis must be negative."; return false; } - - if (rightArm.palmToThumbAxis == Vector3.zero) { + + if (rightArm.palmToThumbAxis == Vector3.zero) + { message = "Right arm 'Palm To Thumb Axis' needs to be set in VRIK. Please select the hand bone, set it to the axis that points from the palm towards the thumb. If the arrow points away from the thumb, axis must be negative."; return false; } - + return true; } @@ -210,32 +245,38 @@ public override bool IsValid(ref string message) { //private Vector3 defaultPelvisLocalPosition; private Quaternion[] defaultLocalRotations = new Quaternion[21]; private Vector3[] defaultLocalPositions = new Vector3[21]; - - private Vector3 GetNormal(Transform[] transforms) { + + private Vector3 GetNormal(Transform[] transforms) + { Vector3 normal = Vector3.zero; Vector3 centroid = Vector3.zero; - for (int i = 0; i < transforms.Length; i++) { + for (int i = 0; i < transforms.Length; i++) + { centroid += transforms[i].position; } centroid /= transforms.Length; - for (int i = 0; i < transforms.Length - 1; i++) { + for (int i = 0; i < transforms.Length - 1; i++) + { normal += Vector3.Cross(transforms[i].position - centroid, transforms[i + 1].position - centroid).normalized; } - + return normal; } - private Vector3 GuessWristToPalmAxis(Transform hand, Transform forearm) { - Vector3 toForearm = forearm.position -hand.position; + private Vector3 GuessWristToPalmAxis(Transform hand, Transform forearm) + { + Vector3 toForearm = forearm.position - hand.position; Vector3 axis = AxisTools.ToVector3(AxisTools.GetAxisToDirection(hand, toForearm)); if (Vector3.Dot(toForearm, hand.rotation * axis) > 0f) axis = -axis; return axis; } - private Vector3 GuessPalmToThumbAxis(Transform hand, Transform forearm) { - if (hand.childCount == 0) { + private Vector3 GuessPalmToThumbAxis(Transform hand, Transform forearm) + { + if (hand.childCount == 0) + { Debug.LogWarning("Hand " + hand.name + " does not have any fingers, VRIK can not guess the hand bone's orientation. Please assign 'Wrist To Palm Axis' and 'Palm To Thumb Axis' manually for both arms in VRIK settings.", hand); return Vector3.zero; } @@ -243,22 +284,25 @@ private Vector3 GuessPalmToThumbAxis(Transform hand, Transform forearm) { float closestSqrMag = Mathf.Infinity; int thumbIndex = 0; - for (int i = 0; i < hand.childCount; i++) { - float sqrMag = Vector3.SqrMagnitude(hand.GetChild(i).position -hand.position); - if (sqrMag < closestSqrMag && sqrMag > 0.00001f) { + for (int i = 0; i < hand.childCount; i++) + { + float sqrMag = Vector3.SqrMagnitude(hand.GetChild(i).position - hand.position); + if (sqrMag < closestSqrMag && sqrMag > 0.00001f) + { closestSqrMag = sqrMag; thumbIndex = i; } } Vector3 handNormal = Vector3.Cross(hand.position - forearm.position, hand.GetChild(thumbIndex).position - hand.position); - Vector3 toThumb = Vector3.Cross(handNormal, hand.position -forearm.position); + Vector3 toThumb = Vector3.Cross(handNormal, hand.position - forearm.position); Vector3 axis = AxisTools.ToVector3(AxisTools.GetAxisToDirection(hand, toThumb)); if (Vector3.Dot(toThumb, hand.rotation * axis) < 0f) axis = -axis; return axis; } - private static Keyframe[] GetSineKeyframes(float mag) { + private static Keyframe[] GetSineKeyframes(float mag) + { Keyframe[] keys = new Keyframe[3]; keys[0].time = 0f; keys[0].value = 0f; @@ -269,22 +313,28 @@ private static Keyframe[] GetSineKeyframes(float mag) { return keys; } - private void UpdateSolverTransforms() { - for (int i = 0; i < solverTransforms.Length; i++) { - if (solverTransforms[i] != null) { + private void UpdateSolverTransforms() + { + for (int i = 0; i < solverTransforms.Length; i++) + { + if (solverTransforms[i] != null) + { readPositions[i] = solverTransforms[i].position; readRotations[i] = solverTransforms[i].rotation; } } } - protected override void OnInitiate() { + protected override void OnInitiate() + { UpdateSolverTransforms(); Read(readPositions, readRotations, hasChest, hasNeck, hasShoulders, hasToes); } - protected override void OnUpdate() { - if (IKPositionWeight > 0f) { + protected override void OnUpdate() + { + if (IKPositionWeight > 0f) + { UpdateSolverTransforms(); Read(readPositions, readRotations, hasChest, hasNeck, hasShoulders, hasToes); @@ -295,12 +345,16 @@ protected override void OnUpdate() { } } - private void WriteTransforms() { - for (int i = 0; i < solverTransforms.Length; i++) { - if (solverTransforms[i] != null) { + private void WriteTransforms() + { + for (int i = 0; i < solverTransforms.Length; i++) + { + if (solverTransforms[i] != null) + { bool isRootOrPelvis = i < 2; bool isArm = i > 5 && i < 14; - if (isRootOrPelvis || isArm) { + if (isRootOrPelvis || isArm) + { solverTransforms[i].position = V3Tools.Lerp(solverTransforms[i].position, GetPosition(i), IKPositionWeight); } @@ -318,25 +372,31 @@ private void WriteTransforms() { private Vector3 bodyOffset; private int supportLegIndex; - private void Read(Vector3[] positions, Quaternion[] rotations, bool hasChest, bool hasNeck, bool hasShoulders, bool hasToes) { - if (rootBone == null) { - rootBone = new VirtualBone (positions [0], rotations [0]); - } else { - rootBone.Read (positions [0], rotations [0]); + private void Read(Vector3[] positions, Quaternion[] rotations, bool hasChest, bool hasNeck, bool hasShoulders, bool hasToes) + { + if (rootBone == null) + { + rootBone = new VirtualBone(positions[0], rotations[0]); + } + else + { + rootBone.Read(positions[0], rotations[0]); } spine.Read(positions, rotations, hasChest, hasNeck, hasShoulders, hasToes, 0, 1); - leftArm.Read(positions, rotations, hasChest, hasNeck, hasShoulders, hasToes, hasChest? 3: 2, 6); - rightArm.Read(positions, rotations, hasChest, hasNeck, hasShoulders, hasToes, hasChest? 3: 2, 10); + leftArm.Read(positions, rotations, hasChest, hasNeck, hasShoulders, hasToes, hasChest ? 3 : 2, 6); + rightArm.Read(positions, rotations, hasChest, hasNeck, hasShoulders, hasToes, hasChest ? 3 : 2, 10); leftLeg.Read(positions, rotations, hasChest, hasNeck, hasShoulders, hasToes, 1, 14); rightLeg.Read(positions, rotations, hasChest, hasNeck, hasShoulders, hasToes, 1, 18); - for (int i = 0; i < rotations.Length; i++) { + for (int i = 0; i < rotations.Length; i++) + { this.solvedPositions[i] = positions[i]; this.solvedRotations[i] = rotations[i]; } - if (!initiated) { + if (!initiated) + { legs = new Leg[2] { leftLeg, rightLeg }; arms = new Arm[2] { leftArm, rightArm }; @@ -346,7 +406,8 @@ private void Read(Vector3[] positions, Quaternion[] rotations, bool hasChest, bo } } - private void Solve() { + private void Solve() + { // Pre-Solving spine.PreSolve(); foreach (Arm arm in arms) arm.PreSolve(); @@ -359,12 +420,14 @@ private void Solve() { // Spine spine.Solve(rootBone, legs, arms); - if (spine.pelvisPositionWeight > 0f && plantFeet) { + if (spine.pelvisPositionWeight > 0f && plantFeet) + { Warning.Log("If VRIK 'Pelvis Position Weight' is > 0, 'Plant Feet' should be disabled to improve performance and stability.", root); } // Locomotion - if (locomotion.weight > 0f) { + if (locomotion.weight > 0f) + { Vector3 leftFootPosition = Vector3.zero; Vector3 rightFootPosition = Vector3.zero; Quaternion leftFootRotation = Quaternion.identity; @@ -411,34 +474,43 @@ private void Solve() { } // Legs - foreach (Leg leg in legs) { + foreach (Leg leg in legs) + { leg.ApplyOffsets(); } - if (!plantFeet) { + if (!plantFeet) + { spine.InverseTranslateToHead(legs, false, false, bodyOffset, 1f); foreach (Leg leg in legs) leg.TranslateRoot(spine.pelvis.solverPosition, spine.pelvis.solverRotation); - foreach (Leg leg in legs) { + foreach (Leg leg in legs) + { leg.Solve(); } - } else { - for (int i = 0; i < 2; i++) { + } + else + { + for (int i = 0; i < 2; i++) + { spine.InverseTranslateToHead(legs, true, i == 0, bodyOffset, 1f); foreach (Leg leg in legs) leg.TranslateRoot(spine.pelvis.solverPosition, spine.pelvis.solverRotation); - foreach (Leg leg in legs) { + foreach (Leg leg in legs) + { leg.Solve(); } } } // Arms - for (int i = 0; i < arms.Length; i++) { + for (int i = 0; i < arms.Length; i++) + { arms[i].TranslateRoot(spine.chest.solverPosition, spine.chest.solverRotation); } - for (int i = 0; i < arms.Length; i++) { + for (int i = 0; i < arms.Length; i++) + { arms[i].Solve(i == 0); } @@ -456,20 +528,24 @@ private void Solve() { // Find the support leg supportLegIndex = -1; float shortestMag = Mathf.Infinity; - for (int i = 0; i < legs.Length; i++) { + for (int i = 0; i < legs.Length; i++) + { float mag = Vector3.SqrMagnitude(legs[i].lastBone.solverPosition - legs[i].bones[0].solverPosition); - if (mag < shortestMag) { + if (mag < shortestMag) + { supportLegIndex = i; shortestMag = mag; } } } - private Vector3 GetPosition(int index) { + private Vector3 GetPosition(int index) + { return solvedPositions[index]; } - private Quaternion GetRotation(int index) { + private Quaternion GetRotation(int index) + { return solvedRotations[index]; } @@ -533,7 +609,8 @@ private Quaternion GetRotation(int index) { private Vector3 debugPos3; private Vector3 debugPos4; - private void Write() { + private void Write() + { solvedPositions[0] = rootBone.solverPosition; solvedRotations[0] = rootBone.solverRotation; spine.Write(ref solvedPositions, ref solvedRotations); @@ -542,7 +619,8 @@ private void Write() { foreach (Arm arm in arms) arm.Write(ref solvedPositions, ref solvedRotations); } - private Vector3 GetPelvisOffset() { + private Vector3 GetPelvisOffset() + { if (locomotion.weight <= 0f) return Vector3.zero; if (locomotion.blockingLayers == -1) return Vector3.zero; @@ -556,12 +634,17 @@ private Vector3 GetPelvisOffset() { //debugPos4 = sampledOrigin; - if (locomotion.raycastRadius <= 0f) { - if (Physics.Raycast(sampledOrigin, direction, out hit, direction.magnitude * 1.1f, locomotion.blockingLayers)) { + if (locomotion.raycastRadius <= 0f) + { + if (Physics.Raycast(sampledOrigin, direction, out hit, direction.magnitude * 1.1f, locomotion.blockingLayers)) + { origin = hit.point; } - } else { - if (Physics.SphereCast(sampledOrigin, locomotion.raycastRadius * 1.1f, direction, out hit, direction.magnitude, locomotion.blockingLayers)) { + } + else + { + if (Physics.SphereCast(sampledOrigin, locomotion.raycastRadius * 1.1f, direction, out hit, direction.magnitude, locomotion.blockingLayers)) + { origin = sampledOrigin + direction.normalized * hit.distance / 1.1f; } } @@ -572,13 +655,18 @@ private Vector3 GetPelvisOffset() { //debugPos1 = origin; //debugPos2 = position; - if (locomotion.raycastRadius <= 0f) { - if (Physics.Raycast(origin, direction, out hit, direction.magnitude, locomotion.blockingLayers)) { + if (locomotion.raycastRadius <= 0f) + { + if (Physics.Raycast(origin, direction, out hit, direction.magnitude, locomotion.blockingLayers)) + { position = hit.point; } - } else { - if (Physics.SphereCast(origin, locomotion.raycastRadius, direction, out hit, direction.magnitude, locomotion.blockingLayers)) { + } + else + { + if (Physics.SphereCast(origin, locomotion.raycastRadius, direction, out hit, direction.magnitude, locomotion.blockingLayers)) + { position = origin + direction.normalized * hit.distance; } } From 6bd52dcb1830e52c17aec289287f00eb26f271f2 Mon Sep 17 00:00:00 2001 From: Assistant Date: Wed, 26 Dec 2018 15:05:51 -0400 Subject: [PATCH 4/8] Worked on FBT, fixed spaces --- CustomAvatar/AvatarBehaviour.cs | 162 +++---- CustomAvatar/AvatarEventsPlayer.cs | 182 ++++---- CustomAvatar/CustomAvatar.cs | 16 +- CustomAvatar/EventManager.cs | 34 +- CustomAvatar/IAvatarFullBodyInput.cs | 14 +- CustomAvatar/IAvatarInput.cs | 6 +- CustomAvatar/PlayerAvatarInput.cs | 175 +++---- CustomAvatar/PlayerAvatarManager.cs | 8 +- CustomAvatar/Plugin.cs | 162 ++++--- CustomAvatar/Properties/AssemblyInfo.cs | 8 +- CustomAvatar/VRIK/AxisTools.cs | 2 +- CustomAvatar/VRIK/DynamicBone.cs | 2 +- CustomAvatar/VRIK/DynamicBoneCollider.cs | 2 +- CustomAvatar/VRIK/Hierarchy.cs | 2 +- CustomAvatar/VRIK/IKManager.cs | 34 +- CustomAvatar/VRIK/IKManagerAdvanced.cs | 532 +++++++++++----------- CustomAvatar/VRIK/IKSolverVRBodyPart.cs | 4 +- CustomAvatar/VRIK/IKSolverVRFootstep.cs | 2 +- CustomAvatar/VRIK/IKSolverVRLeg.cs | 4 +- CustomAvatar/VRIK/IKSolverVRLocomotion.cs | 4 +- CustomAvatar/VRIK/IKSolverVRSpine.cs | 4 +- CustomAvatar/VRIK/IKSolverVRUtilities.cs | 2 +- CustomAvatar/VRIK/Interp.cs | 2 +- CustomAvatar/VRIK/InterpolationMode.cs | 2 +- CustomAvatar/VRIK/TwistRelaxer.cs | 54 +-- CustomAvatar/VRIK/V3Tools.cs | 2 +- CustomAvatar/VRIK/VRIK.cs | 4 +- CustomAvatar/VRIK/Warning.cs | 2 +- 28 files changed, 738 insertions(+), 689 deletions(-) diff --git a/CustomAvatar/AvatarBehaviour.cs b/CustomAvatar/AvatarBehaviour.cs index a61f205..8062990 100644 --- a/CustomAvatar/AvatarBehaviour.cs +++ b/CustomAvatar/AvatarBehaviour.cs @@ -1,4 +1,4 @@ -using System; +using System; using UnityEngine; namespace CustomAvatar @@ -11,21 +11,21 @@ public class AvatarBehaviour : MonoBehaviour private Transform _body; private Transform _leftHand; private Transform _rightHand; - private Transform _leftLeg; - private Transform _rightLeg; - private Transform _pelvis; + private Transform _leftLeg; + private Transform _rightLeg; + private Transform _pelvis; - private Vector3 _prevBodyPos; + private Vector3 _prevBodyPos; - private Vector3 _prevLeftLegPos = default(Vector3); - private Vector3 _prevRightLegPos = default(Vector3); - private Quaternion _prevLeftLegRot = default(Quaternion); - private Quaternion _prevRightLegRot = default(Quaternion); + private Vector3 _prevLeftLegPos = default(Vector3); + private Vector3 _prevRightLegPos = default(Vector3); + private Quaternion _prevLeftLegRot = default(Quaternion); + private Quaternion _prevRightLegRot = default(Quaternion); - private Vector3 _prevPelvisPos = default(Vector3); - private Quaternion _prevPelvisRot = default(Quaternion); + private Vector3 _prevPelvisPos = default(Vector3); + private Quaternion _prevPelvisRot = default(Quaternion); - public void Init(IAvatarInput avatarInput) + public void Init(IAvatarInput avatarInput) { _avatarInput = avatarInput; @@ -33,77 +33,77 @@ public void Init(IAvatarInput avatarInput) _body = gameObject.transform.Find("Body"); _leftHand = gameObject.transform.Find("LeftHand"); _rightHand = gameObject.transform.Find("RightHand"); - _leftLeg = gameObject.transform.Find("LeftLeg"); - _rightLeg = gameObject.transform.Find("RightLeg"); - _pelvis = gameObject.transform.Find("Pelvis"); - } + _leftLeg = gameObject.transform.Find("LeftLeg"); + _rightLeg = gameObject.transform.Find("RightLeg"); + _pelvis = gameObject.transform.Find("Pelvis"); + } private void LateUpdate() { - try - { - var headPosRot = _avatarInput.HeadPosRot; - var leftPosRot = _avatarInput.LeftPosRot; - var rightPosRot = _avatarInput.RightPosRot; - - _head.position = headPosRot.Position; - _head.rotation = headPosRot.Rotation; - - _leftHand.position = leftPosRot.Position; - _leftHand.rotation = leftPosRot.Rotation; - - _rightHand.position = rightPosRot.Position; - _rightHand.rotation = rightPosRot.Rotation; - - if (_leftLeg != null && _rightLeg != null && _avatarInput is IAvatarFullBodyInput) - { - var _fbinput = _avatarInput as IAvatarFullBodyInput; - var leftLegPosRot = _fbinput.LeftLegPosRot; - var rightLegPosRot = _fbinput.RightLegPosRot; - _prevLeftLegPos = Vector3.Lerp(_prevLeftLegPos, leftLegPosRot.Position, 15 * Time.deltaTime); - _prevLeftLegRot = Quaternion.Slerp(_prevLeftLegRot, leftLegPosRot.Rotation, 10 * Time.deltaTime); - _leftLeg.position = _prevLeftLegPos; - _leftLeg.rotation = _prevLeftLegRot; - - _prevRightLegPos = Vector3.Lerp(_prevRightLegPos, rightLegPosRot.Position, 15 * Time.deltaTime); - _prevRightLegRot = Quaternion.Slerp(_prevRightLegRot, rightLegPosRot.Rotation, 10 * Time.deltaTime); - _rightLeg.position = _prevRightLegPos; - _rightLeg.rotation = _prevRightLegRot; - } - - if(_pelvis != null && _avatarInput is IAvatarFullBodyInput) - { - var _fbinput = _avatarInput as IAvatarFullBodyInput; - var pelvisPosRot = _fbinput.PelvisPosRot; - - _prevPelvisPos = Vector3.Lerp(_prevPelvisPos, pelvisPosRot.Position, 17 * Time.deltaTime); - _prevPelvisRot = Quaternion.Slerp(_prevPelvisRot, pelvisPosRot.Rotation, 13 * Time.deltaTime); - _pelvis.position = _prevPelvisPos; - _pelvis.rotation = _prevPelvisRot; - } - - var vrPlatformHelper = PersistentSingleton.instance; - - vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_leftHand); - vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_rightHand); - - if (_body == null) return; - _body.position = _head.position - (_head.transform.up * 0.1f); - - var vel = new Vector3(_body.transform.localPosition.x - _prevBodyPos.x, 0.0f, - _body.localPosition.z - _prevBodyPos.z); - - var rot = Quaternion.Euler(0.0f, _head.localEulerAngles.y, 0.0f); - var tiltAxis = Vector3.Cross(gameObject.transform.up, vel); - _body.localRotation = Quaternion.Lerp(_body.localRotation, - Quaternion.AngleAxis(vel.magnitude * 1250.0f, tiltAxis) * rot, - Time.deltaTime * 10.0f); - - _prevBodyPos = _body.transform.localPosition; - } catch(Exception e) - { - Console.WriteLine("{0}\n{1}", e.Message, e.StackTrace); - } + try + { + var headPosRot = _avatarInput.HeadPosRot; + var leftPosRot = _avatarInput.LeftPosRot; + var rightPosRot = _avatarInput.RightPosRot; + + _head.position = headPosRot.Position; + _head.rotation = headPosRot.Rotation; + + _leftHand.position = leftPosRot.Position; + _leftHand.rotation = leftPosRot.Rotation; + + _rightHand.position = rightPosRot.Position; + _rightHand.rotation = rightPosRot.Rotation; + + if (_leftLeg != null && _rightLeg != null && _avatarInput is IAvatarFullBodyInput) + { + var _fbinput = _avatarInput as IAvatarFullBodyInput; + var leftLegPosRot = _fbinput.LeftLegPosRot; + var rightLegPosRot = _fbinput.RightLegPosRot; + _prevLeftLegPos = Vector3.Lerp(_prevLeftLegPos, leftLegPosRot.Position, 15 * Time.deltaTime); + _prevLeftLegRot = Quaternion.Slerp(_prevLeftLegRot, leftLegPosRot.Rotation, 10 * Time.deltaTime); + _leftLeg.position = _prevLeftLegPos; + _leftLeg.rotation = _prevLeftLegRot; + + _prevRightLegPos = Vector3.Lerp(_prevRightLegPos, rightLegPosRot.Position, 15 * Time.deltaTime); + _prevRightLegRot = Quaternion.Slerp(_prevRightLegRot, rightLegPosRot.Rotation, 10 * Time.deltaTime); + _rightLeg.position = _prevRightLegPos; + _rightLeg.rotation = _prevRightLegRot; + } + + if(_pelvis != null && _avatarInput is IAvatarFullBodyInput) + { + var _fbinput = _avatarInput as IAvatarFullBodyInput; + var pelvisPosRot = _fbinput.PelvisPosRot; + + _prevPelvisPos = Vector3.Lerp(_prevPelvisPos, pelvisPosRot.Position, 17 * Time.deltaTime); + _prevPelvisRot = Quaternion.Slerp(_prevPelvisRot, pelvisPosRot.Rotation, 13 * Time.deltaTime); + _pelvis.position = _prevPelvisPos; + _pelvis.rotation = _prevPelvisRot; + } + + var vrPlatformHelper = PersistentSingleton.instance; + + vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_leftHand); + vrPlatformHelper.AdjustPlatformSpecificControllerTransform(_rightHand); + + if (_body == null) return; + _body.position = _head.position - (_head.transform.up * 0.1f); + + var vel = new Vector3(_body.transform.localPosition.x - _prevBodyPos.x, 0.0f, + _body.localPosition.z - _prevBodyPos.z); + + var rot = Quaternion.Euler(0.0f, _head.localEulerAngles.y, 0.0f); + var tiltAxis = Vector3.Cross(gameObject.transform.up, vel); + _body.localRotation = Quaternion.Lerp(_body.localRotation, + Quaternion.AngleAxis(vel.magnitude * 1250.0f, tiltAxis) * rot, + Time.deltaTime * 10.0f); + + _prevBodyPos = _body.transform.localPosition; + } catch(Exception e) + { + Console.WriteLine("{0}\n{1}", e.Message, e.StackTrace); + } } private Transform GetHeadTransform() @@ -117,4 +117,4 @@ private Transform GetHeadTransform() return gameObject.transform.Find("Head"); } } -} \ No newline at end of file +} diff --git a/CustomAvatar/AvatarEventsPlayer.cs b/CustomAvatar/AvatarEventsPlayer.cs index 343ec7c..a293ac6 100644 --- a/CustomAvatar/AvatarEventsPlayer.cs +++ b/CustomAvatar/AvatarEventsPlayer.cs @@ -6,14 +6,14 @@ namespace CustomAvatar { - public class AvatarEventsPlayer : MonoBehaviour - { - private EventManager _eventManager; - - private ScoreController _scoreController; - private ObstacleSaberSparkleEffectManager _saberCollisionManager; - private GameEnergyCounter _gameEnergyCounter; - private BeatmapObjectCallbackController _beatmapObjectCallbackController; + public class AvatarEventsPlayer : MonoBehaviour + { + private EventManager _eventManager; + + private ScoreController _scoreController; + private ObstacleSaberSparkleEffectManager _saberCollisionManager; + private GameEnergyCounter _gameEnergyCounter; + private BeatmapObjectCallbackController _beatmapObjectCallbackController; private BeatmapDataModel _beatmapDataModel; private int _lastNoteId = -1; @@ -29,10 +29,10 @@ private void _Restart() Start(); } - private void Start() - { - SceneManager.sceneLoaded += SceneManagerOnSceneLoaded; - } + private void Start() + { + SceneManager.sceneLoaded += SceneManagerOnSceneLoaded; + } private void OnDestroy() { @@ -53,36 +53,36 @@ private void OnDestroy() } private void SceneManagerOnSceneLoaded(Scene newScene, LoadSceneMode mode) - { - _eventManager = gameObject.GetComponent(); - if (_eventManager == null) - _eventManager = gameObject.AddComponent(); + { + _eventManager = gameObject.GetComponent(); + if (_eventManager == null) + _eventManager = gameObject.AddComponent(); _scoreController = Resources.FindObjectsOfTypeAll().FirstOrDefault(); - if (_scoreController == null) return; + if (_scoreController == null) return; //_eventManager.OnLevelStart?.Invoke(); // replaced by LevelStartedEvent() _saberCollisionManager = - Resources.FindObjectsOfTypeAll().FirstOrDefault(); - _gameEnergyCounter = Resources.FindObjectsOfTypeAll().FirstOrDefault(); - _beatmapObjectCallbackController = Resources.FindObjectsOfTypeAll().FirstOrDefault(); + Resources.FindObjectsOfTypeAll().FirstOrDefault(); + _gameEnergyCounter = Resources.FindObjectsOfTypeAll().FirstOrDefault(); + _beatmapObjectCallbackController = Resources.FindObjectsOfTypeAll().FirstOrDefault(); _beatmapDataModel = Resources.FindObjectsOfTypeAll().FirstOrDefault(); - _scoreController.noteWasCutEvent += SliceCallBack; - _scoreController.noteWasMissedEvent += NoteMissCallBack; - _scoreController.multiplierDidChangeEvent += MultiplierCallBack; - _scoreController.comboDidChangeEvent += ComboChangeEvent; + _scoreController.noteWasCutEvent += SliceCallBack; + _scoreController.noteWasMissedEvent += NoteMissCallBack; + _scoreController.multiplierDidChangeEvent += MultiplierCallBack; + _scoreController.comboDidChangeEvent += ComboChangeEvent; - if (_saberCollisionManager != null) - { - _saberCollisionManager.sparkleEffectDidStartEvent += SaberStartCollide; - _saberCollisionManager.sparkleEffectDidEndEvent += SaberEndCollide; - } + if (_saberCollisionManager != null) + { + _saberCollisionManager.sparkleEffectDidStartEvent += SaberStartCollide; + _saberCollisionManager.sparkleEffectDidEndEvent += SaberEndCollide; + } - if (_gameEnergyCounter != null) _gameEnergyCounter.gameEnergyDidReach0Event += FailLevelCallBack; + if (_gameEnergyCounter != null) _gameEnergyCounter.gameEnergyDidReach0Event += FailLevelCallBack; - if (_beatmapObjectCallbackController != null) + if (_beatmapObjectCallbackController != null) _beatmapObjectCallbackController.beatmapEventDidTriggerEvent += OnBeatmapEventDidTriggerEvent; _lastNoteId = -1; @@ -106,71 +106,71 @@ private void BeatmapDataChangedCallback() } private void SliceCallBack(NoteData noteData, NoteCutInfo noteCutInfo, int multiplier) - { - if (!noteCutInfo.allIsOK) - { - _eventManager.OnComboBreak?.Invoke(); - } - else - { - _eventManager.OnSlice?.Invoke(); - } + { + if (!noteCutInfo.allIsOK) + { + _eventManager.OnComboBreak?.Invoke(); + } + else + { + _eventManager.OnSlice?.Invoke(); + } if (noteData.id == _lastNoteId) { _eventManager.OnLevelFinish?.Invoke(); } - } - - private void NoteMissCallBack(NoteData noteData, int multiplier) - { - if (noteData.noteType != NoteType.Bomb) - { - _eventManager.OnComboBreak?.Invoke(); - } - } - - private void MultiplierCallBack(int multiplier, float progress) - { - if (multiplier > 1 && progress < 0.1f) - { - _eventManager.MultiplierUp?.Invoke(); - } - } - - private void SaberStartCollide(Saber.SaberType saber) - { - _eventManager.SaberStartColliding?.Invoke(); - } - - private void SaberEndCollide(Saber.SaberType saber) - { - _eventManager.SaberStopColliding?.Invoke(); - } - - private void FailLevelCallBack() - { - _eventManager.OnLevelFail?.Invoke(); - } - - private void OnBeatmapEventDidTriggerEvent (BeatmapEventData beatmapEventData) - { - if ((int) beatmapEventData.type >= 5) return; - - if (beatmapEventData.value > 0 && beatmapEventData.value < 4) - { - _eventManager.OnBlueLightOn?.Invoke(); - } - - if (beatmapEventData.value > 4 && beatmapEventData.value < 8) - { - _eventManager.OnRedLightOn?.Invoke(); - } - } - - private void ComboChangeEvent(int combo) - { - _eventManager.OnComboChanged?.Invoke(combo); + } + + private void NoteMissCallBack(NoteData noteData, int multiplier) + { + if (noteData.noteType != NoteType.Bomb) + { + _eventManager.OnComboBreak?.Invoke(); + } + } + + private void MultiplierCallBack(int multiplier, float progress) + { + if (multiplier > 1 && progress < 0.1f) + { + _eventManager.MultiplierUp?.Invoke(); + } + } + + private void SaberStartCollide(Saber.SaberType saber) + { + _eventManager.SaberStartColliding?.Invoke(); + } + + private void SaberEndCollide(Saber.SaberType saber) + { + _eventManager.SaberStopColliding?.Invoke(); + } + + private void FailLevelCallBack() + { + _eventManager.OnLevelFail?.Invoke(); + } + + private void OnBeatmapEventDidTriggerEvent (BeatmapEventData beatmapEventData) + { + if ((int) beatmapEventData.type >= 5) return; + + if (beatmapEventData.value > 0 && beatmapEventData.value < 4) + { + _eventManager.OnBlueLightOn?.Invoke(); + } + + if (beatmapEventData.value > 4 && beatmapEventData.value < 8) + { + _eventManager.OnRedLightOn?.Invoke(); + } + } + + private void ComboChangeEvent(int combo) + { + _eventManager.OnComboChanged?.Invoke(combo); } public void MenuEnteredEvent() diff --git a/CustomAvatar/CustomAvatar.cs b/CustomAvatar/CustomAvatar.cs index 5e08257..dd31838 100644 --- a/CustomAvatar/CustomAvatar.cs +++ b/CustomAvatar/CustomAvatar.cs @@ -1,4 +1,4 @@ -using System; +using System; using UnityEngine; namespace CustomAvatar @@ -90,13 +90,13 @@ public GameObject GameObject public void Load(Action loadedCallback) { - if (IsLoaded) - { - loadedCallback(this, AvatarLoadResult.Completed); - return; - } + if (IsLoaded) + { + loadedCallback(this, AvatarLoadResult.Completed); + return; + } - void Loaded(AvatarLoadResult result) + void Loaded(AvatarLoadResult result) { loadedCallback(this, result); } @@ -104,4 +104,4 @@ void Loaded(AvatarLoadResult result) _assetBundle.LoadAssetBundle(Loaded); } } -} \ No newline at end of file +} diff --git a/CustomAvatar/EventManager.cs b/CustomAvatar/EventManager.cs index ef16241..08904da 100644 --- a/CustomAvatar/EventManager.cs +++ b/CustomAvatar/EventManager.cs @@ -4,25 +4,25 @@ namespace CustomAvatar { - public class EventManager : MonoBehaviour - { - public UnityEvent OnSlice; - public UnityEvent OnComboBreak; - public UnityEvent MultiplierUp; - public UnityEvent SaberStartColliding; - public UnityEvent SaberStopColliding; - public UnityEvent OnMenuEnter; - public UnityEvent OnLevelStart; - public UnityEvent OnLevelFail; + public class EventManager : MonoBehaviour + { + public UnityEvent OnSlice; + public UnityEvent OnComboBreak; + public UnityEvent MultiplierUp; + public UnityEvent SaberStartColliding; + public UnityEvent SaberStopColliding; + public UnityEvent OnMenuEnter; + public UnityEvent OnLevelStart; + public UnityEvent OnLevelFail; public UnityEvent OnLevelFinish; public UnityEvent OnBlueLightOn; - public UnityEvent OnRedLightOn; + public UnityEvent OnRedLightOn; - [Serializable] - public class ComboChangedEvent : UnityEvent - { - } + [Serializable] + public class ComboChangedEvent : UnityEvent + { + } - public ComboChangedEvent OnComboChanged = new ComboChangedEvent(); - } + public ComboChangedEvent OnComboChanged = new ComboChangedEvent(); + } } diff --git a/CustomAvatar/IAvatarFullBodyInput.cs b/CustomAvatar/IAvatarFullBodyInput.cs index 15b3096..c76fb36 100644 --- a/CustomAvatar/IAvatarFullBodyInput.cs +++ b/CustomAvatar/IAvatarFullBodyInput.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,10 +6,10 @@ namespace CustomAvatar { - interface IAvatarFullBodyInput:IAvatarInput - { - PosRot LeftLegPosRot { get; } - PosRot RightLegPosRot { get; } - PosRot PelvisPosRot { get; } - } + interface IAvatarFullBodyInput:IAvatarInput + { + PosRot LeftLegPosRot { get; } + PosRot RightLegPosRot { get; } + PosRot PelvisPosRot { get; } + } } diff --git a/CustomAvatar/IAvatarInput.cs b/CustomAvatar/IAvatarInput.cs index e1ac7c3..6288e9e 100644 --- a/CustomAvatar/IAvatarInput.cs +++ b/CustomAvatar/IAvatarInput.cs @@ -1,9 +1,9 @@ -namespace CustomAvatar +namespace CustomAvatar { public interface IAvatarInput { PosRot HeadPosRot { get; } PosRot LeftPosRot { get; } PosRot RightPosRot { get; } - } -} \ No newline at end of file + } +} diff --git a/CustomAvatar/PlayerAvatarInput.cs b/CustomAvatar/PlayerAvatarInput.cs index 9d1b4d5..9ff05ea 100644 --- a/CustomAvatar/PlayerAvatarInput.cs +++ b/CustomAvatar/PlayerAvatarInput.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.XR; @@ -7,11 +7,11 @@ namespace CustomAvatar { public class PlayerAvatarInput : IAvatarFullBodyInput { - public PlayerAvatarInput() - { - } + public PlayerAvatarInput() + { + } - public PosRot HeadPosRot + public PosRot HeadPosRot { get { return GetXRNodeWorldPosRot(XRNode.Head); } } @@ -19,98 +19,111 @@ public PosRot HeadPosRot public PosRot LeftPosRot { get - { - if(Plugin.IsTrackerAsHand && Plugin.Trackers.Capacity >= 2) - return GetTrackerWorldPosRot(Plugin.Trackers[0]); - else - return GetXRNodeWorldPosRot(XRNode.LeftHand); - } + { + if(Plugin.IsTrackerAsHand && Plugin.Trackers.Count >= 2) + return GetTrackerWorldPosRot(Plugin.Trackers[0]); + else + return GetXRNodeWorldPosRot(XRNode.LeftHand); + } } public PosRot RightPosRot { get - { - if (Plugin.IsTrackerAsHand && Plugin.Trackers.Capacity >= 2) - return GetTrackerWorldPosRot(Plugin.Trackers[1]); - else - return GetXRNodeWorldPosRot(XRNode.RightHand); - } - } + { + if (Plugin.IsTrackerAsHand && Plugin.Trackers.Count >= 2) + return GetTrackerWorldPosRot(Plugin.Trackers[1]); + else + return GetXRNodeWorldPosRot(XRNode.RightHand); + } + } - public PosRot LeftLegPosRot - { - get - { - if (Plugin.IsFullBodyTracking && Plugin.Trackers.Capacity >= 2) - return GetTrackerWorldPosRot(Plugin.Trackers[0]); - else - return new PosRot(new Vector3(), new Quaternion()); - } - } + public PosRot LeftLegPosRot + { + get + { + if (Plugin.FullBodyTrackingType >= Plugin.TrackingType.Feet && Plugin.Trackers.Count >= 2) + { + return GetTrackerWorldPosRot(Plugin.Trackers[0]); + } + else + return new PosRot(new Vector3(), new Quaternion()); + } + } - public PosRot RightLegPosRot - { - get - { - if (Plugin.IsFullBodyTracking && Plugin.Trackers.Capacity >= 2) - return GetTrackerWorldPosRot(Plugin.Trackers[1]); - else - return new PosRot(new Vector3(), new Quaternion()); - } - } + public PosRot RightLegPosRot + { + get + { + if (Plugin.FullBodyTrackingType >= Plugin.TrackingType.Feet && Plugin.Trackers.Count >= 2) + { + return GetTrackerWorldPosRot(Plugin.Trackers[1]); + } + else + return new PosRot(new Vector3(), new Quaternion()); + } + } - public PosRot PelvisPosRot - { - get - { - if (Plugin.IsFullBodyTracking && Plugin.Trackers.Capacity >= 3) - return GetTrackerWorldPosRot(Plugin.Trackers[3]); - else - return new PosRot(new Vector3(), new Quaternion()); - } - } + public PosRot PelvisPosRot + { + get + { + if (Plugin.FullBodyTrackingType == Plugin.TrackingType.Hips && Plugin.Trackers.Count >= 1) + { + return GetTrackerWorldPosRot(Plugin.Trackers[0]); + } + else + { + if (Plugin.FullBodyTrackingType == Plugin.TrackingType.Full && Plugin.Trackers.Count >= 3) + { + return GetTrackerWorldPosRot(Plugin.Trackers[2]); + } + else + return new PosRot(new Vector3(), new Quaternion()); + } + } + } - private static PosRot GetXRNodeWorldPosRot(XRNode node) + private static PosRot GetXRNodeWorldPosRot(XRNode node) { var pos = InputTracking.GetLocalPosition(node); var rot = InputTracking.GetLocalRotation(node); - var roomCenter = BeatSaberUtil.GetRoomCenter(); + var roomCenter = BeatSaberUtil.GetRoomCenter(); var roomRotation = BeatSaberUtil.GetRoomRotation(); pos = roomRotation * pos; pos += roomCenter; rot = roomRotation * rot; return new PosRot(pos, rot); - } + } - private static PosRot GetTrackerWorldPosRot(XRNodeState tracker) - { - Vector3 pos = new Vector3(); - Quaternion rot = new Quaternion(); - try - { - var notes = new List(); - InputTracking.GetNodeStates(notes); - foreach (XRNodeState note in notes) - { - if (note.uniqueID != tracker.uniqueID) - continue; - if (note.TryGetPosition(out pos) && note.TryGetRotation(out rot)) - { - var roomCenter = BeatSaberUtil.GetRoomCenter(); - var roomRotation = BeatSaberUtil.GetRoomRotation(); - pos = roomRotation * pos; - pos += roomCenter; - rot = roomRotation * rot; - } - } - } - catch (Exception e) - { - Console.WriteLine(e.Message + "\n" + e.StackTrace); - } - return new PosRot(pos, rot); - } - } -} \ No newline at end of file + private static PosRot GetTrackerWorldPosRot(XRNodeState tracker) + { + Vector3 pos = new Vector3(); + Quaternion rot = new Quaternion(); + try + { + var notes = new List(); + InputTracking.GetNodeStates(notes); + foreach (XRNodeState note in notes) + { + if (note.uniqueID != tracker.uniqueID) + continue; + if (note.TryGetPosition(out pos) && note.TryGetRotation(out rot)) + { + var roomCenter = BeatSaberUtil.GetRoomCenter(); + var roomRotation = BeatSaberUtil.GetRoomRotation(); + pos = roomRotation * pos; + pos += roomCenter; + rot = roomRotation * rot; + } + } + } + catch (Exception e) + { + Console.WriteLine(e.Message + "\n" + e.StackTrace); + } + return new PosRot(pos, rot); + } + } +} diff --git a/CustomAvatar/PlayerAvatarManager.cs b/CustomAvatar/PlayerAvatarManager.cs index 45b5249..6164ee9 100644 --- a/CustomAvatar/PlayerAvatarManager.cs +++ b/CustomAvatar/PlayerAvatarManager.cs @@ -104,9 +104,9 @@ public CustomAvatar SwitchToPreviousAvatar() var nextAvatar = avatars[nextIndex]; CurrentPlayerAvatar = nextAvatar; return nextAvatar; - } + } - private void CustomAvatarLoaded(CustomAvatar loadedAvatar, AvatarLoadResult result) + private void CustomAvatarLoaded(CustomAvatar loadedAvatar, AvatarLoadResult result) { if (result != AvatarLoadResult.Completed) { @@ -146,8 +146,8 @@ private void SceneManagerOnSceneLoaded(Scene newScene, LoadSceneMode mode) { ResizePlayerAvatar(); OnFirstPersonEnabledChanged(Plugin.Instance.FirstPersonEnabled); - _currentSpawnedPlayerAvatar?.GameObject.GetComponentInChildren()?.Restart(); - } + _currentSpawnedPlayerAvatar?.GameObject.GetComponentInChildren()?.Restart(); + } public void OnSceneTransitioned(Scene newScene) { diff --git a/CustomAvatar/Plugin.cs b/CustomAvatar/Plugin.cs index cad7f6a..d10abc4 100644 --- a/CustomAvatar/Plugin.cs +++ b/CustomAvatar/Plugin.cs @@ -23,67 +23,69 @@ public class Plugin : IPlugin private WaitForSecondsRealtime _sceneLoadWait = new WaitForSecondsRealtime(0.1f); private GameScenesManager _scenesManager; - private static bool _isTrackerAsHand; - private static bool _isFullBodyTracking; - - public static List Trackers = new List(); - public static bool IsTrackerAsHand - { - get { return _isTrackerAsHand; } - set - { - _isTrackerAsHand = value; - List notes = new List(); - Trackers = new List(); - InputTracking.GetNodeStates(notes); - foreach (XRNodeState note in notes) - { - if (note.nodeType != XRNode.HardwareTracker || !InputTracking.GetNodeName(note.uniqueID).Contains("LHR-")) - continue; - Trackers.Add(note); - } - if (Trackers.Capacity == 0) - _isTrackerAsHand = false; - Console.WriteLine("IsTrackerAsHand : " + IsTrackerAsHand); - } - } - - public static bool IsFullBodyTracking - { - get { return _isFullBodyTracking; } - set - { - _isFullBodyTracking = value; - List notes = new List(); - Trackers = new List(); - InputTracking.GetNodeStates(notes); - foreach (XRNodeState note in notes) - { - if (note.nodeType != XRNode.HardwareTracker || !InputTracking.GetNodeName(note.uniqueID).Contains("LHR-")) - continue; - Trackers.Add(note); - } - if (Trackers.Capacity == 0) - _isFullBodyTracking = false; - var currentAvatar = Instance.PlayerAvatarManager.GetCurrentAvatar(); - if (currentAvatar != null) - { - var _IKManagerAdvanced = currentAvatar.GameObject.GetComponentInChildren(true); - if (_IKManagerAdvanced != null) - { - _IKManagerAdvanced.CheckFullBodyTracking(); - } - } - Console.WriteLine("IsFullBodyTracking : " + _isFullBodyTracking); - } - } + private static bool _isTrackerAsHand; + + public static List Trackers = new List(); + public static bool IsTrackerAsHand + { + get { return _isTrackerAsHand; } + set + { + _isTrackerAsHand = value; + List notes = new List(); + Trackers = new List(); + InputTracking.GetNodeStates(notes); + foreach (XRNodeState note in notes) + { + if (note.nodeType != XRNode.HardwareTracker || !InputTracking.GetNodeName(note.uniqueID).Contains("LHR-")) + continue; + Trackers.Add(note); + } + if (Trackers.Count == 0) + _isTrackerAsHand = false; + Console.WriteLine("IsTrackerAsHand : " + IsTrackerAsHand); + } + } + + public static bool IsFullBodyTracking + { + get { return Plugin.FullBodyTrackingType != Plugin.TrackingType.None; ; } + set + { + List notes = new List(); + Trackers = new List(); + InputTracking.GetNodeStates(notes); + foreach (XRNodeState note in notes) + { + if (note.nodeType != XRNode.HardwareTracker || !InputTracking.GetNodeName(note.uniqueID).Contains("LHR-")) + continue; + Trackers.Add(note); + } + if (Trackers.Count >= 0 && Trackers.Count <= 3) + Plugin.FullBodyTrackingType = (Plugin.TrackingType)Plugin.Trackers.Count; + else + Plugin.FullBodyTrackingType = Plugin.TrackingType.None; + var currentAvatar = Instance.PlayerAvatarManager.GetCurrentAvatar(); + if (currentAvatar != null) + { + var _IKManagerAdvanced = currentAvatar.GameObject.GetComponentInChildren(true); + if (_IKManagerAdvanced != null) + { + _IKManagerAdvanced.CheckFullBodyTracking(); + } + } + bool isFullBodyTracking = Plugin.IsFullBodyTracking; + Console.WriteLine(string.Concat("IsFullBodyTracking : ", isFullBodyTracking.ToString())); + Console.WriteLine(string.Concat("FullBodyTrackingType: ", FullBodyTrackingType.ToString())); + } + } public Plugin() { Instance = this; - } + } - public event Action FirstPersonEnabledChanged; + public event Action FirstPersonEnabledChanged; public static Plugin Instance { get; private set; } public AvatarLoader AvatarLoader { get; private set; } @@ -115,6 +117,20 @@ public bool FirstPersonEnabled } } + public enum TrackingType + { + None, + Hips, + Feet, + Full + } + + public static Plugin.TrackingType FullBodyTrackingType + { + get; + set; + } + public bool RotatePreviewEnabled { get { return AvatarPreviewRotation.rotatePreview; } @@ -167,8 +183,7 @@ public void OnApplicationStart() FirstPersonEnabled = PlayerPrefs.HasKey(FirstPersonEnabledKey); RotatePreviewEnabled = PlayerPrefs.HasKey(RotatePreviewEnabledKey); SceneManager.sceneLoaded += SceneManagerOnSceneLoaded; - IsFullBodyTracking = true; - } + } public void OnApplicationQuit() { @@ -199,7 +214,8 @@ private void AvatarsLoaded(IReadOnlyList loadedAvatars) PlayerAvatarManager = new PlayerAvatarManager(AvatarLoader, AvatarTailor, previousAvatar); PlayerAvatarManager.AvatarChanged += PlayerAvatarManagerOnAvatarChanged; - } + IsFullBodyTracking = true; + } private void SceneManagerOnSceneLoaded(Scene newScene, LoadSceneMode mode) { @@ -223,8 +239,8 @@ private void SceneTransitionDidFinish() private void PlayerAvatarManagerOnAvatarChanged(CustomAvatar newAvatar) { PlayerPrefs.SetString(PreviousAvatarKey, newAvatar.FullPath); - IsFullBodyTracking = IsFullBodyTracking; - } + IsFullBodyTracking = IsFullBodyTracking; + } public void OnUpdate() { @@ -239,16 +255,16 @@ public void OnUpdate() else if (Input.GetKeyDown(KeyCode.Home)) { FirstPersonEnabled = !FirstPersonEnabled; - } - else if (Input.GetKeyDown(KeyCode.F6)) - { - IsTrackerAsHand = !IsTrackerAsHand; - } - else if (Input.GetKeyDown(KeyCode.F5)) - { - IsFullBodyTracking = !IsFullBodyTracking; - } - } + } + else if (Input.GetKeyDown(KeyCode.F6)) + { + IsTrackerAsHand = !IsTrackerAsHand; + } + else if (Input.GetKeyDown(KeyCode.F5)) + { + IsFullBodyTracking = !IsFullBodyTracking; + } + } private void SetCameraCullingMask(Camera camera) { @@ -267,7 +283,7 @@ public void OnLevelWasInitialized(int level) } public void OnLevelWasLoaded(int level) - { - } + { + } } } diff --git a/CustomAvatar/Properties/AssemblyInfo.cs b/CustomAvatar/Properties/AssemblyInfo.cs index 12c0726..e8fac59 100644 --- a/CustomAvatar/Properties/AssemblyInfo.cs +++ b/CustomAvatar/Properties/AssemblyInfo.cs @@ -23,10 +23,10 @@ // Version information for an assembly consists of the following four values: // -// Major Version -// Minor Version -// Build Number -// Revision +// Major Version +// Minor Version +// Build Number +// Revision // // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: diff --git a/CustomAvatar/VRIK/AxisTools.cs b/CustomAvatar/VRIK/AxisTools.cs index f069028..2df2638 100644 --- a/CustomAvatar/VRIK/AxisTools.cs +++ b/CustomAvatar/VRIK/AxisTools.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System.Collections; namespace AvatarScriptPack { diff --git a/CustomAvatar/VRIK/DynamicBone.cs b/CustomAvatar/VRIK/DynamicBone.cs index 784a08d..a02162e 100644 --- a/CustomAvatar/VRIK/DynamicBone.cs +++ b/CustomAvatar/VRIK/DynamicBone.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using UnityEngine; diff --git a/CustomAvatar/VRIK/DynamicBoneCollider.cs b/CustomAvatar/VRIK/DynamicBoneCollider.cs index 42b261b..e1f3fa6 100644 --- a/CustomAvatar/VRIK/DynamicBoneCollider.cs +++ b/CustomAvatar/VRIK/DynamicBoneCollider.cs @@ -1,4 +1,4 @@ -using System; +using System; using UnityEngine; // Token: 0x02000004 RID: 4 diff --git a/CustomAvatar/VRIK/Hierarchy.cs b/CustomAvatar/VRIK/Hierarchy.cs index 0897848..5c75824 100644 --- a/CustomAvatar/VRIK/Hierarchy.cs +++ b/CustomAvatar/VRIK/Hierarchy.cs @@ -1,4 +1,4 @@ -using System; +using System; using UnityEngine; namespace AvatarScriptPack diff --git a/CustomAvatar/VRIK/IKManager.cs b/CustomAvatar/VRIK/IKManager.cs index b1046b8..924bed6 100644 --- a/CustomAvatar/VRIK/IKManager.cs +++ b/CustomAvatar/VRIK/IKManager.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,22 +6,22 @@ namespace AvatarScriptPack { - class IKManager : MonoBehaviour - { - public Transform HeadTarget; - public Transform LeftHandTarget; - public Transform RightHandTarget; + class IKManager : MonoBehaviour + { + public Transform HeadTarget; + public Transform LeftHandTarget; + public Transform RightHandTarget; - public void Start() - { - var vrik = this.gameObject.GetComponent(); + public void Start() + { + var vrik = this.gameObject.GetComponent(); - if (vrik != null) - { - vrik.solver.spine.headTarget = HeadTarget; - vrik.solver.leftArm.target = LeftHandTarget; - vrik.solver.rightArm.target = RightHandTarget; - } - } - } + if (vrik != null) + { + vrik.solver.spine.headTarget = HeadTarget; + vrik.solver.leftArm.target = LeftHandTarget; + vrik.solver.rightArm.target = RightHandTarget; + } + } + } } diff --git a/CustomAvatar/VRIK/IKManagerAdvanced.cs b/CustomAvatar/VRIK/IKManagerAdvanced.cs index 7c38804..e58ab15 100644 --- a/CustomAvatar/VRIK/IKManagerAdvanced.cs +++ b/CustomAvatar/VRIK/IKManagerAdvanced.cs @@ -9,14 +9,14 @@ namespace AvatarScriptPack { - class IKManagerAdvanced : MonoBehaviour - { - [Tooltip("The head target.")] - public Transform HeadTarget; - [Tooltip("The hand target")] - public Transform LeftHandTarget; - [Tooltip("The hand target")] - public Transform RightHandTarget; + class IKManagerAdvanced : MonoBehaviour + { + [Tooltip("The head target.")] + public Transform HeadTarget; + [Tooltip("The hand target")] + public Transform LeftHandTarget; + [Tooltip("The hand target")] + public Transform RightHandTarget; [Space(20)] @@ -29,317 +29,337 @@ class IKManagerAdvanced : MonoBehaviour [Space(20)] - /******************* - * Spine - ******************/ + /******************* + * Spine + ******************/ - [Range(0f, 1f), Tooltip("Positional weight of the head target.")] - public float Spine_positionWeight = 1f; + [Range(0f, 1f), Tooltip("Positional weight of the head target.")] + public float Spine_positionWeight = 1f; - [Range(0f, 1f), Tooltip("Rotational weight of the head target.")] - public float Spine_rotationWeight = 1f; + [Range(0f, 1f), Tooltip("Rotational weight of the head target.")] + public float Spine_rotationWeight = 1f; - [Range(0f, 1f), Tooltip("Positional weight of the pelvis target.")] - public float Spine_pelvisPositionWeight; + [Range(0f, 1f), Tooltip("Positional weight of the pelvis target.")] + public float Spine_pelvisPositionWeight; - [Range(0f, 1f), Tooltip("Rotational weight of the pelvis target.")] - public float Spine_pelvisRotationWeight; + [Range(0f, 1f), Tooltip("Rotational weight of the pelvis target.")] + public float Spine_pelvisRotationWeight; - [Tooltip("If 'Chest Goal Weight' is greater than 0, the chest will be turned towards this Transform.")] - public Transform Spine_chestGoal; + [Tooltip("If 'Chest Goal Weight' is greater than 0, the chest will be turned towards this Transform.")] + public Transform Spine_chestGoal; - [Range(0f, 1f), Tooltip("Rotational weight of the chest target.")] - public float Spine_chestGoalWeight; + [Range(0f, 1f), Tooltip("Rotational weight of the chest target.")] + public float Spine_chestGoalWeight; - [Tooltip("Minimum height of the head from the root of the character.")] - public float Spine_minHeadHeight = 0.8f; + [Tooltip("Minimum height of the head from the root of the character.")] + public float Spine_minHeadHeight = 0.8f; - [Range(0f, 1f), Tooltip("Determines how much the body will follow the position of the head.")] - public float Spine_bodyPosStiffness = 0.55f; + [Range(0f, 1f), Tooltip("Determines how much the body will follow the position of the head.")] + public float Spine_bodyPosStiffness = 0.55f; - [Range(0f, 1f), Tooltip("Determines how much the body will follow the rotation of the head.")] - public float Spine_bodyRotStiffness = 0.1f; + [Range(0f, 1f), Tooltip("Determines how much the body will follow the rotation of the head.")] + public float Spine_bodyRotStiffness = 0.1f; - [Range(0f, 1f), FormerlySerializedAs("chestRotationWeight"), Tooltip("Determines how much the chest will rotate to the rotation of the head.")] - public float Spine_neckStiffness = 0.2f; + [Range(0f, 1f), FormerlySerializedAs("chestRotationWeight"), Tooltip("Determines how much the chest will rotate to the rotation of the head.")] + public float Spine_neckStiffness = 0.2f; - [Range(0f, 1f), Tooltip("Clamps chest rotation.")] - public float Spine_chestClampWeight = 0.5f; + [Range(0f, 1f), Tooltip("Clamps chest rotation.")] + public float Spine_chestClampWeight = 0.5f; - [Range(0f, 1f), Tooltip("Clamps head rotation.")] - public float Spine_headClampWeight = 0.6f; + [Range(0f, 1f), Tooltip("Clamps head rotation.")] + public float Spine_headClampWeight = 0.6f; - [Range(0f, 1f), Tooltip("How much will the pelvis maintain it's animated position?")] - public float Spine_maintainPelvisPosition = 0.2f; + [Range(0f, 1f), Tooltip("How much will the pelvis maintain it's animated position?")] + public float Spine_maintainPelvisPosition = 0.2f; - [Range(0f, 180f), Tooltip("Will automatically rotate the root of the character if the head target has turned past this angle.")] - public float Spine_maxRootAngle = 25f; + [Range(0f, 180f), Tooltip("Will automatically rotate the root of the character if the head target has turned past this angle.")] + public float Spine_maxRootAngle = 25f; - [Space(20)] - - /******************* - * Left Arm - ******************/ - - [Tooltip("The elbow will be bent towards this Transform if 'Bend Goal Weight' > 0.")] - public Transform LeftArm_bendGoal; - - [Range(0f, 1f), Tooltip("Positional weight of the hand target.")] - public float LeftArm_positionWeight = 1f; - - [Range(0f, 1f), Tooltip("Rotational weight of the hand target")] - public float LeftArm_rotationWeight = 1f; - - [Tooltip("Different techniques for shoulder bone rotation.")] - public IKSolverVR.Arm.ShoulderRotationMode LeftArm_shoulderRotationMode; - - [Range(0f, 1f), Tooltip("The weight of shoulder rotation")] - public float LeftArm_shoulderRotationWeight = 1f; - - [Range(0f, 1f), Tooltip("If greater than 0, will bend the elbow towards the 'Bend Goal' Transform.")] - public float LeftArm_bendGoalWeight; - - [Range(-180f, 180f), Tooltip("Angular offset of the elbow bending direction.")] - public float LeftArm_swivelOffset; - - [Tooltip("Local axis of the hand bone that points from the wrist towards the palm. Used for defining hand bone orientation.")] - public Vector3 LeftArm_wristToPalmAxis = Vector3.zero; - - [Tooltip("Local axis of the hand bone that points from the palm towards the thumb. Used for defining hand bone orientation.")] - public Vector3 LeftArm_palmToThumbAxis = Vector3.zero; - - - [Space(20)] - - /******************* - * Right Arm - ******************/ - - [Tooltip("The elbow will be bent towards this Transform if 'Bend Goal Weight' > 0.")] - public Transform RightArm_bendGoal; - - [Range(0f, 1f), Tooltip("Positional weight of the hand target.")] - public float RightArm_positionWeight = 1f; - - [Range(0f, 1f), Tooltip("Rotational weight of the hand target")] - public float RightArm_rotationWeight = 1f; - - [Tooltip("Different techniques for shoulder bone rotation.")] - public IKSolverVR.Arm.ShoulderRotationMode RightArm_shoulderRotationMode; - - [Range(0f, 1f), Tooltip("The weight of shoulder rotation")] - public float RightArm_shoulderRotationWeight = 1f; + [Space(20)] - [Range(0f, 1f), Tooltip("If greater than 0, will bend the elbow towards the 'Bend Goal' Transform.")] - public float RightArm_bendGoalWeight; + /******************* + * Left Arm + ******************/ - [Range(-180f, 180f), Tooltip("Angular offset of the elbow bending direction.")] - public float RightArm_swivelOffset; + [Tooltip("The elbow will be bent towards this Transform if 'Bend Goal Weight' > 0.")] + public Transform LeftArm_bendGoal; - [Tooltip("Local axis of the hand bone that points from the wrist towards the palm. Used for defining hand bone orientation.")] - public Vector3 RightArm_wristToPalmAxis = Vector3.zero; + [Range(0f, 1f), Tooltip("Positional weight of the hand target.")] + public float LeftArm_positionWeight = 1f; - [Tooltip("Local axis of the hand bone that points from the palm towards the thumb. Used for defining hand bone orientation.")] - public Vector3 RightArm_palmToThumbAxis = Vector3.zero; + [Range(0f, 1f), Tooltip("Rotational weight of the hand target")] + public float LeftArm_rotationWeight = 1f; + [Tooltip("Different techniques for shoulder bone rotation.")] + public IKSolverVR.Arm.ShoulderRotationMode LeftArm_shoulderRotationMode; - [Space(20)] + [Range(0f, 1f), Tooltip("The weight of shoulder rotation")] + public float LeftArm_shoulderRotationWeight = 1f; - /******************* - * Left Leg - ******************/ + [Range(0f, 1f), Tooltip("If greater than 0, will bend the elbow towards the 'Bend Goal' Transform.")] + public float LeftArm_bendGoalWeight; - [Tooltip("The knee will be bent towards this Transform if 'Bend Goal Weight' > 0.")] - public Transform LeftLeg_bendGoal; + [Range(-180f, 180f), Tooltip("Angular offset of the elbow bending direction.")] + public float LeftArm_swivelOffset; - [Range(0f, 1f), Tooltip("Positional weight of the toe/foot target.")] - public float LeftLeg_positionWeight; + [Tooltip("Local axis of the hand bone that points from the wrist towards the palm. Used for defining hand bone orientation.")] + public Vector3 LeftArm_wristToPalmAxis = Vector3.zero; - [Range(0f, 1f), Tooltip("Rotational weight of the toe/foot target.")] - public float LeftLeg_rotationWeight; + [Tooltip("Local axis of the hand bone that points from the palm towards the thumb. Used for defining hand bone orientation.")] + public Vector3 LeftArm_palmToThumbAxis = Vector3.zero; - [Range(0f, 1f), Tooltip("If greater than 0, will bend the knee towards the 'Bend Goal' Transform.")] - public float LeftLeg_bendGoalWeight; - [Range(-180f, 180f), Tooltip("Angular offset of the knee bending direction.")] - public float LeftLeg_swivelOffset; + [Space(20)] - [Range(-180f, 180f), Tooltip("Rotation of the knee bend normal value.")] - public float LeftLeg_bendRotation; + /******************* + * Right Arm + ******************/ + [Tooltip("The elbow will be bent towards this Transform if 'Bend Goal Weight' > 0.")] + public Transform RightArm_bendGoal; - [Space(20)] + [Range(0f, 1f), Tooltip("Positional weight of the hand target.")] + public float RightArm_positionWeight = 1f; - /******************* - * Right Leg - ******************/ + [Range(0f, 1f), Tooltip("Rotational weight of the hand target")] + public float RightArm_rotationWeight = 1f; - [Tooltip("The knee will be bent towards this Transform if 'Bend Goal Weight' > 0.")] - public Transform RightLeg_bendGoal; + [Tooltip("Different techniques for shoulder bone rotation.")] + public IKSolverVR.Arm.ShoulderRotationMode RightArm_shoulderRotationMode; - [Range(0f, 1f), Tooltip("Positional weight of the toe/foot target.")] - public float RightLeg_positionWeight; + [Range(0f, 1f), Tooltip("The weight of shoulder rotation")] + public float RightArm_shoulderRotationWeight = 1f; - [Range(0f, 1f), Tooltip("Rotational weight of the toe/foot target.")] - public float RightLeg_rotationWeight; + [Range(0f, 1f), Tooltip("If greater than 0, will bend the elbow towards the 'Bend Goal' Transform.")] + public float RightArm_bendGoalWeight; - [Range(0f, 1f), Tooltip("If greater than 0, will bend the knee towards the 'Bend Goal' Transform.")] - public float RightLeg_bendGoalWeight; + [Range(-180f, 180f), Tooltip("Angular offset of the elbow bending direction.")] + public float RightArm_swivelOffset; - [Range(-180f, 180f), Tooltip("Angular offset of the knee bending direction.")] - public float RightLeg_swivelOffset; + [Tooltip("Local axis of the hand bone that points from the wrist towards the palm. Used for defining hand bone orientation.")] + public Vector3 RightArm_wristToPalmAxis = Vector3.zero; - [Range(-180f, 180f), Tooltip("Rotation of the knee bend normal value.")] - public float RightLeg_bendRotation; + [Tooltip("Local axis of the hand bone that points from the palm towards the thumb. Used for defining hand bone orientation.")] + public Vector3 RightArm_palmToThumbAxis = Vector3.zero; - [Space(20)] + [Space(20)] - /******************* - * Locomotion - ******************/ + /******************* + * Left Leg + ******************/ - [Range(0f, 1f), Tooltip("Used for blending in/out of procedural locomotion.")] - public float Locomotion_weight = 1f; + [Tooltip("The knee will be bent towards this Transform if 'Bend Goal Weight' > 0.")] + public Transform LeftLeg_bendGoal; - [Tooltip("Tries to maintain this distance between the legs.")] - public float Locomotion_footDistance = 0.3f; + [Range(0f, 1f), Tooltip("Positional weight of the toe/foot target.")] + public float LeftLeg_positionWeight; - [Tooltip("Makes a step only if step target position is at least this far from the current footstep or the foot does not reach the current footstep anymore or footstep angle is past the 'Angle Threshold'.")] - public float Locomotion_stepThreshold = 0.4f; + [Range(0f, 1f), Tooltip("Rotational weight of the toe/foot target.")] + public float LeftLeg_rotationWeight; - [Tooltip("Makes a step only if step target position is at least 'Step Threshold' far from the current footstep or the foot does not reach the current footstep anymore or footstep angle is past this value.")] - public float Locomotion_angleThreshold = 60f; + [Range(0f, 1f), Tooltip("If greater than 0, will bend the knee towards the 'Bend Goal' Transform.")] + public float LeftLeg_bendGoalWeight; - [Tooltip("Multiplies angle of the center of mass - center of pressure vector. Larger value makes the character step sooner if losing balance.")] - public float Locomotion_comAngleMlp = 1f; + [Range(-180f, 180f), Tooltip("Angular offset of the knee bending direction.")] + public float LeftLeg_swivelOffset; - [Tooltip("Maximum magnitude of head/hand target velocity used in prediction.")] - public float Locomotion_maxVelocity = 0.4f; + [Range(-180f, 180f), Tooltip("Rotation of the knee bend normal value.")] + public float LeftLeg_bendRotation; - [Tooltip("The amount of head/hand target velocity prediction.")] - public float Locomotion_velocityFactor = 0.4f; - [Range(0.9f, 1f), Tooltip("How much can a leg be extended before it is forced to step to another position? 1 means fully stretched.")] - public float Locomotion_maxLegStretch = 1f; + [Space(20)] - [Tooltip("The speed of lerping the root of the character towards the horizontal mid-point of the footsteps.")] - public float Locomotion_rootSpeed = 20f; + /******************* + * Right Leg + ******************/ - [Tooltip("The speed of steps.")] - public float Locomotion_stepSpeed = 3f; + [Tooltip("The knee will be bent towards this Transform if 'Bend Goal Weight' > 0.")] + public Transform RightLeg_bendGoal; - [Tooltip("The height of the foot by normalized step progress (0 - 1).")] - public AnimationCurve Locomotion_stepHeight; + [Range(0f, 1f), Tooltip("Positional weight of the toe/foot target.")] + public float RightLeg_positionWeight; - [Tooltip("The height offset of the heel by normalized step progress (0 - 1).")] - public AnimationCurve Locomotion_heelHeight; + [Range(0f, 1f), Tooltip("Rotational weight of the toe/foot target.")] + public float RightLeg_rotationWeight; - [Range(0f, 180f), Tooltip("Rotates the foot while the leg is not stepping to relax the twist rotation of the leg if ideal rotation is past this angle.")] - public float Locomotion_relaxLegTwistMinAngle = 20f; + [Range(0f, 1f), Tooltip("If greater than 0, will bend the knee towards the 'Bend Goal' Transform.")] + public float RightLeg_bendGoalWeight; - [Tooltip("The speed of rotating the foot while the leg is not stepping to relax the twist rotation of the leg.")] - public float Locomotion_relaxLegTwistSpeed = 400f; + [Range(-180f, 180f), Tooltip("Angular offset of the knee bending direction.")] + public float RightLeg_swivelOffset; - [Tooltip("Interpolation mode of the step.")] - public InterpolationMode Locomotion_stepInterpolation = InterpolationMode.InOutSine; + [Range(-180f, 180f), Tooltip("Rotation of the knee bend normal value.")] + public float RightLeg_bendRotation; - [Tooltip("Offset for the approximated center of mass.")] - public Vector3 Locomotion_offset; - [Tooltip("Called when the left foot has finished a step.")] - public UnityEvent Locomotion_onLeftFootstep = new UnityEvent(); + [Space(20)] - [Tooltip("Called when the right foot has finished a step")] - public UnityEvent Locomotion_onRightFootstep = new UnityEvent(); + /******************* + * Locomotion + ******************/ + + [Range(0f, 1f), Tooltip("Used for blending in/out of procedural locomotion.")] + public float Locomotion_weight = 1f; + + [Tooltip("Tries to maintain this distance between the legs.")] + public float Locomotion_footDistance = 0.3f; + + [Tooltip("Makes a step only if step target position is at least this far from the current footstep or the foot does not reach the current footstep anymore or footstep angle is past the 'Angle Threshold'.")] + public float Locomotion_stepThreshold = 0.4f; + + [Tooltip("Makes a step only if step target position is at least 'Step Threshold' far from the current footstep or the foot does not reach the current footstep anymore or footstep angle is past this value.")] + public float Locomotion_angleThreshold = 60f; + + [Tooltip("Multiplies angle of the center of mass - center of pressure vector. Larger value makes the character step sooner if losing balance.")] + public float Locomotion_comAngleMlp = 1f; + + [Tooltip("Maximum magnitude of head/hand target velocity used in prediction.")] + public float Locomotion_maxVelocity = 0.4f; + + [Tooltip("The amount of head/hand target velocity prediction.")] + public float Locomotion_velocityFactor = 0.4f; + + [Range(0.9f, 1f), Tooltip("How much can a leg be extended before it is forced to step to another position? 1 means fully stretched.")] + public float Locomotion_maxLegStretch = 1f; + + [Tooltip("The speed of lerping the root of the character towards the horizontal mid-point of the footsteps.")] + public float Locomotion_rootSpeed = 20f; + + [Tooltip("The speed of steps.")] + public float Locomotion_stepSpeed = 3f; + + [Tooltip("The height of the foot by normalized step progress (0 - 1).")] + public AnimationCurve Locomotion_stepHeight; + + [Tooltip("The height offset of the heel by normalized step progress (0 - 1).")] + public AnimationCurve Locomotion_heelHeight; + + [Range(0f, 180f), Tooltip("Rotates the foot while the leg is not stepping to relax the twist rotation of the leg if ideal rotation is past this angle.")] + public float Locomotion_relaxLegTwistMinAngle = 20f; + + [Tooltip("The speed of rotating the foot while the leg is not stepping to relax the twist rotation of the leg.")] + public float Locomotion_relaxLegTwistSpeed = 400f; + + [Tooltip("Interpolation mode of the step.")] + public InterpolationMode Locomotion_stepInterpolation = InterpolationMode.InOutSine; + + [Tooltip("Offset for the approximated center of mass.")] + public Vector3 Locomotion_offset; + + [Tooltip("Called when the left foot has finished a step.")] + public UnityEvent Locomotion_onLeftFootstep = new UnityEvent(); + + [Tooltip("Called when the right foot has finished a step")] + public UnityEvent Locomotion_onRightFootstep = new UnityEvent(); + + public void Start() + { + VRIK _VRIK = base.gameObject.GetComponent(); + if (_VRIK != null) + { + _VRIK.solver.spine.headTarget = this.HeadTarget; + _VRIK.solver.leftArm.target = this.LeftHandTarget; + _VRIK.solver.rightArm.target = this.RightHandTarget; + + Type type = this.GetType(); + FieldInfo[] fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.Public); + foreach (FieldInfo fieldInfo in fieldInfos) + { + string[] propertyName = fieldInfo.Name.Split('_'); + var value = fieldInfo.GetValue(this); + if (propertyName.Count() > 1) + { + if ("Spine" == propertyName[0]) + { + SetProperty(_VRIK.solver.spine, propertyName[1], value); + } + else if ("LeftArm" == propertyName[0]) + { + SetProperty(_VRIK.solver.leftArm, propertyName[1], value); + } + else if ("RightArm" == propertyName[0]) + { + SetProperty(_VRIK.solver.rightArm, propertyName[1], value); + } + else if ("LeftLeg" == propertyName[0]) + { + SetProperty(_VRIK.solver.leftLeg, propertyName[1], value); + } + else if ("RightLeg" == propertyName[0]) + { + SetProperty(_VRIK.solver.rightLeg, propertyName[1], value); + } + else if ("Locomotion" == propertyName[0]) + { + SetProperty(_VRIK.solver.locomotion, propertyName[1], value); + } + } + } + CheckFullBodyTracking(); + } + } - public void Start() - { - VRIK _VRIK = base.gameObject.GetComponent(); - if (_VRIK != null) - { - _VRIK.solver.spine.headTarget = this.HeadTarget; - _VRIK.solver.leftArm.target = this.LeftHandTarget; - _VRIK.solver.rightArm.target = this.RightHandTarget; - - Type type = this.GetType(); - FieldInfo[] fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.Public); - foreach (FieldInfo fieldInfo in fieldInfos) - { - string[] propertyName = fieldInfo.Name.Split('_'); - var value = fieldInfo.GetValue(this); - if (propertyName.Count() > 1) - { - if ("Spine" == propertyName[0]) - { - SetProperty(_VRIK.solver.spine, propertyName[1], value); - } - else if ("LeftArm" == propertyName[0]) - { - SetProperty(_VRIK.solver.leftArm, propertyName[1], value); - } - else if ("RightArm" == propertyName[0]) - { - SetProperty(_VRIK.solver.rightArm, propertyName[1], value); - } - else if ("LeftLeg" == propertyName[0]) - { - SetProperty(_VRIK.solver.leftLeg, propertyName[1], value); - } - else if ("RightLeg" == propertyName[0]) - { - SetProperty(_VRIK.solver.rightLeg, propertyName[1], value); - } - else if ("Locomotion" == propertyName[0]) - { - SetProperty(_VRIK.solver.locomotion, propertyName[1], value); - } - } - } - CheckFullBodyTracking(); - } - } - - public void CheckFullBodyTracking() - { + public void CheckFullBodyTracking() + { #if PLUGIN - VRIK _VRIK = base.gameObject.GetComponent(); - if (!CustomAvatar.Plugin.IsFullBodyTracking) - { - _VRIK.solver.leftLeg.positionWeight = 0; - _VRIK.solver.leftLeg.rotationWeight = 0; - _VRIK.solver.rightLeg.positionWeight = 0; - _VRIK.solver.rightLeg.rotationWeight = 0; - } - else - { - _VRIK.solver.leftLeg.positionWeight = LeftLeg_positionWeight; - _VRIK.solver.leftLeg.rotationWeight = LeftLeg_rotationWeight; - _VRIK.solver.rightLeg.positionWeight = RightLeg_positionWeight; - _VRIK.solver.rightLeg.rotationWeight = RightLeg_rotationWeight; - } + VRIK _VRIK = base.gameObject.GetComponent(); + + _VRIK.solver.spine.pelvisPositionWeight = 0f; + _VRIK.solver.spine.pelvisRotationWeight = 0f; + _VRIK.solver.leftLeg.positionWeight = 0f; + _VRIK.solver.leftLeg.rotationWeight = 0f; + _VRIK.solver.rightLeg.positionWeight = 0f; + _VRIK.solver.rightLeg.rotationWeight = 0f; + switch (CustomAvatar.Plugin.FullBodyTrackingType) + { + case CustomAvatar.Plugin.TrackingType.Hips: + { + _VRIK.solver.spine.pelvisPositionWeight = this.Spine_pelvisPositionWeight; + _VRIK.solver.spine.pelvisRotationWeight = this.Spine_pelvisRotationWeight; + break; + } + case CustomAvatar.Plugin.TrackingType.Feet: + { + _VRIK.solver.leftLeg.positionWeight = this.LeftLeg_positionWeight; + _VRIK.solver.leftLeg.rotationWeight = this.LeftLeg_rotationWeight; + _VRIK.solver.rightLeg.positionWeight = this.RightLeg_positionWeight; + _VRIK.solver.rightLeg.rotationWeight = this.RightLeg_rotationWeight; + break; + } + case CustomAvatar.Plugin.TrackingType.Full: + { + _VRIK.solver.spine.pelvisPositionWeight = this.Spine_pelvisPositionWeight; + _VRIK.solver.spine.pelvisRotationWeight = this.Spine_pelvisRotationWeight; + _VRIK.solver.leftLeg.positionWeight = this.LeftLeg_positionWeight; + _VRIK.solver.leftLeg.rotationWeight = this.LeftLeg_rotationWeight; + _VRIK.solver.rightLeg.positionWeight = this.RightLeg_positionWeight; + _VRIK.solver.rightLeg.rotationWeight = this.RightLeg_rotationWeight; + break; + } + } #endif } public static void SetProperty(object obj, string fieldName, object value) - { - obj.GetType().GetField(fieldName).SetValue(obj, value); - } - - - public void ToggleLeftHandTarget(Transform leftHandTarget) - { - Console.WriteLine("ToggleLeftHandTarget"); - VRIK _VRIK = base.gameObject.GetComponent(); - if ("RightHand".Equals(_VRIK.solver.leftArm.target.parent.name)) - { - _VRIK.solver.leftArm.target = this.LeftHandTarget; - } - else - { - _VRIK.solver.leftArm.target = leftHandTarget; - } - } - } + { + obj.GetType().GetField(fieldName).SetValue(obj, value); + } + + + public void ToggleLeftHandTarget(Transform leftHandTarget) + { + Console.WriteLine("ToggleLeftHandTarget"); + VRIK _VRIK = base.gameObject.GetComponent(); + if ("RightHand".Equals(_VRIK.solver.leftArm.target.parent.name)) + { + _VRIK.solver.leftArm.target = this.LeftHandTarget; + } + else + { + _VRIK.solver.leftArm.target = leftHandTarget; + } + } + } } diff --git a/CustomAvatar/VRIK/IKSolverVRBodyPart.cs b/CustomAvatar/VRIK/IKSolverVRBodyPart.cs index abbae5a..bb21e2f 100644 --- a/CustomAvatar/VRIK/IKSolverVRBodyPart.cs +++ b/CustomAvatar/VRIK/IKSolverVRBodyPart.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System.Collections; using System; using AvatarScriptPack; @@ -97,4 +97,4 @@ public void Visualize() { } } } -} \ No newline at end of file +} diff --git a/CustomAvatar/VRIK/IKSolverVRFootstep.cs b/CustomAvatar/VRIK/IKSolverVRFootstep.cs index 798563e..b792666 100644 --- a/CustomAvatar/VRIK/IKSolverVRFootstep.cs +++ b/CustomAvatar/VRIK/IKSolverVRFootstep.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System.Collections; using UnityEngine.Events; diff --git a/CustomAvatar/VRIK/IKSolverVRLeg.cs b/CustomAvatar/VRIK/IKSolverVRLeg.cs index ba8bd63..84336c7 100644 --- a/CustomAvatar/VRIK/IKSolverVRLeg.cs +++ b/CustomAvatar/VRIK/IKSolverVRLeg.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System.Collections; using System; using AvatarScriptPack; @@ -257,4 +257,4 @@ public override void ResetOffsets() { } } } -} \ No newline at end of file +} diff --git a/CustomAvatar/VRIK/IKSolverVRLocomotion.cs b/CustomAvatar/VRIK/IKSolverVRLocomotion.cs index cfe795f..3e75c4b 100644 --- a/CustomAvatar/VRIK/IKSolverVRLocomotion.cs +++ b/CustomAvatar/VRIK/IKSolverVRLocomotion.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System.Collections; using UnityEngine.Events; @@ -408,4 +408,4 @@ private static bool GetLineSphereCollision(Vector3 lineStart, Vector3 lineEnd, V } } } -} \ No newline at end of file +} diff --git a/CustomAvatar/VRIK/IKSolverVRSpine.cs b/CustomAvatar/VRIK/IKSolverVRSpine.cs index 3cca42f..2dc1c24 100644 --- a/CustomAvatar/VRIK/IKSolverVRSpine.cs +++ b/CustomAvatar/VRIK/IKSolverVRSpine.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System.Collections; using System; using AvatarScriptPack; @@ -584,4 +584,4 @@ private void Bend(VirtualBone[] bones, int firstIndex, int lastIndex, Quaternion } } } -} \ No newline at end of file +} diff --git a/CustomAvatar/VRIK/IKSolverVRUtilities.cs b/CustomAvatar/VRIK/IKSolverVRUtilities.cs index 6da0d97..672fe57 100644 --- a/CustomAvatar/VRIK/IKSolverVRUtilities.cs +++ b/CustomAvatar/VRIK/IKSolverVRUtilities.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System.Collections; using System; using AvatarScriptPack; diff --git a/CustomAvatar/VRIK/Interp.cs b/CustomAvatar/VRIK/Interp.cs index b2c2d2a..31ed141 100644 --- a/CustomAvatar/VRIK/Interp.cs +++ b/CustomAvatar/VRIK/Interp.cs @@ -1,4 +1,4 @@ -using System; +using System; using UnityEngine; namespace AvatarScriptPack diff --git a/CustomAvatar/VRIK/InterpolationMode.cs b/CustomAvatar/VRIK/InterpolationMode.cs index 7b112e8..bd0f4af 100644 --- a/CustomAvatar/VRIK/InterpolationMode.cs +++ b/CustomAvatar/VRIK/InterpolationMode.cs @@ -1,4 +1,4 @@ -using System; +using System; namespace AvatarScriptPack { diff --git a/CustomAvatar/VRIK/TwistRelaxer.cs b/CustomAvatar/VRIK/TwistRelaxer.cs index 9fd95ff..9f81e26 100644 --- a/CustomAvatar/VRIK/TwistRelaxer.cs +++ b/CustomAvatar/VRIK/TwistRelaxer.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System.Collections; namespace AvatarScriptPack { @@ -10,17 +10,17 @@ public class TwistRelaxer : MonoBehaviour { public IK ik; - [Tooltip("The child transform to use as a target. If not defined, defaults to first child of current transform or next child of parent.")] - public Transform child; + [Tooltip("The child transform to use as a target. If not defined, defaults to first child of current transform or next child of parent.")] + public Transform child; - [Tooltip("The weight of relaxing the twist of this Transform")] + [Tooltip("The weight of relaxing the twist of this Transform")] [Range(0f, 1f)] public float weight = 1f; [Tooltip("If 0.5, this Transform will be twisted half way from parent to child. If 1, the twist angle will be locked to the child and will rotate with along with it.")] [Range(0f, 1f)] public float parentChildCrossfade = 0.5f; - [Tooltip("Rotation offset around the twist axis.")] - [Range(-180f, 180f)] public float twistAngleOffset; + [Tooltip("Rotation offset around the twist axis.")] + [Range(-180f, 180f)] public float twistAngleOffset; /// /// Rotate this Transform to relax it's twist angle relative to the "parent" and "child" Transforms. @@ -28,9 +28,9 @@ public class TwistRelaxer : MonoBehaviour { public void Relax() { if (weight <= 0f) return; // Nothing to do here - Quaternion rotation = transform.rotation; - Quaternion twistOffset = Quaternion.AngleAxis(twistAngleOffset, rotation * twistAxis); - rotation = twistOffset * rotation; + Quaternion rotation = transform.rotation; + Quaternion twistOffset = Quaternion.AngleAxis(twistAngleOffset, rotation * twistAxis); + rotation = twistOffset * rotation; // Find the world space relaxed axes of the parent and child Vector3 relaxedAxisParent = twistOffset * parent.rotation * axisRelativeToParentDefault; @@ -65,24 +65,24 @@ void Start() { parent = transform.parent; if (child == null) - { - if (transform.childCount == 0) - { - var children = parent.GetComponentsInChildren(); - for (int i = 0; i < children.Length; i++) - { - if (children[i] != transform) - { - child = children[i]; - break; - } - } - } - else - { - child = transform.GetChild(0); - } - } + { + if (transform.childCount == 0) + { + var children = parent.GetComponentsInChildren(); + for (int i = 0; i < children.Length; i++) + { + if (children[i] != transform) + { + child = children[i]; + break; + } + } + } + else + { + child = transform.GetChild(0); + } + } twistAxis = transform.InverseTransformDirection(child.position - transform.position); axis = new Vector3(twistAxis.y, twistAxis.z, twistAxis.x); diff --git a/CustomAvatar/VRIK/V3Tools.cs b/CustomAvatar/VRIK/V3Tools.cs index 64afc90..7f55237 100644 --- a/CustomAvatar/VRIK/V3Tools.cs +++ b/CustomAvatar/VRIK/V3Tools.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System.Collections; namespace AvatarScriptPack diff --git a/CustomAvatar/VRIK/VRIK.cs b/CustomAvatar/VRIK/VRIK.cs index 7de867d..6eaaed5 100644 --- a/CustomAvatar/VRIK/VRIK.cs +++ b/CustomAvatar/VRIK/VRIK.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System.Collections; using System.Collections.Generic; @@ -118,7 +118,7 @@ public static bool AutoDetectReferences(Transform root, out References reference references = new References(); var animator = root.GetComponentInChildren(); - Debug.Log("Root: " + root + " " + root.name); + Debug.Log("Root: " + root + " " + root.name); if (animator == null || !animator.isHuman) { Debug.LogWarning("VRIK needs a Humanoid Animator to auto-detect biped references. Please assign references manually."); return false; diff --git a/CustomAvatar/VRIK/Warning.cs b/CustomAvatar/VRIK/Warning.cs index 770943d..2930983 100644 --- a/CustomAvatar/VRIK/Warning.cs +++ b/CustomAvatar/VRIK/Warning.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System.Collections; namespace AvatarScriptPack { From 8d32fd5e41ea32c959ed4b742d94e7b85c77b1dd Mon Sep 17 00:00:00 2001 From: Assistant Date: Fri, 11 Jan 2019 19:20:11 -0400 Subject: [PATCH 5/8] FBT fix (?) --- CustomAvatar/PlayerAvatarManager.cs | 5 +++ CustomAvatar/Plugin.cs | 2 +- CustomAvatar/VRIK/IKManagerAdvanced.cs | 50 ++++++++++++++++++++++---- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/CustomAvatar/PlayerAvatarManager.cs b/CustomAvatar/PlayerAvatarManager.cs index 6164ee9..f55b630 100644 --- a/CustomAvatar/PlayerAvatarManager.cs +++ b/CustomAvatar/PlayerAvatarManager.cs @@ -51,6 +51,11 @@ public CustomAvatar GetCurrentAvatar() return CurrentPlayerAvatar; } + public SpawnedAvatar GetSpawnedAvatar() + { + return _currentSpawnedPlayerAvatar; + } + public void SwitchToAvatar(CustomAvatar customAvatar) { CurrentPlayerAvatar = customAvatar; diff --git a/CustomAvatar/Plugin.cs b/CustomAvatar/Plugin.cs index d10abc4..9916437 100644 --- a/CustomAvatar/Plugin.cs +++ b/CustomAvatar/Plugin.cs @@ -65,7 +65,7 @@ public static bool IsFullBodyTracking Plugin.FullBodyTrackingType = (Plugin.TrackingType)Plugin.Trackers.Count; else Plugin.FullBodyTrackingType = Plugin.TrackingType.None; - var currentAvatar = Instance.PlayerAvatarManager.GetCurrentAvatar(); + var currentAvatar = Instance.PlayerAvatarManager.GetSpawnedAvatar(); if (currentAvatar != null) { var _IKManagerAdvanced = currentAvatar.GameObject.GetComponentInChildren(true); diff --git a/CustomAvatar/VRIK/IKManagerAdvanced.cs b/CustomAvatar/VRIK/IKManagerAdvanced.cs index e58ab15..69619c1 100644 --- a/CustomAvatar/VRIK/IKManagerAdvanced.cs +++ b/CustomAvatar/VRIK/IKManagerAdvanced.cs @@ -273,27 +273,65 @@ public void Start() { if ("Spine" == propertyName[0]) { - SetProperty(_VRIK.solver.spine, propertyName[1], value); + try + { + SetProperty(_VRIK.solver.spine, propertyName[1], value); + } catch (Exception e) { + Console.WriteLine("Cannot SetProperty: Spine"); + } } else if ("LeftArm" == propertyName[0]) { - SetProperty(_VRIK.solver.leftArm, propertyName[1], value); + try { + SetProperty(_VRIK.solver.leftArm, propertyName[1], value); + } catch (Exception e) + { + Console.WriteLine("Cannot SetProperty: LeftArm"); + } } else if ("RightArm" == propertyName[0]) { - SetProperty(_VRIK.solver.rightArm, propertyName[1], value); + try + { + SetProperty(_VRIK.solver.rightArm, propertyName[1], value); + } + catch (Exception e) + { + Console.WriteLine("Cannot SetProperty: RightArm"); + } } else if ("LeftLeg" == propertyName[0]) { - SetProperty(_VRIK.solver.leftLeg, propertyName[1], value); + try + { + SetProperty(_VRIK.solver.leftLeg, propertyName[1], value); + } + catch (Exception e) + { + Console.WriteLine("Cannot SetProperty: LeftLeg"); + } } else if ("RightLeg" == propertyName[0]) { - SetProperty(_VRIK.solver.rightLeg, propertyName[1], value); + try + { + SetProperty(_VRIK.solver.rightLeg, propertyName[1], value); + } + catch (Exception e) + { + Console.WriteLine("Cannot SetProperty: RightLeg"); + } } else if ("Locomotion" == propertyName[0]) { - SetProperty(_VRIK.solver.locomotion, propertyName[1], value); + try + { + SetProperty(_VRIK.solver.locomotion, propertyName[1], value); + } + catch (Exception e) + { + Console.WriteLine("Cannot SetProperty: Locomotion"); + } } } } From 22fc2fcd5b6b425a6b0b8df078f4640375b46bc6 Mon Sep 17 00:00:00 2001 From: Assistant Date: Tue, 15 Jan 2019 01:51:13 -0400 Subject: [PATCH 6/8] Cosmetic changes to the IK Manager Advanced --- CustomAvatar/VRIK/IKManagerAdvanced.cs | 48 ++++++++++++-------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/CustomAvatar/VRIK/IKManagerAdvanced.cs b/CustomAvatar/VRIK/IKManagerAdvanced.cs index 69619c1..7d94f40 100644 --- a/CustomAvatar/VRIK/IKManagerAdvanced.cs +++ b/CustomAvatar/VRIK/IKManagerAdvanced.cs @@ -11,21 +11,37 @@ namespace AvatarScriptPack { class IKManagerAdvanced : MonoBehaviour { + [Space(5)] + [Header("IK Targets")] [Tooltip("The head target.")] public Transform HeadTarget; - [Tooltip("The hand target")] + [Tooltip("The hand target.")] public Transform LeftHandTarget; - [Tooltip("The hand target")] + [Tooltip("The hand target.")] public Transform RightHandTarget; - [Space(20)] + [Space(5)] + [Header("Full Body Tracking")] + [Tooltip("The pelvis target, useful with seated rigs.")] + public Transform Spine_pelvisTarget; + [Range(0f, 1f), Tooltip("Positional weight of the pelvis target.")] + public float Spine_pelvisPositionWeight; + [Range(0f, 1f), Tooltip("Rotational weight of the pelvis target.")] + public float Spine_pelvisRotationWeight; [Tooltip("The toe/foot target.")] public Transform LeftLeg_target; + [Range(0f, 1f), Tooltip("Positional weight of the toe/foot target.")] + public float LeftLeg_positionWeight; + [Range(0f, 1f), Tooltip("Rotational weight of the toe/foot target.")] + public float LeftLeg_rotationWeight; + [Tooltip("The toe/foot target.")] public Transform RightLeg_target; - [Tooltip("The pelvis target, useful with seated rigs.")] - public Transform Spine_pelvisTarget; + [Range(0f, 1f), Tooltip("Positional weight of the toe/foot target.")] + public float RightLeg_positionWeight; + [Range(0f, 1f), Tooltip("Rotational weight of the toe/foot target.")] + public float RightLeg_rotationWeight; [Space(20)] @@ -34,16 +50,10 @@ class IKManagerAdvanced : MonoBehaviour ******************/ [Range(0f, 1f), Tooltip("Positional weight of the head target.")] - public float Spine_positionWeight = 1f; + public float Head_positionWeight = 1f; [Range(0f, 1f), Tooltip("Rotational weight of the head target.")] - public float Spine_rotationWeight = 1f; - - [Range(0f, 1f), Tooltip("Positional weight of the pelvis target.")] - public float Spine_pelvisPositionWeight; - - [Range(0f, 1f), Tooltip("Rotational weight of the pelvis target.")] - public float Spine_pelvisRotationWeight; + public float Head_rotationWeight = 1f; [Tooltip("If 'Chest Goal Weight' is greater than 0, the chest will be turned towards this Transform.")] public Transform Spine_chestGoal; @@ -153,12 +163,6 @@ class IKManagerAdvanced : MonoBehaviour [Tooltip("The knee will be bent towards this Transform if 'Bend Goal Weight' > 0.")] public Transform LeftLeg_bendGoal; - [Range(0f, 1f), Tooltip("Positional weight of the toe/foot target.")] - public float LeftLeg_positionWeight; - - [Range(0f, 1f), Tooltip("Rotational weight of the toe/foot target.")] - public float LeftLeg_rotationWeight; - [Range(0f, 1f), Tooltip("If greater than 0, will bend the knee towards the 'Bend Goal' Transform.")] public float LeftLeg_bendGoalWeight; @@ -178,12 +182,6 @@ class IKManagerAdvanced : MonoBehaviour [Tooltip("The knee will be bent towards this Transform if 'Bend Goal Weight' > 0.")] public Transform RightLeg_bendGoal; - [Range(0f, 1f), Tooltip("Positional weight of the toe/foot target.")] - public float RightLeg_positionWeight; - - [Range(0f, 1f), Tooltip("Rotational weight of the toe/foot target.")] - public float RightLeg_rotationWeight; - [Range(0f, 1f), Tooltip("If greater than 0, will bend the knee towards the 'Bend Goal' Transform.")] public float RightLeg_bendGoalWeight; From 8c71ee4a33871537b55be76f3282ea4c6394c117 Mon Sep 17 00:00:00 2001 From: Assistant Date: Tue, 15 Jan 2019 01:53:54 -0400 Subject: [PATCH 7/8] Updated version to 4.5.0 --- CustomAvatar/Plugin.cs | 2 +- CustomAvatar/Properties/AssemblyInfo.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CustomAvatar/Plugin.cs b/CustomAvatar/Plugin.cs index 9916437..367c538 100644 --- a/CustomAvatar/Plugin.cs +++ b/CustomAvatar/Plugin.cs @@ -158,7 +158,7 @@ public string Name public string Version { - get { return "4.4.0"; } + get { return "4.5.0"; } } public static void Log(object message) diff --git a/CustomAvatar/Properties/AssemblyInfo.cs b/CustomAvatar/Properties/AssemblyInfo.cs index e8fac59..5cc1286 100644 --- a/CustomAvatar/Properties/AssemblyInfo.cs +++ b/CustomAvatar/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("4.4.0.0")] -[assembly: AssemblyFileVersion("4.4.0.0")] +[assembly: AssemblyVersion("4.5.0.0")] +[assembly: AssemblyFileVersion("4.5.0.0")] From c609d7811ae313b5bc84e5cc48c30e4f144e6830 Mon Sep 17 00:00:00 2001 From: Assistant Date: Thu, 17 Jan 2019 18:08:27 -0400 Subject: [PATCH 8/8] Removed useless logging. --- CustomAvatar/VRIK/IKManagerAdvanced.cs | 30 +++++++------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/CustomAvatar/VRIK/IKManagerAdvanced.cs b/CustomAvatar/VRIK/IKManagerAdvanced.cs index 7d94f40..31ded60 100644 --- a/CustomAvatar/VRIK/IKManagerAdvanced.cs +++ b/CustomAvatar/VRIK/IKManagerAdvanced.cs @@ -274,18 +274,16 @@ public void Start() try { SetProperty(_VRIK.solver.spine, propertyName[1], value); - } catch (Exception e) { - Console.WriteLine("Cannot SetProperty: Spine"); } + catch { } } else if ("LeftArm" == propertyName[0]) { - try { - SetProperty(_VRIK.solver.leftArm, propertyName[1], value); - } catch (Exception e) + try { - Console.WriteLine("Cannot SetProperty: LeftArm"); + SetProperty(_VRIK.solver.leftArm, propertyName[1], value); } + catch { } } else if ("RightArm" == propertyName[0]) { @@ -293,10 +291,7 @@ public void Start() { SetProperty(_VRIK.solver.rightArm, propertyName[1], value); } - catch (Exception e) - { - Console.WriteLine("Cannot SetProperty: RightArm"); - } + catch { } } else if ("LeftLeg" == propertyName[0]) { @@ -304,10 +299,7 @@ public void Start() { SetProperty(_VRIK.solver.leftLeg, propertyName[1], value); } - catch (Exception e) - { - Console.WriteLine("Cannot SetProperty: LeftLeg"); - } + catch { } } else if ("RightLeg" == propertyName[0]) { @@ -315,10 +307,7 @@ public void Start() { SetProperty(_VRIK.solver.rightLeg, propertyName[1], value); } - catch (Exception e) - { - Console.WriteLine("Cannot SetProperty: RightLeg"); - } + catch { } } else if ("Locomotion" == propertyName[0]) { @@ -326,10 +315,7 @@ public void Start() { SetProperty(_VRIK.solver.locomotion, propertyName[1], value); } - catch (Exception e) - { - Console.WriteLine("Cannot SetProperty: Locomotion"); - } + catch { } } } }