コード例 #1
0
ファイル: ComponentService.cs プロジェクト: thurn/tarkin
        GraphicRaycaster UpdateGraphicRaycaster(ZGraphicRaycaster zGraphicRaycaster, GameObject gameObject)
        {
            GraphicRaycaster graphicRaycaster = GetOrAddComponent <GraphicRaycaster>(gameObject);

            if (zGraphicRaycaster.IgnoreReversedGraphics.HasValue)
            {
                graphicRaycaster.ignoreReversedGraphics = zGraphicRaycaster.IgnoreReversedGraphics.Value;
            }

            switch (zGraphicRaycaster.BlockingObjects)
            {
            case ZGraphicRaycaster.Types.BlockingObjects.None:
                graphicRaycaster.blockingObjects = GraphicRaycaster.BlockingObjects.None;
                break;

            case ZGraphicRaycaster.Types.BlockingObjects.TwoD:
                graphicRaycaster.blockingObjects = GraphicRaycaster.BlockingObjects.TwoD;
                break;

            case ZGraphicRaycaster.Types.BlockingObjects.ThreeD:
                graphicRaycaster.blockingObjects = GraphicRaycaster.BlockingObjects.ThreeD;
                break;

            case ZGraphicRaycaster.Types.BlockingObjects.All:
                graphicRaycaster.blockingObjects = GraphicRaycaster.BlockingObjects.All;
                break;
            }

            return(graphicRaycaster);
        }
コード例 #2
0
        private RaycastResult Raycast(Ray ray, int layerMask)
        {
            // Initialize the raycast result.
            RaycastResult result = default(RaycastResult);

            result.distance       = this.DefaultHitDistance;
            result.worldNormal    = -ray.direction;
            result.worldPosition  = ray.GetPoint(result.distance);
            result.screenPosition = this.EventCamera.Camera.WorldToScreenPoint(
                result.worldPosition);

            // Perform a physics raycast to determine if any physics objects
            // are hit.
            RaycastHit physicsResult;

            if (Physics.Raycast(
                    ray, out physicsResult, result.distance, layerMask))
            {
                result.distance       = physicsResult.distance;
                result.worldPosition  = physicsResult.point;
                result.worldNormal    = physicsResult.normal;
                result.gameObject     = physicsResult.collider.gameObject;
                result.screenPosition =
                    this.EventCamera.Camera.WorldToScreenPoint(
                        physicsResult.point);
            }

            // Perform a graphics raycast to determine if any UI objects
            // are hit.
            RaycastResult graphicResult;

            if (ZGraphicRaycaster.Raycast(
                    ray, out graphicResult, result.distance, layerMask))
            {
                result = graphicResult;
            }

            // Perform a physics spherecast if the hit radius is greater
            // than zero to determine if any physics objects are within the
            // hit radius.
            float radius = this.DefaultHitRadius;

            if (radius > 0)
            {
                // Increase the hit radius by 10% when an object is intersected
                // to eliminate instability issues in collision detection results
                // when the pointer is straddling intersection boundaries.
                if (this.HitInfo.gameObject != null)
                {
                    radius *= 1.1f;
                }

                if (this.SphereCast(
                        ray, radius, out physicsResult, result.distance, layerMask))
                {
                    if (result.gameObject == null ||
                        result.gameObject == physicsResult.collider.gameObject)
                    {
                        result.distance       = physicsResult.distance;
                        result.worldPosition  = physicsResult.point;
                        result.worldNormal    = physicsResult.normal;
                        result.gameObject     = physicsResult.collider.gameObject;
                        result.screenPosition =
                            this.EventCamera.Camera.WorldToScreenPoint(
                                physicsResult.point);
                    }
                }
            }

            // If neither the physics nor graphics raycast succeeded,
            // perform a raycast against the default collision plane.
            if (result.gameObject == null &&
                this.DefaultCollisionPlane == CollisionPlane.Screen)
            {
                result = this.Raycast(
                    ray, this.EventCamera.ZeroParallaxPlane, false);
            }

            return(result);
        }