Skip to content

Commit

Permalink
resizing algorithm modification / simple player size scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
Ibodan committed Aug 11, 2018
1 parent f49d476 commit cebb6fe
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
22 changes: 22 additions & 0 deletions CustomAvatar/AvatarMeasurement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,27 @@ public static float MeasureHeight(GameObject avatarGameObject, Transform viewPoi

return Mathf.Clamp(height, MinHeight, MaxHeight);
}

public static float? MeasureHandToHand(GameObject avatarGameObject)
{
var animator = avatarGameObject.GetComponentInChildren<Animator>();
if (animator == null || animator.avatar == null || !animator.isHuman || !animator.enabled)
{
Plugin.Log("animator for human not found");
return null;
}

var leftShoulder = animator.GetBoneTransform(HumanBodyBones.LeftUpperArm).position;
var rightShoulder = animator.GetBoneTransform(HumanBodyBones.RightUpperArm).position;
var leftHand = Vector3.Lerp(
animator.GetBoneTransform(HumanBodyBones.LeftHand).position,
animator.GetBoneTransform(HumanBodyBones.LeftMiddleProximal).position, 0.8f);
var rightHand = Vector3.Lerp(
animator.GetBoneTransform(HumanBodyBones.RightHand).position,
animator.GetBoneTransform(HumanBodyBones.RightMiddleProximal).position, 0.8f);
return Vector3.Distance(leftHand, leftShoulder)
+ Vector3.Distance(leftShoulder, rightShoulder)
+ Vector3.Distance(rightHand, rightShoulder);
}
}
}
77 changes: 70 additions & 7 deletions CustomAvatar/PlayerAvatarManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,81 @@ private void SceneManagerOnActiveSceneChanged(Scene oldScene, Scene newScene)
ResizePlayerAvatar();
}

protected static string PlayerHandToHandKey = "AvatarAutoFitting.PlayerHandToHand";
protected static string PlayerViewPortYKey = "AvatarAutoFitting.PlayerViewPortY";

private void ResizePlayerAvatar()
{
if (_currentSpawnedPlayerAvatar?.GameObject == null) return;
if (!_currentSpawnedPlayerAvatar.CustomAvatar.AllowHeightCalibration) return;

var playerHeight = BeatSaberUtil.GetPlayerHeight();
if (playerHeight == _prevPlayerHeight) return;
_prevPlayerHeight = playerHeight;
_currentSpawnedPlayerAvatar.GameObject.transform.localScale =
_startAvatarLocalScale * (playerHeight / _currentSpawnedPlayerAvatar.CustomAvatar.Height);
Plugin.Log("Resizing avatar to " + (playerHeight / _currentSpawnedPlayerAvatar.CustomAvatar.Height) +
"x scale");
float PlayerHandToHand = PlayerPrefs.GetFloat(PlayerHandToHandKey, 1.5f);
float PlayerViewPointY = PlayerPrefs.GetFloat(PlayerViewPortYKey, 1.5f);

var avatarHandToHand = AvatarMeasurement.MeasureHandToHand(_currentSpawnedPlayerAvatar.GameObject) ?? PlayerHandToHand;
Plugin.Log("HandToHand: " + avatarHandToHand);

var avatarViewPointY = _currentSpawnedPlayerAvatar.CustomAvatar.ViewPoint?.position.y ?? PlayerViewPointY;

// fbx root gameobject
var animator = _currentSpawnedPlayerAvatar.GameObject.GetComponentInChildren<Animator>();
if (animator == null) { Plugin.Log("Animator not found"); return; }

// scale
var scale = PlayerHandToHand / avatarHandToHand;
_currentSpawnedPlayerAvatar.GameObject.transform.localScale = _startAvatarLocalScale * scale;

// translate root for floor level
const float FloorLevelOffset = 0.04f; // a heuristic value from testing on oculus rift
var offset = (PlayerViewPointY - (avatarViewPointY * scale)) + FloorLevelOffset;
animator.transform.Translate(Vector3.up * offset);

Plugin.Log("Avatar fitted with scale: " + scale + " yoffset: " + offset);
}

public void MeasurePlayerSize()
{
var active = SceneManager.GetActiveScene().GetRootGameObjects()[0].GetComponent<PlayerSizeScanner>();
if (active != null)
{
GameObject.Destroy(active);
}
SceneManager.GetActiveScene().GetRootGameObjects()[0].AddComponent<PlayerSizeScanner>();
}

private class PlayerSizeScanner : MonoBehaviour
{
private PlayerAvatarInput playerInput = new PlayerAvatarInput();
private const float initialValue = 0.5f;
private float maxViewPointY = initialValue;
private float maxHandToHandLength = initialValue;
private int scanCount = 0;

void Scan()
{
maxViewPointY = Math.Max(maxViewPointY, playerInput.HeadPosRot.Position.y);
maxHandToHandLength = Math.Max(maxHandToHandLength, Vector3.Distance(playerInput.LeftPosRot.Position, playerInput.RightPosRot.Position));
if (scanCount++ > 15)
{
Plugin.Log("Scanning finished. viewporty: " + maxViewPointY + " handtohand: " + maxHandToHandLength);
if (maxViewPointY > initialValue && maxHandToHandLength > initialValue)
{
PlayerPrefs.SetFloat(PlayerViewPortYKey, maxViewPointY);
PlayerPrefs.SetFloat(PlayerHandToHandKey, maxHandToHandLength);
PlayerPrefs.Save();
}
Destroy(this);
}
}
void Start()
{
Plugin.Log("PlayerSizeScanner starts scannig");
InvokeRepeating("Scan", 1.0f, 0.2f);
}
void OnDestroy()
{
CancelInvoke();
}
}
}
}
4 changes: 4 additions & 0 deletions CustomAvatar/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ public void OnUpdate()
{
FirstPersonEnabled = !FirstPersonEnabled;
}
else if (Input.GetKeyDown(KeyCode.End))
{
PlayerAvatarManager.MeasurePlayerSize();
}
}

private static void SetCameraCullingMask()
Expand Down

0 comments on commit cebb6fe

Please sign in to comment.