version 2.5.1

This commit is contained in:
Sean Lu
2025-01-08 10:28:35 +08:00
parent 2bfa2ad4c7
commit ae66f9c2fe
91 changed files with 7009 additions and 2005 deletions

View File

@@ -8,12 +8,13 @@
// conditions signed by you and all SDK and API requirements,
// specifications, and documentation provided by HTC to You."
using System.Collections.Generic;
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
using System.Linq;
using UnityEngine.InputSystem;
namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
{
@@ -129,6 +130,7 @@ namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
private HandGrabInteractable candidate = null;
private Pose wristPose = Pose.identity;
private Quaternion[] fingerJointRotation = new Quaternion[jointsPathMapping.Count];
private bool isNewInputSystem = false;
#region MonoBehaviours
@@ -156,6 +158,7 @@ namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
sb.Append("However, you still can record grab pose if you use direct preview mode.");
WARNING(sb);
}
isNewInputSystem = Keyboard.current != null;
}
private void OnDisable()
@@ -199,7 +202,7 @@ namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
}
}
if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
if (IsEnterPressed())
{
FindNearInteractable();
SavePoseWithCandidate();
@@ -326,6 +329,19 @@ namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
return updated;
}
private bool IsEnterPressed()
{
if (isNewInputSystem)
{
return (Keyboard.current.enterKey?.wasPressedThisFrame ?? false) ||
(Keyboard.current.numpadEnterKey?.wasPressedThisFrame ?? false);
}
else
{
return Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter);
}
}
/// <summary>
/// Finds the nearest interactable object to the hand.
/// </summary>

View File

@@ -8,7 +8,6 @@
// conditions signed by you and all SDK and API requirements,
// specifications, and documentation provided by HTC to You."
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
@@ -87,8 +86,8 @@ namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
[SerializeField]
private IOneHandContraintMovement m_OneHandContraintMovement;
public IOneHandContraintMovement oneHandContraintMovement { get { return m_OneHandContraintMovement; } set { m_OneHandContraintMovement = value; } }
public bool isContraint => m_OneHandContraintMovement != null;
public IOneHandContraintMovement oneHandContraintMovement { get { return m_OneHandContraintMovement; } set { m_OneHandContraintMovement = value; } }
public bool isContraint => m_OneHandContraintMovement != null;
#pragma warning disable
[SerializeField]
@@ -183,8 +182,9 @@ namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
{
if (!isGrabbable || isGrabbed) { return 0; }
Vector3 closestPoint = GetClosestPoint(grabberPos);
float distacne = Vector3.Distance(grabberPos, closestPoint);
return distacne > grabDistance ? 0 : 1 - (distacne / grabDistance);
float distanceSqr = (grabberPos - closestPoint).sqrMagnitude;
float grabDistSqr = grabDistance * grabDistance;
return distanceSqr > grabDistSqr ? 0 : 1 - (distanceSqr / grabDistSqr);
}
/// <summary>
@@ -263,35 +263,41 @@ namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
private Vector3 GetClosestPoint(Vector3 sourcePos)
{
Vector3 closestPoint = Vector3.zero;
float shortDistance = float.MaxValue;
float shortDistanceSqr = float.MaxValue;
for (int i = 0; i < allColliders.Count; i++)
{
Collider collider = allColliders[i];
Vector3 closePoint = collider.ClosestPointOnBounds(sourcePos);
float distance = Vector3.Distance(sourcePos, closePoint);
float distanceSqr = (sourcePos - closePoint).sqrMagnitude;
if (distanceSqr < 0.001f)
{
return closePoint;
}
if (collider.bounds.Contains(closePoint))
{
Vector3 direction = closePoint - sourcePos;
direction.Normalize();
int hitCount = Physics.RaycastNonAlloc(sourcePos, direction, hitResults, distance);
int hitCount = Physics.RaycastNonAlloc(sourcePos, direction, hitResults, Mathf.Sqrt(distanceSqr));
for (int j = 0; j < hitCount; j++)
{
RaycastHit hit = hitResults[j];
if (hit.collider == collider)
{
float hitDistance = Vector3.Distance(sourcePos, hit.point);
if (distance > hitDistance)
float hitDistanceSqr = (sourcePos - hit.point).sqrMagnitude;
if (distanceSqr > hitDistanceSqr)
{
distance = hitDistance;
distanceSqr = hitDistanceSqr;
closePoint = hit.point;
}
}
}
}
if (shortDistance > distance)
if (shortDistanceSqr > distanceSqr)
{
shortDistance = distance;
shortDistanceSqr = distanceSqr;
closestPoint = closePoint;
}
}
@@ -361,9 +367,12 @@ namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
/// <param name="index">The index of the indicator to show.</param>
private void ShowIndicatorByIndex(int index)
{
foreach (var grabPose in m_GrabPoses)
for (int i = 0; i < m_GrabPoses.Count; i++)
{
grabPose.indicator.SetActive(false);
if (index != i)
{
m_GrabPoses[i].indicator.SetActive(false);
}
}
if (index >= 0 && index < m_GrabPoses.Count &&
m_GrabPoses[index].indicator.enableIndicator)
@@ -379,9 +388,9 @@ namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
/// <param name="isLeft">Whether the hand side is left.</param>
private void ShowAllIndicator(bool isLeft)
{
foreach (var grabPose in m_GrabPoses)
for (int i = 0; i < m_GrabPoses.Count; i++)
{
grabPose.indicator.SetActive(false);
m_GrabPoses[i].indicator.SetActive(false);
}
foreach (var grabPose in m_GrabPoses)
{

View File

@@ -8,7 +8,6 @@
// conditions signed by you and all SDK and API requirements,
// specifications, and documentation provided by HTC to You."
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
@@ -79,6 +78,13 @@ namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
private Pose wristPose = Pose.identity;
private Vector3[] fingerTipPosition = new Vector3[(int)FingerId.Count];
private const int kMaxCacheSize = 100;
private int lastBufferCount = 0;
private Collider[] colliderBuffer = new Collider[50];
private HandGrabInteractable[] grabbableBuffer = new HandGrabInteractable[50];
private LinkedList<Collider> lruList = new LinkedList<Collider>();
private Dictionary<Collider, LinkedListNode<Collider>> unusedColliders = new Dictionary<Collider, LinkedListNode<Collider>>();
#region MonoBehaviour
private void Awake()
{
@@ -159,7 +165,6 @@ namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
/// </summary>
private void FindCandidate()
{
currentCandidate = null;
float distanceScore = float.MinValue;
if (GetClosestGrabbable(m_GrabDistance, out HandGrabInteractable grabbable, out float score) && score > distanceScore)
{
@@ -189,28 +194,47 @@ namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
grabbable = null;
maxScore = 0f;
Collider[] nearColliders = Physics.OverlapSphere(wristPose.position, 0.5f);
List<HandGrabInteractable> nearHandGrabInteractables = new List<HandGrabInteractable>();
for (int i = 0; i < nearColliders.Length; i++)
for (int i = 0; i < lastBufferCount; i++)
{
HandGrabInteractable interactable = nearColliders[i].GetComponentInParent<HandGrabInteractable>();
if (interactable && !nearHandGrabInteractables.Contains(interactable))
{
nearHandGrabInteractables.Add(interactable);
continue;
}
interactable = nearColliders[i].GetComponentInChildren<HandGrabInteractable>();
if (interactable && !nearHandGrabInteractables.Contains(interactable))
{
nearHandGrabInteractables.Add(interactable);
continue;
}
HandGrabInteractable interactable = grabbableBuffer[i];
interactable.ShowIndicator(false, this);
}
for (int i = 0; i < nearHandGrabInteractables.Count; i++)
int colliderCount = Physics.OverlapSphereNonAlloc(wristPose.position, grabDistance * 5, colliderBuffer);
int interactableCount = 0;
for (int i = 0; i < colliderCount; i++)
{
HandGrabInteractable interactable = nearHandGrabInteractables[i];
interactable.ShowIndicator(false, this);
Collider collider = colliderBuffer[i];
if (unusedColliders.TryGetValue(collider, out _)) { continue; }
HandGrabInteractable interactable = collider.GetComponentInParent<HandGrabInteractable>()
?? collider.GetComponentInChildren<HandGrabInteractable>();
if (interactable != null)
{
bool isUnique = true;
for (int j = 0; j < interactableCount; j++)
{
if (grabbableBuffer[j] == interactable)
{
isUnique = false;
break;
}
}
if (isUnique)
{
grabbableBuffer[interactableCount++] = interactable;
}
}
else
{
AddUnusedColliders(collider);
}
}
lastBufferCount = interactableCount;
for (int i = 0; i < interactableCount; i++)
{
HandGrabInteractable interactable = grabbableBuffer[i];
for (int j = 0; j < fingerTipPosition.Length; j++)
{
float distanceScore = interactable.CalculateDistanceScore(fingerTipPosition[j], grabDistance);
@@ -279,5 +303,18 @@ namespace VIVE.OpenXR.Toolkits.RealisticHandInteraction
}
m_Grabbable.UpdatePositionAndRotation(wristPose);
}
private void AddUnusedColliders(Collider collider)
{
if (lruList.Count >= kMaxCacheSize)
{
var oldest = lruList.First;
unusedColliders.Remove(oldest.Value);
lruList.RemoveFirst();
}
var node = lruList.AddLast(collider);
unusedColliders[collider] = node;
}
}
}