Skip to content

Commit

Permalink
Fix some issues with resizing/trackers
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoco007 committed Feb 2, 2020
1 parent 055a4dc commit efb81b6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
61 changes: 37 additions & 24 deletions Source/CustomAvatar/AvatarBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public float scale
}

public AvatarInput input;
public CustomAvatar customAvatar;

private Vector3 _initialPosition;
private Vector3 _initialScale;
Expand All @@ -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;
Expand Down Expand Up @@ -80,19 +85,14 @@ private void Awake()
_preUpdateDelegate = typeof(BeatSaberDynamicBone::DynamicBone).CreatePrivateMethodDelegate<Action<BeatSaberDynamicBone::DynamicBone>>("PreUpdate");
_updateDynamicBonesDelegate = typeof(BeatSaberDynamicBone::DynamicBone).CreatePrivateMethodDelegate<Action<BeatSaberDynamicBone::DynamicBone, float>>("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<BeatSaberDynamicBone::DynamicBone>();

if (input == null)
{
input = new VRAvatarInput();
}

foreach (TwistRelaxer twistRelaxer in GetComponentsInChildren<TwistRelaxer>())
{
twistRelaxer.enabled = false;
Expand All @@ -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<VRIK>())
{
Destroy(vrik);
Expand Down Expand Up @@ -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<FirstPersonExclusion>())
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -263,8 +269,8 @@ private void OnDestroy()
{
input.inputChanged -= OnInputChanged;

_leftHandAnimAction.Dispose();
_rightHandAnimAction.Dispose();
_leftHandAnimAction?.Dispose();
_rightHandAnimAction?.Dispose();
}

// ReSharper restore UnusedMember.Local
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 5 additions & 3 deletions Source/CustomAvatar/AvatarManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ private void OnSceneLoaded(Scene newScene, LoadSceneMode mode)
{
avatarTailor.CalibrateFullBodyTracking();
}

ResizeCurrentAvatar();
}

private void OnSceneTransitionDidFinish(ScenesTransitionSetupDataSO setupData, DiContainer container)
Expand All @@ -159,16 +161,16 @@ private void OnSceneTransitionDidFinish(ScenesTransitionSetupDataSO setupData, D
{
currentlySpawnedAvatar.eventsPlayer.MenuEnteredEvent();
}

ResizeCurrentAvatar();
}

private static SpawnedAvatar SpawnAvatar(CustomAvatar customAvatar, AvatarInput input)
{
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;
}
Expand Down
6 changes: 5 additions & 1 deletion Source/CustomAvatar/SpawnedAvatar.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CustomAvatar.Tracking;
using CustomAvatar.Utilities;
using UnityEngine;
using Object = UnityEngine.Object;
Expand All @@ -13,14 +14,17 @@ 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);

eventsPlayer = _gameObject.AddComponent<AvatarEventsPlayer>();
behaviour = _gameObject.AddComponent<AvatarBehaviour>();

behaviour.customAvatar = customAvatar;
behaviour.input = input;

Object.DontDestroyOnLoad(_gameObject);
}

Expand Down

0 comments on commit efb81b6

Please sign in to comment.