From efb81b64920a6e6d5c2f71da77cf84b3ef66c8d7 Mon Sep 17 00:00:00 2001 From: Nicolas Gnyra Date: Sun, 2 Feb 2020 18:33:06 -0500 Subject: [PATCH] Fix some issues with resizing/trackers --- Source/CustomAvatar/AvatarBehaviour.cs | 61 ++++++++++++++++---------- Source/CustomAvatar/AvatarManager.cs | 8 ++-- Source/CustomAvatar/SpawnedAvatar.cs | 6 ++- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/Source/CustomAvatar/AvatarBehaviour.cs b/Source/CustomAvatar/AvatarBehaviour.cs index 46afbe47..006f875b 100644 --- a/Source/CustomAvatar/AvatarBehaviour.cs +++ b/Source/CustomAvatar/AvatarBehaviour.cs @@ -32,6 +32,7 @@ public float scale } public AvatarInput input; + public CustomAvatar customAvatar; private Vector3 _initialPosition; private Vector3 _initialScale; @@ -44,6 +45,10 @@ public float scale private Transform _rightLeg; private Transform _pelvis; + private Pose _initialPelvisPose; + private Pose _initialLeftFootPose; + private Pose _initialRightFootPose; + private Vector3 _prevBodyPos = Vector3.zero; private Vector3 _prevPelvisPos = Vector3.zero; private Vector3 _prevLeftLegPos = Vector3.zero; @@ -80,19 +85,14 @@ private void Awake() _preUpdateDelegate = typeof(BeatSaberDynamicBone::DynamicBone).CreatePrivateMethodDelegate>("PreUpdate"); _updateDynamicBonesDelegate = typeof(BeatSaberDynamicBone::DynamicBone).CreatePrivateMethodDelegate>("UpdateDynamicBones"); - _initialPosition = transform.position; - _initialScale = transform.localScale; + _initialPosition = transform.position; + _initialScale = transform.localScale; _leftHandAnimAction = new SkeletalInput("/actions/customavatars/in/lefthandanim"); _rightHandAnimAction = new SkeletalInput("/actions/customavatars/in/righthandanim"); _dynamicBones = GetComponentsInChildren(); - if (input == null) - { - input = new VRAvatarInput(); - } - foreach (TwistRelaxer twistRelaxer in GetComponentsInChildren()) { twistRelaxer.enabled = false; @@ -101,6 +101,18 @@ private void Awake() private void Start() { + if (input == null) + { + Destroy(this); + throw new ArgumentNullException(nameof(input)); + } + + if (customAvatar == null) + { + Destroy(this); + throw new ArgumentNullException(nameof(customAvatar)); + } + foreach (VRIK vrik in GetComponentsInChildren()) { Destroy(vrik); @@ -132,6 +144,10 @@ private void Start() _rightLeg = transform.Find("RightLeg"); _pelvis = transform.Find("Pelvis"); + _initialPelvisPose = new Pose(_pelvis.position, _pelvis.rotation); + _initialLeftFootPose = new Pose(_leftLeg.position, _leftLeg.rotation); + _initialRightFootPose = new Pose(_rightLeg.position, _rightLeg.rotation); + SetVrikReferences(); foreach (FirstPersonExclusion firstPersonExclusion in GetComponentsInChildren()) @@ -180,21 +196,11 @@ private void LateUpdate() _vrPlatformHelper.AdjustPlatformSpecificControllerTransform(XRNode.LeftHand, _leftHand, controllerPositionOffset, controllerRotationOffset); } - float playerEyeHeight = BeatSaberUtil.GetPlayerEyeHeight(); - float feetOffset = playerEyeHeight - position.y; - - if (SettingsManager.settings.moveFloorWithRoomAdjust) - { - feetOffset += BeatSaberUtil.GetRoomCenter().y; - } - - float positionScale = feetOffset / playerEyeHeight; - if (_leftLeg && input.TryGetLeftFootPose(out Pose leftFootPose)) { Pose correction = SettingsManager.settings.fullBodyCalibration.leftLeg; - _prevLeftLegPos = Vector3.Lerp(_prevLeftLegPos, AdjustTransformPosition(leftFootPose.position, correction.position, positionScale), SettingsManager.settings.fullBodyMotionSmoothing.feet.position * Time.deltaTime); + _prevLeftLegPos = Vector3.Lerp(_prevLeftLegPos, AdjustTransformPosition(leftFootPose.position, correction.position, _initialLeftFootPose.position), SettingsManager.settings.fullBodyMotionSmoothing.feet.position * Time.deltaTime); _prevLeftLegRot = Quaternion.Slerp(_prevLeftLegRot, leftFootPose.rotation * correction.rotation, SettingsManager.settings.fullBodyMotionSmoothing.feet.rotation * Time.deltaTime); _leftLeg.position = _prevLeftLegPos; _leftLeg.rotation = _prevLeftLegRot; @@ -204,7 +210,7 @@ private void LateUpdate() { Pose correction = SettingsManager.settings.fullBodyCalibration.rightLeg; - _prevRightLegPos = Vector3.Lerp(_prevRightLegPos, AdjustTransformPosition(rightFootPose.position, correction.position, positionScale), SettingsManager.settings.fullBodyMotionSmoothing.feet.position * Time.deltaTime); + _prevRightLegPos = Vector3.Lerp(_prevRightLegPos, AdjustTransformPosition(rightFootPose.position, correction.position, _initialRightFootPose.position), SettingsManager.settings.fullBodyMotionSmoothing.feet.position * Time.deltaTime); _prevRightLegRot = Quaternion.Slerp(_prevRightLegRot, rightFootPose.rotation * correction.rotation, SettingsManager.settings.fullBodyMotionSmoothing.feet.rotation * Time.deltaTime); _rightLeg.position = _prevRightLegPos; _rightLeg.rotation = _prevRightLegRot; @@ -214,7 +220,7 @@ private void LateUpdate() { Pose correction = SettingsManager.settings.fullBodyCalibration.rightLeg; - _prevPelvisPos = Vector3.Lerp(_prevPelvisPos, AdjustTransformPosition(pelvisPose.position, correction.position, positionScale), SettingsManager.settings.fullBodyMotionSmoothing.waist.position * Time.deltaTime); + _prevPelvisPos = Vector3.Lerp(_prevPelvisPos, AdjustTransformPosition(pelvisPose.position, correction.position, _initialPelvisPose.position), SettingsManager.settings.fullBodyMotionSmoothing.waist.position * Time.deltaTime); _prevPelvisRot = Quaternion.Slerp(_prevPelvisRot, pelvisPose.rotation * correction.rotation, SettingsManager.settings.fullBodyMotionSmoothing.waist.rotation * Time.deltaTime); _pelvis.position = _prevPelvisPos; _pelvis.rotation = _prevPelvisRot; @@ -263,8 +269,8 @@ private void OnDestroy() { input.inputChanged -= OnInputChanged; - _leftHandAnimAction.Dispose(); - _rightHandAnimAction.Dispose(); + _leftHandAnimAction?.Dispose(); + _rightHandAnimAction?.Dispose(); } // ReSharper restore UnusedMember.Local @@ -343,10 +349,17 @@ private void SetVrikReferences() UpdateVrikReferences(); } - private Vector3 AdjustTransformPosition(Vector3 original, Vector3 correction, float scale) + private Vector3 AdjustTransformPosition(Vector3 original, Vector3 correction, Vector3 originalPosition) { Vector3 corrected = original + correction; - return new Vector3(corrected.x, corrected.y * scale, corrected.z); + float y = position.y; + + if (SettingsManager.settings.moveFloorWithRoomAdjust) + { + y -= BeatSaberUtil.GetRoomCenter().y; + } + + return new Vector3(corrected.x, corrected.y + (1 - originalPosition.y / customAvatar.eyeHeight) * y, corrected.z); } private void UpdateVrikReferences() diff --git a/Source/CustomAvatar/AvatarManager.cs b/Source/CustomAvatar/AvatarManager.cs index 9e1f2b01..72d58969 100644 --- a/Source/CustomAvatar/AvatarManager.cs +++ b/Source/CustomAvatar/AvatarManager.cs @@ -144,6 +144,8 @@ private void OnSceneLoaded(Scene newScene, LoadSceneMode mode) { avatarTailor.CalibrateFullBodyTracking(); } + + ResizeCurrentAvatar(); } private void OnSceneTransitionDidFinish(ScenesTransitionSetupDataSO setupData, DiContainer container) @@ -159,6 +161,8 @@ private void OnSceneTransitionDidFinish(ScenesTransitionSetupDataSO setupData, D { currentlySpawnedAvatar.eventsPlayer.MenuEnteredEvent(); } + + ResizeCurrentAvatar(); } private static SpawnedAvatar SpawnAvatar(CustomAvatar customAvatar, AvatarInput input) @@ -166,9 +170,7 @@ private static SpawnedAvatar SpawnAvatar(CustomAvatar customAvatar, AvatarInput if (customAvatar == null) throw new ArgumentNullException(nameof(customAvatar)); if (input == null) throw new ArgumentNullException(nameof(input)); - var spawnedAvatar = new SpawnedAvatar(customAvatar); - - spawnedAvatar.behaviour.input = input; + var spawnedAvatar = new SpawnedAvatar(customAvatar, input); return spawnedAvatar; } diff --git a/Source/CustomAvatar/SpawnedAvatar.cs b/Source/CustomAvatar/SpawnedAvatar.cs index e3be6e82..05cd7727 100644 --- a/Source/CustomAvatar/SpawnedAvatar.cs +++ b/Source/CustomAvatar/SpawnedAvatar.cs @@ -1,4 +1,5 @@ using System; +using CustomAvatar.Tracking; using CustomAvatar.Utilities; using UnityEngine; using Object = UnityEngine.Object; @@ -13,7 +14,7 @@ internal class SpawnedAvatar private readonly GameObject _gameObject; - public SpawnedAvatar(CustomAvatar customAvatar) + public SpawnedAvatar(CustomAvatar customAvatar, AvatarInput input) { this.customAvatar = customAvatar ?? throw new ArgumentNullException(nameof(customAvatar)); _gameObject = Object.Instantiate(customAvatar.gameObject); @@ -21,6 +22,9 @@ public SpawnedAvatar(CustomAvatar customAvatar) eventsPlayer = _gameObject.AddComponent(); behaviour = _gameObject.AddComponent(); + behaviour.customAvatar = customAvatar; + behaviour.input = input; + Object.DontDestroyOnLoad(_gameObject); }