Exemplo n.º 1
0
        private bool CheckGraphicAddedToRaycastGraphicRegistry()
        {
            var graphicList     = GraphicRegistry.GetRaycastableGraphicsForCanvas(m_canvas);
            var graphicListSize = graphicList.Count;

            return(ContainsGraphic(graphicList, graphicListSize, m_graphic));
        }
Exemplo n.º 2
0
        /// <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);
        }