Hand.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. using Rokid.UXR.Utility;
  5. namespace Rokid.UXR.Interaction
  6. {
  7. [Flags]
  8. public enum GrabFlags
  9. {
  10. SnapOnAttach = 1 << 0, // The object should snap to the position of the specified attachment point on the hand.
  11. DetachOthers = 1 << 1, // Other objects attached to this hand will be detached.
  12. ReleaseFromOtherHand = 1 << 2, // This object will be detached from the other hand.
  13. ParentToHand = 1 << 3, // The object will be parented to the hand.
  14. VelocityMovement = 1 << 4, // The object will attempt to move to match the position and rotation of the hand.
  15. TurnOnKinematic = 1 << 5, // The object will not respond to external physics.
  16. TurnOffGravity = 1 << 6, // The object will not respond to external physics.
  17. };
  18. public struct GrabbedObject
  19. {
  20. public GameObject grabbedObject;
  21. public GrabInteractable interactable;
  22. public Rigidbody grabbedRigidbody;
  23. public CollisionDetectionMode collisionDetectionMode;
  24. public bool grabbedRigidbodyWasKinematic;
  25. public bool grabbedRigidbodyUsedGravity;
  26. public GameObject originalParent;
  27. public bool isParentedToHand;
  28. public GrabTypes grabbedWithType;
  29. public GrabFlags grabFlags;
  30. public Vector3 initialPositionalOffset;
  31. public Quaternion initialRotationalOffset;
  32. public Transform attachedOffsetTransform;
  33. public Transform handAttachmentPointTransform;
  34. public Vector3 easeSourcePosition;
  35. public Quaternion easeSourceRotation;
  36. public float grabTime;
  37. public bool HasAttachFlag(GrabFlags flag)
  38. {
  39. return (grabFlags & flag) == flag;
  40. }
  41. }
  42. public class Hand : MonoBehaviour
  43. {
  44. #region Event
  45. public static event Action<HandType, GameObject> OnHandHoverBegin;
  46. public static event Action<HandType, GameObject> OnHandHoverUpdate;
  47. public static event Action<HandType> OnHandHoverEnd;
  48. public static event Action<HandType, GameObject> OnGrabbedToHand;
  49. public static event Action<HandType, GameObject> OnGrabbedUpdate;
  50. public static event Action<HandType> OnReleasedFromHand;
  51. #endregion
  52. public const GrabFlags defaultGrabFlags = GrabFlags.ParentToHand |
  53. GrabFlags.DetachOthers |
  54. GrabFlags.ReleaseFromOtherHand |
  55. GrabFlags.TurnOnKinematic |
  56. GrabFlags.SnapOnAttach;
  57. public HandType handType;
  58. public Hand otherHand;
  59. public bool useHoverSphere;
  60. public float hoverUpdateInterval = 0.1f;
  61. public LayerMask hoverLayerMask = -1;
  62. public Transform hoverSphereTransform;
  63. public float hoverSphereRadius = 0.05f;
  64. private int prevOverlappingColliders = 0;
  65. private const int ColliderArraySize = 32;
  66. private Collider[] overlappingColliders;
  67. public bool hoverLocked { get; private set; }
  68. public bool hovering { get; private set; }
  69. /// <summary>
  70. /// 抓取的对象
  71. /// </summary>
  72. /// <typeparam name="GrabbedObject"></typeparam>
  73. /// <returns></returns>
  74. private List<GrabbedObject> grabbedObjects = new List<GrabbedObject>();
  75. private TextMesh debugText;
  76. public Transform objectGrabbedPoint;
  77. public Transform objectSnapPoint;
  78. private float fallbackMaxDistanceNoItem = 10.0f;
  79. private float fallbackMaxDistanceWithItem = 0.5f;
  80. private float fallbackInteractorDistance = -1.0f;
  81. private bool spewDebugText = false;
  82. private bool showDebugInteractables = false;
  83. public bool showDebugText = false;
  84. /// <summary>
  85. /// 当前覆盖的物体
  86. /// </summary>
  87. private GrabInteractable _hoveringInteractable;
  88. public GrabInteractable hoveringInteractable
  89. {
  90. get { return _hoveringInteractable; }
  91. set
  92. {
  93. if (_hoveringInteractable != value)
  94. {
  95. if (_hoveringInteractable != null)
  96. {
  97. HandDebugLog("HoverEnd " + _hoveringInteractable.gameObject);
  98. _hoveringInteractable.SendMessage(HandEventConst.OnHandHoverEnd, this, SendMessageOptions.DontRequireReceiver);
  99. }
  100. //Note: The _hoveringInteractable can change after sending the OnHandHoverEnd message so we need to check it again before broadcasting this message
  101. if (_hoveringInteractable != null)
  102. {
  103. this.BroadcastMessage(HandEventConst.OnParentHoverEnd, _hoveringInteractable, SendMessageOptions.DontRequireReceiver); // let objects attached to the hand know that a hover has ended
  104. }
  105. _hoveringInteractable = value;
  106. if (hoveringInteractable != null)
  107. {
  108. HandDebugLog("HoverBegin " + _hoveringInteractable.gameObject);
  109. _hoveringInteractable.SendMessage(HandEventConst.OnHandHoverBegin, this, SendMessageOptions.DontRequireReceiver);
  110. //Note: The _hoveringInteractable can change after sending the OnHandHoverBegin message so we need to check it again before broadcasting this message
  111. if (_hoveringInteractable != null)
  112. {
  113. this.BroadcastMessage(HandEventConst.OnParentHoverBegin, _hoveringInteractable, SendMessageOptions.DontRequireReceiver); // let objects attached to the hand know that a hover has begun
  114. }
  115. }
  116. }
  117. }
  118. }
  119. public GameObject currentGrabbedObject
  120. {
  121. get
  122. {
  123. CleanUpGrabbedObjectStack();
  124. if (grabbedObjects.Count > 0)
  125. {
  126. return grabbedObjects[grabbedObjects.Count - 1].grabbedObject;
  127. }
  128. return null;
  129. }
  130. }
  131. public GrabbedObject? currentGrabbedObjectInfo
  132. {
  133. get
  134. {
  135. CleanUpGrabbedObjectStack();
  136. if (grabbedObjects.Count > 0)
  137. {
  138. return grabbedObjects[grabbedObjects.Count - 1];
  139. }
  140. return null;
  141. }
  142. }
  143. public void ForceHoverUnlock()
  144. {
  145. hoverLocked = false;
  146. }
  147. private void HandDebugLog(string msg)
  148. {
  149. if (spewDebugText)
  150. {
  151. RKLog.Info($"<b>[Rokid Interaction ]</b>] Hand {this.name}:{msg}");
  152. }
  153. }
  154. protected virtual void UpdateHovering()
  155. {
  156. if (hoverLocked) return;
  157. float closestDistance = float.MaxValue;
  158. GrabInteractable closestInteractable = null;
  159. if (useHoverSphere)
  160. {
  161. float scaledHoverRadius = hoverSphereRadius * Mathf.Abs(hoverSphereTransform.transform.lossyScale.x);
  162. // RKLog.Info("scaleHoverRadius:" + scaledHoverRadius);
  163. CheckHoveringForTransform(hoverSphereTransform.position, scaledHoverRadius, ref closestDistance, ref closestInteractable, Color.green);
  164. }
  165. hoveringInteractable = closestInteractable;
  166. }
  167. private void CleanUpGrabbedObjectStack()
  168. {
  169. grabbedObjects.RemoveAll(l => l.grabbedObject == null);
  170. }
  171. public bool ObjectIsGrabbed(GameObject go)
  172. {
  173. for (int i = 0; i < grabbedObjects.Count; i++)
  174. {
  175. if (grabbedObjects[i].grabbedObject == go)
  176. return true;
  177. }
  178. return false;
  179. }
  180. protected virtual void Start()
  181. {
  182. if (this.gameObject.layer == 0)
  183. RKLog.Warning("<b>[Rokid Interaction]</b> Hand is on default layer. This puts unnecessary strain on hover checks as it is always true for hand colliders (which are then ignored).");
  184. else
  185. hoverLayerMask &= ~(1 << this.gameObject.layer); //ignore self for hovering
  186. // allocate array for colliders
  187. overlappingColliders = new Collider[ColliderArraySize];
  188. if (showDebugText)
  189. {
  190. if (debugText == null)
  191. {
  192. debugText = new GameObject("_debug_text").AddComponent<TextMesh>();
  193. debugText.fontSize = 120;
  194. debugText.characterSize = 0.001f;
  195. debugText.transform.parent = transform;
  196. debugText.color = Color.green;
  197. debugText.transform.localScale = Vector3.one;
  198. debugText.transform.localRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
  199. }
  200. debugText.gameObject.SetActive(false);
  201. }
  202. GesEventInput.OnTrackedFailed += OnTrackedFailed;
  203. GesEventInput.OnTrackedSuccess += OnTrackSuccess;
  204. InteractorStateChange.OnHandDragStatusChanged += OnHandDragStatusChanged;
  205. }
  206. private void OnHandDragStatusChanged(HandType hand, bool dragging)
  207. {
  208. if (hand == this.handType)
  209. {
  210. hoverLocked = dragging;
  211. }
  212. }
  213. private void OnTrackSuccess(HandType handType)
  214. {
  215. if (handType == this.handType)
  216. {
  217. debugText?.gameObject.SetActive(true);
  218. }
  219. }
  220. private void OnTrackedFailed(HandType handType)
  221. {
  222. if (handType == this.handType || handType == HandType.None)
  223. {
  224. debugText?.gameObject.SetActive(false);
  225. while (grabbedObjects.Count > 0)
  226. {
  227. ReleaseObject(grabbedObjects[0].grabbedObject);
  228. }
  229. OnHandHoverEnd?.Invoke(handType);
  230. OnReleasedFromHand?.Invoke(handType);
  231. }
  232. }
  233. private void OnDestroy()
  234. {
  235. GesEventInput.OnTrackedFailed -= OnTrackedFailed;
  236. GesEventInput.OnTrackedSuccess -= OnTrackSuccess;
  237. InteractorStateChange.OnHandDragStatusChanged -= OnHandDragStatusChanged;
  238. }
  239. private void OnEnable()
  240. {
  241. float hoverUpdateBegin = ((otherHand != null) && (otherHand.GetInstanceID() < GetInstanceID())) ? (0.5f * hoverUpdateInterval) : (0.0f);
  242. InvokeRepeating("UpdateHovering", hoverUpdateBegin, hoverUpdateInterval);
  243. InvokeRepeating("UpdateDebugText", hoverUpdateBegin, hoverUpdateInterval);
  244. }
  245. protected virtual void Update()
  246. {
  247. UpdateFallback();
  248. GameObject grabbedObject = currentGrabbedObject;
  249. if (grabbedObject != null)
  250. {
  251. grabbedObject.SendMessage(HandEventConst.OnGrabbedUpdate, this, SendMessageOptions.DontRequireReceiver);
  252. OnGrabbedUpdate?.Invoke(handType, grabbedObject);
  253. }
  254. if (hoveringInteractable != null)
  255. {
  256. if (hovering == false)
  257. {
  258. OnHandHoverBegin?.Invoke(handType, hoveringInteractable.gameObject);
  259. hovering = true;
  260. }
  261. OnHandHoverUpdate?.Invoke(handType, hoveringInteractable.gameObject);
  262. RKLog.KeyInfo($"====Hand==== Hovering :{hoveringInteractable.gameObject.name}");
  263. hoveringInteractable.SendMessage(HandEventConst.OnHandHoverUpdate, this, SendMessageOptions.DontRequireReceiver);
  264. }
  265. else
  266. {
  267. if (hovering)
  268. {
  269. OnHandHoverEnd?.Invoke(handType);
  270. hovering = false;
  271. }
  272. }
  273. }
  274. private void UpdateDebugText()
  275. {
  276. if (showDebugText)
  277. {
  278. if (handType == HandType.RightHand)
  279. {
  280. debugText.transform.localPosition = new Vector3(-0.05f, 0.0f, 0.0f);
  281. debugText.alignment = TextAlignment.Right;
  282. debugText.anchor = TextAnchor.UpperRight;
  283. }
  284. else
  285. {
  286. debugText.transform.localPosition = new Vector3(0.05f, 0.0f, 0.0f);
  287. debugText.alignment = TextAlignment.Left;
  288. debugText.anchor = TextAnchor.UpperLeft;
  289. }
  290. debugText.text = string.Format(
  291. "Hovering: {0}\n" +
  292. "Hover Lock: {1}\n" +
  293. "Grabbed: {2}\n" +
  294. "Total Grabbed: {3}\n" +
  295. "Type: {4}\n",
  296. (hoveringInteractable ? hoveringInteractable.gameObject.name : "null"),
  297. hoverLocked,
  298. (currentGrabbedObject ? currentGrabbedObject.name : "null"),
  299. grabbedObjects.Count,
  300. handType.ToString());
  301. }
  302. else
  303. {
  304. if (debugText != null)
  305. {
  306. Destroy(debugText.gameObject);
  307. }
  308. }
  309. }
  310. //-------------------------------------------------
  311. protected virtual void UpdateFallback()
  312. {
  313. if (IsUnityEditor() && GesEventInput.Instance.GetInteractorType(handType) == InteractorType.Near)
  314. {
  315. Ray ray = MainCameraCache.mainCamera.ScreenPointToRay(Input.mousePosition);
  316. if (grabbedObjects.Count > 0)
  317. {
  318. // Holding down the mouse:
  319. // move around a fixed distance from the camera
  320. transform.position = ray.origin + fallbackInteractorDistance * ray.direction;
  321. }
  322. else
  323. {
  324. // Not holding down the mouse:
  325. // cast out a ray to see what we should mouse over
  326. // Don't want to hit the hand and anything underneath it
  327. // So move it back behind the camera when we do the raycast
  328. Vector3 oldPosition = transform.position;
  329. transform.position = MainCameraCache.mainCamera.transform.forward * (-1000.0f);
  330. RaycastHit raycastHit;
  331. if (Physics.Raycast(ray, out raycastHit, fallbackMaxDistanceNoItem))
  332. {
  333. transform.position = raycastHit.point;
  334. // Remember this distance in case we click and drag the mouse
  335. fallbackInteractorDistance = Mathf.Min(fallbackMaxDistanceNoItem, raycastHit.distance);
  336. }
  337. else if (fallbackInteractorDistance > 0.0f)
  338. {
  339. // Move it around at the distance we last had a hit
  340. transform.position = ray.origin + Mathf.Min(fallbackMaxDistanceNoItem, fallbackInteractorDistance) * ray.direction;
  341. }
  342. else
  343. {
  344. // Didn't hit, just leave it where it was
  345. transform.position = oldPosition;
  346. }
  347. }
  348. }
  349. }
  350. public void Hide()
  351. {
  352. }
  353. public void Show()
  354. {
  355. }
  356. /// <summary>
  357. /// 释放物体
  358. /// </summary>
  359. /// <param name="objectToRelease">需要释放的对象</param>
  360. /// <param name="restoreOriginalParent">是否将对象设置回原来的父对象</param>
  361. public void ReleaseObject(GameObject objectToRelease, bool restoreOriginalParent = true)
  362. {
  363. int index = grabbedObjects.FindIndex(l => l.grabbedObject == objectToRelease);
  364. if (index != -1)
  365. {
  366. HandDebugLog("ReleaseObject " + objectToRelease);
  367. GameObject prevTopObject = currentGrabbedObject;
  368. // if (grabbedObjects[index].interactable != null)
  369. // {
  370. // if (grabbedObjects[index].interactable.hideHandOnGrabbed)
  371. // Show();
  372. // }
  373. Transform parentTransform = null;
  374. if (grabbedObjects[index].isParentedToHand)
  375. {
  376. if (restoreOriginalParent && (grabbedObjects[index].originalParent != null))
  377. {
  378. parentTransform = grabbedObjects[index].originalParent.transform;
  379. }
  380. if (grabbedObjects[index].grabbedObject != null)
  381. {
  382. grabbedObjects[index].grabbedObject.transform.parent = parentTransform;
  383. }
  384. }
  385. if (grabbedObjects[index].HasAttachFlag(GrabFlags.TurnOnKinematic))
  386. {
  387. if (grabbedObjects[index].grabbedRigidbody != null)
  388. {
  389. grabbedObjects[index].grabbedRigidbody.isKinematic = grabbedObjects[index].grabbedRigidbodyWasKinematic;
  390. grabbedObjects[index].grabbedRigidbody.collisionDetectionMode = grabbedObjects[index].collisionDetectionMode;
  391. }
  392. }
  393. if (grabbedObjects[index].HasAttachFlag(GrabFlags.TurnOffGravity))
  394. {
  395. if (grabbedObjects[index].grabbedObject != null)
  396. {
  397. if (grabbedObjects[index].grabbedRigidbody != null)
  398. grabbedObjects[index].grabbedRigidbody.useGravity = grabbedObjects[index].grabbedRigidbodyUsedGravity;
  399. }
  400. }
  401. if (grabbedObjects[index].grabbedObject != null)
  402. {
  403. if (grabbedObjects[index].interactable == null || (grabbedObjects[index].interactable != null && grabbedObjects[index].interactable.isDestroying == false))
  404. grabbedObjects[index].grabbedObject.SetActive(true);
  405. grabbedObjects[index].grabbedObject.SendMessage(HandEventConst.OnReleaseFromHand, this, SendMessageOptions.DontRequireReceiver);
  406. OnReleasedFromHand?.Invoke(handType);
  407. }
  408. grabbedObjects.RemoveAt(index);
  409. CleanUpGrabbedObjectStack();
  410. GameObject newTopObject = currentGrabbedObject;
  411. hoverLocked = false;
  412. //Give focus to the top most object on the stack if it changed
  413. if (newTopObject != null && newTopObject != prevTopObject)
  414. {
  415. newTopObject.SetActive(true);
  416. newTopObject.SendMessage(HandEventConst.OnHandFocusAcquired, this, SendMessageOptions.DontRequireReceiver);
  417. }
  418. }
  419. CleanUpGrabbedObjectStack();
  420. }
  421. protected virtual void FixedUpdate()
  422. {
  423. if (currentGrabbedObject != null)
  424. {
  425. GrabbedObject grabbedInfo = currentGrabbedObjectInfo.Value;
  426. if (grabbedInfo.grabbedObject != null)
  427. {
  428. if (grabbedInfo.HasAttachFlag(GrabFlags.VelocityMovement))
  429. {
  430. if (grabbedInfo.interactable.attachEaseIn == false || grabbedInfo.interactable.snapAttachEaseInCompleted)
  431. UpdateAttachedVelocity(grabbedInfo);
  432. /*if (attachedInfo.interactable.handFollowTransformPosition)
  433. {
  434. skeleton.transform.position = TargetSkeletonPosition(attachedInfo);
  435. skeleton.transform.rotation = attachedInfo.attachedObject.transform.rotation * attachedInfo.skeletonLockRotation;
  436. }*/
  437. }
  438. else
  439. {
  440. if (grabbedInfo.HasAttachFlag(GrabFlags.ParentToHand))
  441. {
  442. grabbedInfo.grabbedObject.transform.position = TargetItemPosition(grabbedInfo);
  443. grabbedInfo.grabbedObject.transform.rotation = TargetItemRotation(grabbedInfo);
  444. }
  445. }
  446. if (grabbedInfo.interactable.attachEaseIn)
  447. {
  448. float t = RemapNumberClamped(Time.time, grabbedInfo.grabTime, grabbedInfo.grabTime + grabbedInfo.interactable.snapAttachEaseInTime, 0.0f, 1.0f);
  449. if (t < 1.0f)
  450. {
  451. if (grabbedInfo.HasAttachFlag(GrabFlags.VelocityMovement))
  452. {
  453. grabbedInfo.grabbedRigidbody.velocity = Vector3.zero;
  454. grabbedInfo.grabbedRigidbody.angularVelocity = Vector3.zero;
  455. }
  456. t = grabbedInfo.interactable.snapAttachEaseInCurve.Evaluate(t);
  457. grabbedInfo.grabbedObject.transform.position = Vector3.Lerp(grabbedInfo.easeSourcePosition, TargetItemPosition(grabbedInfo), t);
  458. grabbedInfo.grabbedObject.transform.rotation = Quaternion.Lerp(grabbedInfo.easeSourceRotation, TargetItemRotation(grabbedInfo), t);
  459. }
  460. else if (!grabbedInfo.interactable.snapAttachEaseInCompleted)
  461. {
  462. grabbedInfo.interactable.gameObject.SendMessage("OnThrowableAttachEaseInCompleted", this, SendMessageOptions.DontRequireReceiver);
  463. grabbedInfo.interactable.snapAttachEaseInCompleted = true;
  464. }
  465. }
  466. }
  467. }
  468. }
  469. public static float RemapNumberClamped(float num, float low1, float high1, float low2, float high2)
  470. {
  471. return Mathf.Clamp(RemapNumber(num, low1, high1, low2, high2), Mathf.Min(low2, high2), Mathf.Max(low2, high2));
  472. }
  473. //-------------------------------------------------
  474. // Remap num from range 1 to range 2
  475. //-------------------------------------------------
  476. public static float RemapNumber(float num, float low1, float high1, float low2, float high2)
  477. {
  478. return low2 + (num - low1) * (high2 - low2) / (high1 - low1);
  479. }
  480. /// <summary>
  481. /// 抓取物体
  482. /// </summary>
  483. /// <param name="objectToGrabbed">被抓取的物体</param>
  484. /// <param name="grabbedWithType">抓取的类型</param>
  485. /// <param name="flags">抓取的flag</param>
  486. /// <param name="grabbedOffset">抓取的位置偏差</param>
  487. public void GrabObject(GameObject objectToGrabbed, GrabTypes grabbedWithType, GrabFlags flags = defaultGrabFlags, Transform grabbedOffset = null)
  488. {
  489. GrabbedObject grabObject = new GrabbedObject();
  490. grabObject.grabFlags = flags;
  491. grabObject.attachedOffsetTransform = grabbedOffset;
  492. grabObject.grabTime = Time.time;
  493. if (flags == 0)
  494. {
  495. flags = defaultGrabFlags;
  496. }
  497. //Make suer top object on stack in non-null
  498. CleanUpGrabbedObjectStack();
  499. if (ObjectIsGrabbed(objectToGrabbed))
  500. ReleaseObject(objectToGrabbed);
  501. if (grabObject.HasAttachFlag(GrabFlags.ReleaseFromOtherHand))
  502. {
  503. if (otherHand != null)
  504. otherHand.ReleaseObject(objectToGrabbed);
  505. }
  506. if (grabObject.HasAttachFlag(GrabFlags.DetachOthers))
  507. {
  508. //Detach all the object from the stack
  509. while (grabbedObjects.Count > 0)
  510. {
  511. ReleaseObject(grabbedObjects[0].grabbedObject);
  512. }
  513. }
  514. if (currentGrabbedObject)
  515. {
  516. currentGrabbedObject.SendMessage(HandEventConst.OnHandFocusLost, this, SendMessageOptions.DontRequireReceiver);
  517. }
  518. grabObject.grabbedObject = objectToGrabbed;
  519. grabObject.interactable = objectToGrabbed.GetComponent<GrabInteractable>();
  520. grabObject.handAttachmentPointTransform = this.transform;
  521. if (grabObject.interactable != null)
  522. {
  523. if (grabObject.interactable.attachEaseIn)
  524. {
  525. grabObject.easeSourcePosition = grabObject.grabbedObject.transform.position;
  526. grabObject.easeSourceRotation = grabObject.grabbedObject.transform.rotation;
  527. grabObject.interactable.snapAttachEaseInCompleted = false;
  528. }
  529. if (grabObject.interactable.useHandObjectAttachmentPoint)
  530. grabObject.handAttachmentPointTransform = objectGrabbedPoint;
  531. // if (grabObject.interactable.hideHandOnGrabbed)
  532. // Hide();
  533. }
  534. grabObject.originalParent = objectToGrabbed.transform.parent != null ? objectToGrabbed.transform.parent.gameObject : null;
  535. grabObject.grabbedRigidbody = objectToGrabbed.GetComponent<Rigidbody>();
  536. if (grabObject.grabbedRigidbody != null)
  537. {
  538. if (grabObject.interactable.grabbedToHand != null)//already attached to another hand
  539. {
  540. // if it was attached to another hand ,get the flags from that hand
  541. for (int attachedIndex = 0; attachedIndex < grabObject.interactable.grabbedToHand.grabbedObjects.Count; attachedIndex++)
  542. {
  543. GrabbedObject attachedObjectInList = grabObject.interactable.grabbedToHand.grabbedObjects[attachedIndex];
  544. if (attachedObjectInList.interactable == grabObject.interactable)
  545. {
  546. grabObject.grabbedRigidbodyWasKinematic = attachedObjectInList.grabbedRigidbodyWasKinematic;
  547. grabObject.grabbedRigidbodyUsedGravity = attachedObjectInList.grabbedRigidbodyUsedGravity;
  548. grabObject.originalParent = attachedObjectInList.originalParent;
  549. }
  550. }
  551. }
  552. else
  553. {
  554. grabObject.grabbedRigidbodyWasKinematic = grabObject.grabbedRigidbody.isKinematic;
  555. grabObject.grabbedRigidbodyUsedGravity = grabObject.grabbedRigidbody.useGravity;
  556. }
  557. }
  558. grabObject.grabbedWithType = grabbedWithType;
  559. if (grabObject.HasAttachFlag(GrabFlags.ParentToHand))
  560. {
  561. //Parent the object to the hand
  562. objectToGrabbed.transform.parent = this.transform;
  563. grabObject.isParentedToHand = true;
  564. grabObject.initialPositionalOffset = grabObject.handAttachmentPointTransform.InverseTransformPoint(objectToGrabbed.transform.position);
  565. grabObject.initialRotationalOffset = Quaternion.Inverse(grabObject.handAttachmentPointTransform.rotation) * objectToGrabbed.transform.rotation;
  566. }
  567. else
  568. {
  569. grabObject.isParentedToHand = false;
  570. }
  571. if (grabObject.HasAttachFlag(GrabFlags.SnapOnAttach))
  572. {
  573. if (grabbedWithType == GrabTypes.Pinch)
  574. {
  575. grabObject.handAttachmentPointTransform = objectSnapPoint;
  576. grabObject.initialPositionalOffset = grabObject.handAttachmentPointTransform.InverseTransformPoint(objectToGrabbed.transform.position);
  577. grabObject.initialRotationalOffset = Quaternion.Inverse(grabObject.handAttachmentPointTransform.rotation) * objectToGrabbed.transform.rotation;
  578. }
  579. }
  580. if (grabObject.HasAttachFlag(GrabFlags.TurnOnKinematic))
  581. {
  582. if (grabObject.grabbedRigidbody != null)
  583. {
  584. grabObject.collisionDetectionMode = grabObject.grabbedRigidbody.collisionDetectionMode;
  585. if (grabObject.collisionDetectionMode == CollisionDetectionMode.Continuous)
  586. grabObject.grabbedRigidbody.collisionDetectionMode = CollisionDetectionMode.Discrete;
  587. grabObject.grabbedRigidbody.isKinematic = true;
  588. }
  589. }
  590. if (grabObject.HasAttachFlag(GrabFlags.TurnOffGravity))
  591. {
  592. if (grabObject.grabbedRigidbody != null)
  593. {
  594. grabObject.grabbedRigidbody.useGravity = false;
  595. }
  596. }
  597. if (grabObject.interactable != null && grabObject.interactable.attachEaseIn)
  598. {
  599. grabObject.grabbedObject.transform.position = grabObject.easeSourcePosition;
  600. grabObject.grabbedObject.transform.rotation = grabObject.easeSourceRotation;
  601. }
  602. grabbedObjects.Add(grabObject);
  603. UpdateHovering();
  604. HandDebugLog("GrabbedToHand" + objectToGrabbed);
  605. objectToGrabbed.SendMessage(HandEventConst.OnGrabbedToHand, this, SendMessageOptions.DontRequireReceiver);
  606. OnGrabbedToHand?.Invoke(handType, objectToGrabbed);
  607. }
  608. /// <summary>
  609. /// 检查所有覆盖的物体
  610. /// </summary>
  611. /// <param name="hoverPosition"></param>
  612. /// <param name="hoverRadius"></param>
  613. /// <param name="closestDistance"></param>
  614. /// <param name="closestInteractable"></param>
  615. /// <param name="debugColor"></param>
  616. /// <returns></returns>
  617. protected virtual bool CheckHoveringForTransform(Vector3 hoverPosition, float hoverRadius, ref float closestDistance, ref GrabInteractable closestInteractable, Color debugColor)
  618. {
  619. bool foundCloser = false;
  620. //null out old vals
  621. for (int i = 0; i < overlappingColliders.Length; i++)
  622. {
  623. overlappingColliders[i] = null;
  624. }
  625. int numColliding = Physics.OverlapSphereNonAlloc(hoverPosition, hoverRadius, overlappingColliders, hoverLayerMask.value);
  626. if (numColliding >= ColliderArraySize)
  627. RKLog.Warning("This hand is overlapping the max number of colliders");
  628. int iActualColliderCount = 0;
  629. // Pick the closest hovering
  630. for (int colliderIndex = 0; colliderIndex < overlappingColliders.Length; colliderIndex++)
  631. {
  632. Collider collider = overlappingColliders[colliderIndex];
  633. if (collider == null)
  634. continue;
  635. GrabInteractable contacting = collider.GetComponentInParent<GrabInteractable>();
  636. // Yeah, it's null, skip
  637. if (contacting == null)
  638. continue;
  639. // Can't hover over the object if it's attached
  640. bool hoveringOverAttached = false;
  641. for (int attachedIndex = 0; attachedIndex < grabbedObjects.Count; attachedIndex++)
  642. {
  643. if (grabbedObjects[attachedIndex].grabbedObject == contacting.gameObject)
  644. {
  645. hoveringOverAttached = true;
  646. break;
  647. }
  648. }
  649. if (hoveringOverAttached)
  650. continue;
  651. // Best candidate so far...
  652. float distance = Vector3.Distance(contacting.transform.position, hoverPosition);
  653. //float distance = Vector3.Distance(collider.bounds.center, hoverPosition);
  654. bool lowerPriority = false;
  655. if (closestInteractable != null)
  656. { // compare to closest interactable to check priority
  657. lowerPriority = contacting.hoverPriority < closestInteractable.hoverPriority;
  658. }
  659. bool isCloser = (distance < closestDistance);
  660. if (isCloser && !lowerPriority)
  661. {
  662. closestDistance = distance;
  663. closestInteractable = contacting;
  664. foundCloser = true;
  665. }
  666. iActualColliderCount++;
  667. }
  668. if (showDebugInteractables && foundCloser)
  669. {
  670. Debug.DrawLine(hoverPosition, closestInteractable.transform.position, debugColor, .05f, false);
  671. }
  672. if (iActualColliderCount > 0 && iActualColliderCount != prevOverlappingColliders)
  673. {
  674. prevOverlappingColliders = iActualColliderCount;
  675. }
  676. return foundCloser;
  677. }
  678. //-------------------------------------------------
  679. // Continue to hover over this object indefinitely, whether or not the Hand moves out of its interaction trigger volume.
  680. //
  681. // interactable - The Interactable to hover over indefinitely.
  682. //-------------------------------------------------
  683. public void HoverLock(GrabInteractable interactable)
  684. {
  685. HandDebugLog("HoverLock " + interactable);
  686. hoverLocked = true;
  687. hoveringInteractable = interactable;
  688. }
  689. //-------------------------------------------------
  690. // Stop hovering over this object indefinitely.
  691. //
  692. // interactable - The hover-locked Interactable to stop hovering over indefinitely.
  693. //-------------------------------------------------
  694. public void HoverUnlock(GrabInteractable interactable)
  695. {
  696. HandDebugLog("HoverUnlock " + interactable);
  697. if (hoveringInteractable == interactable)
  698. {
  699. hoverLocked = false;
  700. }
  701. }
  702. public bool IsGrabEnding(GameObject grabbedObject)
  703. {
  704. for (int i = 0; i < grabbedObjects.Count; i++)
  705. {
  706. if (grabbedObjects[i].grabbedObject == grabbedObject)
  707. {
  708. return IsGrabRelease();
  709. }
  710. }
  711. return false;
  712. }
  713. public bool IsGrabRelease()
  714. {
  715. if (IsUnityEditor())
  716. {
  717. return !Input.GetMouseButton(0);
  718. }
  719. Vector3 indexPos = GesEventInput.Instance.GetSkeletonPose(SkeletonIndexFlag.INDEX_FINGER_TIP, handType).position;
  720. Vector3 thumbPos = GesEventInput.Instance.GetSkeletonPose(SkeletonIndexFlag.THUMB_TIP, handType).position;
  721. return HandUtils.UnPinchForDistance(indexPos, thumbPos, 0.04f) && GesEventInput.Instance.GetGestureType(handType) != GestureType.Grip;
  722. }
  723. public GrabTypes GetBestGrabbingType(GrabTypes preferred, bool forcePreference = false)
  724. {
  725. if (IsUnityEditor())
  726. {
  727. if (Input.GetMouseButton(0))
  728. return preferred;
  729. else
  730. return GrabTypes.None;
  731. }
  732. if (preferred == GrabTypes.Pinch)
  733. {
  734. if (GesEventInput.Instance.GetHandPress(handType, true))
  735. return GrabTypes.Pinch;
  736. else if (forcePreference)
  737. return GrabTypes.None;
  738. }
  739. if (preferred == GrabTypes.Grip)
  740. {
  741. if (GesEventInput.Instance.GetHandPress(handType, false))
  742. return GrabTypes.Grip;
  743. else if (forcePreference)
  744. return GrabTypes.None;
  745. }
  746. if (GesEventInput.Instance.GetHandPress(handType, true))
  747. return GrabTypes.Pinch;
  748. if (GesEventInput.Instance.GetHandPress(handType, false))
  749. return GrabTypes.Grip;
  750. return GrabTypes.None;
  751. }
  752. /// <summary>
  753. /// 获取开始抓取的类型
  754. /// </summary>
  755. /// <param name="explicitType"></param>
  756. /// <returns></returns>
  757. public GrabTypes GetGrabStarting(GrabTypes explicitType = GrabTypes.None)
  758. {
  759. if (explicitType != GrabTypes.None)
  760. {
  761. if (IsUnityEditor())
  762. {
  763. if (Input.GetMouseButtonDown(0))
  764. {
  765. return explicitType;
  766. }
  767. else
  768. {
  769. return GrabTypes.None;
  770. }
  771. }
  772. else
  773. {
  774. if (explicitType == GrabTypes.Pinch)
  775. return GrabTypes.Pinch;
  776. if (explicitType == GrabTypes.Grip)
  777. return GrabTypes.Grip;
  778. }
  779. }
  780. else
  781. {
  782. if (IsUnityEditor())
  783. {
  784. if (Input.GetMouseButtonDown(0))
  785. return GrabTypes.Grip;
  786. else
  787. return GrabTypes.None;
  788. }
  789. else
  790. {
  791. if (GesEventInput.Instance.GetHandDown(handType, false))
  792. return GrabTypes.Grip;
  793. if (GesEventInput.Instance.GetHandDown(handType, true))
  794. return GrabTypes.Pinch;
  795. }
  796. }
  797. return GrabTypes.None;
  798. }
  799. /// <summary>
  800. /// 获取结束抓取的类型
  801. /// </summary>
  802. /// <param name="explicitType"></param>
  803. /// <returns></returns>
  804. public GrabTypes GetGrabEnding(GrabTypes explicitType = GrabTypes.None)
  805. {
  806. if (explicitType != GrabTypes.None)
  807. {
  808. if (IsUnityEditor())
  809. {
  810. if (Input.GetMouseButtonUp(0))
  811. return explicitType;
  812. else
  813. return GrabTypes.None;
  814. }
  815. else
  816. {
  817. if (explicitType == GrabTypes.Pinch)
  818. return GrabTypes.Pinch;
  819. if (explicitType == GrabTypes.Grip)
  820. return GrabTypes.Grip;
  821. }
  822. }
  823. else
  824. {
  825. if (IsUnityEditor())
  826. {
  827. if (Input.GetMouseButtonUp(0))
  828. return GrabTypes.Grip;
  829. else
  830. return GrabTypes.None;
  831. }
  832. else
  833. {
  834. if (GesEventInput.Instance.GetHandUp(handType, false))
  835. return GrabTypes.Grip;
  836. if (GesEventInput.Instance.GetHandUp(handType, true))
  837. return GrabTypes.Pinch;
  838. }
  839. }
  840. return GrabTypes.None;
  841. }
  842. private bool IsUnityEditor()
  843. {
  844. return Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.WindowsEditor;
  845. }
  846. protected const float MaxVelocityChange = 10f;
  847. protected const float VelocityMagic = 1500f;
  848. protected const float AngularVelocityMagic = 50f;
  849. protected const float MaxAngularVelocityChange = 20f;
  850. protected bool GetUpdatedAttachedVelocities(GrabbedObject grabbedObjectInfo, out Vector3 velocityTarget, out Vector3 angularTarget)
  851. {
  852. bool realNumbers = false;
  853. float velocityMagic = VelocityMagic;
  854. float angularVelocityMagic = AngularVelocityMagic;
  855. Vector3 targetItemPosition = TargetItemPosition(grabbedObjectInfo);
  856. Vector3 positionDelta = (targetItemPosition - grabbedObjectInfo.grabbedRigidbody.position);
  857. velocityTarget = (positionDelta * velocityMagic * Time.deltaTime);
  858. if (float.IsNaN(velocityTarget.x) == false && float.IsInfinity(velocityTarget.x) == false)
  859. {
  860. if (IsUnityEditor())
  861. velocityTarget /= 10; //hacky fix for fallback
  862. realNumbers = true;
  863. }
  864. else
  865. velocityTarget = Vector3.zero;
  866. Quaternion targetItemRotation = TargetItemRotation(grabbedObjectInfo);
  867. Quaternion rotationDelta = targetItemRotation * Quaternion.Inverse(grabbedObjectInfo.grabbedRigidbody.transform.rotation);
  868. float angle;
  869. Vector3 axis;
  870. rotationDelta.ToAngleAxis(out angle, out axis);
  871. if (angle > 180)
  872. angle -= 360;
  873. if (angle != 0 && float.IsNaN(axis.x) == false && float.IsInfinity(axis.x) == false)
  874. {
  875. angularTarget = angle * axis * angularVelocityMagic * Time.deltaTime;
  876. if (IsUnityEditor())
  877. angularTarget /= 10; //hacky fix for fallback
  878. realNumbers &= true;
  879. }
  880. else
  881. angularTarget = Vector3.zero;
  882. // RKLog.Info($"====Hand====: 手的移动速度: {velocityTarget},手的角速度: {angularTarget}");
  883. return realNumbers;
  884. }
  885. protected void UpdateAttachedVelocity(GrabbedObject grabbedObjectInfo)
  886. {
  887. RKLog.Info("Update Attached Velocity");
  888. Vector3 velocityTarget, angularTarget;
  889. bool success = GetUpdatedAttachedVelocities(grabbedObjectInfo, out velocityTarget, out angularTarget);
  890. if (success)
  891. {
  892. float scale = Utils.GetLossyScale(currentGrabbedObjectInfo.Value.handAttachmentPointTransform);
  893. float maxAngularVelocityChange = MaxAngularVelocityChange * scale;
  894. float maxVelocityChange = MaxVelocityChange * scale;
  895. grabbedObjectInfo.grabbedRigidbody.velocity = Vector3.MoveTowards(grabbedObjectInfo.grabbedRigidbody.velocity, velocityTarget, maxVelocityChange);
  896. grabbedObjectInfo.grabbedRigidbody.angularVelocity = Vector3.MoveTowards(grabbedObjectInfo.grabbedRigidbody.angularVelocity, angularTarget, maxAngularVelocityChange);
  897. }
  898. }
  899. protected Quaternion TargetItemRotation(GrabbedObject grabbedObject)
  900. {
  901. return currentGrabbedObjectInfo.Value.handAttachmentPointTransform.rotation * grabbedObject.initialRotationalOffset;
  902. }
  903. protected Vector3 TargetItemPosition(GrabbedObject grabbedObject)
  904. {
  905. RKLog.Info("currentGrabbedObjectInfo.Value.handAttachmentPointTransform:" + currentGrabbedObjectInfo.Value.handAttachmentPointTransform);
  906. return currentGrabbedObjectInfo.Value.handAttachmentPointTransform.TransformPoint(grabbedObject.initialPositionalOffset);
  907. }
  908. public Vector3 GetTrackedObjectVelocity(float timeOffset = 0)
  909. {
  910. Vector3 velocityTarget, angularTarget;
  911. GetUpdatedAttachedVelocities(currentGrabbedObjectInfo.Value, out velocityTarget, out angularTarget);
  912. return velocityTarget;
  913. }
  914. public Vector3 GetTrackedObjectAngularVelocity(float timeOffset = 0)
  915. {
  916. Vector3 velocityTarget, angularTarget;
  917. GetUpdatedAttachedVelocities(currentGrabbedObjectInfo.Value, out velocityTarget, out angularTarget);
  918. return angularTarget;
  919. }
  920. }
  921. }