Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jakzo committed Aug 25, 2024
1 parent ffe689d commit a96a19f
Show file tree
Hide file tree
Showing 19 changed files with 647 additions and 63 deletions.
7 changes: 7 additions & 0 deletions SlzMods.sln
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Project("{74264C91-52E5-80AD-8566-C7C096F66DE6}") = "BoneworksFlatPlayer", "proj
EndProject
Project("{74264C91-52E5-80AD-8566-C7C096F66DE6}") = "BonelabPerformanceProfiler", "projects\Bonelab\PerformanceProfiler\Project.csproj", "{CA7518A5-AB0F-40B0-B1D1-1954BCA3D0E4}"
EndProject
Project("{74264C91-52E5-80AD-8566-C7C096F66DE6}") = "BonelabTesting", "projects\Bonelab\Testing\Project.csproj", "{1F09D27B-6C9F-48B2-86A9-DC5F7A70A45D}"
EndProject
Project("{74264C91-52E5-80AD-8566-C7C096F66DE6}") = "BonelabHandTracking", "projects\Bonelab\HandTracking\Project.csproj", "{A1F8D1A5-8964-4F37-A314-F53C7C5D5AA4}"
EndProject
Project("{74264C91-52E5-80AD-8566-C7C096F66DE6}") = "BoneworksRandomizer", "projects\Boneworks\Randomizer\Project.csproj", "{9746DCEC-6A9C-4E84-98E2-B141A702FDD9}"
Expand Down Expand Up @@ -192,6 +194,11 @@ Global
{A1F8D1A5-8964-4F37-A314-F53C7C5D5AA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1F8D1A5-8964-4F37-A314-F53C7C5D5AA4}.Release|Any CPU.Build.0 = Release|Any CPU

{1F09D27B-6C9F-48B2-86A9-DC5F7A70A45D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F09D27B-6C9F-48B2-86A9-DC5F7A70A45D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F09D27B-6C9F-48B2-86A9-DC5F7A70A45D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F09D27B-6C9F-48B2-86A9-DC5F7A70A45D}.Release|Any CPU.Build.0 = Release|Any CPU

{9746DCEC-6A9C-4E84-98E2-B141A702FDD9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9746DCEC-6A9C-4E84-98E2-B141A702FDD9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9746DCEC-6A9C-4E84-98E2-B141A702FDD9}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
13 changes: 10 additions & 3 deletions common/Utilities/Dbg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

namespace Sst {
public class Dbg {
private static MelonLogger.Instance _logger;
private static MelonPreferences_Entry<bool> _prefPrintDebugLogs;

public static void Init(string prefCategoryId) {
public static void
Init(string prefCategoryId, MelonLogger.Instance logger = null) {
_logger = logger;
var category = MelonPreferences.CreateCategory(prefCategoryId);
_prefPrintDebugLogs = category.CreateEntry(
"printDebugLogs", false, "Print debug logs",
Expand All @@ -17,9 +20,13 @@ public static void Log(params object[] data) {
#if !DEBUG
if (_prefPrintDebugLogs.Value) {
#endif
var msg =
var msg = "dbg: " +
string.Join(" ", data.Select(d => d == null ? "" : d.ToString()));
MelonLogger.Msg($"dbg: {msg}");
if (_logger != null) {
_logger.Msg(msg);
} else {
MelonLogger.Msg(msg);
}
#if !DEBUG
}
#endif
Expand Down
14 changes: 8 additions & 6 deletions common/Utilities/Shaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ public static class Shaders {
public static Shader DefaultShader {
get => _defaultShader != null
? _defaultShader
: (_defaultShader = Resources.FindObjectsOfTypeAll<Shader>().First(
shader => shader.name == "Sprites/Default"
));
: (_defaultShader =
UnityEngine.Resources.FindObjectsOfTypeAll<Shader>().First(
shader => shader.name == "Sprites/Default"
));
}

private static Shader _highlightShader;
public static Shader HighlightShader {
get => _highlightShader != null
? _highlightShader
: (_highlightShader = Resources.FindObjectsOfTypeAll<Shader>().First(
shader => shader.name == "SLZ/Highlighter"
));
: (_highlightShader =
UnityEngine.Resources.FindObjectsOfTypeAll<Shader>().First(
shader => shader.name == "SLZ/Highlighter"
));
}
}
}
4 changes: 3 additions & 1 deletion projects/Bonelab/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

<PropertyGroup Condition=" '$(IsNewProject)' == 'true' ">
<!-- Defaults (used by IDE autocomplete/syntax highlighting) -->
<IsIde Condition=" '$(Patch)' == '' ">true</IsIde>
<DefaultPatch>5</DefaultPatch>
<DefaultMelonLoader>6</DefaultMelonLoader>
<DefaultMelonLoader>5</DefaultMelonLoader>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<TargetFrameworks Condition=" '$(MelonLoader)' == '' ">net6.0;net472</TargetFrameworks>

Expand All @@ -16,6 +17,7 @@

<TargetFramework Condition=" '$(MelonLoader)' == '6' ">net6.0</TargetFramework>
<TargetFramework Condition=" '$(MelonLoader)' == '5' ">net472</TargetFramework>
<TargetFramework Condition=" '$(IsIde)' == 'true' ">net472</TargetFramework>

<IsStandalone Condition=" '$(DefaultPatch)' == '4' and '$(DefaultMelonLoader)' == '5' ">true</IsStandalone>

Expand Down
113 changes: 69 additions & 44 deletions projects/Bonelab/HandTracking/src/AutoSight.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using SLZ.Bonelab;
using SLZ.Interaction;
using Sst.Utilities;
using SLZ.Rig;
using SLZ.Marrow;
using HarmonyLib;
using SLZ.VRMK;

using SLZ.Marrow;
using SLZ.Interaction;
using SLZ.Rig;
using SLZ.Bonelab;

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_SIMILARITY_ACTIVATION_THRESHOLD = 0.98f;
private const float ROTATION_SIMILARITY_DEACTIVATION_THRESHOLD = 0.96f;
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
Expand All @@ -26,53 +26,41 @@ public class AutoSight {
public InteractableHost Weapon;
public bool IsActive;
public Quaternion TargetHandRotation;
public Vector3 DampedRemapHandPos;
public Vector3 DampedVirtualHandPos;

public AutoSight(HandTracker tracker, HandTracker otherTracker) {
Tracker = tracker;
OtherTracker = otherTracker;
}

public void UpdateHand() {
// TODO: More efficient way to get held gun and cache all this stuff?
var hand = Tracker.GetPhysicalHand();
if (hand == null)
return;

var host = hand?.AttachedReceiver?.TryCast<TargetGrip>()
?.Host?.TryCast<InteractableHost>();
var gun = host?.GetComponent<Gun>();
if (gun?.firePointTransform == null || host?.Rb == null)
var result = CalculateHandToSightOffset();
if (!result.HasValue)
return;
var handToSight = result.Value;

// TODO: Do for both eyes
var eye = LevelHooks.RigManager.controllerRig.TryCast<OpenControllerRig>()
?.cameras?[0];
if (eye == null)
return;

var sightRot = gun.firePointTransform.rotation;
var sightPos = gun.firePointTransform.position + sightRot * SIGHT_OFFSET;

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 virtualRig = LevelHooks.RigManager.virtualHeptaRig;
var virtualHand =
Tracker.Opts.isLeft ? virtualRig.m_handLf : virtualRig.m_handRt;
var virtualHandPos = virtualHand.position;
var virtualHandRot = virtualHand.rotation;

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 sightPosOfHand =
virtualHand.position + virtualHand.rotation * handToSight.Pos;
var sightRotOfHand = virtualHand.rotation * handToSight.Rot;

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

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

if (IsActive) {
if (rotationSimilarity < ROTATION_SIMILARITY_DEACTIVATION_THRESHOLD) {
Expand All @@ -83,24 +71,61 @@ public void UpdateHand() {
} else if (rotationSimilarity >= ROTATION_SIMILARITY_ACTIVATION_THRESHOLD) {
Tracker.Log("Auto-sight activated");
IsActive = true;
DampedRemapHandPos = remapHand.localPosition;
DampedVirtualHandPos = virtualHand.localPosition;
} else {
return;
}

remapHand.rotation = TargetHandRotation;
virtualHand.rotation = TargetHandRotation;

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

public (Vector3 Pos, Quaternion Rot)? CalculateHandToSightOffset() {
// TODO: More efficient way to get held gun and cache all this stuff?
var physicalHand = Tracker.GetPhysicalHand();
if (physicalHand == null)
return null;

var host = physicalHand?.AttachedReceiver?.TryCast<TargetGrip>()
?.Host?.TryCast<InteractableHost>();

var result = GetSight(host);
if (!result.HasValue)
return null;
var sight = result.Value;

// TODO: This is a little bit off for some reason (depends on gun)
var handToSightPos = physicalHand.jointStartRotation *
(Quaternion.Inverse(host.Rb.rotation) * (sight.Pos - host.Rb.position) -
physicalHand.joint.connectedAnchor + physicalHand.joint.anchor);
var handToSightRot = sight.Rot * Quaternion.Inverse(host.Rb.rotation) *
physicalHand.jointStartRotation;

return (handToSightPos, handToSightRot);
}

public (Vector3 Pos, Quaternion Rot)? GetSight(InteractableHost host) {
var gun = host?.GetComponent<Gun>();
if (gun?.firePointTransform == null || host?.Rb == null)
return null;

var sightRot = gun.firePointTransform.rotation;
var sightPos = gun.firePointTransform.position + sightRot * SIGHT_OFFSET;

return (sightPos, sightRot);
}

[HarmonyPatch(typeof(RemapRig), nameof(RemapRig.OnEarlyUpdate))]
internal static class RemapRig_OnEarlyUpdate {
[HarmonyPatch(
typeof(GameWorldSkeletonRig), nameof(GameWorldSkeletonRig.OnFixedUpdate)
)]
internal static class RemapRig_OnFixedUpdate {
[HarmonyPostfix]
private static void Postfix(RemapRig __instance) {
if (!__instance.Equals(LevelHooks.RigManager?.remapHeptaRig))
private static void Postfix(GameWorldSkeletonRig __instance) {
if (!__instance.Equals(LevelHooks.RigManager?.virtualHeptaRig))
return;
// Mod.Instance.TrackerLeft?.AutoSight.UpdateHand();
// Mod.Instance.TrackerRight?.AutoSight.UpdateHand();
Expand Down
90 changes: 90 additions & 0 deletions projects/Bonelab/HandTracking/src/Debugging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Linq;
using UnityEngine;
using Sst.Utilities;
using SLZ.Interaction;
using SLZ.Bonelab;
using SLZ.Rig;
using SLZ.Marrow;
using TMPro;
Expand All @@ -20,10 +22,17 @@ public class DebugHand {
private Transform _root;
private Joint[] _joints;
private Transform _marker;
private Transform _calculatedSight;
private Transform _actualSight;

public DebugHand(OVRPlugin.Skeleton2 skeleton) { _skeleton = skeleton; }

public void Update(HandState handState) {
HandTrackingOnUpdate(handState);
AutoSightOnUpdate(handState);
}

private void HandTrackingOnUpdate(HandState handState) {
if (!Mod.Preferences.DebugShowHandTracking.Value ||
LevelHooks.RigManager == null)
return;
Expand Down Expand Up @@ -150,6 +159,87 @@ private void CreateVisualization() {
private bool IsTip(OVRPlugin.BoneId boneId
) => boneId >= OVRPlugin.BoneId.Hand_MaxSkinnable;

private void AutoSightOnUpdate(HandState handState) {
if (!Mod.Preferences.DebugShowAutoSight.Value ||
LevelHooks.RigManager == null)
return;

var tracker =
handState.IsLeft ? Mod.Instance.TrackerLeft : Mod.Instance.TrackerRight;

if (_calculatedSight == null)
CreateAutoSightVisualization(tracker);

var result = tracker?.AutoSight.CalculateHandToSightOffset();
if (!result.HasValue) {
if (_actualSight.gameObject.active) {
Dbg.Log("Gun dropped");
_actualSight.gameObject.SetActive(false);
_calculatedSight.gameObject.SetActive(false);
}
return;
}

if (!_actualSight.gameObject.active) {
Dbg.Log("Gun held");
_actualSight.gameObject.SetActive(true);
_calculatedSight.gameObject.SetActive(true);
}

var handToSight = result.Value;
var virtualRig = LevelHooks.RigManager.virtualHeptaRig;
var hand = tracker.Opts.isLeft ? virtualRig.m_handLf : virtualRig.m_handRt;
_calculatedSight.SetPositionAndRotation(
hand.position + hand.rotation * handToSight.Pos,
hand.rotation * handToSight.Rot
);

var color = tracker.AutoSight.IsActive ? Color.magenta : Color.green;
var renderer = _calculatedSight.gameObject.GetComponent<MeshRenderer>();
if (color != renderer.material.color) {
renderer.material.color = color;
}

var sight = tracker.AutoSight
.GetSight(tracker.GetPhysicalHand()
.AttachedReceiver.TryCast<TargetGrip>()
.Host.TryCast<InteractableHost>())
.Value;
_actualSight.SetPositionAndRotation(sight.Pos, sight.Rot);
}

private void CreateAutoSightVisualization(HandTracker tracker) {
var size = JOINT_SIZE / 2f;
var cubePrefab = Geometry.CreatePrefabCube(
"HandTracking_DebugAutoSight", Color.red, -size, size, -size, size,
-size, size
);
Geometry.SetMaterial(cubePrefab, Color.red, Shaders.HighlightShader);

_calculatedSight = GameObject.Instantiate(cubePrefab).transform;
Geometry.SetMaterial(
_calculatedSight.gameObject, Color.green, Shaders.HighlightShader
);

_actualSight = GameObject.Instantiate(cubePrefab).transform;
Geometry.SetMaterial(
_actualSight.gameObject, Color.blue, Shaders.HighlightShader
);

foreach (var (rig, color) in new(Rig, Color)[] {
(LevelHooks.RigManager.virtualHeptaRig, Color.green),
(LevelHooks.RigManager.physicsRig, Color.blue),
}) {
var transform = tracker.Opts.isLeft ? rig.m_handLf : rig.m_handRt;
var cube = GameObject.Instantiate(cubePrefab, transform);
Geometry.SetMaterial(cube, color, Shaders.HighlightShader);
cube.SetActive(true);
}

GameObject.Destroy(cubePrefab);
Dbg.Log("Created auto-sight visualization");
}

private class Joint {
public OVRPlugin.BoneId BoneId;
public Transform Transform;
Expand Down
Loading

0 comments on commit a96a19f

Please sign in to comment.