SteamVR_LaserPointer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEngine;
  4. public class SteamVR_LaserPointer : MonoBehaviour
  5. {
  6. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  7. public event PointerEventHandler PointerIn;
  8. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  9. public event PointerEventHandler PointerOut;
  10. private void Start()
  11. {
  12. this.holder = new GameObject();
  13. this.holder.transform.parent = base.transform;
  14. this.holder.transform.localPosition = Vector3.zero;
  15. this.pointer = GameObject.CreatePrimitive(PrimitiveType.Cube);
  16. this.pointer.transform.parent = this.holder.transform;
  17. this.pointer.transform.localScale = new Vector3(this.thickness, this.thickness, 100f);
  18. this.pointer.transform.localPosition = new Vector3(0f, 0f, 50f);
  19. BoxCollider component = this.pointer.GetComponent<BoxCollider>();
  20. if (this.addRigidBody)
  21. {
  22. if (component)
  23. {
  24. component.isTrigger = true;
  25. }
  26. Rigidbody rigidbody = this.pointer.AddComponent<Rigidbody>();
  27. rigidbody.isKinematic = true;
  28. }
  29. else if (component)
  30. {
  31. UnityEngine.Object.Destroy(component);
  32. }
  33. Material material = new Material(Shader.Find("Unlit/Color"));
  34. material.SetColor("_Color", this.color);
  35. this.pointer.GetComponent<MeshRenderer>().material = material;
  36. }
  37. public virtual void OnPointerIn(PointerEventArgs e)
  38. {
  39. if (this.PointerIn != null)
  40. {
  41. this.PointerIn(this, e);
  42. }
  43. }
  44. public virtual void OnPointerOut(PointerEventArgs e)
  45. {
  46. if (this.PointerOut != null)
  47. {
  48. this.PointerOut(this, e);
  49. }
  50. }
  51. private void Update()
  52. {
  53. if (!this.isActive)
  54. {
  55. this.isActive = true;
  56. base.transform.GetChild(0).gameObject.SetActive(true);
  57. }
  58. float num = 100f;
  59. SteamVR_TrackedController component = base.GetComponent<SteamVR_TrackedController>();
  60. Ray ray = new Ray(base.transform.position, base.transform.forward);
  61. RaycastHit raycastHit;
  62. bool flag = Physics.Raycast(ray, out raycastHit);
  63. if (this.previousContact && this.previousContact != raycastHit.transform)
  64. {
  65. PointerEventArgs e = default(PointerEventArgs);
  66. if (component != null)
  67. {
  68. e.controllerIndex = component.controllerIndex;
  69. }
  70. e.distance = 0f;
  71. e.flags = 0U;
  72. e.target = this.previousContact;
  73. this.OnPointerOut(e);
  74. this.previousContact = null;
  75. }
  76. if (flag && this.previousContact != raycastHit.transform)
  77. {
  78. PointerEventArgs e2 = default(PointerEventArgs);
  79. if (component != null)
  80. {
  81. e2.controllerIndex = component.controllerIndex;
  82. }
  83. e2.distance = raycastHit.distance;
  84. e2.flags = 0U;
  85. e2.target = raycastHit.transform;
  86. this.OnPointerIn(e2);
  87. this.previousContact = raycastHit.transform;
  88. }
  89. if (!flag)
  90. {
  91. this.previousContact = null;
  92. }
  93. if (flag && raycastHit.distance < 100f)
  94. {
  95. num = raycastHit.distance;
  96. }
  97. if (component != null && component.triggerPressed)
  98. {
  99. this.pointer.transform.localScale = new Vector3(this.thickness * 5f, this.thickness * 5f, num);
  100. }
  101. else
  102. {
  103. this.pointer.transform.localScale = new Vector3(this.thickness, this.thickness, num);
  104. }
  105. this.pointer.transform.localPosition = new Vector3(0f, 0f, num / 2f);
  106. }
  107. public bool active = true;
  108. public Color color;
  109. public float thickness = 0.002f;
  110. public GameObject holder;
  111. public GameObject pointer;
  112. private bool isActive;
  113. public bool addRigidBody;
  114. public Transform reference;
  115. private Transform previousContact;
  116. }