SteamVR_LaserPointer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using System.Collections;
  4. public struct PointerEventArgs
  5. {
  6. public uint controllerIndex;
  7. public uint flags;
  8. public float distance;
  9. public Transform target;
  10. }
  11. public delegate void PointerEventHandler(object sender, PointerEventArgs e);
  12. public class SteamVR_LaserPointer : MonoBehaviour
  13. {
  14. public bool active = true;
  15. public Color color;
  16. public float thickness = 0.002f;
  17. public GameObject holder;
  18. public GameObject pointer;
  19. bool isActive = false;
  20. public bool addRigidBody = false;
  21. public Transform reference;
  22. public event PointerEventHandler PointerIn;
  23. public event PointerEventHandler PointerOut;
  24. Transform previousContact = null;
  25. // Use this for initialization
  26. void Start ()
  27. {
  28. holder = new GameObject();
  29. holder.transform.parent = this.transform;
  30. holder.transform.localPosition = Vector3.zero;
  31. holder.transform.localRotation = Quaternion.identity;
  32. pointer = GameObject.CreatePrimitive(PrimitiveType.Cube);
  33. pointer.transform.parent = holder.transform;
  34. pointer.transform.localScale = new Vector3(thickness, thickness, 100f);
  35. pointer.transform.localPosition = new Vector3(0f, 0f, 50f);
  36. pointer.transform.localRotation = Quaternion.identity;
  37. BoxCollider collider = pointer.GetComponent<BoxCollider>();
  38. if (addRigidBody)
  39. {
  40. if (collider)
  41. {
  42. collider.isTrigger = true;
  43. }
  44. Rigidbody rigidBody = pointer.AddComponent<Rigidbody>();
  45. rigidBody.isKinematic = true;
  46. }
  47. else
  48. {
  49. if(collider)
  50. {
  51. Object.Destroy(collider);
  52. }
  53. }
  54. Material newMaterial = new Material(Shader.Find("Unlit/Color"));
  55. newMaterial.SetColor("_Color", color);
  56. pointer.GetComponent<MeshRenderer>().material = newMaterial;
  57. }
  58. public virtual void OnPointerIn(PointerEventArgs e)
  59. {
  60. if (PointerIn != null)
  61. PointerIn(this, e);
  62. }
  63. public virtual void OnPointerOut(PointerEventArgs e)
  64. {
  65. if (PointerOut != null)
  66. PointerOut(this, e);
  67. }
  68. // Update is called once per frame
  69. void Update ()
  70. {
  71. if (!isActive)
  72. {
  73. isActive = true;
  74. this.transform.GetChild(0).gameObject.SetActive(true);
  75. }
  76. float dist = 100f;
  77. SteamVR_TrackedController controller = GetComponent<SteamVR_TrackedController>();
  78. Ray raycast = new Ray(transform.position, transform.forward);
  79. RaycastHit hit;
  80. bool bHit = Physics.Raycast(raycast, out hit);
  81. if(previousContact && previousContact != hit.transform)
  82. {
  83. PointerEventArgs args = new PointerEventArgs();
  84. if (controller != null)
  85. {
  86. args.controllerIndex = controller.controllerIndex;
  87. }
  88. args.distance = 0f;
  89. args.flags = 0;
  90. args.target = previousContact;
  91. OnPointerOut(args);
  92. previousContact = null;
  93. }
  94. if(bHit && previousContact != hit.transform)
  95. {
  96. PointerEventArgs argsIn = new PointerEventArgs();
  97. if (controller != null)
  98. {
  99. argsIn.controllerIndex = controller.controllerIndex;
  100. }
  101. argsIn.distance = hit.distance;
  102. argsIn.flags = 0;
  103. argsIn.target = hit.transform;
  104. OnPointerIn(argsIn);
  105. previousContact = hit.transform;
  106. }
  107. if(!bHit)
  108. {
  109. previousContact = null;
  110. }
  111. if (bHit && hit.distance < 100f)
  112. {
  113. dist = hit.distance;
  114. }
  115. if (controller != null && controller.triggerPressed)
  116. {
  117. pointer.transform.localScale = new Vector3(thickness * 5f, thickness * 5f, dist);
  118. }
  119. else
  120. {
  121. pointer.transform.localScale = new Vector3(thickness, thickness, dist);
  122. }
  123. pointer.transform.localPosition = new Vector3(0f, 0f, dist/2f);
  124. }
  125. }