ScaleUseCorner.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace EZXR.Glass.Inputs
  5. {
  6. public class ScaleUseCorner : MonoBehaviour
  7. {
  8. HandInfo handInfo;
  9. /// <summary>
  10. /// 射线上次射击到的Collider,用于将射线结果转化成OnTriggerStay和OnTriggerExit
  11. /// </summary>
  12. Collider lastRayCastResult = null;
  13. //public LayerMask layerMask;
  14. /// <summary>
  15. /// 要被缩放的目标物体
  16. /// </summary>
  17. Transform target;
  18. Vector3 curTargetScale;
  19. /// <summary>
  20. /// 当前控制的是哪个缩放角
  21. /// </summary>
  22. Transform curCorner;
  23. /// <summary>
  24. /// 缩放角被捏住一瞬间的position
  25. /// </summary>
  26. Vector3 curCornerPos;
  27. /// <summary>
  28. /// 缩放角被捏住一瞬间,缩放角与射线起始点的偏移量
  29. /// </summary>
  30. Vector3 offset;
  31. /// <summary>
  32. /// 缩放角被捏住一瞬间,缩放角和物体坐标点的距离
  33. /// </summary>
  34. float curCornerLength;
  35. /// <summary>
  36. /// 抓取状态,0为没有手进入此物体的Trigger区域,1为手已经进入了Trigger区域(待抓取),2为已经被左手抓起来了
  37. /// </summary>
  38. int grabState;
  39. /// <summary>
  40. /// 缩放倍率
  41. /// </summary>
  42. float scaleFactor;
  43. Vector3 projectedPos;
  44. // Start is called before the first frame update
  45. void Start()
  46. {
  47. handInfo = GetComponent<HandInfo>();
  48. handInfo.Event_GetRayCastResult += OnRayCastHit;
  49. }
  50. private void OnDisable()
  51. {
  52. grabState = 0;
  53. if (curCorner != null)
  54. {
  55. curCorner.GetChild(0).GetComponent<Renderer>().material.color = Color.white;
  56. curCorner.GetChild(1).GetComponent<Renderer>().material.color = Color.white;
  57. curCorner.GetChild(2).GetComponent<Renderer>().material.color = Color.white;
  58. curCorner = null;
  59. }
  60. target = null;
  61. }
  62. // Update is called once per frame
  63. void Update()
  64. {
  65. switch (grabState)
  66. {
  67. case 1:
  68. if (handInfo.isPinching)
  69. {
  70. curCornerPos = curCorner.position;
  71. //缩放角被捏住一瞬间,缩放角与射线起始点的偏移量
  72. offset = curCorner.position - handInfo.rayPoint_Start;
  73. curCornerLength = (curCornerPos - target.position).magnitude;
  74. curTargetScale = target.localScale;
  75. grabState = 2;
  76. }
  77. break;
  78. case 2:
  79. if (handInfo.isPinching)
  80. {
  81. NativeSwapManager.Point3 temp = new NativeSwapManager.Point3(handInfo.rayPoint_Start + offset);
  82. NativeSwapManager.filterPoint(ref temp, curCorner.GetInstanceID());
  83. //射线发射点-目标物体得到的向量在被拽住的缩放角-目标物体得到的向量上进行投影并得到投影长度,除以刚捏住时刻缩放角与目标物体的距离长度,得到缩放系数
  84. projectedPos = Vector3.Project(new Vector3(temp.x, temp.y, temp.z) - target.position, curCornerPos - target.position);
  85. scaleFactor = projectedPos.magnitude / curCornerLength;
  86. target.localScale = scaleFactor * curTargetScale;
  87. }
  88. else
  89. {
  90. grabState = 0;
  91. if (curCorner != null)
  92. {
  93. curCorner.GetChild(0).GetComponent<Renderer>().material.color = Color.white;
  94. curCorner.GetChild(1).GetComponent<Renderer>().material.color = Color.white;
  95. curCorner.GetChild(2).GetComponent<Renderer>().material.color = Color.white;
  96. curCorner = null;
  97. }
  98. target = null;
  99. }
  100. break;
  101. }
  102. //Main.Instance.textMesh.text = "grabState: " + grabState + ",isTouching: ";
  103. }
  104. private void OnDrawGizmos()
  105. {
  106. if (target != null)
  107. {
  108. Gizmos.DrawLine(target.position, projectedPos);
  109. Gizmos.color = Color.red;
  110. Gizmos.DrawLine(target.position, curCornerPos);
  111. //Gizmos.DrawLine(target.position, transform.position);
  112. }
  113. }
  114. /// <summary>
  115. /// 获得射线检测结果,转化成OnTriggerStay和OnTriggerExit
  116. /// </summary>
  117. public void OnRayCastHit(Collider other, bool isUI)
  118. {
  119. //触发和OnTriggerExit基本一致的逻辑
  120. if (lastRayCastResult != other)
  121. {
  122. if (lastRayCastResult != null)
  123. {
  124. //OnTriggerExit(lastRayCastResult);
  125. if (!handInfo.isPinching)
  126. {
  127. if (other != null && other.tag == "SpatialHandler" && lastRayCastResult.name == "ScaleCorner(Clone)")
  128. {
  129. grabState = 0;
  130. if (curCorner != null)
  131. {
  132. curCorner.GetChild(0).GetComponent<Renderer>().material.color = Color.white;
  133. curCorner.GetChild(1).GetComponent<Renderer>().material.color = Color.white;
  134. curCorner.GetChild(2).GetComponent<Renderer>().material.color = Color.white;
  135. }
  136. curCorner = null;
  137. target = null;
  138. }
  139. }
  140. }
  141. lastRayCastResult = other;
  142. }
  143. //触发和OnTriggerStay基本一致的逻辑
  144. if (other != null)
  145. {
  146. if (!handInfo.isPinching && !handInfo.IsCloseContacting)
  147. {
  148. if (other.tag == "SpatialHandler" && other.name == "ScaleCorner(Clone)")
  149. {
  150. grabState = 1;
  151. curCorner = other.transform;
  152. curCorner.GetChild(0).GetComponent<Renderer>().material.color = Color.red;
  153. curCorner.GetChild(1).GetComponent<Renderer>().material.color = Color.red;
  154. curCorner.GetChild(2).GetComponent<Renderer>().material.color = Color.red;
  155. //设置要被缩放的目标物体
  156. target = other.transform.parent;
  157. }
  158. }
  159. }
  160. }
  161. public void ForOnTriggerEnter(Collider other)
  162. {
  163. ForOnTriggerStay(other);
  164. }
  165. public void ForOnTriggerStay(Collider other)
  166. {
  167. if (enabled && other.tag == "SpatialHandler" && other.name == "ScaleCorner(Clone)")
  168. {
  169. grabState = 1;
  170. curCorner = other.transform;
  171. curCorner.GetChild(0).GetComponent<Renderer>().material.color = Color.red;
  172. curCorner.GetChild(1).GetComponent<Renderer>().material.color = Color.red;
  173. curCorner.GetChild(2).GetComponent<Renderer>().material.color = Color.red;
  174. //设置要被缩放的目标物体
  175. target = other.transform.parent;
  176. }
  177. }
  178. public void ForOnTriggerExit(Collider other)
  179. {
  180. if (enabled && other.tag == "SpatialHandler" && other.name == "ScaleCorner(Clone)")
  181. {
  182. grabState = 0;
  183. if (curCorner != null)
  184. {
  185. curCorner.GetChild(0).GetComponent<Renderer>().material.color = Color.white;
  186. curCorner.GetChild(1).GetComponent<Renderer>().material.color = Color.white;
  187. curCorner.GetChild(2).GetComponent<Renderer>().material.color = Color.white;
  188. curCorner = null;
  189. }
  190. target = null;
  191. }
  192. }
  193. }
  194. }