Esempio n. 1
0
        public static Plane AdjustSoBoxSitsBehind(this Plane plane, OrientedBox orientedBox)
        {
            List <Vector3> boxCornerPoints = orientedBox.GetCornerPoints();
            Vector3        furthestPointInFront;

            if (plane.GetFurthestPointInFront(boxCornerPoints, out furthestPointInFront))
            {
                return(new Plane(plane.normal, furthestPointInFront));
            }
            else
            {
                Vector3 closestPointBehind, pointOnPlane;

                if (plane.GetFirstPointOnPlane(boxCornerPoints, out pointOnPlane))
                {
                    return(new Plane(plane.normal, pointOnPlane));
                }
                if (plane.GetClosestPointBehind(boxCornerPoints, out closestPointBehind))
                {
                    return(new Plane(plane.normal, closestPointBehind));
                }
            }

            // If there are no points behind or in front, it means all points are on the plane, so we return the umodified plane instance
            return(plane);
        }
        public static void PlaceHierarchyOnPlane(this GameObject root, Vector3 ptOnPlane, Vector3 planeNormal, int alignAxisIndex)
        {
            Transform rootTransform = root.transform;

            /*Box hierarchyModelSpaceAABB = root.GetHierarchyModelSpaceBox();
             * Vector3 worldAABBSize = Vector3.Scale(rootTransform.lossyScale, hierarchyModelSpaceAABB.Size);
             *
             * planeNormal.Normalize();
             * Plane plane = new Plane(planeNormal, ptOnPlane);
             *
             * if (alignAxisIndex >= 0 && alignAxisIndex < 3)
             * {
             *  root.AlignAxis(planeNormal, alignAxisIndex);
             *
             *  Vector3 worldAABBCenter = rootTransform.TransformPoint(hierarchyModelSpaceAABB.Center);
             *  float sizeAlongAxis = worldAABBSize[alignAxisIndex];
             *
             *  Vector3 ptOnBoxFace = worldAABBCenter - plane.normal * sizeAlongAxis * 0.5f;
             *  Vector3 fromPtToPos = rootTransform.position - ptOnBoxFace;
             *  Vector3 projectedPt = worldAABBCenter - plane.normal * plane.GetDistanceToPoint(worldAABBCenter);
             *  projectedPt += (ptOnPlane - projectedPt);
             *
             *  rootTransform.position = projectedPt + fromPtToPos;
             * }
             * else rootTransform.position = ptOnPlane;*/

            Plane plane = new Plane(planeNormal, ptOnPlane);

            if (alignAxisIndex >= 0 && alignAxisIndex < 3)
            {
                root.AlignAxis(planeNormal, alignAxisIndex);
            }

            OrientedBox    oobb         = root.GetWorldOrientedBox();
            List <Vector3> oobbPts      = oobb.GetCornerPoints();
            int            pivotPtIndex = plane.GetIndexOfFurthestPointBehind(oobbPts);

            if (pivotPtIndex < 0)
            {
                pivotPtIndex = plane.GetIndexOfClosestPointInFrontOrOnPlane(oobbPts);
            }
            if (pivotPtIndex >= 0)
            {
                Vector3 projectedPt = plane.ProjectPoint(oobbPts[pivotPtIndex]);
                rootTransform.position += (projectedPt - oobbPts[pivotPtIndex]);
            }
        }