OVRRaycastManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.XR;
  8. public class OVRRaycastManager : MonoBehaviour
  9. {
  10. public Transform RightHand;
  11. public OVRPhysicsRaycaster OVRPhysicsRaycaster;
  12. public LineRenderer LineRendererRay;
  13. public LineRenderer LineRendererRaySelected;
  14. public Vector3 f = new Vector3(0, 0, 1);
  15. Vector3 u = new Vector3(0, 1, 1);
  16. Vector3 r = new Vector3(1, 0, 1);
  17. public bool IsInterationMode;
  18. private float Distance = 0.15f;
  19. private float SpeedRotate = 10f;
  20. private float CentrPositionInInteractMode;
  21. private Vector3 lastPositionHand;
  22. public GameObject Ball;
  23. public GameObject BallVisible;
  24. public GameObject Plane;
  25. public bool SelectedObj;
  26. private Vector3 position;
  27. private List<Rigidbody> _listRigs = new List<Rigidbody>();
  28. private List<JointsHighlighter.ChildObject> _listChildObjects = new List<JointsHighlighter.ChildObject>();
  29. void Awake()
  30. {
  31. OVRPhysicsRaycaster = GetComponent<OVRPhysicsRaycaster>();
  32. }
  33. void FixedUpdate()
  34. {
  35. float v = OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, OVRInput.Controller.RTouch);
  36. if (OVRPhysicsRaycaster!= null && OVRPhysicsRaycaster.CurrentGameObjectInteraction!= null)
  37. {
  38. if (v > 0.2f && OVRPhysicsRaycaster.CurrentGameObjectInteraction != null && !SelectedObj)
  39. {
  40. SelectedObj = true;
  41. hj = OVRPhysicsRaycaster.CurrentGameObjectInteraction.GetComponent<ColliderProxy>().HingeJoint;
  42. cj = OVRPhysicsRaycaster.CurrentGameObjectInteraction.GetComponent<ColliderProxy>().ConfJoint;
  43. InitBall(hj);
  44. if (hj == null)
  45. {
  46. OVRPhysicsRaycaster.CurrentGameObjectInteraction = cj.gameObject;
  47. Ball.transform.SetParent(cj.transform.parent);
  48. Ball.transform.position = cj.transform.position + cj.transform.forward * 0.3f;
  49. BallVisible.transform.SetParent(cj.transform.parent);
  50. BallVisible.transform.position = cj.transform.position + cj.transform.forward * 0.3f;
  51. Plane.transform.SetParent(cj.transform);
  52. Plane.transform.localPosition = Vector3.zero;
  53. Plane.transform.localEulerAngles = Vector3.zero;
  54. Plane.transform.SetParent(cj.transform.parent);
  55. position = InputTracking.GetLocalPosition(XRNode.RightHand) - offset;
  56. lastPositionHand = position;
  57. IsInterationMode = true;
  58. BallVisible.SetActive(true);
  59. }
  60. else
  61. {
  62. _listChildObjects = hj.GetComponent<JointsHighlighter>().GetChildren();
  63. Rigidbody[] rigs = hj.transform.GetComponentsInChildren<Rigidbody>();
  64. _listRigs = rigs.ToList();
  65. if (hj.transform.parent.parent.GetComponent<Rigidbody>() != null)
  66. {
  67. _listRigs.Add(hj.transform.parent.parent.GetComponent<Rigidbody>());
  68. }
  69. if (hj.transform.parent.GetComponent<Rigidbody>() != null)
  70. {
  71. _listRigs.Add(hj.transform.parent.GetComponent<Rigidbody>());
  72. }
  73. _listRigs.Add(hj.GetComponent<Rigidbody>());
  74. foreach (Rigidbody r in _listRigs)
  75. {
  76. if (hj.useLimits )
  77. {
  78. r.isKinematic = false;
  79. }
  80. else
  81. {
  82. r.isKinematic = true;
  83. }
  84. r.angularVelocity = Vector3.zero;
  85. }
  86. Ball.transform.SetParent(hj.transform.parent);
  87. Ball.transform.position = hj.transform.position + hj.transform.forward * 0.3f;
  88. BallVisible.transform.SetParent(hj.transform.parent);
  89. Plane.transform.SetParent(hj.transform);
  90. Plane.transform.localPosition = Vector3.zero;
  91. Plane.transform.localEulerAngles = Vector3.zero;
  92. Plane.transform.SetParent(hj.transform.parent);
  93. position = InputTracking.GetLocalPosition(XRNode.RightHand) - offset;
  94. lastPositionHand = position;
  95. IsInterationMode = true;
  96. LineRendererRay.enabled = false;
  97. BallVisible.SetActive(true);
  98. LineRendererRaySelected.enabled = true;
  99. }
  100. }
  101. if (hj == null)
  102. {
  103. if (IsInterationMode && OVRPhysicsRaycaster.CurrentGameObjectInteraction != null)
  104. {
  105. UpdateConfigurableJointRotation();
  106. }
  107. }
  108. else
  109. {
  110. if (IsInterationMode && OVRPhysicsRaycaster.CurrentGameObjectInteraction != null)
  111. {
  112. UpdateConfigurableJointRotation();
  113. }
  114. }
  115. LineRendererRaySelected.SetPosition(0, RightHand.transform.position);
  116. LineRendererRaySelected.SetPosition(1, BallVisible.transform.position);
  117. }
  118. if (v<=0)
  119. {
  120. SelectedObj = false;
  121. IsInterationMode = false;
  122. LineRendererRay.enabled= true;
  123. OVRPhysicsRaycaster.CurrentGameObjectInteraction = null;
  124. hj = null;
  125. cj = null;
  126. IsGotFirstPosition = false;
  127. _listChildObjects.Clear();
  128. foreach (Rigidbody r in _listRigs)
  129. {
  130. r.angularVelocity = Vector3.zero;
  131. r.isKinematic = false;
  132. }
  133. LineRendererRaySelected.enabled = false;
  134. BallVisible.SetActive(false);
  135. }
  136. }
  137. private Vector3 offset;
  138. private void UpdateConfigurableJointRotation()
  139. {
  140. if (hj == null)
  141. {
  142. hj = OVRPhysicsRaycaster.CurrentGameObjectInteraction.GetComponent<JointController>().HingeJoint;
  143. }
  144. if (hj != null)
  145. {
  146. float p = 0;
  147. var position = InputTracking.GetLocalPosition(XRNode.RightHand) - offset;
  148. var delta = lastPositionHand - position;
  149. Ball.transform.position -= delta;
  150. BallVisible.transform.position -= delta;
  151. lastPositionHand = position;
  152. Vector3 Point = Vector3.zero;
  153. Math3d.LinePlaneIntersection(out Point, BallVisible.transform.position, Plane.transform.up, Plane.transform.up * -1, Plane.transform.position);
  154. Ball.transform.position = Point;
  155. var pos = hj.transform.position + (Ball.transform.position - hj.transform.position).normalized * Distance;
  156. offset += Ball.transform.position - pos;
  157. //BallVisible.transform.position -= offset;
  158. Ball.transform.position = pos;
  159. var lighter = OVRPhysicsRaycaster.CurrentGameObjectInteraction.GetComponent<ColliderProxy>().HingeJoint.GetComponent<JointsHighlighter>();
  160. lighter.Highlighted();
  161. var rig = OVRPhysicsRaycaster.CurrentGameObjectInteraction.GetComponent<ColliderProxy>().HingeJoint.GetComponent<Rigidbody>();
  162. var go = new GameObject();
  163. go.transform.position = rig.transform.position;
  164. go.transform.LookAt(Ball.transform);
  165. go.transform.SetParent(rig.transform.parent);
  166. if (!IsGotFirstPosition)
  167. {
  168. LastPosition = go.transform.rotation;
  169. IsGotFirstPosition = true;
  170. Normal = go.transform.up;
  171. return;
  172. }
  173. var deltaRotation = Quaternion.Angle(go.transform.rotation, LastPosition);
  174. if (deltaRotation > 160)
  175. {
  176. deltaRotation -= 180;
  177. }
  178. Vector3 forward = rig.transform.TransformDirection(Vector3.forward);
  179. Vector3 toOther = Ball.transform.position - rig.transform.position;
  180. float sign = 1;
  181. if (Vector3.Dot(forward, toOther) < 0)
  182. {
  183. sign = -1;
  184. }
  185. rig.MoveRotation(rig.rotation * Quaternion.AngleAxis(deltaRotation * sign /** Time.deltaTime * 30*/, Vector3.up) /*Quaternion.Euler(deltaRotation * sign *Vector3.up)*/);
  186. Destroy(go);
  187. LastPosition = go.transform.rotation;
  188. Normal = go.transform.up;
  189. }
  190. }
  191. private bool IsPartHinge;
  192. HingeJoint hj;
  193. ConfigurableJoint cj;
  194. private bool IsGotFirstPosition;
  195. private Quaternion LastPosition;
  196. private Vector3 Normal;
  197. private void InitBall(HingeJoint hj)
  198. {
  199. //Vector3 point;
  200. //Math3d.LinePlaneIntersection(out point, RightHand.position, RightHand.forward, Plane.transform.up * -1, Plane.transform.position);
  201. Vector3 Point = Vector3.zero;
  202. Math3d.LinePlaneIntersection(out Point, BallVisible.transform.position, Plane.transform.up, Plane.transform.up * -1, Plane.transform.position);
  203. Ball.transform.position = Point;
  204. BallVisible.transform.position = OVRPhysicsRaycaster.PosBall;
  205. var pos = hj.transform.position + (Ball.transform.position - hj.transform.position) * Distance ;
  206. offset = Ball.transform.position - pos;
  207. Ball.transform.position = pos;
  208. }
  209. //private void UpdateObjectRotation()
  210. //{
  211. // foreach (var childobject in _listChildObjects)
  212. // {
  213. // childobject.Update();
  214. // }
  215. // hj = OVRPhysicsRaycaster.CurrentGameObjectInteraction.GetComponent<ColliderProxy>().HingeJoint;
  216. // if(hj == null)
  217. // {
  218. // hj = OVRPhysicsRaycaster.CurrentGameObjectInteraction.GetComponent<JointController>().HingeJoint;
  219. // OVRPhysicsRaycaster.CurrentGameObjectInteraction = hj.gameObject;
  220. // }
  221. // float p = 0;
  222. // var position = InputTracking.GetLocalPosition(XRNode.RightHand);
  223. // var delta = lastPositionHand - position;
  224. // Ball.transform.position -= delta;
  225. // lastPositionHand = position;
  226. // Vector3 Point = Vector3.zero;
  227. // Math3d.LinePlaneIntersection(out Point, RightHand.position, RightHand.forward, Plane.transform.up * -1, Plane.transform.position);
  228. // Ball.transform.position = Point;
  229. // var lighter = OVRPhysicsRaycaster.CurrentGameObjectInteraction.GetComponent<ColliderProxy>().HingeJoint.GetComponent<JointsHighlighter>();
  230. // lighter.Highlighted();
  231. // var rig = OVRPhysicsRaycaster.CurrentGameObjectInteraction.GetComponent<ColliderProxy>().HingeJoint.GetComponent<Rigidbody>();
  232. // var go = new GameObject();
  233. // go.transform.position = rig.transform.position;
  234. // go.transform.LookAt(Ball.transform);
  235. // go.transform.SetParent(rig.transform.parent);
  236. // if(!IsGotFirstPosition)
  237. // {
  238. // LastPosition = go.transform.rotation;
  239. // IsGotFirstPosition = true;
  240. // Normal = go.transform.up;
  241. // return;
  242. // }
  243. // var deltaRotation = Quaternion.Angle(go.transform.rotation, LastPosition);
  244. // if(deltaRotation>160)
  245. // {
  246. // deltaRotation -= 180;
  247. // }
  248. // Vector3 forward = rig.transform.TransformDirection(Vector3.forward);
  249. // Vector3 toOther = Ball.transform.position - rig.transform.position;
  250. // float sign = 1;
  251. // if (Vector3.Dot(forward, toOther) < 0)
  252. // {
  253. // sign = -1;
  254. // }
  255. // rig.MoveRotation(rig.rotation * Quaternion.AngleAxis(deltaRotation * sign /** Time.deltaTime * 30*/, Vector3.up) /*Quaternion.Euler(deltaRotation * sign *Vector3.up)*/);
  256. // Destroy(go);
  257. // LastPosition = go.transform.rotation;
  258. // Normal = go.transform.up;
  259. //}
  260. void OnDrawGizmos()
  261. {
  262. if (IsInterationMode && hj != null)
  263. {
  264. // Draws a blue line from this transform to the target
  265. Gizmos.color = Color.blue;
  266. Gizmos.DrawLine(hj.transform.position, Ball.transform.position);
  267. }
  268. }
  269. Quaternion GetAngleBeetweenQuaternions(Quaternion a, Quaternion b)
  270. {
  271. var v1 = a.eulerAngles;
  272. var v2 = b.eulerAngles;
  273. var cross = Vector3.Cross(v1, v2);
  274. var w = Mathf.Sqrt(v1.normalized.magnitude * v1.normalized.magnitude * v2.normalized.magnitude * v2.normalized.magnitude) + Vector3.Dot(v1,v2);
  275. return new Quaternion(cross.x, cross.y, cross.z, w);
  276. }
  277. }