SteamVR_LaserPointer.cs 3.2 KB

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