OvrGripCollider.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using wf;
  5. [RequireComponent(typeof(VelocityTracker))]
  6. public class OvrGripCollider : MonoBehaviour
  7. {
  8. public virtual void Init(AVRControllerButtons f_ctrl)
  9. {
  10. this.ctrl_btn_ = f_ctrl;
  11. this.collder_ = base.gameObject.GetComponent<SphereCollider>();
  12. if (this.collder_ == null)
  13. {
  14. this.collder_ = base.gameObject.AddComponent<SphereCollider>();
  15. }
  16. float radius = 0.1f;
  17. this.collder_.radius = radius;
  18. this.collder_.isTrigger = true;
  19. this.rigidbody_ = base.gameObject.GetComponent<Rigidbody>();
  20. if (this.rigidbody_ == null)
  21. {
  22. this.rigidbody_ = base.gameObject.AddComponent<Rigidbody>();
  23. }
  24. this.rigidbody_.useGravity = false;
  25. this.rigidbody_.isKinematic = true;
  26. this.velocity_tracker_ = base.gameObject.GetComponent<VelocityTracker>();
  27. if (this.velocity_tracker_ == null)
  28. {
  29. this.velocity_tracker_ = base.gameObject.AddComponent<VelocityTracker>();
  30. }
  31. this.velocity_tracker_.m_showGizmos = false;
  32. this.trans_dummy_ = base.gameObject.transform.Find("trans_dummy");
  33. if (this.trans_dummy_ == null)
  34. {
  35. this.trans_dummy_ = new GameObject
  36. {
  37. name = "trans_dummy"
  38. }.transform;
  39. this.trans_dummy_.SetParent(base.transform, false);
  40. }
  41. base.gameObject.layer = 18;
  42. }
  43. public virtual void Update()
  44. {
  45. if (this.ctrl_btn_ != null)
  46. {
  47. if (this.ctrl_btn_.GetPressDown(AVRControllerButtons.BTN.VIRTUAL_GRUB))
  48. {
  49. this.press_down_trigger_ = true;
  50. }
  51. if (!this.ctrl_btn_.GetPress(AVRControllerButtons.BTN.VIRTUAL_GRUB))
  52. {
  53. if (this.mode_scale_control_)
  54. {
  55. this.pair_obj_.press_down_trigger_ = false;
  56. if (this.pair_obj_.ResetGrip())
  57. {
  58. this.pair_obj_.ctrl_btn_.Haptic(this.m_byHaptic, this.m_fTime);
  59. }
  60. }
  61. this.press_down_trigger_ = false;
  62. if (this.ResetGrip())
  63. {
  64. this.ctrl_btn_.Haptic(this.m_byHaptic, this.m_fTime);
  65. }
  66. return;
  67. }
  68. }
  69. if (!this.grip)
  70. {
  71. return;
  72. }
  73. if (this.mode_scale_control_)
  74. {
  75. float num = Vector3.Distance(this.pair_object.transform.position, base.transform.position) / this.scale_distance_;
  76. float num2 = wf.Math.RoundMinMax(this.init_scale_ * num, this.scale_min_max_.x, this.scale_min_max_.y);
  77. this.lock_object_trans_.localScale = new Vector3(num2, num2, num2);
  78. WorldTransformAxis component = this.lock_object_trans_.GetComponent<WorldTransformAxis>();
  79. if (component != null)
  80. {
  81. component.Apply();
  82. }
  83. }
  84. else if (this.m_bEnableGrab)
  85. {
  86. this.lock_object_trans_.position = this.trans_dummy_.position;
  87. this.lock_object_trans_.rotation = this.trans_dummy_.rotation;
  88. WorldTransformAxis component2 = this.lock_object_trans_.GetComponent<WorldTransformAxis>();
  89. if (component2 != null)
  90. {
  91. component2.Apply();
  92. }
  93. }
  94. }
  95. public virtual bool ResetGrip()
  96. {
  97. if (this.trans_dummy_ == null)
  98. {
  99. return false;
  100. }
  101. this.mode_scale_control_ = false;
  102. this.m_bEnableGrab = true;
  103. bool grip = this.grip;
  104. GameObject gripObject = null;
  105. if (grip)
  106. {
  107. gripObject = this.lock_object_trans_.gameObject;
  108. }
  109. if (this.lock_object_trans_ != null)
  110. {
  111. WorldTransformAxis component = this.lock_object_trans_.GetComponent<WorldTransformAxis>();
  112. if (component != null)
  113. {
  114. component.is_grip = false;
  115. }
  116. }
  117. this.lock_object_trans_ = null;
  118. if (this.trans_dummy_.transform.parent != base.gameObject.transform)
  119. {
  120. this.trans_dummy_.SetParent(base.gameObject.transform, false);
  121. }
  122. if (grip && this.on_release_object_ != null)
  123. {
  124. this.on_release_object_(gripObject, this.velocity_tracker_.TrackedLinearVelocity, this.velocity_tracker_.TrackedAngularVelocity);
  125. }
  126. return grip;
  127. }
  128. public virtual void OnTriggerEnter(Collider other)
  129. {
  130. this.m_listCollider.RemoveAll((GameObject a) => a == null);
  131. if (this.m_listCollider.Find((GameObject a) => a.GetInstanceID() == other.gameObject.GetInstanceID()) == null)
  132. {
  133. this.m_listCollider.Add(other.gameObject);
  134. }
  135. }
  136. protected virtual void OnTriggerStay(Collider col)
  137. {
  138. if (this.grip || this.mode_scale_control_)
  139. {
  140. return;
  141. }
  142. if (this.press_down_trigger_)
  143. {
  144. this.press_down_trigger_ = false;
  145. Transform transform = this.GetTargetTransform(col.gameObject);
  146. if (transform == null)
  147. {
  148. transform = this.GetTargetTransformComm(col.gameObject);
  149. if (transform == null)
  150. {
  151. return;
  152. }
  153. }
  154. if (this.pair_obj_ != null && this.pair_obj_.lock_object_trans_ != null && this.pair_obj_.lock_object_trans_ == transform)
  155. {
  156. GameObject worldTransformAxisParent = PhotoWindowManager.GetWorldTransformAxisParent();
  157. if (worldTransformAxisParent != null)
  158. {
  159. bool flag = transform.GetComponent<WorldTransformAxis>() != null;
  160. if (!flag)
  161. {
  162. WorldTransformAxis[] componentsInChildren = worldTransformAxisParent.GetComponentsInChildren<WorldTransformAxis>(true);
  163. foreach (WorldTransformAxis worldTransformAxis in componentsInChildren)
  164. {
  165. if (!(worldTransformAxis.TargetObject == null))
  166. {
  167. if (worldTransformAxis.TargetObject.transform == this.pair_obj_.lock_object_trans_ && worldTransformAxis.ScaleMinMax.x != worldTransformAxis.ScaleMinMax.y)
  168. {
  169. this.scale_min_max_ = worldTransformAxis.ScaleMinMax;
  170. flag = true;
  171. break;
  172. }
  173. }
  174. }
  175. }
  176. else
  177. {
  178. this.scale_min_max_ = transform.GetComponent<WorldTransformAxis>().ScaleMinMax;
  179. if (this.scale_min_max_.x == this.scale_min_max_.y)
  180. {
  181. flag = false;
  182. }
  183. }
  184. if (flag)
  185. {
  186. this.init_scale_ = this.pair_obj_.lock_object_trans_.localScale.x;
  187. this.scale_distance_ = Vector3.Distance(this.pair_obj_.transform.position, base.transform.position);
  188. this.ResetGrip();
  189. this.pair_obj_.ResetGrip();
  190. this.lock_object_trans_ = transform;
  191. this.ctrl_btn_.Haptic(this.m_byHaptic, this.m_fTime);
  192. this.pair_obj_.ctrl_btn_.Haptic(this.m_byHaptic, this.m_fTime);
  193. this.pair_obj_.mode_scale_control_ = (this.mode_scale_control_ = true);
  194. }
  195. }
  196. return;
  197. }
  198. this.ResetGrip();
  199. this.lock_object_trans_ = transform;
  200. this.ctrl_btn_.Haptic(this.m_byHaptic, this.m_fTime);
  201. WorldTransformAxis component = this.lock_object_trans_.GetComponent<WorldTransformAxis>();
  202. if (component != null)
  203. {
  204. component.is_grip = true;
  205. }
  206. this.m_bEnableGrab = this.IsEnableGrab(col.gameObject);
  207. if (this.m_bEnableGrab)
  208. {
  209. this.trans_dummy_.SetParent(base.gameObject.transform, false);
  210. this.trans_dummy_.SetParent(this.lock_object_trans_.parent, true);
  211. this.trans_dummy_.localPosition = this.lock_object_trans_.localPosition;
  212. this.trans_dummy_.localRotation = this.lock_object_trans_.localRotation;
  213. this.trans_dummy_.SetParent(base.gameObject.transform, true);
  214. }
  215. if (this.on_grab_object_ != null)
  216. {
  217. this.on_grab_object_(col.gameObject);
  218. }
  219. }
  220. }
  221. protected virtual void OnTriggerExit(Collider other)
  222. {
  223. this.m_listCollider.RemoveAll((GameObject a) => a == null);
  224. GameObject gameObject = this.m_listCollider.Find((GameObject g) => g.GetInstanceID() == other.gameObject.GetInstanceID());
  225. if (gameObject != null)
  226. {
  227. this.m_listCollider.Remove(gameObject);
  228. }
  229. }
  230. protected virtual Transform GetTargetTransform(GameObject obj)
  231. {
  232. if (obj == null || obj.transform == null)
  233. {
  234. return null;
  235. }
  236. bool flag = false;
  237. if (obj.layer == 17)
  238. {
  239. flag = true;
  240. }
  241. string b = "AllOffset";
  242. Transform transform = null;
  243. if (flag)
  244. {
  245. transform = obj.transform;
  246. while (!(transform == null) && !(transform.parent == null))
  247. {
  248. if (transform.parent.gameObject.name == b)
  249. {
  250. return transform;
  251. }
  252. transform = transform.parent;
  253. }
  254. return null;
  255. }
  256. GameObject worldTransformAxisParent = PhotoWindowManager.GetWorldTransformAxisParent();
  257. if (worldTransformAxisParent != null)
  258. {
  259. Transform transform2 = obj.transform;
  260. while (!(transform2 == null) && !(transform2.GetComponent<GizmoRender>() != null))
  261. {
  262. transform2 = transform2.parent;
  263. }
  264. bool flag2 = true;
  265. if (flag2 && transform2 != null)
  266. {
  267. WorldTransformAxis[] componentsInChildren = worldTransformAxisParent.GetComponentsInChildren<WorldTransformAxis>(true);
  268. foreach (WorldTransformAxis worldTransformAxis in componentsInChildren)
  269. {
  270. if (worldTransformAxis.TargetObject != null && worldTransformAxis.TargetObject == transform2.gameObject)
  271. {
  272. transform = transform2;
  273. break;
  274. }
  275. }
  276. }
  277. }
  278. return transform;
  279. }
  280. protected virtual Transform GetTargetTransformComm(GameObject obj)
  281. {
  282. if (obj == null || obj.transform == null)
  283. {
  284. return null;
  285. }
  286. if (obj.layer != 17)
  287. {
  288. return null;
  289. }
  290. return obj.transform;
  291. }
  292. protected virtual bool IsEnableGrab(GameObject obj)
  293. {
  294. VREventTriggerGrab component = obj.GetComponent<VREventTriggerGrab>();
  295. return !(component != null) || !component.m_bNoMove;
  296. }
  297. public new virtual bool enabled
  298. {
  299. get
  300. {
  301. return base.enabled;
  302. }
  303. set
  304. {
  305. if (!value)
  306. {
  307. this.ClearColliderList();
  308. }
  309. base.enabled = value;
  310. this.collder.enabled = value;
  311. }
  312. }
  313. public virtual void OnDisable()
  314. {
  315. this.ClearColliderList();
  316. }
  317. public virtual void ClearColliderList()
  318. {
  319. this.ResetGrip();
  320. this.m_listCollider.Clear();
  321. }
  322. public virtual SphereCollider collder
  323. {
  324. get
  325. {
  326. return this.collder_;
  327. }
  328. }
  329. public virtual Rigidbody rigidbody
  330. {
  331. get
  332. {
  333. return this.rigidbody_;
  334. }
  335. }
  336. public virtual OvrGripCollider pair_object
  337. {
  338. get
  339. {
  340. return this.pair_obj_;
  341. }
  342. set
  343. {
  344. this.pair_obj_ = value;
  345. }
  346. }
  347. public virtual bool grip
  348. {
  349. get
  350. {
  351. return this.lock_object_trans_ != null;
  352. }
  353. }
  354. public virtual List<GameObject> collider_list
  355. {
  356. get
  357. {
  358. return this.m_listCollider;
  359. }
  360. }
  361. public virtual OvrGripCollider.OnGrabObject on_grab_obj_callback
  362. {
  363. set
  364. {
  365. this.on_grab_object_ = value;
  366. }
  367. }
  368. public virtual OvrGripCollider.OnReleaseObject on_release_obj_callback
  369. {
  370. set
  371. {
  372. this.on_release_object_ = value;
  373. }
  374. }
  375. public byte m_byHaptic = 200;
  376. public float m_fTime = 0.05f;
  377. protected bool m_bEnableGrab = true;
  378. protected bool mode_scale_control_;
  379. protected float scale_distance_;
  380. protected float init_scale_;
  381. protected Vector2 scale_min_max_;
  382. protected Transform trans_dummy_;
  383. protected Transform lock_object_trans_;
  384. protected OvrGripCollider pair_obj_;
  385. protected SphereCollider collder_;
  386. protected Rigidbody rigidbody_;
  387. protected VelocityTracker velocity_tracker_;
  388. protected List<GameObject> m_listCollider = new List<GameObject>();
  389. protected OvrGripCollider.OnGrabObject on_grab_object_;
  390. protected OvrGripCollider.OnReleaseObject on_release_object_;
  391. protected AVRControllerButtons ctrl_btn_;
  392. protected bool press_down_trigger_;
  393. public delegate void OnGrabObject(GameObject target_obj);
  394. public delegate void OnReleaseObject(GameObject gripObject, Vector3 linearVelocity, Vector3 angularVelocity);
  395. }