TriggerForPinch.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace EZXR.Glass.Inputs
  6. {
  7. /// <summary>
  8. /// 用于触发近距离交互的基础检测区域,位于拇指指尖和食指指尖中间,像近距离捏住后进行移动、旋转、缩放等操作都是以这个区域的检测为基准
  9. /// </summary>
  10. public class TriggerForPinch : MonoBehaviour
  11. {
  12. public HandInfo handInfo;
  13. public HandTouch objectGrab;
  14. public RotateUseHandle rotateUseHandle;
  15. public ScaleUseCorner scaleUseCorner;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. }
  20. // Update is called once per frame
  21. void Update()
  22. {
  23. #region 设置触发区的位置为拇指和食指指尖中心,长度也和两个指尖的距离相关
  24. Vector3 thumbTip;
  25. Vector3 indexTip;
  26. if (handInfo.Exist)
  27. {
  28. thumbTip = handInfo.GetJointData(HandJointType.Thumb_3).position;
  29. indexTip = handInfo.GetJointData(HandJointType.Index_4).position;
  30. }
  31. else
  32. {
  33. thumbTip = new Vector3(9999, 9999, 9999);
  34. indexTip = new Vector3(10000, 10000, 10000);
  35. }
  36. transform.position = (thumbTip + indexTip) / 2.0f;
  37. transform.localScale = new Vector3(0.01f, 0.01f, (thumbTip - indexTip).magnitude + 0.02f);//食指和拇指指尖的碰撞检测区域的长度要完全覆盖食指和拇指的球(为了可以用手指指尖做单指触碰)
  38. transform.LookAt(indexTip);
  39. #endregion
  40. }
  41. /// <summary>
  42. /// 配置拇指和食指之间触发区的起点和终点(通常就是食指指尖点和拇指指尖点)
  43. /// </summary>
  44. /// <param name="joint0"></param>
  45. /// <param name="joint1"></param>
  46. /// <param name="handRoot">用于获取所属的手的信息</param>
  47. public void SetUp(Transform handRoot)
  48. {
  49. handInfo = handRoot.GetComponent<HandInfo>();
  50. objectGrab = handRoot.GetComponent<HandTouch>();
  51. rotateUseHandle = handRoot.GetComponent<RotateUseHandle>();
  52. scaleUseCorner = handRoot.GetComponent<ScaleUseCorner>();
  53. }
  54. private void OnTriggerEnter(Collider other)
  55. {
  56. if (other.gameObject.layer != gameObject.layer)
  57. {
  58. if (handInfo != null)
  59. {
  60. //“当用户的手是up状态”且“当前手没有进入其他Trigger”时
  61. if (!handInfo.isPinching /*&& !handInfo.isCloseContacting*/)
  62. {
  63. if (other.tag == "SpatialObject")
  64. {
  65. //handInfo.isCloseContacting = true;
  66. handInfo.CurCloseContactingTarget = other;
  67. objectGrab.ForOnTriggerEnter(other);
  68. }
  69. else if (other.tag == "SpatialHandler")
  70. {
  71. rotateUseHandle.ForOnTriggerEnter(other);
  72. scaleUseCorner.ForOnTriggerEnter(other);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. private void OnTriggerStay(Collider other)
  79. {
  80. if (other.gameObject.layer != gameObject.layer)
  81. {
  82. if (handInfo != null)
  83. {
  84. //“当用户的手是up状态”且“当前手没有进入其他Trigger”时
  85. if (!handInfo.isPinching /*&& !handInfo.isCloseContacting*/)
  86. {
  87. if (other.tag == "SpatialObject")
  88. {
  89. //handInfo.isCloseContacting = true;
  90. handInfo.CurCloseContactingTarget = other;
  91. objectGrab.ForOnTriggerStay(other);
  92. }
  93. else if (other.tag == "SpatialHandler")
  94. {
  95. rotateUseHandle.ForOnTriggerStay(other);
  96. scaleUseCorner.ForOnTriggerStay(other);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. private void OnTriggerExit(Collider other)
  103. {
  104. if (other.gameObject.layer != gameObject.layer)
  105. {
  106. if (handInfo != null)
  107. {
  108. //没有进行抓取操作的时候,通过这里进行一些状态重置
  109. if (!handInfo.isPinching)
  110. {
  111. if (other.tag == "SpatialObject")
  112. {
  113. if (handInfo.CurCloseContactingTarget == other)
  114. {
  115. handInfo.CurCloseContactingTarget = null;
  116. }
  117. Debug.Log("InteractWithFingerTip exit: " + other.name);
  118. objectGrab.ForOnTriggerExit(other);
  119. }
  120. else if (other.tag == "SpatialHandler")
  121. {
  122. rotateUseHandle.ForOnTriggerExit(other);
  123. scaleUseCorner.ForOnTriggerExit(other);
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }