/// <summary> /// Fills the list of cast rays /// </summary> protected virtual void AppendToListAllCurrentRaycasts(ref List <RaycastResult> resultAppendList, ref List <RaycastResult> raycastResults) { int totalCount = raycastResults.Count; for (int index = 0; index < totalCount; index++) { VRTK4_SharedMethods.AddListValue(resultAppendList, raycastResults[index]); } }
protected virtual void LateUpdate() { if (pointerEventData == null) { pointerEventData = new PointerEventData(EventSystem.current); } pointerEventData.pointerId = GetIndexOfUIPointer(); VRTK4_SharedMethods.AddDictionaryValue(pointerLengths, pointerEventData.pointerId, maximumLength, true); }
/// <summary> /// The GetPointerLength method retrieves the maximum UI Pointer length for the given pointer ID. /// </summary> /// <param name="pointerId">The pointer ID for the UI Pointer to recieve the length for.</param> /// <returns>The maximum length the UI Pointer will cast to.</returns> internal static float GetPointerLength(int pointerId) { if (EventSystem.current == null) { return(0); } var asVrtk4System = EventSystem.current as VRTK4_EventSystem; if (asVrtk4System == null) { return(0); } return(VRTK4_SharedMethods.GetDictionaryValue(pointerLengths, pointerId, float.MaxValue)); }
/// <summary> /// Changes - ignore displays. /// </summary> /// <param name="eventCameraIn"></param> /// <param name="eventData"></param> /// <param name="ray"></param> /// <param name="helperList"></param> protected virtual void Raycast(Camera eventCameraIn, PointerEventData eventData, Ray ray, ref List <RaycastResult> helperList) { RaycastHit hitResult = default; float hitDistance = VRTK4_UIPointer.GetPointerLength(eventData.pointerId); bool isSuccesfullRaycasted = TryGetHitDistance(ray, ref hitDistance, out hitResult); ClearArrNonAlloc3D(); if (isSuccesfullRaycasted) { Transform graphicTransform = hitResult.transform; Vector3 graphicForward = graphicTransform.forward; float distance = Vector3.Dot(graphicForward, graphicTransform.position - ray.origin) / Vector3.Dot(graphicForward, ray.direction); if (distance < 0) { return; } Vector3 position = ray.GetPoint(distance); Vector2 pointerPosition = eventCameraIn.WorldToScreenPoint(position); var result = new RaycastResult { gameObject = hitResult.collider.gameObject, module = this, distance = hitResult.distance, worldPosition = hitResult.point, worldNormal = hitResult.normal, screenPosition = pointerPosition, depth = 0, sortingLayer = 0, sortingOrder = 7 }; VRTK4_SharedMethods.AddListValue(helperList, result); helperList.Sort(ComparisonInversedDistance); } }
/// <summary> /// Changes - ignore displays. Improved Performance : no need to sort graphics only, we can sort all raycast once. /// </summary> /// <param name="canvasIn"></param> /// <param name="eventCameraIn"></param> /// <param name="eventData"></param> /// <param name="ray"></param> /// <param name="helperList"></param> protected virtual void Raycast(Canvas canvasIn, Camera eventCameraIn, PointerEventData eventData, Ray ray, ref List <RaycastResult> helperList) { // bool checkRaycastGraphic = false; float hitDistance = GetHitDistance(ray, VRTK4_UIPointer.GetPointerLength(eventData.pointerId)); IList <Graphic> canvasGraphics = null; #if UNITY_2020_3_OR_NEWER canvasGraphics = GraphicRegistry.GetRaycastableGraphicsForCanvas(canvasIn); #else canvasGraphics = GraphicRegistry.GetGraphicsForCanvas(canvasIn); #endif int totalCount = canvasGraphics.Count; for (int i = 0; i < totalCount; ++i) { Graphic graphic = canvasGraphics[i]; if (graphic.depth == -1 || !graphic.raycastTarget || graphic.canvasRenderer.cull) { continue; } Transform graphicTransform = graphic.transform; Vector3 graphicForward = graphicTransform.forward; float distance = Vector3.Dot(graphicForward, graphicTransform.position - ray.origin) / Vector3.Dot(graphicForward, ray.direction); if (distance < 0) { continue; } //Prevents "flickering hover" on items near canvas center. if ((distance - UI_CONTROL_OFFSET) > hitDistance) { continue; } Vector3 position = ray.GetPoint(distance); Vector2 pointerPosition = eventCameraIn.WorldToScreenPoint(position); #if UNITY_2020_3_OR_NEWER if (!RectTransformUtility.RectangleContainsScreenPoint( graphic.rectTransform, pointerPosition, eventCameraIn, graphic.raycastPadding)) { continue; } #else if (!RectTransformUtility.RectangleContainsScreenPoint( graphic.rectTransform, pointerPosition, eventCameraIn)) { continue; } #endif if (graphic.Raycast(pointerPosition, eventCameraIn)) { RaycastResult result = new RaycastResult() { gameObject = graphic.gameObject, module = this, distance = distance, worldPosition = position, worldNormal = -graphicForward, screenPosition = pointerPosition, depth = graphic.depth + canvasIn.sortingLayerID * 1000 + canvasIn.sortingOrder, sortingLayer = canvasIn.sortingLayerID, sortingOrder = canvasIn.sortingOrder, }; VRTK4_SharedMethods.AddListValue(helperList, result); } } helperList.Sort(ComparisonInversed); }