NxrLaserPointer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. namespace Nxr.Internal
  6. {
  7. public struct PointerEventArgs
  8. {
  9. public uint controllerIndex;
  10. public uint flags;
  11. public float distance;
  12. public Transform target;
  13. }
  14. public delegate void PointerEventHandler(object sender, PointerEventArgs e);
  15. public class NxrLaserPointer : MonoBehaviour
  16. {
  17. public Color color = Color.white;
  18. public float thickness = 0.004f;
  19. public GameObject holder;
  20. public GameObject pointer;
  21. private GameObject losdot;
  22. private GameObject hitObject;
  23. bool isActive = false;
  24. public bool addRigidBody = false;
  25. public event PointerEventHandler PointerIn;
  26. public event PointerEventHandler PointerOut;
  27. public NxrInstantNativeApi.NibiruDeviceType deviceType = NxrInstantNativeApi.NibiruDeviceType.RightController;
  28. Transform previousContact = null;
  29. float zDistance = 200.0f;
  30. Transform cacheTransform;
  31. public Transform GetTransform()
  32. {
  33. return cacheTransform;
  34. }
  35. public void SetHolderLocalPosition(Vector3 localPosition)
  36. {
  37. if (holder == null)
  38. {
  39. holder = new GameObject("NxrLaserPointer");
  40. holder.transform.parent = this.transform;
  41. holder.transform.localPosition = localPosition;
  42. holder.transform.localRotation = Quaternion.identity;
  43. } else
  44. {
  45. holder.transform.localPosition = localPosition;
  46. }
  47. }
  48. NxrUIPointer mNxrUIPointer;
  49. private void Awake()
  50. {
  51. mNxrUIPointer = GetComponent<NxrUIPointer>();
  52. if (mNxrUIPointer == null)
  53. {
  54. mNxrUIPointer = gameObject.AddComponent<NxrUIPointer>();
  55. }
  56. }
  57. Ray raycast;
  58. RaycastHit hit;
  59. // Use this for initialization
  60. void Start()
  61. {
  62. raycast = new Ray();
  63. hit = new RaycastHit();
  64. cacheTransform = transform;
  65. if(holder == null)
  66. {
  67. holder = new GameObject("NxrLaserPointer");
  68. holder.transform.parent = this.transform;
  69. holder.transform.localPosition = new Vector3(0, -0.005f, 0.08f);
  70. holder.transform.localRotation = Quaternion.identity;
  71. }
  72. pointer = GameObject.CreatePrimitive(PrimitiveType.Cube);
  73. pointer.transform.parent = holder.transform;
  74. pointer.transform.localScale = new Vector3(thickness, thickness, zDistance);
  75. pointer.transform.localPosition = new Vector3(0f, 0f, 50f);
  76. pointer.transform.localRotation = Quaternion.identity;
  77. BoxCollider collider = pointer.GetComponent<BoxCollider>();
  78. if (addRigidBody)
  79. {
  80. if (collider)
  81. {
  82. collider.isTrigger = true;
  83. }
  84. Rigidbody rigidBody = pointer.AddComponent<Rigidbody>();
  85. rigidBody.isKinematic = true;
  86. }
  87. else
  88. {
  89. if (collider)
  90. {
  91. Object.Destroy(collider);
  92. }
  93. }
  94. Material newMaterial = new Material(Shader.Find("Unlit/Color"));
  95. newMaterial.SetColor("_Color", color);
  96. pointer.GetComponent<MeshRenderer>().material = newMaterial;
  97. losdot = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/NxrLosDot"));
  98. // 解决射线白点有偏转问题
  99. // losdot.transform.parent = holder.transform;
  100. losdot.gameObject.name = "LosDot_" + Time.frameCount +(Time.deltaTime * 1000) + "_" + deviceType.ToString();
  101. var meshRenderer = losdot.GetComponent<MeshRenderer>();
  102. if (meshRenderer)
  103. {
  104. meshRenderer.material.renderQueue = 3500;
  105. }
  106. losdot.SetActive(false);
  107. }
  108. public virtual void OnPointerIn(PointerEventArgs e)
  109. {
  110. if (PointerIn != null)
  111. PointerIn(this, e);
  112. }
  113. public virtual void OnPointerOut(PointerEventArgs e)
  114. {
  115. if (PointerOut != null)
  116. PointerOut(this, e);
  117. }
  118. void OnDisable()
  119. {
  120. if (losdot != null)
  121. {
  122. Destroy(losdot);
  123. losdot = null;
  124. }
  125. }
  126. // Update is called once per frame
  127. void Update()
  128. {
  129. if (!isActive)
  130. {
  131. if(holder != null)
  132. {
  133. holder.SetActive(true);
  134. isActive = true;
  135. }
  136. }
  137. if(losdot != null && holder != null)
  138. {
  139. losdot.SetActive(holder.activeSelf);
  140. if(pointer != null) pointer.SetActive(false);
  141. if (mNxrUIPointer != null)
  142. {
  143. mNxrUIPointer.enabled = holder.activeSelf;
  144. }
  145. }
  146. if(holder == null || pointer == null || !holder.activeSelf)
  147. {
  148. return;
  149. }
  150. float dist = zDistance;
  151. raycast.origin = cacheTransform.position;
  152. raycast.direction = cacheTransform.forward;
  153. bool bHit = Physics.Raycast(raycast, out hit, zDistance); // max distance
  154. #if UNITY_EDITOR
  155. Debug.DrawRay(cacheTransform.position, cacheTransform.forward, Color.yellow);
  156. #endif
  157. CheckCanvasGraphicRayCaster(hit.transform, previousContact);
  158. if (previousContact && previousContact != hit.transform)
  159. {
  160. PointerEventArgs args = new PointerEventArgs();
  161. args.distance = 0f;
  162. args.flags = 0;
  163. args.target = previousContact;
  164. OnPointerOut(args);
  165. previousContact = null;
  166. }
  167. if (bHit && previousContact != hit.transform)
  168. {
  169. PointerEventArgs argsIn = new PointerEventArgs();
  170. argsIn.distance = hit.distance;
  171. argsIn.flags = 0;
  172. argsIn.target = hit.transform;
  173. OnPointerIn(argsIn);
  174. previousContact = hit.transform;
  175. hitObject = hit.collider.gameObject;
  176. }
  177. if (!bHit)
  178. {
  179. previousContact = null;
  180. hitObject = null;
  181. if(losdot != null) losdot.SetActive(false);
  182. }
  183. if (bHit && hit.distance < zDistance)
  184. {
  185. dist = hit.distance;
  186. if (losdot != null)
  187. {
  188. bool isUICanvasObject = (hit.transform.GetComponent<Canvas>() != null) && (hit.transform.GetComponent<NxrUICanvas>());
  189. if (isUICanvasObject)
  190. {
  191. dist = zDistance;
  192. losdot.SetActive(false);
  193. var eventSystem = EventSystem.current;
  194. // Debug.Log("EventSystem exists:"+(eventSystem == null));
  195. if (eventSystem)
  196. {
  197. var pointerData = new PointerEventData(eventSystem);
  198. var raycastResult = new RaycastResult();
  199. raycastResult.worldPosition = transform.position;
  200. raycastResult.worldNormal = transform.forward;
  201. pointerData.pointerCurrentRaycast = raycastResult;
  202. var raycastResults= new List<RaycastResult>();
  203. eventSystem.RaycastAll(pointerData,raycastResults);
  204. // Debug.Log("RaycastResultsCount:"+raycastResults.Count);
  205. for (var i = raycastResults.Count-1; i >=0; i--)
  206. {
  207. // Debug.Log("RaycastResultName"+i+":"+raycastResults[i].gameObject.name);
  208. var canvas = raycastResults[i].gameObject.GetComponent<Canvas>();
  209. if (canvas)
  210. {
  211. raycastResults.RemoveAt(i);
  212. }
  213. }
  214. if (raycastResults.Count > 0)
  215. {
  216. dist = hit.distance;
  217. losdot.SetActive(true);
  218. var scale = 0.005f * dist;
  219. losdot.transform.localScale = new Vector3(scale,0.002f,scale);
  220. losdot.transform.position = hit.point - new Vector3(0, -holder.transform.localPosition.y , 0.01f);
  221. }
  222. }
  223. }
  224. else
  225. {
  226. losdot.SetActive(true);
  227. var scale = 0.005f * dist;
  228. losdot.transform.localScale = new Vector3(scale,0.002f,scale);
  229. losdot.transform.position = hit.point - new Vector3(0, -holder.transform.localPosition.y , 0.01f);
  230. }
  231. }
  232. }
  233. if (pointer)
  234. {
  235. pointer.transform.localScale = new Vector3(thickness, thickness, dist);
  236. pointer.transform.localPosition = new Vector3(0f, 0f, dist / 2f);
  237. }
  238. }
  239. public GameObject GetLosDot()
  240. {
  241. return losdot;
  242. }
  243. private void CheckCanvasGraphicRayCaster(Transform currentHit, Transform lastHit)
  244. {
  245. if (currentHit != null && lastHit != null && currentHit != lastHit)
  246. {
  247. if (lastHit.GetComponent<NxrUIGraphicRaycaster>() && lastHit.transform.gameObject.activeInHierarchy && lastHit.GetComponent<NxrUIGraphicRaycaster>().enabled)
  248. {
  249. lastHit.GetComponent<NxrUIGraphicRaycaster>().enabled = false;
  250. }
  251. }
  252. if (currentHit != null && lastHit != null && currentHit == lastHit)
  253. {
  254. if (currentHit.GetComponent<NxrUIGraphicRaycaster>() && !currentHit.GetComponent<NxrUIGraphicRaycaster>().enabled)
  255. {
  256. currentHit.GetComponent<NxrUIGraphicRaycaster>().enabled = true;
  257. }
  258. }
  259. }
  260. }
  261. }