示例#1
0
        public static void RenderXZOrientedQuadBorderLines(XZOrientedQuad3D orientedQuad, Color color, float offsetAlongNormal)
        {
            List <Vector3> cornerPoints = orientedQuad.GetCornerPoints();

            cornerPoints = Vector3Extensions.ApplyOffsetToPoints(cornerPoints, orientedQuad.Plane.normal * offsetAlongNormal);
            RenderLinesBetweenPoints(cornerPoints, color);
        }
        private void StoreObjectPivotPoints(List <Vector3> projectedFacePoints)
        {
            List <Vector3> projectedPointsAndCenter = new List <Vector3>(projectedFacePoints);

            projectedPointsAndCenter.Insert(IndexOfPointInCenter, Vector3Extensions.GetAveragePoint(projectedFacePoints));

            _pivotPointCollection.SetPivotPoints(projectedPointsAndCenter, _pivotPointCollection.IndexOfActivePoint);
        }
示例#3
0
        public List <Vector3> GetExtentsPoints()
        {
            // First egenrate the points in model space in the following order: top, right, bottom, left.
            List <Vector3> points = new List <Vector3> {
                Vector3.forward *_radiusZ, Vector3.right *_radiusX, Vector3.forward * -_radiusZ, Vector3.right * -_radiusX
            };

            // Transform the points and return them
            return(Vector3Extensions.GetTransformedPoints(points, TransformMatrix.ToMatrix4x4x));
        }
示例#4
0
        public Box GetEncapsulatingBox()
        {
            List <Vector3> points = GetPoints();

            Vector3 minPoint, maxPoint;

            Vector3Extensions.GetMinMaxPoints(points, out minPoint, out maxPoint);

            return(new Box((minPoint + maxPoint) * 0.5f, maxPoint - minPoint));
        }
示例#5
0
        private static List <Vector3> GenerateXYCircleVertexPositions(float circleRadius, int numberOfCircleSlices)
        {
            int numberOfCircumferenceVerts = numberOfCircleSlices + 1;
            int totalNumberOfVerts         = numberOfCircumferenceVerts + 1;

            var vertexPositions = new List <Vector3>(totalNumberOfVerts);

            vertexPositions.Add(Vector3.zero);
            vertexPositions.AddRange(Vector3Extensions.Get3DEllipseCircumferencePoints(circleRadius, circleRadius, Vector3.right, Vector3.up, Vector3.zero, numberOfCircleSlices));

            return(vertexPositions);
        }
        public List <Segment3D> Calculate()
        {
            Vector3Extensions.EliminateDuplicatePoints(_polygonPointsOnSamePlane);
            if (_polygonPointsOnSamePlane.Count < 3)
            {
                return(new List <Segment3D>());
            }

            List <Vector3> workingPointList = new List <Vector3>(_polygonPointsOnSamePlane);

            Vector3 minPoint, maxPoint;

            CalculateInitialMinMaxPoints(out minPoint, out maxPoint);
            workingPointList.RemoveAll(item => item == minPoint || item == maxPoint);

            List <Segment3D> allHullSegments       = new List <Segment3D>();
            Segment3D        minMaxSegment         = new Segment3D(minPoint, maxPoint);
            Plane            minMaxSegmentPlane    = GetSegmentPlane(minMaxSegment);
            bool             furthestPointIsBehind = false;
            int indexOfPointFurthestFromPlane      = minMaxSegmentPlane.GetIndexOfFurthestPointInFront(workingPointList);

            if (indexOfPointFurthestFromPlane < 0)
            {
                indexOfPointFurthestFromPlane = minMaxSegmentPlane.GetIndexOfFurthestPointBehind(workingPointList);
                furthestPointIsBehind         = true;
            }
            if (indexOfPointFurthestFromPlane >= 0)
            {
                Vector3 furthestPointFromPlane = workingPointList[indexOfPointFurthestFromPlane];
                workingPointList.RemoveAt(indexOfPointFurthestFromPlane);

                List <Segment3D> hullSegments = new List <Segment3D>();
                if (furthestPointIsBehind)
                {
                    hullSegments.Add(new Segment3D(minMaxSegment.EndPoint, furthestPointFromPlane));
                    hullSegments.Add(new Segment3D(furthestPointFromPlane, minMaxSegment.StartPoint));
                    hullSegments.Add(minMaxSegment);
                    QuickHull(workingPointList, hullSegments);
                }
                else
                {
                    hullSegments.Add(new Segment3D(furthestPointFromPlane, minMaxSegment.EndPoint));
                    hullSegments.Add(new Segment3D(minMaxSegment.StartPoint, furthestPointFromPlane));
                    hullSegments.Add(new Segment3D(minMaxSegment.EndPoint, minMaxSegment.StartPoint));      // Reversed min-max segment
                    QuickHull(workingPointList, hullSegments);
                }

                allHullSegments.AddRange(hullSegments);
            }

            return(allHullSegments);
        }
示例#7
0
 public Vector3 GetPointClosestToPoint(Vector3 point, bool includeMidEdgePoints)
 {
     if (includeMidEdgePoints)
     {
         List <Vector3> points = GetCenterAndCornerPoints();
         points.AddRange(GetMidEdgePoints());
         return(Vector3Extensions.GetClosestPointToPoint(points, point));
     }
     else
     {
         return(Vector3Extensions.GetClosestPointToPoint(GetCenterAndCornerPoints(), point));
     }
 }
示例#8
0
        public static Mesh CreateXZRectangleMesh(float rectangleWidth, float rectangleDepth, Color rectangleColor)
        {
            if (!ValidateXYRectangleDimensions(rectangleWidth, rectangleDepth))
            {
                return(null);
            }

            var vertexPositions = Vector3Extensions.Get3DRectangleCircumferencePoints(rectangleWidth, rectangleDepth, Vector3.right, Vector3.forward, Vector3.zero);
            var vertexColors    = ListExtensions.GetFilledList(rectangleColor, 4);
            var vertexNormals   = ListExtensions.GetFilledList(Vector3.up, 4);
            var vertexIndices   = GenerateXZRectangleVertexIndices();

            return(ProceduralMeshFactory.CreateTriangleMesh(vertexPositions.ToArray(), vertexIndices, vertexColors.ToArray(), vertexNormals.ToArray()));
        }
示例#9
0
        public void RenderGizmos(XZGrid grid, CameraViewVolume cameraViewVolume)
        {
            if (!grid.RenderSettings.IsVisible)
            {
                return;
            }

            // Note: Can not figure out how to render a finite grid inside a shader yet... :D
            if (grid.DimensionSettings.DimensionType == XZGridDimensionType.Finite)
            {
                RenderGizmos_Obsolete(grid, cameraViewVolume);
                return;
            }

            Plane   gridPlane       = grid.Plane;
            Vector3 gridPlaneCenter = gridPlane.ProjectPoint(SceneViewCamera.Camera.transform.position);

            Box            camVolumeAABB          = cameraViewVolume.WorldSpaceAABB;
            List <Vector3> projectedVolumeAABBPts = gridPlane.ProjectAllPoints(camVolumeAABB.GetCornerPoints());
            List <Vector3> modelSpacePrjPts       = Vector3Extensions.GetTransformedPoints(projectedVolumeAABBPts, grid.TransformMatrix.ToMatrix4x4x.inverse);
            Box            modelSpacePtsBox       = Box.FromPoints(modelSpacePrjPts);
            Vector3        gridPlaneSize          = modelSpacePtsBox.Size;

            Matrix4x4 planeTransformMatrix = Matrix4x4.TRS(gridPlaneCenter, grid.Rotation, gridPlaneSize);
            Material  xzGridMaterial       = MaterialPool.Get().XZGridMaterial;

            xzGridMaterial.SetFloat("_CellSizeX", grid.CellSizeSettings.CellSizeX);
            xzGridMaterial.SetFloat("_CellSizeZ", grid.CellSizeSettings.CellSizeZ);
            xzGridMaterial.SetVector("_CellOffset", grid.GetOriginPosition());
            xzGridMaterial.SetColor("_LineColor", grid.RenderSettings.CellLineColor);
            xzGridMaterial.SetColor("_PlaneColor", grid.RenderSettings.PlaneColor);
            xzGridMaterial.SetFloat("_CamFarPlaneDist", SceneViewCamera.Camera.farClipPlane);
            xzGridMaterial.SetVector("_CamWorldPos", SceneViewCamera.Camera.transform.position);
            xzGridMaterial.SetMatrix("_InvRotMatrix", Matrix4x4.TRS(Vector3.zero, grid.Rotation, Vector3.one).inverse);
            xzGridMaterial.SetMatrix("_PlaneTransformMtx", planeTransformMatrix);

            int numPasses = xzGridMaterial.passCount;

            for (int passIndex = 0; passIndex < numPasses; ++passIndex)
            {
                xzGridMaterial.SetPass(passIndex);
                Graphics.DrawMeshNow(GizmosEx.XZRectangleMesh, planeTransformMatrix);
            }

            GizmosMatrix.Push(grid.TransformMatrix.ToMatrix4x4x);
            grid.RenderableCoordinateSystem.RenderGizmos();
            GizmosMatrix.Pop();
        }
示例#10
0
        public List <Vector3> GetWorldBorderLinesInFrontOfCamera(Camera camera, float distanceFromCamNearPlane, int numberOfEllipseSlices)
        {
            List <Vector3> worldExtentPoints = GetWorldExtentPointsInFrontOfCamera(camera, distanceFromCamNearPlane);

            Vector3 worldCenterPoint = GetWorldCenterPointInFrontOfCamera(camera, distanceFromCamNearPlane);
            Vector3 ellipseUp        = worldExtentPoints[0] - worldCenterPoint;
            Vector3 ellipseRight     = worldExtentPoints[1] - worldCenterPoint;

            float radiusX = ellipseRight.magnitude;
            float radiusY = ellipseUp.magnitude;

            ellipseUp.Normalize();
            ellipseRight.Normalize();

            return(Vector3Extensions.Get3DEllipseCircumferencePoints(radiusX, radiusY, ellipseRight, ellipseUp, worldCenterPoint, numberOfEllipseSlices));
        }
示例#11
0
        public void SetPointsOnPolygonPlaneAndNormal(List <Vector3> pointsOnPolygonPlane, Vector3 polygonNormal)
        {
            _normal = polygonNormal;
            _normal.Normalize();

            var quickHull = new Polygon3DQuickHull(pointsOnPolygonPlane, polygonNormal);

            _edges = quickHull.Calculate();

            _pointsOnPolygonPlane.Clear();
            foreach (var edge in _edges)
            {
                _pointsOnPolygonPlane.Add(edge.StartPoint);
                _pointsOnPolygonPlane.Add(edge.EndPoint);
            }
            Vector3Extensions.EliminateDuplicatePoints(_pointsOnPolygonPlane);
        }
示例#12
0
        public Vector3 GetSnapDestinationPointClosestToCursorPickPoint()
        {
            if (IsValid)
            {
                if (_type == SnapSurfaceType.GridCell)
                {
                    List <Vector3> snapDestinationPoints = _surfaceQuad.GetCenterAndCornerPoints();
                    snapDestinationPoints.AddRange(_surfaceQuad.GetMidEdgePoints());

                    return(Vector3Extensions.GetClosestPointToPoint(snapDestinationPoints, _mouseCursorPickPoint));
                }
                else
                {
                    XZOrientedQuad3D gridCellQuad          = _objectBoxSnapSurfaceGrid.GetCellFromPoint(_mouseCursorPickPoint).Quad;
                    List <Vector3>   snapDestinationPoints = gridCellQuad.GetCenterAndCornerPoints();
                    snapDestinationPoints.AddRange(gridCellQuad.GetMidEdgePoints());

                    return(Vector3Extensions.GetClosestPointToPoint(snapDestinationPoints, _mouseCursorPickPoint));
                }
            }
            return(Vector3.zero);
        }
示例#13
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();
                }
            }
        }
示例#14
0
        public List <Vector3> GetCornerPoints()
        {
            List <Vector3> points = _modelSpaceBox.GetCornerPoints();

            return(Vector3Extensions.GetTransformedPoints(points, TransformMatrix.ToMatrix4x4x));
        }
 private void StoreUnprojectedPivotPoints(List <Vector3> cornerPointsOfFaceMostAlignedWithSurface)
 {
     _unprojectedPivotPoints = new List <Vector3>(cornerPointsOfFaceMostAlignedWithSurface);
     _unprojectedPivotPoints.Insert(IndexOfPointInCenter, Vector3Extensions.GetAveragePoint(cornerPointsOfFaceMostAlignedWithSurface));
 }
 public void MovePoints(Vector3 moveVector)
 {
     _unprojectedPivotPoints = Vector3Extensions.ApplyOffsetToPoints(_unprojectedPivotPoints, moveVector);
     _pivotPointCollection.MovePoints(moveVector);
     _pointsPlane = new Plane(_pointsPlane.normal, _pivotPointCollection.GetPointByIndex(0));
 }
        public List <Segment3D> GetAllBoundarySegments()
        {
            List <Vector3> allPointsNoCenter = GetAllPointsExcludingCenter();

            return(Vector3Extensions.GetSegmentsBetweenPoints(allPointsNoCenter, true));
        }
 public int GetIndexOfPointClosestToPoint(Vector3 point)
 {
     return(Vector3Extensions.GetIndexOfPointClosestToPoint(AllPoints, point));
 }
示例#19
0
        public List <Vector3> GetBoxFaceCornerPoints(BoxFace boxFace)
        {
            List <Vector3> modelSpacePoints = _modelSpaceBox.GetBoxFaceCornerPoints(boxFace);

            return(Vector3Extensions.GetTransformedPoints(modelSpacePoints, TransformMatrix.ToMatrix4x4x));
        }
        public void UpdateForMouseMovement()
        {
            if (!_isActive)
            {
                return;
            }

            if (MouseButtonStates.Instance.IsMouseButtonDown(MouseButton.Left))
            {
                _state = ObjectVertexSnapSessionState.SnapToDestination;
            }
            else
            {
                _state = ObjectVertexSnapSessionState.SelectSourceVertex;
            }

            if (_state == ObjectVertexSnapSessionState.SelectSourceVertex)
            {
                if (_sourceObjects == null || _sourceObjects.Count == 0)
                {
                    _objectMask.ObjectCollectionMask.UnmaskAll();
                    MouseCursorRayHit cursorRayHit = GetCursorRayHit();
                    if (cursorRayHit.WasAnObjectHit)
                    {
                        GameObjectRayHit objectRayHit = cursorRayHit.ClosestObjectRayHit;
                        MeshRayHit       meshRayHit   = objectRayHit.ObjectMeshHit;
                        if (meshRayHit != null)
                        {
                            Octave3DMesh octaveMesh = meshRayHit.HitMesh;

                            Triangle3D sourceTriangle = octaveMesh.GetTriangle(meshRayHit.HitTriangleIndex);
                            sourceTriangle.TransformPoints(objectRayHit.HitObject.transform.localToWorldMatrix);

                            _sourceVertex = sourceTriangle.GetPointClosestToPoint(meshRayHit.HitPoint);
                            _sourceObject = objectRayHit.HitObject;
                            _objectMask.ObjectCollectionMask.Mask(_sourceObject.transform.root.gameObject.GetAllChildrenIncludingSelf());
                        }
                        else
                        {
                            SpriteRenderer spriteRenderer = objectRayHit.HitObject.GetComponent <SpriteRenderer>();
                            if (spriteRenderer != null)
                            {
                                _sourceObject = objectRayHit.HitObject;
                                _sourceVertex = Vector3Extensions.GetClosestPointToPoint(objectRayHit.ObjectBoxHit.HitBox.GetCenterAndCornerPoints(), objectRayHit.HitPoint);
                                _objectMask.ObjectCollectionMask.Mask(_sourceObject.transform.root.gameObject.GetAllChildrenIncludingSelf());
                            }
                        }
                    }
                }
                else
                {
                    MouseCursorRayHit cursorRayHit = GetCursorRayHit();
                    if (cursorRayHit.WasAnObjectHit)
                    {
                        GameObjectRayHit objectRayHit = cursorRayHit.ClosestObjectRayHit;
                        MeshRayHit       meshRayHit   = objectRayHit.ObjectMeshHit;
                        if (meshRayHit != null)
                        {
                            Octave3DMesh octaveMesh = meshRayHit.HitMesh;

                            Triangle3D sourceTriangle = octaveMesh.GetTriangle(meshRayHit.HitTriangleIndex);
                            sourceTriangle.TransformPoints(objectRayHit.HitObject.transform.localToWorldMatrix);
                            _sourceVertex = sourceTriangle.GetPointClosestToPoint(meshRayHit.HitPoint);
                        }
                        else
                        {
                            SpriteRenderer spriteRenderer = objectRayHit.HitObject.GetComponent <SpriteRenderer>();
                            if (spriteRenderer != null)
                            {
                                _sourceVertex = Vector3Extensions.GetClosestPointToPoint(objectRayHit.ObjectBoxHit.HitBox.GetCenterAndCornerPoints(), objectRayHit.HitPoint);
                            }
                        }
                    }

                    foreach (var parent in _sourceParents)
                    {
                        _objectMask.ObjectCollectionMask.Mask(parent.GetAllChildrenIncludingSelf());
                    }
                }
            }
            else
            {
                MouseCursorRayHit cursorRayHit = GetCursorRayHit();
                if (cursorRayHit.WasAnythingHit)
                {
                    bool useGridCellHit = false;
                    if (!cursorRayHit.WasAnObjectHit)
                    {
                        useGridCellHit = true;
                    }
                    else
                    if (cursorRayHit.WasAnObjectHit && cursorRayHit.WasACellHit)
                    {
                        float gridCellHitEnter = cursorRayHit.GridCellRayHit.HitEnter;
                        float objectHitEnter   = cursorRayHit.ClosestObjectRayHit.HitEnter;
                        if (gridCellHitEnter < Mathf.Max(0.0f, (objectHitEnter - 1e-3f)))
                        {
                            useGridCellHit = true;
                        }
                    }

                    if (useGridCellHit)
                    {
                        XZGridCell       hitCell  = cursorRayHit.GridCellRayHit.HitCell;
                        XZOrientedQuad3D cellQuad = hitCell.Quad;

                        _destinationObject   = null;
                        _destinationGridCell = hitCell;

                        _snapPosition = cellQuad.GetPointClosestToPoint(cursorRayHit.GridCellRayHit.HitPoint, true);
                        SnapToDestination();
                    }
                    else
                    {
                        GameObjectRayHit objectRayHit = cursorRayHit.ClosestObjectRayHit;
                        MeshRayHit       meshRayHit   = objectRayHit.ObjectMeshHit;
                        if (meshRayHit != null)
                        {
                            _destinationObject = objectRayHit.HitObject;
                            Triangle3D destinationTriangle = meshRayHit.HitMesh.GetTriangle(meshRayHit.HitTriangleIndex);
                            destinationTriangle.TransformPoints(_destinationObject.transform.localToWorldMatrix);
                            _destinationGridCell = null;

                            _snapPosition = destinationTriangle.GetPointClosestToPoint(meshRayHit.HitPoint);
                            SnapToDestination();
                        }
                        else
                        {
                            SpriteRenderer spriteRenderer = objectRayHit.HitObject.GetComponent <SpriteRenderer>();
                            if (spriteRenderer != null)
                            {
                                _destinationGridCell = null;
                                _destinationObject   = objectRayHit.HitObject;

                                _snapPosition = Vector3Extensions.GetClosestPointToPoint(objectRayHit.ObjectBoxHit.HitBox.GetCenterAndCornerPoints(), objectRayHit.HitPoint);
                                SnapToDestination();
                            }
                        }
                    }
                }
            }
        }
示例#21
0
 protected override List <Vector3> GenerateRenderPoints()
 {
     return(Vector3Extensions.Get3DEllipseCircumferencePoints(ModelSpaceRadii.x, ModelSpaceRadii.y, _ellipse.GetLocalAxis(CoordinateSystemAxis.PositiveRight), _ellipse.GetLocalAxis(CoordinateSystemAxis.PositiveLook), Center, _numberOfEllipseSlices));
 }
 public void MovePoints(Vector3 moveVector)
 {
     _pivotPoints = Vector3Extensions.ApplyOffsetToPoints(_pivotPoints, moveVector);
 }
示例#23
0
 public List <Segment3D> GetBoundarySegments()
 {
     return(Vector3Extensions.GetSegmentsBetweenPoints(GetCornerPoints(), true));
 }
示例#24
0
 public Vector3 GetPointClosestToPoint(Vector3 point)
 {
     return(Vector3Extensions.GetClosestPointToPoint(GetPoints(), point));
 }