Beispiel #1
0
        private void KeepSnappedHierarchyInSnapSurfaceArea(GameObject hierarchyRoot, ProjectedBoxFacePivotPoints projectedHierarchyBoxFacePivotPoints)
        {
            OrientedBox      hierarchyWorldOrientedBox = hierarchyRoot.GetHierarchyWorldOrientedBox();
            List <Vector3>   worldBoxPoints            = hierarchyWorldOrientedBox.GetCenterAndCornerPoints();
            XZOrientedQuad3D snapSurfaceQuad           = _objectSnapSurface.SurfaceQuad;

            List <Plane>   quadSegmentPlanes = snapSurfaceQuad.GetBoundarySegmentPlanesFacingOutward();
            List <Vector3> pushVectors       = new List <Vector3>(quadSegmentPlanes.Count);

            // All box points which are in front of the surface quad's plane are outside
            // the surface so we will have to push them back.
            for (int segmentPlaneIndex = 0; segmentPlaneIndex < quadSegmentPlanes.Count; ++segmentPlaneIndex)
            {
                Plane   segmentPlane = quadSegmentPlanes[segmentPlaneIndex];
                Vector3 furthestPointInFront;
                if (segmentPlane.GetFurthestPointInFront(worldBoxPoints, out furthestPointInFront))
                {
                    Vector3 projectedPoint = segmentPlane.ProjectPoint(furthestPointInFront);
                    pushVectors.Add(projectedPoint - furthestPointInFront);
                }
            }

            Transform hierarchyRootTransform = hierarchyRoot.transform;

            foreach (Vector3 pushVector in pushVectors)
            {
                hierarchyRootTransform.position += pushVector;
                projectedHierarchyBoxFacePivotPoints.MovePoints(pushVector);
            }
        }
        private List <Plane> GetPlanesOnWhichOrientedBoxResides(OrientedBox worldOrientedBox)
        {
            if (HasCursorPickedTerrainObject())
            {
                List <Vector3> boxCenterAndCornerPoints = worldOrientedBox.GetCenterAndCornerPoints();
                var            planes = new List <Plane>(boxCenterAndCornerPoints.Count);

                Octave3DColliderRayHit colliderRayHit;
                Octave3DCollider       hitCollider = _cursorRayHit.ClosestObjectRayHit.HitCollider;
                foreach (Vector3 boxPoint in boxCenterAndCornerPoints)
                {
                    Ray ray = new Ray(boxPoint, Normal);
                    if (hitCollider.RaycastBothDirections(ray, out colliderRayHit))
                    {
                        planes.Add(new Plane(colliderRayHit.HitNormal, colliderRayHit.HitPoint));
                    }
                    else
                    {
                        planes.Add(Plane);
                    }
                }

                return(planes);
            }

            return(new List <Plane> {
                Plane
            });
        }
Beispiel #3
0
        public void Encapsulate(OrientedBox orientedBox)
        {
            List <Vector3> orientedBoxPoints = orientedBox.GetCenterAndCornerPoints();

            foreach (Vector3 point in orientedBoxPoints)
            {
                AddPoint(point);
            }
        }
Beispiel #4
0
        public bool AreAllBoxPointsOnOrInFrontOfAnyFacePlane(OrientedBox otherBox)
        {
            List <Vector3> otherBoxPoints = otherBox.GetCenterAndCornerPoints();
            List <Plane>   allFacePlanes  = GetBoxFacePlanes();

            foreach (Plane plane in allFacePlanes)
            {
                if (PlaneExtensions.AreAllPointsInFrontOrOnPlane(plane, otherBoxPoints))
                {
                    return(true);
                }
            }

            return(false);
        }
        public static BoxPlaneClassificationResult ClassifyOrientedBox(this Plane plane, OrientedBox orientedBox)
        {
            List <Vector3> boxCenterAndCornerPoints = orientedBox.GetCenterAndCornerPoints();

            bool foundPointInFront = false;
            bool foundPointBehind  = false;
            bool foundPointOnPlane = false;

            foreach (Vector3 boxPoint in boxCenterAndCornerPoints)
            {
                PointPlaneClassificationResult pointClassificationResult = plane.ClassifyPoint(boxPoint);
                if (pointClassificationResult == PointPlaneClassificationResult.InFront)
                {
                    foundPointInFront = true;
                    if (foundPointBehind)
                    {
                        return(BoxPlaneClassificationResult.Spanning);
                    }
                }
                else
                if (pointClassificationResult == PointPlaneClassificationResult.Behind)
                {
                    foundPointBehind = true;
                    if (foundPointInFront)
                    {
                        return(BoxPlaneClassificationResult.Spanning);
                    }
                }
                else
                {
                    foundPointOnPlane = true;
                }
            }

            if (foundPointOnPlane && (!foundPointInFront && !foundPointBehind))
            {
                return(BoxPlaneClassificationResult.OnPlane);
            }
            else
            {
                if (foundPointInFront)
                {
                    return(BoxPlaneClassificationResult.InFront);
                }
                return(BoxPlaneClassificationResult.Behind);
            }
        }
Beispiel #6
0
        public List <Triangle3D> GetOverlappedWorldTriangles(OrientedBox box, TransformMatrix meshTransformMatrix)
        {
            // If the tree was not yet build, we need to build it because we need
            // the triangle information in order to perform the raycast.
            if (!_wasBuilt)
            {
                Build();
            }

            // Work in mesh model space because the tree data exists in model space
            OrientedBox meshSpaceBox     = new OrientedBox(box);
            Matrix4x4   inverseTransform = meshTransformMatrix.ToMatrix4x4x.inverse;

            meshSpaceBox.Transform(inverseTransform);

            List <SphereTreeNode <MeshSphereTreeTriangle> > overlappedNodes = _sphereTree.OverlapBox(meshSpaceBox);

            if (overlappedNodes.Count == 0)
            {
                return(new List <Triangle3D>());
            }

            Box queryBox = Box.FromPoints(meshSpaceBox.GetCenterAndCornerPoints());
            var overlappedWorldTriangles = new List <Triangle3D>(50);

            foreach (var node in overlappedNodes)
            {
                int triangleIndex             = node.Data.TriangleIndex;
                MeshTriangleInfo triangleInfo = _octave3DMesh.GetMeshTriangleInfo(triangleIndex);

                if (triangleInfo.ModelSpaceTriangle.IntersectsBox(queryBox))
                {
                    triangleInfo.ModelSpaceTriangle.TransformPoints(meshTransformMatrix);
                    overlappedWorldTriangles.Add(triangleInfo.ModelSpaceTriangle);
                }
            }

            return(overlappedWorldTriangles);
        }
Beispiel #7
0
        public void UpdateForMouseMovement()
        {
            if (_state == State.Inactive)
            {
                return;
            }

            if (MouseButtonStates.Instance.IsMouseButtonDown(MouseButton.Left))
            {
                _state = State.Snap;
            }
            else
            {
                _state = State.SelectPivot;
            }

            if (_state == State.SelectPivot && _selectedParents.Count != 0)
            {
                Camera  camera   = SceneViewCamera.Camera;
                Vector2 mousePos = Event.current.InvMousePos(camera);

                _isPivotAvailable = false;
                float minDistanceSq = float.MaxValue;
                foreach (var parent in _selectedParents)
                {
                    if (parent == null)
                    {
                        continue;
                    }

                    OrientedBox worldOOBB = parent.GetHierarchyWorldOrientedBox();
                    if (worldOOBB.IsValid())
                    {
                        List <Vector3> centerAndCorners = worldOOBB.GetCenterAndCornerPoints();
                        List <Vector2> oobbScreenPts    = Vector2Extensions.GetScreenPoints(centerAndCorners, camera);

                        for (int ptIndex = 0; ptIndex < centerAndCorners.Count; ++ptIndex)
                        {
                            Vector3 worldPt  = centerAndCorners[ptIndex];
                            Vector2 screenPt = oobbScreenPts[ptIndex];
                            float   distSq   = (mousePos - screenPt).sqrMagnitude;
                            if (distSq < minDistanceSq)
                            {
                                minDistanceSq     = distSq;
                                _pivot            = worldPt;
                                _isPivotAvailable = true;
                            }
                        }
                    }
                }
            }
            else
            if (_state == State.Snap && _isPivotAvailable)
            {
                GameObjectExtensions.RecordObjectTransformsForUndo(_selectedParents);
                MouseCursorRayHit cursorHit = MouseCursor.Instance.GetCursorRayHitForGridCell();
                if (cursorHit.WasACellHit)
                {
                    Camera         camera          = SceneViewCamera.Camera;
                    Vector2        mousePos        = Event.current.InvMousePos(camera);
                    GridCellRayHit cellRayHit      = cursorHit.GridCellRayHit;
                    Vector3        snapDestination = Vector3Extensions.GetClosestPointToPoint(cellRayHit.HitCell.Quad.GetCenterAndCornerPoints(), cellRayHit.HitPoint);

                    Vector3 moveVector = snapDestination - _pivot;
                    foreach (var parent in _selectedParents)
                    {
                        if (parent != null)
                        {
                            parent.transform.position += moveVector;
                        }
                    }

                    _pivot = snapDestination;

                    ObjectSelection.Get().ObjectSelectionTransformGizmoSystem.OnObjectSelectionUpdated();
                }
            }
        }
Beispiel #8
0
        public static void RenderOrientedBoxCornerEdges(OrientedBox orientedBox, float cornerEdgeLengthPercentage, Color color)
        {
            cornerEdgeLengthPercentage = Mathf.Clamp(cornerEdgeLengthPercentage, 0.0f, 1.0f);
            List <Vector3> boxCornerPoints = orientedBox.GetCenterAndCornerPoints();

            GizmosColor.Push(color);

            // Render the corner edges along the top edge of the box's front face
            Segment3D segment    = new Segment3D(boxCornerPoints[(int)BoxPoint.FrontTopLeft], boxCornerPoints[(int)BoxPoint.FrontTopRight]);
            float     edgeLength = segment.HalfLength * cornerEdgeLengthPercentage;

            if (edgeLength > 0.0f)
            {
                Gizmos.DrawLine(segment.StartPoint, segment.StartPoint + segment.NormalizedDirection * edgeLength);
                Gizmos.DrawLine(segment.EndPoint, segment.EndPoint - segment.NormalizedDirection * edgeLength);
            }

            // Render the corner edges along the bottom edge of the box's front face
            segment    = new Segment3D(boxCornerPoints[(int)BoxPoint.FrontBottomLeft], boxCornerPoints[(int)BoxPoint.FrontBottomRight]);
            edgeLength = segment.HalfLength * cornerEdgeLengthPercentage;
            if (edgeLength > 0.0f)
            {
                Gizmos.DrawLine(segment.StartPoint, segment.StartPoint + segment.NormalizedDirection * edgeLength);
                Gizmos.DrawLine(segment.EndPoint, segment.EndPoint - segment.NormalizedDirection * edgeLength);
            }

            // Render the corner edges along the left edge of the box's front face
            segment    = new Segment3D(boxCornerPoints[(int)BoxPoint.FrontTopLeft], boxCornerPoints[(int)BoxPoint.FrontBottomLeft]);
            edgeLength = segment.HalfLength * cornerEdgeLengthPercentage;
            if (edgeLength > 0.0f)
            {
                Gizmos.DrawLine(segment.StartPoint, segment.StartPoint + segment.NormalizedDirection * edgeLength);
                Gizmos.DrawLine(segment.EndPoint, segment.EndPoint - segment.NormalizedDirection * edgeLength);
            }

            // Render the corner edges along the right edge of the box's front face
            segment    = new Segment3D(boxCornerPoints[(int)BoxPoint.FrontTopRight], boxCornerPoints[(int)BoxPoint.FrontBottomRight]);
            edgeLength = segment.HalfLength * cornerEdgeLengthPercentage;
            if (edgeLength > 0.0f)
            {
                Gizmos.DrawLine(segment.StartPoint, segment.StartPoint + segment.NormalizedDirection * edgeLength);
                Gizmos.DrawLine(segment.EndPoint, segment.EndPoint - segment.NormalizedDirection * edgeLength);
            }

            // Render the corner edges along the top edge of the box's back face
            segment    = new Segment3D(boxCornerPoints[(int)BoxPoint.BackTopLeft], boxCornerPoints[(int)BoxPoint.BackTopRight]);
            edgeLength = segment.HalfLength * cornerEdgeLengthPercentage;
            if (edgeLength > 0.0f)
            {
                Gizmos.DrawLine(segment.StartPoint, segment.StartPoint + segment.NormalizedDirection * edgeLength);
                Gizmos.DrawLine(segment.EndPoint, segment.EndPoint - segment.NormalizedDirection * edgeLength);
            }

            // Render the corner edges along the bottom edge of the box's back face
            segment    = new Segment3D(boxCornerPoints[(int)BoxPoint.BackBottomLeft], boxCornerPoints[(int)BoxPoint.BackBottomRight]);
            edgeLength = segment.HalfLength * cornerEdgeLengthPercentage;
            if (edgeLength > 0.0f)
            {
                Gizmos.DrawLine(segment.StartPoint, segment.StartPoint + segment.NormalizedDirection * edgeLength);
                Gizmos.DrawLine(segment.EndPoint, segment.EndPoint - segment.NormalizedDirection * edgeLength);
            }

            // Render the corner edges along the left edge of the box's back face
            segment    = new Segment3D(boxCornerPoints[(int)BoxPoint.BackTopLeft], boxCornerPoints[(int)BoxPoint.BackBottomLeft]);
            edgeLength = segment.HalfLength * cornerEdgeLengthPercentage;
            if (edgeLength > 0.0f)
            {
                Gizmos.DrawLine(segment.StartPoint, segment.StartPoint + segment.NormalizedDirection * edgeLength);
                Gizmos.DrawLine(segment.EndPoint, segment.EndPoint - segment.NormalizedDirection * edgeLength);
            }

            // Render the corner edges along the right edge of the box's back face
            segment    = new Segment3D(boxCornerPoints[(int)BoxPoint.BackTopRight], boxCornerPoints[(int)BoxPoint.BackBottomRight]);
            edgeLength = segment.HalfLength * cornerEdgeLengthPercentage;
            if (edgeLength > 0.0f)
            {
                Gizmos.DrawLine(segment.StartPoint, segment.StartPoint + segment.NormalizedDirection * edgeLength);
                Gizmos.DrawLine(segment.EndPoint, segment.EndPoint - segment.NormalizedDirection * edgeLength);
            }

            // Render the corner edges along the left edge of the box's top face
            segment    = new Segment3D(boxCornerPoints[(int)BoxPoint.FrontTopLeft], boxCornerPoints[(int)BoxPoint.BackTopRight]);
            edgeLength = segment.HalfLength * cornerEdgeLengthPercentage;
            if (edgeLength > 0.0f)
            {
                Gizmos.DrawLine(segment.StartPoint, segment.StartPoint + segment.NormalizedDirection * edgeLength);
                Gizmos.DrawLine(segment.EndPoint, segment.EndPoint - segment.NormalizedDirection * edgeLength);
            }

            // Render the corner edges along the right edge of the box's top face
            segment    = new Segment3D(boxCornerPoints[(int)BoxPoint.FrontTopRight], boxCornerPoints[(int)BoxPoint.BackTopLeft]);
            edgeLength = segment.HalfLength * cornerEdgeLengthPercentage;
            if (edgeLength > 0.0f)
            {
                Gizmos.DrawLine(segment.StartPoint, segment.StartPoint + segment.NormalizedDirection * edgeLength);
                Gizmos.DrawLine(segment.EndPoint, segment.EndPoint - segment.NormalizedDirection * edgeLength);
            }

            // Render the corner edges along the left edge of the box's bottom face
            segment    = new Segment3D(boxCornerPoints[(int)BoxPoint.FrontBottomLeft], boxCornerPoints[(int)BoxPoint.BackBottomRight]);
            edgeLength = segment.HalfLength * cornerEdgeLengthPercentage;
            if (edgeLength > 0.0f)
            {
                Gizmos.DrawLine(segment.StartPoint, segment.StartPoint + segment.NormalizedDirection * edgeLength);
                Gizmos.DrawLine(segment.EndPoint, segment.EndPoint - segment.NormalizedDirection * edgeLength);
            }

            // Render the corner edges along the right edge of the box's bottom face
            segment    = new Segment3D(boxCornerPoints[(int)BoxPoint.FrontBottomRight], boxCornerPoints[(int)BoxPoint.BackBottomLeft]);
            edgeLength = segment.HalfLength * cornerEdgeLengthPercentage;
            if (edgeLength > 0.0f)
            {
                Gizmos.DrawLine(segment.StartPoint, segment.StartPoint + segment.NormalizedDirection * edgeLength);
                Gizmos.DrawLine(segment.EndPoint, segment.EndPoint - segment.NormalizedDirection * edgeLength);
            }

            GizmosColor.Pop();
        }