private bool GetPlaneIntersection(Vector2 guiPosition, out Vector3 worldPlanePosition)
        {
#if ENABLE_DEBUG_GRID
            Grid.debugGrid = null;
#endif
            if (worldSlideGrid == null)
            {
                worldPlanePosition = worldSlideOrigin;
                return(false);
            }

            var originSnappedPlane = new Plane(worldSlidePlane.normal, worldSlideOrigin);

            var worldRay = UnityEditor.HandleUtility.GUIPointToWorldRay(guiPosition);
            var dist     = 0.0f;

            var camera  = Camera.current;
            var forward = camera.transform.forward;
            if (Mathf.Abs(Vector3.Dot(originSnappedPlane.normal, forward)) < 0.125f)
            {
                var normal = worldSlideGrid.GetClosestAxisVector(forward);
                var origin = originSnappedPlane.ClosestPointOnPlane(worldSlideOrigin);
                return(GetIntersectionOnAlternativePlane(worldRay, normal, origin, out worldPlanePosition));
            }

            if (!originSnappedPlane.Raycast(worldRay, out dist))
            {
                dist = float.PositiveInfinity;
            }

            float farClipPlaneDistance = camera.farClipPlane * 0.5f;
            if (dist > farClipPlaneDistance)
            {
                var normal = worldSlideGrid.GetClosestAxisVector(forward);
                var origin = originSnappedPlane.ClosestPointOnPlane(camera.transform.position) + (normal * farClipPlaneDistance);
                return(GetIntersectionOnAlternativePlane(worldRay, normal, origin, out worldPlanePosition));
            }
            else
            {
                if (!originSnappedPlane.SignedRaycast(worldRay, out dist))
                {
                    worldPlanePosition = worldSlideOrigin; return(false);
                }

                worldPlanePosition = worldRay.GetPoint(Mathf.Abs(dist));
#if ENABLE_DEBUG_GRID
                {
                    var tangent          = GeometryUtility.CalculateTangent(worldSlidePlane.normal);
                    var planeOrientation = Quaternion.LookRotation(tangent, worldSlidePlane.normal);
                    Grid.debugGrid = new Grid(Matrix4x4.TRS(worldPlanePosition, planeOrientation, Vector3.one));
                }
#endif

                return(true);
            }
        }
Пример #2
0
 public static void CalculateTangents(Vector3 normal, out Vector3 tangent, out Vector3 binormal)
 {
     tangent  = Vector3.Cross(normal, GeometryUtility.CalculateTangent(normal)).normalized;
     binormal = Vector3.Cross(normal, tangent).normalized;
 }