NRMultScrPointerRaycaster.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * NRSDK is distributed in the hope that it will be usefull
  7. *
  8. * https://www.nreal.ai/
  9. *
  10. *****************************************************************************/
  11. namespace NRKernal
  12. {
  13. using System.Collections.Generic;
  14. using UnityEngine;
  15. using UnityEngine.EventSystems;
  16. using UnityEngine.UI;
  17. /// <summary> A nr multiply screen pointer raycaster. </summary>
  18. [DisallowMultipleComponent]
  19. public class NRMultScrPointerRaycaster : NRPointerRaycaster
  20. {
  21. /// <summary> The mouse. </summary>
  22. public GameObject Mouse;
  23. /// <summary> The camera. </summary>
  24. private Camera m_UICamera;
  25. /// <summary> Width of the screen. </summary>
  26. private float m_ScreenWidth;
  27. /// <summary> Height of the screen. </summary>
  28. private float m_ScreenHeight;
  29. /// <summary> The last touch. </summary>
  30. private Vector3 m_LastTouch = m_FarAwayPos;
  31. /// <summary> The far away position. </summary>
  32. private static Vector3 m_FarAwayPos = Vector3.one * 10000f;
  33. /// <summary> <para>See MonoBehaviour.Awake.</para> </summary>
  34. protected override void Awake()
  35. {
  36. base.Awake();
  37. m_UICamera = gameObject.GetComponent<Camera>();
  38. //var resolution = NRPhoneScreen.Resolution * NRVirtualDisplayer.ScaleFactor;
  39. var resolution = NRPhoneScreen.Resolution;
  40. m_ScreenWidth = resolution.x;
  41. m_ScreenHeight = resolution.y;
  42. }
  43. /// <summary> Updates the screen size described by size. </summary>
  44. /// <param name="size"> The size.</param>
  45. public void UpdateScreenSize(Vector2 size)
  46. {
  47. m_ScreenWidth = size.x;
  48. m_ScreenHeight = size.y;
  49. }
  50. /// <summary> Raycasts this object. </summary>
  51. public override void Raycast()
  52. {
  53. sortedRaycastResults.Clear();
  54. breakPoints.Clear();
  55. var zScale = transform.lossyScale.z;
  56. var amountDistance = (FarDistance - NearDistance) * zScale;
  57. float distance;
  58. Ray ray;
  59. RaycastResult firstHit = default(RaycastResult);
  60. distance = amountDistance;
  61. var touch_x = NRVirtualDisplayer.SystemButtonState.originTouch.x;
  62. var touch_y = NRVirtualDisplayer.SystemButtonState.originTouch.y;
  63. var realTouchPos = new Vector3((touch_x + 1) * m_ScreenWidth * 0.5f, (touch_y + 1) * m_ScreenHeight * 0.5f, 0f);
  64. Vector3 touchpos = NRVirtualDisplayer.SystemButtonState.pressing ? realTouchPos : m_LastTouch;
  65. m_LastTouch = NRVirtualDisplayer.SystemButtonState.pressing ? touchpos : m_FarAwayPos;
  66. touchpos = m_UICamera.ScreenToWorldPoint(touchpos);
  67. //NRDebugger.Info("[PhoneDisplay] origin touch:{0} realTouchPos:{1} touchpos:{2} screenWidth:{3} screenHeight:{4}",
  68. // MultiScreenController.SystemButtonState.originTouch, realTouchPos, touchpos, m_ScreenWidth, m_ScreenHeight);
  69. if (Mouse)
  70. Mouse.transform.position = touchpos + m_UICamera.transform.forward * 0.3f;
  71. ray = new Ray(touchpos, m_UICamera.transform.forward);
  72. breakPoints.Add(touchpos);
  73. eventCamera.farClipPlane = eventCamera.nearClipPlane + distance;
  74. eventCamera.orthographicSize = m_UICamera.orthographicSize;
  75. eventCamera.aspect = m_UICamera.aspect;
  76. eventCamera.transform.position = ray.origin - (ray.direction * eventCamera.nearClipPlane);
  77. eventCamera.transform.rotation = Quaternion.LookRotation(ray.direction, transform.up);
  78. Raycast(ray, distance, sortedRaycastResults);
  79. firstHit = FirstRaycastResult();
  80. if (firstHit.isValid && firstHit.gameObject)
  81. {
  82. var button = firstHit.gameObject.GetComponent<NRButton>();
  83. if (button != null)
  84. {
  85. button.OnHover(firstHit);
  86. }
  87. }
  88. breakPoints.Add(firstHit.isValid ? firstHit.worldPosition : ray.GetPoint(distance));
  89. #if UNITY_EDITOR
  90. if (showDebugRay)
  91. Debug.DrawLine(breakPoints[0], breakPoints[1], firstHit.isValid ? Color.green : Color.red);
  92. #endif
  93. }
  94. /// <summary> Graphic raycast. </summary>
  95. /// <param name="canvas"> The canvas.</param>
  96. /// <param name="ignoreReversedGraphics"> True to ignore reversed graphics.</param>
  97. /// <param name="ray"> The ray.</param>
  98. /// <param name="distance"> The distance.</param>
  99. /// <param name="raycaster"> The raycaster.</param>
  100. /// <param name="raycastResults"> The raycast results.</param>
  101. public override void GraphicRaycast(ICanvasRaycastTarget raycastTarget, bool ignoreReversedGraphics, Ray ray, float distance, NRPointerRaycaster raycaster, List<RaycastResult> raycastResults)
  102. {
  103. if (raycastTarget.canvas == null) { return; }
  104. var eventCamera = raycaster.eventCamera;
  105. var screenCenterPoint = eventCamera.WorldToScreenPoint(eventCamera.transform.position);
  106. raycastTarget.GraphicRaycast(ignoreReversedGraphics, ray, distance, screenCenterPoint, raycaster, raycastResults);
  107. }
  108. }
  109. }