VRGrabber.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [RequireComponent(typeof(Rigidbody))]
  5. [RequireComponent(typeof(VelocityTracker))]
  6. public class VRGrabber : MonoBehaviour
  7. {
  8. public void ForceRelease(VRGrabbable grabbable)
  9. {
  10. bool flag = this.m_grabbedObj != null && this.m_grabbedObj == grabbable;
  11. if (flag)
  12. {
  13. this.GrabEnd();
  14. }
  15. }
  16. public void SetGripTransform(string name)
  17. {
  18. for (int i = 0; i < this.gripTransforms.Length; i++)
  19. {
  20. if (this.gripTransforms[i].name == name)
  21. {
  22. this.gripTransform = this.gripTransforms[i].transform;
  23. break;
  24. }
  25. }
  26. }
  27. private void Awake()
  28. {
  29. this.m_anchorOffsetPosition = base.transform.localPosition;
  30. this.m_anchorOffsetRotation = base.transform.localRotation;
  31. if (this.gripTransform == null)
  32. {
  33. this.gripTransform = this.gripTransforms[0].transform;
  34. }
  35. }
  36. private void Start()
  37. {
  38. this.m_velocityTracker = base.GetComponent<VelocityTracker>();
  39. this.m_lastPos = base.transform.position;
  40. this.m_lastRot = base.transform.rotation;
  41. this.m_parentTransform = base.gameObject.transform.parent.transform;
  42. }
  43. private void Update()
  44. {
  45. if (this.m_vrController == null)
  46. {
  47. return;
  48. }
  49. if (this.m_vrController.GetPressDown(AVRControllerButtons.BTN.TRIGGER))
  50. {
  51. this.GrabBegin();
  52. }
  53. else if (this.m_vrController.GetPressUp(AVRControllerButtons.BTN.TRIGGER))
  54. {
  55. this.GrabEnd();
  56. }
  57. }
  58. private void FixedUpdate()
  59. {
  60. if (this.m_vrController == null)
  61. {
  62. return;
  63. }
  64. Vector3 position = this.m_vrController.transform.position;
  65. Quaternion rotation = this.m_vrController.transform.rotation;
  66. Vector3 vector = this.m_parentTransform.TransformPoint(this.m_anchorOffsetPosition + position);
  67. Quaternion rot = this.m_parentTransform.rotation * rotation;
  68. base.GetComponent<Rigidbody>().MovePosition(vector);
  69. base.GetComponent<Rigidbody>().MoveRotation(rot);
  70. if (!this.m_useFixedJointForGrabbedObject && !this.m_parentHeldObject)
  71. {
  72. this.MoveGrabbedObject(vector, rot, false);
  73. }
  74. this.m_lastPos = base.transform.position;
  75. this.m_lastRot = base.transform.rotation;
  76. }
  77. private void OnDestroy()
  78. {
  79. if (this.m_grabbedObj != null)
  80. {
  81. this.GrabEnd();
  82. }
  83. }
  84. private void OnTriggerEnter(Collider otherCollider)
  85. {
  86. VRGrabbable vrgrabbable = otherCollider.GetComponent<VRGrabbable>() ?? otherCollider.GetComponentInParent<VRGrabbable>();
  87. if (vrgrabbable == null)
  88. {
  89. return;
  90. }
  91. int num = 0;
  92. this.m_grabCandidates.TryGetValue(vrgrabbable, out num);
  93. this.m_grabCandidates[vrgrabbable] = num + 1;
  94. }
  95. private void OnTriggerExit(Collider otherCollider)
  96. {
  97. VRGrabbable vrgrabbable = otherCollider.GetComponent<VRGrabbable>() ?? otherCollider.GetComponentInParent<VRGrabbable>();
  98. if (vrgrabbable == null)
  99. {
  100. return;
  101. }
  102. int num = 0;
  103. if (!this.m_grabCandidates.TryGetValue(vrgrabbable, out num))
  104. {
  105. return;
  106. }
  107. if (num > 1)
  108. {
  109. this.m_grabCandidates[vrgrabbable] = num - 1;
  110. }
  111. else
  112. {
  113. this.m_grabCandidates.Remove(vrgrabbable);
  114. }
  115. }
  116. private void CheckForGrabOrRelease(float prevFlex)
  117. {
  118. if (this.m_prevFlex >= 0.55f && prevFlex < 0.55f)
  119. {
  120. this.GrabBegin();
  121. }
  122. else if (this.m_prevFlex <= 0.35f && prevFlex > 0.35f)
  123. {
  124. this.GrabEnd();
  125. }
  126. }
  127. public void GrabBegin(VRGrabbable grabbable)
  128. {
  129. if (grabbable == null || this.m_grabbedObj != null || grabbable.GrabPoints == null || grabbable.GrabPoints.Length <= 0)
  130. {
  131. return;
  132. }
  133. this.OnTriggerEnter(grabbable.GrabPoints[0]);
  134. this.GrabVolumeEnable(true);
  135. this.GrabBegin();
  136. }
  137. private void GrabBegin()
  138. {
  139. float num = float.MaxValue;
  140. VRGrabbable vrgrabbable = null;
  141. Collider grabPoint = null;
  142. foreach (VRGrabbable vrgrabbable2 in this.m_grabCandidates.Keys)
  143. {
  144. if (!vrgrabbable2.IsGrabbed || vrgrabbable2.AllowOffhandGrab)
  145. {
  146. for (int i = 0; i < vrgrabbable2.GrabPoints.Length; i++)
  147. {
  148. Collider collider = vrgrabbable2.GrabPoints[i];
  149. Vector3 b = collider.ClosestPointOnBounds(this.gripTransform.position);
  150. float sqrMagnitude = (this.gripTransform.position - b).sqrMagnitude;
  151. if (sqrMagnitude < num)
  152. {
  153. num = sqrMagnitude;
  154. vrgrabbable = vrgrabbable2;
  155. grabPoint = collider;
  156. }
  157. }
  158. }
  159. }
  160. this.GrabVolumeEnable(false);
  161. if (vrgrabbable != null)
  162. {
  163. if (vrgrabbable.IsGrabbed)
  164. {
  165. vrgrabbable.GrabbedBy.OffhandGrabbed(vrgrabbable);
  166. }
  167. this.m_grabbedObj = vrgrabbable;
  168. this.m_grabbedObj.GrabBegin(this, grabPoint);
  169. if (this.m_useFixedJointForGrabbedObject)
  170. {
  171. FixedJoint fixedJoint = base.gameObject.GetComponent<FixedJoint>() ?? base.gameObject.AddComponent<FixedJoint>();
  172. fixedJoint.connectedBody = this.m_grabbedObj.GetComponent<Rigidbody>();
  173. }
  174. else
  175. {
  176. this.m_lastPos = base.transform.position;
  177. this.m_lastRot = base.transform.rotation;
  178. if (this.m_grabbedObj.SnapPosition)
  179. {
  180. this.m_grabbedObjectPosOff = this.gripTransform.localPosition;
  181. if (this.m_grabbedObj.SnapOffset)
  182. {
  183. Vector3 localPosition = this.m_grabbedObj.SnapOffset.localPosition;
  184. if (this.m_vrController.HandSide)
  185. {
  186. localPosition.x = -localPosition.x;
  187. }
  188. this.m_grabbedObjectPosOff += localPosition;
  189. }
  190. }
  191. else
  192. {
  193. Vector3 vector = this.m_grabbedObj.transform.position - base.transform.position;
  194. vector = Quaternion.Inverse(base.transform.rotation) * vector;
  195. this.m_grabbedObjectPosOff = vector;
  196. }
  197. if (this.m_grabbedObj.SnapOrientation)
  198. {
  199. this.m_grabbedObjectRotOff = this.gripTransform.localRotation;
  200. if (this.m_grabbedObj.SnapOffset)
  201. {
  202. this.m_grabbedObjectRotOff = this.m_grabbedObj.SnapOffset.rotation * this.m_grabbedObjectRotOff;
  203. }
  204. }
  205. else
  206. {
  207. Quaternion grabbedObjectRotOff = Quaternion.Inverse(base.transform.rotation) * this.m_grabbedObj.transform.rotation;
  208. this.m_grabbedObjectRotOff = grabbedObjectRotOff;
  209. }
  210. this.MoveGrabbedObject(this.m_lastPos, this.m_lastRot, true);
  211. if (this.m_parentHeldObject)
  212. {
  213. this.m_grabbedObj.transform.parent = base.transform;
  214. }
  215. }
  216. }
  217. }
  218. private void MoveGrabbedObject(Vector3 pos, Quaternion rot, bool forceTeleport = false)
  219. {
  220. if (this.m_grabbedObj == null)
  221. {
  222. return;
  223. }
  224. if (this.m_grabbedObj.SnapPosition && this.m_frameUpdateSnapPosition)
  225. {
  226. this.m_grabbedObjectPosOff = this.gripTransform.localPosition;
  227. if (this.m_grabbedObj.SnapOffset)
  228. {
  229. Vector3 localPosition = this.m_grabbedObj.SnapOffset.localPosition;
  230. if (this.m_vrController.HandSide)
  231. {
  232. localPosition.x = -localPosition.x;
  233. }
  234. this.m_grabbedObjectPosOff += localPosition;
  235. }
  236. this.m_grabbedObjectRotOff = this.gripTransform.localRotation;
  237. if (this.m_grabbedObj.SnapOffset)
  238. {
  239. this.m_grabbedObjectRotOff = this.m_grabbedObj.SnapOffset.rotation * this.m_grabbedObjectRotOff;
  240. }
  241. }
  242. Rigidbody grabbedRigidbody = this.m_grabbedObj.GrabbedRigidbody;
  243. Vector3 position = pos + rot * this.m_grabbedObjectPosOff;
  244. Quaternion quaternion = rot * this.m_grabbedObjectRotOff;
  245. if (forceTeleport)
  246. {
  247. grabbedRigidbody.transform.position = position;
  248. grabbedRigidbody.transform.rotation = quaternion;
  249. }
  250. else
  251. {
  252. grabbedRigidbody.MovePosition(position);
  253. grabbedRigidbody.MoveRotation(quaternion);
  254. }
  255. }
  256. public void GrabEnd(Vector3 trackedLinearVelocity, Vector3 TrackedAngularVelocity)
  257. {
  258. if (this.m_grabbedObj != null)
  259. {
  260. this.VRGrabbableRelease(trackedLinearVelocity, TrackedAngularVelocity);
  261. }
  262. this.GrabVolumeEnable(true);
  263. }
  264. public void GrabEnd()
  265. {
  266. if (this.m_grabbedObj != null)
  267. {
  268. bool flag = this.m_velocityTracker.TrackedLinearVelocity.magnitude >= 1f;
  269. Vector3 linearVelocity = Vector3.zero;
  270. Vector3 angularVelocity = Vector3.zero;
  271. if (flag)
  272. {
  273. linearVelocity = this.m_velocityTracker.TrackedLinearVelocity;
  274. angularVelocity = this.m_velocityTracker.TrackedAngularVelocity;
  275. }
  276. else
  277. {
  278. linearVelocity = this.m_velocityTracker.FrameLinearVelocity;
  279. angularVelocity = this.m_velocityTracker.FrameAngularVelocity;
  280. }
  281. this.VRGrabbableRelease(linearVelocity, angularVelocity);
  282. }
  283. this.GrabVolumeEnable(true);
  284. }
  285. private void VRGrabbableRelease(Vector3 linearVelocity, Vector3 angularVelocity)
  286. {
  287. Debug.Log(linearVelocity.ToString("G"));
  288. Debug.Log(angularVelocity.ToString("G"));
  289. UnityEngine.Object.Destroy(base.gameObject.GetComponent<FixedJoint>());
  290. this.m_grabbedObj.GrabEnd(linearVelocity, angularVelocity);
  291. if (this.m_parentHeldObject)
  292. {
  293. this.m_grabbedObj.transform.parent = null;
  294. }
  295. this.m_grabbedObj = null;
  296. }
  297. private void GrabVolumeEnable(bool enabled)
  298. {
  299. if (this.m_grabVolumeEnabled == enabled)
  300. {
  301. return;
  302. }
  303. this.m_grabVolumeEnabled = enabled;
  304. for (int i = 0; i < this.m_grabVolumes.Length; i++)
  305. {
  306. Collider collider = this.m_grabVolumes[i];
  307. collider.enabled = this.m_grabVolumeEnabled;
  308. }
  309. if (!this.m_grabVolumeEnabled)
  310. {
  311. this.m_grabCandidates.Clear();
  312. }
  313. }
  314. private void OffhandGrabbed(VRGrabbable grabbable)
  315. {
  316. if (this.m_grabbedObj == grabbable)
  317. {
  318. this.VRGrabbableRelease(Vector3.zero, Vector3.zero);
  319. }
  320. }
  321. public const float THRESH_GRAB_BEGIN = 0.55f;
  322. public const float THRESH_GRAB_END = 0.35f;
  323. public const float THRESH_THROW_SPEED = 1f;
  324. [SerializeField]
  325. private bool m_useFixedJointForGrabbedObject;
  326. [SerializeField]
  327. private bool m_parentHeldObject;
  328. [SerializeField]
  329. private VRGrabber.GripTransformData[] gripTransforms;
  330. [SerializeField]
  331. private Collider[] m_grabVolumes;
  332. [SerializeField]
  333. public AVRControllerButtons m_vrController;
  334. [SerializeField]
  335. private bool m_frameUpdateSnapPosition;
  336. private VelocityTracker m_velocityTracker;
  337. private bool m_grabVolumeEnabled = true;
  338. private Vector3 m_lastPos;
  339. private Quaternion m_lastRot;
  340. private Quaternion m_anchorOffsetRotation;
  341. private Vector3 m_anchorOffsetPosition;
  342. private float m_prevFlex;
  343. private Transform m_parentTransform;
  344. private VRGrabbable m_grabbedObj;
  345. private Vector3 m_grabbedObjectPosOff;
  346. private Quaternion m_grabbedObjectRotOff;
  347. private Dictionary<VRGrabbable, int> m_grabCandidates = new Dictionary<VRGrabbable, int>();
  348. private Transform gripTransform;
  349. [Serializable]
  350. private struct GripTransformData
  351. {
  352. public Transform transform;
  353. public string name;
  354. }
  355. }