Skip to content

Commit

Permalink
small things
Browse files Browse the repository at this point in the history
  • Loading branch information
jakzo committed Aug 24, 2024
1 parent b7a5b44 commit b0a3fc9
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 37 deletions.
2 changes: 1 addition & 1 deletion projects/Bonelab/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PropertyGroup Condition=" '$(IsNewProject)' == 'true' ">
<!-- Defaults (used by IDE autocomplete/syntax highlighting) -->
<DefaultPatch>5</DefaultPatch>
<DefaultMelonLoader>5</DefaultMelonLoader>
<DefaultMelonLoader>6</DefaultMelonLoader>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<TargetFrameworks Condition=" '$(MelonLoader)' == '' ">net6.0;net472</TargetFrameworks>

Expand Down
2 changes: 2 additions & 0 deletions projects/Bonelab/FlatPlayer/Project.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<!-- All game/Melon Loader version combinations to build -->
<MSBuild Projects="$(MSBuildProjectFile)" Targets="Build"
Properties="Configuration=$(Configuration);Patch=3;MelonLoader=5" />
<MSBuild Projects="$(MSBuildProjectFile)" Targets="Build"
Properties="Configuration=$(Configuration);Patch=5;MelonLoader=6" />
</Target>

<PropertyGroup>
Expand Down
16 changes: 8 additions & 8 deletions projects/Bonelab/FlatPlayer/src/FlatMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class FlatMode {
public HandActionMap LeftHand;
public HandActionMap RightHand;

public Camera mainCamera;
public bool isReady;
public Camera MainCamera;
public bool IsReady;

private static FlatMode _instance;
private MelonPreferences_Entry<float> _prefCameraSpeed;
Expand Down Expand Up @@ -121,18 +121,18 @@ public void Start() {
DataManager.Instance._settings.SpectatorSettings.SpectatorCameraMode ==
SpectatorCameraMode.Passthrough;
if (isPassthroughCamera) {
mainCamera = Camera.main;
mainCamera.cameraType = CameraType.SceneView;
mainCamera.fieldOfView = 90f;
MainCamera = Camera.main;
MainCamera.cameraType = CameraType.SceneView;
MainCamera.fieldOfView = 90f;
}

ResetRotation();
isReady = true;
IsReady = true;
}

public void Stop() {
ResetRotation();
isReady = false;
IsReady = false;
}

public void ResetRotation() {
Expand Down Expand Up @@ -256,7 +256,7 @@ private bool IsTriggerPressed() =>
Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftControl);

public void OnUpdate() {
if (!isReady)
if (!IsReady)
return;

if (Input.GetKey(KeyCode.Escape))
Expand Down
71 changes: 44 additions & 27 deletions projects/Bonelab/HandTracking/src/AutoSight.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using SLZ.Bonelab;
using SLZ.Interaction;
Expand All @@ -13,7 +14,8 @@ namespace Sst.HandTracking;
public class AutoSight {
private const float ROTATION_SIMILARITY_ACTIVATION_THRESHOLD = 0.95f;
private const float ROTATION_SIMILARITY_DEACTIVATION_THRESHOLD = 0.9f;
private const float ROTATION_FACTOR = 0.1f;
private const float ROTATION_FACTOR = 0.25f;
private const float POSITION_DAMPING_FACTOR = 0.25f;
// Sights have a slightly different offset depending on the gun but finding
// the specific value per gun is a lot of manual effort and won't work for
// modded guns whereas hardcoding it works well enough
Expand All @@ -24,6 +26,7 @@ public class AutoSight {
public InteractableHost Weapon;
public bool IsActive;
public Quaternion TargetHandRotation;
public Vector3 DampedRemapHandPos;

public AutoSight(HandTracker tracker, HandTracker otherTracker) {
Tracker = tracker;
Expand All @@ -50,35 +53,61 @@ public void UpdateHand() {
var sightRot = gun.firePointTransform.rotation;
var sightPos = gun.firePointTransform.position + sightRot * SIGHT_OFFSET;

var targetRotation =
Quaternion.LookRotation(sightPos - eye.transform.position);
var rotationSimilarity = Quaternion.Dot(targetRotation, sightRot);
var handToSightPos = sightPos - host.Rb.transform.position -
hand.joint.connectedAnchor + hand.joint.anchor;
var sightToHandPos = -handToSightPos;
var sightToHandRot =
sightRot * host.Rb.transform.rotation * hand.joint.targetRotation;
var handToSightRot = Quaternion.Inverse(sightToHandRot);

var sightPosOfHand = hand.transform.position + handToSightPos;
var sightRotOfHand = hand.transform.rotation * handToSightRot;

var remapRig = LevelHooks.RigManager.remapHeptaRig;
var remapHand = Tracker.Opts.isLeft ? remapRig.m_handLf : remapRig.m_handRt;
var remapHandPos = remapHand.position;
var remapHandRot = remapHand.rotation;

var targetSightRotation =
Quaternion.LookRotation(sightPosOfHand - eye.transform.position);
TargetHandRotation = targetSightRotation * sightToHandRot;

var rotationSimilarity = Quaternion.Dot(TargetHandRotation, remapHandRot);

if (IsActive) {
if (rotationSimilarity < ROTATION_SIMILARITY_DEACTIVATION_THRESHOLD) {
Dbg.Log("Auto-sight deactivated");
Tracker.Log("Auto-sight deactivated");
IsActive = false;
return;
}
} else if (rotationSimilarity >= ROTATION_SIMILARITY_ACTIVATION_THRESHOLD) {
Dbg.Log("Auto-sight activated");
Tracker.Log("Auto-sight activated");
IsActive = true;
DampedRemapHandPos = remapHand.localPosition;
} else {
return;
}

var handToSightPos = sightPos - host.Rb.transform.position -
hand.joint.connectedAnchor + hand.joint.anchor;
var handToSightRot = sightRot *
Quaternion.Inverse(host.Rb.transform.rotation) *
Quaternion.Inverse(hand.joint.targetRotation);
var sightToHandRot =
sightRot * host.Rb.transform.rotation * hand.joint.targetRotation;
remapHand.rotation = TargetHandRotation;

TargetHandRotation = targetRotation * sightToHandRot;
// TODO: Scale up damping factor when delta from last position increases
var remapHandPosDelta = remapHand.localPosition - DampedRemapHandPos;
DampedRemapHandPos += remapHandPosDelta * POSITION_DAMPING_FACTOR;
remapHand.localPosition = DampedRemapHandPos;
}

// TODO: Get this working
[HarmonyPatch(typeof(RemapRig), nameof(RemapRig.OnEarlyUpdate))]
internal static class RemapRig_OnEarlyUpdate {
[HarmonyPostfix]
private static void Postfix(RemapRig __instance) {
if (!__instance.Equals(LevelHooks.RigManager?.remapHeptaRig))
return;
// Mod.Instance.TrackerLeft?.AutoSight.UpdateHand();
// Mod.Instance.TrackerRight?.AutoSight.UpdateHand();
}
}

// TODO: Do we still need this?
// [HarmonyPatch(
// typeof(GameWorldSkeletonRig),
// nameof(GameWorldSkeletonRig.OnFixedUpdate)
Expand All @@ -98,18 +127,6 @@ public void UpdateHand() {
// }
// }

// [HarmonyPatch(
// typeof(RemapRig), nameof(RemapRig.OnEarlyUpdate)
// )]
// internal static class RemapRig_OnEarlyUpdate {
// [HarmonyPostfix]
// private static void Postfix(RemapRig __instance) {
// __instance.m_handLf.localPosition =
// new Vector3(Mathf.Sin(Time.time) * 0.25f, 1.5f, 0.2f);
// __instance.m_handLf.localRotation = Quaternion.identity;
// }
// }

// [HarmonyPatch(typeof(PhysHand), nameof(PhysHand.UpdateArmTargets))]
// internal static class PhysHand_UpdateArmTargets {
// [HarmonyPrefix]
Expand Down
2 changes: 1 addition & 1 deletion projects/Bonelab/HandTracking/src/HeadLocomotion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class HeadLocomotion : Locomotion {
// TODO: Account for forwards HMD movement due to looking down or crouching
private const float DEADZONE = 0.1f;
private const float MAX_INPUT_DIST = DEADZONE + 0.1f;
private const float MAX_OFFSET = MAX_INPUT_DIST + 0.15f;
private const float MAX_OFFSET = MAX_INPUT_DIST + 0.2f;
private const float DEFAULT_OFFSET = MAX_OFFSET;
private const float LOCK_DISENGAGE_POINT = 0.25f;
private const float WALK_SPEED = 0.4f;
Expand Down

0 comments on commit b0a3fc9

Please sign in to comment.