SteamVR_LaserPointerMod.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. // Modified by Trung-Hoa Ha, Lydia Ebbinghaus and Jana-Sophie Schönfeld.
  3. // Enable code that is commented out to use the laser for scrolling.
  4. // Experimental Idea: If Laser is on the bottom of the menu: Scroll Down. If on top of the menu Scroll Up.
  5. // Improvement needed.
  6. using UnityEngine;
  7. using Valve.VR;
  8. public class SteamVR_LaserPointerMod : MonoBehaviour
  9. {
  10. public SteamVR_Behaviour_Pose pose;
  11. //public SteamVR_Action_Boolean interactWithUI = SteamVR_Input.__actions_default_in_InteractUI;
  12. public SteamVR_Action_Boolean interactWithUI = SteamVR_Input.GetBooleanAction("InteractUI");
  13. //public SteamVR_Action_Boolean touchup = SteamVR_Input.GetBooleanAction("TouchUp");
  14. //public SteamVR_Action_Boolean touchdown = SteamVR_Input.GetBooleanAction("TouchDown");
  15. public bool active = true;
  16. public Color color;
  17. public float thickness = 0.002f;
  18. public Color clickColor = Color.green;
  19. public GameObject holder;
  20. public GameObject pointer;
  21. bool isActive = false;
  22. public bool addRigidBody = false;
  23. public Transform reference;
  24. public event PointerEventHandler PointerIn;
  25. public event PointerEventHandler PointerOut;
  26. public event PointerEventHandler PointerClick;
  27. // Modified.
  28. /*public event PointerEventHandler PointerUp;
  29. public event PointerEventHandler PointerDown;*/
  30. Transform previousContact = null;
  31. private void Start()
  32. {
  33. if (pose == null)
  34. pose = this.GetComponent<SteamVR_Behaviour_Pose>();
  35. if (pose == null)
  36. Debug.LogError("No SteamVR_Behaviour_Pose component found on this object", this);
  37. if (interactWithUI == null)
  38. Debug.LogError("No ui interaction action has been set on this component.", this);
  39. holder = new GameObject();
  40. holder.transform.parent = this.transform;
  41. holder.transform.localPosition = Vector3.zero;
  42. holder.transform.localRotation = Quaternion.identity;
  43. pointer = GameObject.CreatePrimitive(PrimitiveType.Cube);
  44. pointer.transform.parent = holder.transform;
  45. pointer.transform.localScale = new Vector3(thickness, thickness, 100f);
  46. pointer.transform.localPosition = new Vector3(0f, 0f, 50f);
  47. pointer.transform.localRotation = Quaternion.identity;
  48. BoxCollider collider = pointer.GetComponent<BoxCollider>();
  49. if (addRigidBody)
  50. {
  51. if (collider)
  52. {
  53. collider.isTrigger = true;
  54. }
  55. Rigidbody rigidBody = pointer.AddComponent<Rigidbody>();
  56. rigidBody.isKinematic = true;
  57. }
  58. else
  59. {
  60. if (collider)
  61. {
  62. Object.Destroy(collider);
  63. }
  64. }
  65. Material newMaterial = new Material(Shader.Find("Unlit/Color"));
  66. newMaterial.SetColor("_Color", color);
  67. pointer.GetComponent<MeshRenderer>().material = newMaterial;
  68. }
  69. public virtual void OnPointerIn(PointerEventArgs e)
  70. {
  71. if (PointerIn != null)
  72. PointerIn(this, e);
  73. }
  74. public virtual void OnPointerClick(PointerEventArgs e)
  75. {
  76. if (PointerClick != null)
  77. PointerClick(this, e);
  78. }
  79. // Modified.
  80. /*public virtual void OnPointerDownClick(PointerEventArgs e)
  81. {
  82. if (PointerDown != null)
  83. PointerDown(this, e);
  84. }
  85. public virtual void OnPointerUpClick(PointerEventArgs e)
  86. {
  87. if (PointerUp != null)
  88. PointerUp(this, e);
  89. }*/
  90. public virtual void OnPointerOut(PointerEventArgs e)
  91. {
  92. if (PointerOut != null)
  93. PointerOut(this, e);
  94. }
  95. private void Update()
  96. {
  97. // Modified.
  98. if(active != isActive)
  99. {
  100. isActive = active;
  101. pointer?.SetActive(isActive);
  102. }
  103. if(!isActive) return;
  104. float dist = 100f;
  105. Ray raycast = new Ray(transform.position, transform.forward);
  106. RaycastHit hit;
  107. bool bHit = Physics.Raycast(raycast, out hit);
  108. // Modified.
  109. /*if(bHit)
  110. {
  111. bool scrollUp = hit.point.y > hit.transform.position.y +hit.transform.localScale.y-0.5f
  112. && (hit.point.y <= hit.transform.position.y +hit.transform.localScale.y);
  113. bool scrollDown = (hit.point.y < (hit.transform.position.y + 0.5f))
  114. && (hit.point.y >= hit.transform.position.y);
  115. if(bHit)
  116. {
  117. if(scrollUp)
  118. {
  119. Debug.Log("Scrolling up." + hit.transform.name);
  120. PointerEventArgs argsUp = new PointerEventArgs();
  121. argsUp.fromInputSource = pose.inputSource;
  122. argsUp.distance = hit.distance;
  123. argsUp.flags = 0;
  124. argsUp.target = hit.transform;
  125. OnPointerUpClick(argsUp);
  126. }
  127. else if(scrollDown)
  128. {
  129. Debug.Log("Scrolling down." + hit.transform.name);
  130. PointerEventArgs argsDown = new PointerEventArgs();
  131. argsDown.fromInputSource = pose.inputSource;
  132. argsDown.distance = hit.distance;
  133. argsDown.flags = 0;
  134. argsDown.target = hit.transform;
  135. OnPointerDownClick(argsDown);
  136. }
  137. }
  138. }
  139. */
  140. if (previousContact && previousContact != hit.transform)
  141. {
  142. PointerEventArgs args = new PointerEventArgs();
  143. args.fromInputSource = pose.inputSource;
  144. args.distance = 0f;
  145. args.flags = 0;
  146. args.target = previousContact;
  147. OnPointerOut(args);
  148. previousContact = null;
  149. }
  150. if (bHit && previousContact != hit.transform)
  151. {
  152. PointerEventArgs argsIn = new PointerEventArgs();
  153. argsIn.fromInputSource = pose.inputSource;
  154. argsIn.distance = hit.distance;
  155. argsIn.flags = 0;
  156. argsIn.target = hit.transform;
  157. OnPointerIn(argsIn);
  158. previousContact = hit.transform;
  159. }
  160. if (!bHit)
  161. {
  162. previousContact = null;
  163. }
  164. if (bHit && hit.distance < 100f)
  165. {
  166. dist = hit.distance;
  167. }
  168. if (bHit && interactWithUI.GetStateUp(pose.inputSource))
  169. {
  170. PointerEventArgs argsClick = new PointerEventArgs();
  171. argsClick.fromInputSource = pose.inputSource;
  172. argsClick.distance = hit.distance;
  173. argsClick.flags = 0;
  174. argsClick.target = hit.transform;
  175. OnPointerClick(argsClick);
  176. }
  177. // Modified.
  178. /*
  179. if (bHit && touchdown.GetState(pose.inputSource))
  180. {
  181. PointerEventArgs argsClick = new PointerEventArgs();
  182. argsClick.fromInputSource = pose.inputSource;
  183. argsClick.distance = hit.distance;
  184. argsClick.flags = 0;
  185. argsClick.target = hit.transform;
  186. OnPointerDownClick(argsClick);
  187. }
  188. if (bHit && touchup.GetState(pose.inputSource))
  189. {
  190. PointerEventArgs argsClick = new PointerEventArgs();
  191. argsClick.fromInputSource = pose.inputSource;
  192. argsClick.distance = hit.distance;
  193. argsClick.flags = 0;
  194. argsClick.target = hit.transform;
  195. OnPointerUpClick(argsClick);
  196. }*/
  197. if (interactWithUI != null && interactWithUI.GetState(pose.inputSource))
  198. {
  199. pointer.transform.localScale = new Vector3(thickness * 5f, thickness * 5f, dist);
  200. pointer.GetComponent<MeshRenderer>().material.color = clickColor;
  201. }
  202. else
  203. {
  204. pointer.transform.localScale = new Vector3(thickness, thickness, dist);
  205. pointer.GetComponent<MeshRenderer>().material.color = color;
  206. }
  207. pointer.transform.localPosition = new Vector3(0f, 0f, dist / 2f);
  208. }
  209. }
  210. public struct PointerEventArgs
  211. {
  212. public SteamVR_Input_Sources fromInputSource;
  213. public uint flags;
  214. public float distance;
  215. public Transform target;
  216. }
  217. public delegate void PointerEventHandler(object sender, PointerEventArgs e);