Skip to content

Commit

Permalink
feat(input): use right hand as target origin
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenira committed Dec 3, 2023
1 parent 0897dca commit 8e3a749
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 2 deletions.
11 changes: 9 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ CAUTION: This mod is still in early development and *will* contain bugs. Use at
. Run the game. The mod should be loaded automatically by BepInEx.

=== Disabling the Mod
To disable the mod either remove the mod's files from the `BepInEx/plugins` folder or rename the `BepInEx/plugins/techtonica_vr.dll` file to something else not ending in `.dll`.
To disable the mod change the `Enabled` under `[General]` value in the `de.xenira.techtonica.cfg` file in the `BepInEx/config` folder to `false`.`

=== Uninstalling the Mod
To uninstall the mod remove the mod's files from the `BepInEx/plugins` folder. This should be the following files:
- `techtonica_vr.dll`
- 'techtonica_vr' folder


This will be improved in the future.

Expand All @@ -50,12 +56,13 @@ While the mod is in a playable state, it is still in early development. Some fea
- Default bindings for VR controllers other than the Valve Index Controllers
- Configuration options
- Object outlines. Disabled for now as the shader is broken in VR.
- Finger tracking
- Ability to yeet paladin down the waterfall

=== Known Issues
- The players position is not synced correctly with the game when using roomscale. The characters position is in the center of the playspace. This might cause issues with collisions and other things.
- Some UI elements might be outside of the players field of view or scaled incorrectly.
- Button prompts are not for VR controllers.
- Targeting is bound to the player's head.
- Haptics are played on both controllers by the game. One improvement would be to play them on the hand that is actually holding the tool.
- When gathering it goes into auto mode and won't stop until you mine something.
- Performance needs improvement
Expand Down
3 changes: 3 additions & 0 deletions plugin/TechtonicaVr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
<Reference Include="UnityEngine.UIModule">
<HintPath>..\strip-dll\UnityEngine.UIModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.PhysicsModule">
<HintPath>..\strip-dll\UnityEngine.PhysicsModule.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'">
Expand Down
8 changes: 8 additions & 0 deletions plugin/src/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ public class VRLoader : MonoBehaviour

private void Awake()
{
ModConfig.Init(Config);

// Plugin startup logic
Logger = base.Logger;
Logger.LogInfo($"Loading plugin {MyPluginInfo.PLUGIN_GUID} version {MyPluginInfo.PLUGIN_VERSION}...");

if (!ModConfig.ModEnabled())
{
Logger.LogInfo("Mod is disabled, skipping...");
return;
}

Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly());

if (staticVrLoader == null)
Expand Down
53 changes: 53 additions & 0 deletions plugin/src/camera/patch/target_raycast_patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using HarmonyLib;
using Plugin.Input;
using UnityEngine;

namespace TechtonicaVR.VRCamera.Patch;

[HarmonyPatch]
public class TargetRaycastPatch
{

public static Transform cursorTlc;

[HarmonyPrefix]
[HarmonyPatch(typeof(PlayerFirstPersonController), nameof(PlayerFirstPersonController.UpdateAimingRaycasts))]
public static bool Postfix(PlayerFirstPersonController __instance)
{
var right_hand_transform = SteamVRInputMapper.rightHandObject.transform;
var forward = right_hand_transform.forward;

// Rotate the forward vector 45 degrees down
var rotation = Quaternion.Euler(45f, 0f, 0f);
forward = rotation * forward;

__instance._hasCamHit = Physics.Raycast(new Ray(right_hand_transform.position, forward), out __instance._camHit, __instance.cam.farClipPlane, __instance.aimLayer, QueryTriggerInteraction.Collide);
if (__instance.hasCamHit)
{
__instance.lookAtTarget.position = __instance.camHit.point;
__instance._hasPlayerAimHit = __instance._hasCamHit;
}
else
{
__instance.lookAtTarget.position = __instance.cam.transform.position + __instance.cam.transform.forward * __instance.cam.farClipPlane;
__instance._camHit = default;
__instance._playerAimHit = default;
}
UIManager.instance.hud.cursorDotUI.cursorSetting = CursorDotUI.CursorSetting.Default;

return false;
}

[HarmonyPostfix]
[HarmonyPatch(typeof(CursorDotUI), nameof(PlayerFirstPersonController.LateUpdate))]
public static void Postfix(CursorDotUI __instance)
{
if (cursorTlc != null && Player.instance.fpcontroller.hasCamHit)
{
var camHit = Player.instance.fpcontroller.camHit;
Vector3 screenPoint = Player.instance.fpcontroller.cam.WorldToScreenPoint(camHit.point);
Vector3 canvasPoint = cursorTlc.parent.GetComponent<Canvas>().worldCamera.ScreenToWorldPoint(screenPoint);
cursorTlc.position = canvasPoint;
}
}
}
31 changes: 31 additions & 0 deletions plugin/src/debug/debug_line.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using UnityEngine;

namespace TechtonicaVR.Debug;

public class DebugLine : MonoBehaviour
{
public Transform start;
public Transform end;

private LineRenderer line;

private void Start()
{
if (ModConfig.DebugLineEnabled() == false)
{
Destroy(this);
return;
}

line = gameObject.AddComponent<LineRenderer>();
line.startWidth = 0.01f;
line.endWidth = 0.01f;
line.positionCount = 2;
}

private void Update()
{
line.SetPosition(0, start.position);
line.SetPosition(1, end.position);
}
}
1 change: 1 addition & 0 deletions plugin/src/main_game_patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void Start()
new InventoryAndCraftingPatch(),
new DialoguePopupPatch(),
new DisableComponentPatch<OutlinePostProcess>(),
new CursorCanvasPatch(),
]).ToArray();

Plugin.Logger.LogDebug("Hello World!");
Expand Down
33 changes: 33 additions & 0 deletions plugin/src/mod_config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using BepInEx.Configuration;

namespace TechtonicaVR;

public class ModConfig
{
// General
public static ConfigEntry<bool> modEnabled;

// Debug
public static ConfigEntry<bool> debugMode;
public static ConfigEntry<bool> debugLineEnabled;

public static void Init(ConfigFile config)
{
// General
modEnabled = config.Bind("General", "Enabled", true, "Enable mod");

// Debug
debugMode = config.Bind("Debug", "Debug Mode", false, "Enable debug mode");
debugLineEnabled = config.Bind("Debug", "Debug Line Enabled", false, "Enable debug lines");
}

public static bool ModEnabled()
{
return modEnabled.Value;
}

public static bool DebugLineEnabled()
{
return debugMode.Value && debugLineEnabled.Value;
}
}
26 changes: 26 additions & 0 deletions plugin/src/patch/main_game/ui/cursor_canvas_patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Plugin.Input;
using TechtonicaVR.Debug;
using TechtonicaVR.VRCamera.Patch;
using UnityEngine;

namespace TechtonicaVR.Patches.MainGame.UI;

public class CursorCanvasPatch : GameObjectPatch
{
public CursorCanvasPatch() : base("Cursor Canvas")
{
}

protected override bool Apply(GameObject gameObject)
{
var tlc = gameObject.transform.GetChild(0);
TargetRaycastPatch.cursorTlc = tlc;

// Debug
var line = tlc.gameObject.AddComponent<DebugLine>();
line.start = tlc;
line.end = SteamVRInputMapper.rightHandObject.transform;

return true;
}
}
Binary file added strip-dll/UnityEngine.PhysicsModule.dll
Binary file not shown.

0 comments on commit 8e3a749

Please sign in to comment.