Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 77 additions & 20 deletions Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,21 @@ public class VRHeroBody : MonoBehaviour
// Knocked out / dead / asleep body and camera (Accessibility option heroFallMode).
private VrHeroDownPose _downPose;

// The eye height of the sneak pose at scale 1 (S_SNEAK, measured once per built body), -1 = not yet.
// The eye height of the sneak pose (S_SNEAK, 1.40 m in G1/G2) and the kneel pose (S_CHESTSMALL_S0, 1.08 m)
// at scale 1, measured once per built body, -1 = not yet.
private float _sneakEyeHeight = -1f;
private float _kneelEyeHeight = -1f;
// Feet and toes - the floor under the sneak pose (the arms can reach lower).
private readonly List<Transform> _footBones = new();
private const string _sneakAnimation = "S_SNEAK";
// Crouched and standing still for this long: the hero kneels (the camera goes down to the kneel pose's eyes,
// e.g. at a chest), moving takes him back to the sneak pose. Short stops while walking don't bob the camera.
private const float _kneelDelay = 1f;
// Kneeling ends after moving this long without a break: the thumbstick does that at once, a push of the body
// capsule (the head leant into the chest) doesn't take the camera up 30 cm at the lock.
private const float _standUpDelay = 0.15f;
private float _lastCrouchMoveTime = float.NegativeInfinity;
private float _movingSince = -1f;
private bool _isKneeling;

private SkinnedMeshRenderer _bodyRenderer;

Expand Down Expand Up @@ -246,7 +256,8 @@ private void LateUpdate()
new Vector3(head.position.x, feetY, head.position.z) - forward * (_torsoBackOffset * scale),
Quaternion.LookRotation(forward));

_locomotion.Update(hero, _body.transform, rig, scale, isCrouching);
UpdateKneeling(isCrouching);
_locomotion.Update(hero, _body.transform, rig, scale, isCrouching, _isKneeling);
UpdateCrouchHeight(rig, scale);

// The neck right under the goggles (and a bit behind them), whatever the model's root/bone layout or
Expand All @@ -261,36 +272,81 @@ private void LateUpdate()
}

/// <summary>
/// The crouch button takes the camera to the eyes of the sneak pose (S_SNEAK): lower, the neck under the
/// goggles pushed the body's feet into the floor. Measured from the body's own root once the pose is fully
/// blended in - whatever the camera does meanwhile. The first crouch of a body starts at the rig's own height.
/// Crouched without moving for _kneelDelay: the hero kneels (VrHeroLocomotion.KneelAnimation). Head movement
/// doesn't count (the body capsule follows the head without moving), only the thumbstick/teleport, and while
/// kneeling only after _standUpDelay of it.
/// </summary>
private void UpdateKneeling(bool isCrouching)
{
if (_locomotion.IsMoving)
{
if (_movingSince < 0f)
_movingSince = Time.time;
}
else
{
_movingSince = -1f;
}

var isStandingUp = _movingSince >= 0f && (!_isKneeling || Time.time - _movingSince >= _standUpDelay);
if (!isCrouching || isStandingUp)
_lastCrouchMoveTime = Time.time;

var isKneeling = Time.time - _lastCrouchMoveTime >= _kneelDelay;
if (isKneeling == _isKneeling)
return;

_isKneeling = isKneeling;
Logger.Log(isKneeling
? "[VRHeroBody] Crouched and still - the hero kneels."
: "[VRHeroBody] Crouch ended or moving - the hero sneaks/stands.", LogCat.VR);
}

/// <summary>
/// The crouch button takes the camera to the eyes of the body's pose: the sneak pose (S_SNEAK) while moving,
/// the kneel pose (S_CHESTSMALL_S0, e.g. at a chest) while standing still - lower, the neck under the goggles
/// pushed the body's feet into the floor. Each measured from the body's own root once the pose is fully
/// blended in - whatever the camera does meanwhile. The first crouch of a body starts at the rig's own height,
/// the first kneel at the sneak pose's.
/// </summary>
private void UpdateCrouchHeight(IVrRig rig, float scale)
{
if (_headBone == null || _animator == null || !_animator.IsValid)
return;

if (_sneakEyeHeight < 0f && _animator.IsPlaying(_sneakAnimation) && _footBones.Count > 0)
{
// Like _modelHeadHeight: the head above the lowest foot bone, in the body's own units (scale 1) - the
// body's origin isn't at its feet.
var bodyTransform = _body.transform;
var lowestFoot = float.MaxValue;
foreach (var foot in _footBones)
lowestFoot = Mathf.Min(lowestFoot, bodyTransform.InverseTransformPoint(foot.position).y);
var headHeight = bodyTransform.InverseTransformPoint(_headBone.position).y - lowestFoot;
_sneakEyeHeight = headHeight + _neckBelowEyes;
Logger.Log($"[VRHeroBody] Sneak pose: eyes {_sneakEyeHeight:F2} m above the feet (scale 1) - the " +
"crouch button's camera height.", LogCat.VR);
}
if (_sneakEyeHeight < 0f)
_sneakEyeHeight = MeasureEyeHeight(VrHeroLocomotion.SneakAnimation, "Sneak");
if (_kneelEyeHeight < 0f)
_kneelEyeHeight = MeasureEyeHeight(VrHeroLocomotion.KneelAnimation, "Kneel");

var eyeHeight = _isKneeling && _kneelEyeHeight > 0f ? _kneelEyeHeight : _sneakEyeHeight;
if (eyeHeight < 0f)
return;

var height = _sneakEyeHeight * scale;
var height = eyeHeight * scale;
if (!rig.CrouchHeightOverride.HasValue || Mathf.Abs(rig.CrouchHeightOverride.Value - height) > 0.01f)
rig.CrouchHeightOverride = height;
}

/// <summary>
/// The eyes above the lowest foot bone while the pose is fully blended in, in the body's own units (scale 1) -
/// the body's origin isn't at its feet. -1 while the pose doesn't play.
/// </summary>
private float MeasureEyeHeight(string animation, string poseName)
{
if (!_animator.IsPlaying(animation) || _footBones.Count == 0)
return -1f;

var bodyTransform = _body.transform;
var lowestFoot = float.MaxValue;
foreach (var foot in _footBones)
lowestFoot = Mathf.Min(lowestFoot, bodyTransform.InverseTransformPoint(foot.position).y);
var eyeHeight = bodyTransform.InverseTransformPoint(_headBone.position).y - lowestFoot + _neckBelowEyes;
Logger.Log($"[VRHeroBody] {poseName} pose ({animation}): eyes {eyeHeight:F2} m above the feet (scale 1) " +
"- the crouch button's camera height.", LogCat.VR);
return eyeHeight;
}

private void SolveArms()
{
GetSecondHandGripPoses(out var leftGrip, out var rightGrip);
Expand Down Expand Up @@ -383,6 +439,7 @@ private void EnsureBody(NpcContainer hero)
_renderers.Clear();
_bodyRenderer = null;
_sneakEyeHeight = -1f;
_kneelEyeHeight = -1f;
_footBones.Clear();
_sleeves.Clear();

Expand Down
29 changes: 24 additions & 5 deletions Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ public class VrHeroLocomotion
// every frame while it's missing (e.g. no S_SNEAKBL). Per built body (Bind).
private readonly Dictionary<(string, string), string> _missingAnimationFallbacks = new();

/// <summary>
/// Crouched and standing still: the hero kneels like at a chest in Gothic (G1/G2 HUMANS.MDS). Bodies without
/// it sneak (S_SNEAK).
/// </summary>
public const string KneelAnimation = "S_CHESTSMALL_S0";
public const string SneakAnimation = "S_SNEAK";

/// <summary>
/// The player moved (walk/run/sneak loop, not idle) in the last Update.
/// </summary>
public bool IsMoving { get; private set; }

/// <summary>
/// The animator of a newly built body.
/// </summary>
Expand All @@ -49,9 +61,11 @@ public void Bind(VRHeroBodyAnimator animator)

/// <summary>
/// Picks and plays this frame's animation and advances the animator. body: the placed body (its yaw and
/// facing), scale: its scale (the walk/run loops' real speed).
/// facing), scale: its scale (the walk/run loops' real speed), isKneeling: crouched and standing still for a
/// moment (VRHeroBody) - the kneel pose instead of the sneak pose.
/// </summary>
public void Update(NpcContainer hero, Transform body, IVrRig rig, float scale, bool isCrouching)
public void Update(NpcContainer hero, Transform body, IVrRig rig, float scale, bool isCrouching,
bool isKneeling)
{
if (_animator == null || !_animator.IsValid)
return;
Expand All @@ -70,12 +84,14 @@ public void Update(NpcContainer hero, Transform body, IVrRig rig, float scale, b

var bodyState = _swimPose.GetBodyState(rig, hero.Props.BodyState);
var isMoving = speed >= _idleSpeed;
IsMoving = isMoving;
var isBackwards = local.z < -_backwardsShare * speed;
var isTurning = Mathf.Abs(_bodyYawSpeed) >= _turnAnimationSpeed;

var animation = _swimPose.GetAnimation(_animator, bodyState, speed, isMoving, isBackwards, isTurning,
_bodyYawSpeed < 0f)
?? GetWalkAnimation(rig, velocity, speed, local, isMoving, isBackwards, isTurning, isCrouching);
?? GetWalkAnimation(rig, velocity, speed, local, isMoving, isBackwards, isTurning, isCrouching,
isKneeling);

if (!_animator.HasAnimation(animation))
{
Expand Down Expand Up @@ -121,13 +137,16 @@ private string GetMissingAnimationFallback(string animation)
/// On land: falling, sneaking, idle/turning on the spot, walking backwards, strafing, walking/running.
/// </summary>
private string GetWalkAnimation(IVrRig rig, Vector3 velocity, float speed, Vector3 local, bool isMoving,
bool isBackwards, bool isTurning, bool isCrouching)
bool isBackwards, bool isTurning, bool isCrouching, bool isKneeling)
{
if (rig != null && !rig.IsGrounded && velocity.y < -1f && !_swimPose.IsInWater &&
_animator.HasAnimation(_fallAnimation))
return _fallAnimation;
// Kneeling holds the pose through a short push (VRHeroBody ends it once the player really moves).
if (isCrouching && (isKneeling || !isMoving))
return isKneeling && _animator.HasAnimation(KneelAnimation) ? KneelAnimation : SneakAnimation;
if (isCrouching)
return !isMoving ? "S_SNEAK" : isBackwards ? "S_SNEAKBL" : "S_SNEAKL";
return isBackwards ? "S_SNEAKBL" : "S_SNEAKL";
if (!isMoving)
return isTurning ? (_bodyYawSpeed < 0f ? "T_RUNTURNL" : "T_RUNTURNR") : _idleAnimation;
if (isBackwards)
Expand Down