Skip to content

Commit

Permalink
Boop Support
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeSpork committed Mar 27, 2021
1 parent 1e14457 commit 1dafd7b
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 2 deletions.
2 changes: 2 additions & 0 deletions HS2VR/HS2VR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
<Compile Include="Interpreters\HS2Interpreter.cs" />
<Compile Include="Interpreters\OtherSceneInterpreter.cs" />
<Compile Include="Interpreters\SceneInterpreter.cs" />
<Compile Include="VRCollider.cs" />
<Compile Include="VRColliderHelper.cs" />
<Compile Include="VRLoader.cs" />
<Compile Include="VRPlugin.cs" />
<Compile Include="VRPatcher.cs" />
Expand Down
172 changes: 172 additions & 0 deletions HS2VR/VRCollider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using IllusionUtility.GetUtility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using VRGIN.Core;

namespace HS2VR
{
// Code contribute from thojmr - Much Appreciated
public static class VRCollider
{
/// <summary>
/// Searches for dynamic bones, and when found links them to the colliders set on the controllers
/// </summary>
internal static void SetVRControllerColliderToDynamicBones()
{
//Get all dynamic bones
var dynamicBonesV2 = GameObject.FindObjectsOfType<DynamicBone_Ver02>();
var dynamicBones = GameObject.FindObjectsOfType<DynamicBone>();
if (dynamicBonesV2.Length == 0 && dynamicBones.Length == 0)
{
VRLog.Info("No DB Bones Present, Nothing To Do");
return;
}

//Get the controller objects we want to attach colliders to.transform.gameObject
var leftHand = VR.Controller.Left?.gameObject;
var rightHand = VR.Controller.Right?.gameObject;
if (leftHand == null && rightHand == null)
{
VRLog.Info("No Hands, skipping for a round");
return;
}

VRLog.Info("Found Hands, Updating DB Bones");

//Attach a dynamic bone collider to each, then link that to all dynamic bones
if (leftHand) AttachToControllerAndLink(leftHand, leftHand.GetInstanceID().ToString(), dynamicBones, dynamicBonesV2);
if (rightHand) AttachToControllerAndLink(rightHand, rightHand.GetInstanceID().ToString(), dynamicBones, dynamicBonesV2);
}


/// <summary>
/// Gets the transform of the squeeze button
/// </summary>
internal static Transform GetColliderPosition(GameObject hand)
{
//render location
var renderTf = hand.transform.FindLoop("Model");
if (renderTf == null)
{
VRLog.Info("No Render Loop Found");
return null;
}

return renderTf;
}


/// <summary>
/// Adds the colliders to the controllers, and then links the dynamic bones
/// </summary>
internal static void AttachToControllerAndLink(GameObject controller, string name, DynamicBone[] dynamicBones, DynamicBone_Ver02[] dynamicBonesV2)
{
//For each vr controller add dynamic bone collider to it
var controllerCollider = GetOrAttachCollider(controller, name);
if (controllerCollider == null) return;

//For each controller, make it collidable with all dynaic bones (Did I miss any?)
AddControllerColliderToDBv2(controllerCollider, dynamicBonesV2);
AddControllerColliderToDB(controllerCollider, dynamicBones);
}


/// <summary>
/// Checks for existing controller collider, or creates them
/// </summary>
internal static DynamicBoneCollider GetOrAttachCollider(GameObject controllerGameObject, string colliderName)
{
if (controllerGameObject == null) return null;

//Check for existing DB collider that may have been attached earlier
var existingDBCollider = controllerGameObject.GetComponentInChildren<DynamicBoneCollider>();
if (existingDBCollider == null)
{
//Add a DB collider to the controller
return AddDBCollider(controllerGameObject, colliderName);
}

return existingDBCollider;
}


/// <summary>
/// Adds a dynamic bone collider to a controller GO (Thanks Anon11)
/// </summary>
internal static DynamicBoneCollider AddDBCollider(GameObject controllerGameObject, string colliderName, float colliderRadius = 0.1f, float collierHeight = 0f,
Vector3 colliderCenter = new Vector3(), DynamicBoneCollider.Direction colliderDirection = default)
{
var renderModelTf = GetColliderPosition(controllerGameObject);
if (renderModelTf == null) return null;

//Build the dynamic bone collider
var colliderObject = new GameObject(colliderName);
var collider = colliderObject.AddComponent<DynamicBoneCollider>();
collider.m_Radius = colliderRadius;
collider.m_Height = collierHeight;
collider.m_Center = colliderCenter;
collider.m_Direction = colliderDirection;
colliderObject.transform.SetParent(renderModelTf, false);

//Move the collider more into the hand for the index controller
// var localPos = renderModelTf.up * -0.09f + renderModelTf.forward * -0.075f;
// var localPos = renderModelTf.forward * -0.075f;
// colliderObject.transform.localPosition = localPos;

VRLog.Info($"Added DB Collider to {controllerGameObject.name}");

return collider;
}


/// <summary>
/// Links V2 dynamic bones to a controller collider
/// </summary>
internal static void AddControllerColliderToDBv2(DynamicBoneCollider controllerCollider, DynamicBone_Ver02[] dynamicBones)
{
if (controllerCollider == null) return;
if (dynamicBones.Length == 0) return;

int newDBCount = 0;

//For each heroine dynamic bone, add controller collider
for (int z = 0; z < dynamicBones.Length; z++)
{
//Check for existing interaction
if (!dynamicBones[z].Colliders.Contains(controllerCollider))
{
dynamicBones[z].Colliders.Add(controllerCollider);
newDBCount++;
}
}

}

/// <summary>
/// Links V1 dynamic bones to a controller collider
/// </summary>
internal static void AddControllerColliderToDB(DynamicBoneCollider controllerCollider, DynamicBone[] dynamicBones)
{
if (controllerCollider == null) return;
if (dynamicBones.Length == 0) return;

int newDBCount = 0;

//For each heroine dynamic bone, add controller collider
for (int z = 0; z < dynamicBones.Length; z++)
{
//Check for existing interaction
if (!dynamicBones[z].m_Colliders.Contains(controllerCollider))
{
dynamicBones[z].m_Colliders.Add(controllerCollider);
newDBCount++;
}
}
}

}
}
55 changes: 55 additions & 0 deletions HS2VR/VRColliderHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using VRGIN.Core;

namespace HS2VR
{
// Code contributed by thojmr
public static class VRColliderHelper
{
internal static bool coroutineActive = false;
internal static VRPlugin pluginInstance;


internal static void TriggerHelperCoroutine()
{
//Only trigger if not already running, and in main game
if (coroutineActive) return;
coroutineActive = true;

pluginInstance.StartCoroutine(LoopEveryXSeconds());
}


internal static void StopHelperCoroutine()
{
pluginInstance.StopCoroutine(LoopEveryXSeconds());
coroutineActive = false;
}


/// <summary>
/// Got tired of searching for the correct hooks, just check for new dynamic bones on a loop. Genious! (Should be able to use CharCustFunCtrl for this later)
/// </summary>
internal static IEnumerator LoopEveryXSeconds()
{
while (coroutineActive)
{
try
{
VRCollider.SetVRControllerColliderToDynamicBones();
}
catch (Exception e)
{
VRLog.Error("Error in Collider Helper: " + e.Message, e.StackTrace);
}
yield return new WaitForSeconds(3);
}
}
}
}
7 changes: 5 additions & 2 deletions HS2VR/VRPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace HS2VR
/// <summary>
/// This is an example for a VR plugin. At the same time, it also functions as a generic one.
/// </summary>
[BepInPlugin(GUID: "HS2VR.unofficial", Name: "HS2VR", Version: "0.0.4.0")]
[BepInPlugin(GUID: "HS2VR.unofficial", Name: "HS2VR", Version: "0.0.5.0")]
[BepInProcess("HoneySelect2")]
[BepInProcess("StudioNEOV2")]
public class VRPlugin : BaseUnityPlugin
Expand All @@ -34,7 +34,7 @@ public string Version
{
get
{
return "0.0.4.0";
return "0.0.5.0";
}
}

Expand All @@ -59,6 +59,9 @@ void Awake()
{
VR_ACTIVATED = true;
VRLoader.Create(true);

VRColliderHelper.pluginInstance = this;
VRColliderHelper.TriggerHelperCoroutine();
}
else
{
Expand Down

0 comments on commit 1dafd7b

Please sign in to comment.