9 using System.Collections.Generic;
12 namespace HoloToolkit.Examples.SpatialUnderstandingFeatureOverview
17 public const float RayCastLength = 10.0f;
39 rayPos.x, rayPos.y, rayPos.z,
40 rayVec.x, rayVec.y, rayVec.z,
44 float rayCastResultDist = Vector3.Distance(rayPos, rayCastResult.IntersectPoint);
45 float resultDist = Vector3.Distance(rayPos, result.
Position);
49 rayCastResultDist < resultDist)
52 result.
Position = rayCastResult.IntersectPoint;
53 result.
Normal = rayCastResult.IntersectNormal;
62 public bool RayCastUI(out Vector3 hitPos, out Vector3 hitNormal, out
Button hitButton)
65 hitPos = Vector3.zero;
66 hitNormal = Vector3.zero;
73 if (Physics.Raycast(uiRayCastOrigin, uiRayCastDirection, out hitInfo, RayCastLength, UILayerMask))
75 Canvas canvas = hitInfo.collider.gameObject.GetComponent<Canvas>();
78 GraphicRaycaster canvasRaycaster = canvas.gameObject.GetComponent<GraphicRaycaster>();
79 if (canvasRaycaster != null)
82 PointerEventData pData =
new PointerEventData(EventSystem.current);
84 pData.position =
new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);
85 pData.delta = Vector2.zero;
86 pData.scrollDelta = Vector2.zero;
88 List<
UnityEngine.EventSystems.RaycastResult> canvasHits =
new List<
UnityEngine.EventSystems.RaycastResult>();
89 canvasRaycaster.Raycast(pData, canvasHits);
90 for (
int i = 0; i < canvasHits.Count; ++i)
92 Button button = canvasHits[i].gameObject.GetComponent<
Button>();
95 hitPos = uiRayCastOrigin + uiRayCastDirection * canvasHits[i].distance;
96 hitNormal = canvasHits[i].gameObject.transform.forward;
103 hitPos = hitInfo.point;
104 hitNormal = hitInfo.normal;
123 CursorText.gameObject.SetActive(
false);
132 if ((rayCastResult != null) &&
135 CursorText.gameObject.SetActive(
true);
136 CursorText.text = rayCastResult.SurfaceType.ToString();
138 CursorText.transform.rotation = Quaternion.LookRotation(
CameraCache.
Main.transform.forward, Vector3.up);
139 CursorText.transform.position = transform.position +
CameraCache.
Main.transform.right * 0.05f;
143 CursorText.gameObject.SetActive(
false);
147 Vector3 hitPos, hitNormal;
149 float textAlpha = RayCastUI(out hitPos, out hitNormal, out hitButton) ? 0.15f : 1.0f;
150 CursorText.color =
new Color(1.0f, 1.0f, 1.0f, textAlpha);