Example #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);
        }
        public void RenderGizmos(ObjectPlacementExtensionPlane extensionPlane)
        {
            ObjectPlacementExtensionPlaneRenderSettings renderSettings = extensionPlane.RenderSettings;
            XZOrientedQuad3D planeQuad = extensionPlane.PlaneQuad;

            planeQuad.SetScale(renderSettings.PlaneScale);

            // Note: Add a small offset to avoid Z wars when the extension plane sits on top of other objects.
            const float quadOffset = 0.005f;

            GizmosEx.RenderXZOrientedQuad(planeQuad, renderSettings.PlaneColor, quadOffset);
            GizmosEx.RenderXZOrientedQuadBorderLines(planeQuad, renderSettings.PlaneBorderLineColor, quadOffset);

            // Render the plane normals
            List <Vector3> quadCornerPoints  = planeQuad.GetCornerPoints();
            Vector3        offsetToEndOfLine = extensionPlane.Plane.normal * renderSettings.PlaneNormalLineLength;

            foreach (Vector3 quadCornerPoint in quadCornerPoints)
            {
                GizmosEx.RenderLine(quadCornerPoint, quadCornerPoint + offsetToEndOfLine, renderSettings.PlaneNormalLineColor);
            }
        }
        public void FromXZOrientedQuad(XZOrientedQuad3D orientedQuad)
        {
            float desiredCellSize = ObjectSnapSettings.Get().ObjectColliderSnapSurfaceGridSettings.DesiredCellSize;

            // As a first step, ensure that the grid sits in the middle of the quad and has the same orientation
            Grid.SetOriginPosition(orientedQuad.GetOriginPosition());
            Grid.SetRotation(orientedQuad.GetRotation());

            Vector2 quadSize  = orientedQuad.ScaledXZSize;
            float   cellSizeX = desiredCellSize;
            float   cellSizeZ = desiredCellSize;

            int cellCountX, cellCountZ;

            if (cellSizeX > quadSize.x)
            {
                cellSizeX  = quadSize.x;
                cellCountX = 1;
            }
            else
            {
                // Adjust the cell size in such a way that we get an integer number of cells along each dimension
                float divisionResult = quadSize.x / cellSizeX;
                cellCountX = (int)divisionResult;
                float fractionalValue = divisionResult - cellCountX;
                if (fractionalValue != 0.0f)
                {
                    cellSizeX += (fractionalValue / cellCountX) * desiredCellSize;                              // If the fractional value is not 0, it means the cell size needs to be adjusted
                }
                // in such a way that the calculated number of cells ('cellCountX') covers the entire
                // quad area along the corresponding dimension.
            }

            if (cellSizeZ > quadSize.y)
            {
                cellSizeZ  = quadSize.y;
                cellCountZ = 1;
            }
            else
            {
                float divisionResult = quadSize.y / cellSizeZ;
                cellCountZ = (int)divisionResult;
                float fractionalValue = divisionResult - cellCountZ;
                if (fractionalValue != 0.0f)
                {
                    cellSizeZ += (fractionalValue / cellCountZ) * desiredCellSize;
                }
            }

            // Make sure the cell size is not larger than the quad
            if (cellSizeX > quadSize.x)
            {
                cellSizeX = quadSize.x;
            }
            if (cellSizeZ > quadSize.y)
            {
                cellSizeZ = quadSize.y;
            }

            Grid.CellSizeSettings.CellSizeX = cellSizeX;
            Grid.CellSizeSettings.CellSizeZ = cellSizeZ;

            // Store the cell count without taking into consideration the cell which sits at the origin of the grid
            int cellCountXNoMiddle = cellCountX - 1;
            int cellCountZNoMiddle = cellCountZ - 1;

            if (cellCountXNoMiddle % 2 == 0)
            {
                int halfCount = cellCountXNoMiddle / 2;
                Grid.DimensionSettings.FiniteDimensionSettings.XAxisCellIndexRange.Min = -halfCount;
                Grid.DimensionSettings.FiniteDimensionSettings.XAxisCellIndexRange.Max = halfCount;
            }
            else
            {
                int halfCount = cellCountXNoMiddle / 2;
                Grid.DimensionSettings.FiniteDimensionSettings.XAxisCellIndexRange.Min = -halfCount;
                Grid.DimensionSettings.FiniteDimensionSettings.XAxisCellIndexRange.Max = halfCount + 1;
            }

            if (cellCountZNoMiddle % 2 == 0)
            {
                int halfCount = cellCountZNoMiddle / 2;
                Grid.DimensionSettings.FiniteDimensionSettings.ZAxisCellIndexRange.Min = -halfCount;
                Grid.DimensionSettings.FiniteDimensionSettings.ZAxisCellIndexRange.Max = halfCount;
            }
            else
            {
                int halfCount = cellCountZNoMiddle / 2;
                Grid.DimensionSettings.FiniteDimensionSettings.ZAxisCellIndexRange.Min = -halfCount;
                Grid.DimensionSettings.FiniteDimensionSettings.ZAxisCellIndexRange.Max = halfCount + 1;
            }

            // We need to make sure that the grid nicely sits within the boundaries of the quad. In order to do this, we will
            // align the top left corners of the quad and the grid's top left cell.
            Vector3          quadTopLeftCornerPoint     = orientedQuad.GetCornerPoints()[(int)XZOrientedQuad3DCornerPoint.TopLeft];
            XZOrientedQuad3D topLeftCellQuad            = Grid.CalculateCellQuad(Grid.DimensionSettings.FiniteDimensionSettings.XAxisCellIndexRange.Min, Grid.DimensionSettings.FiniteDimensionSettings.ZAxisCellIndexRange.Max);
            Vector3          cellQuadTopLeftCornerPoint = topLeftCellQuad.GetCornerPoints()[(int)XZOrientedQuad3DCornerPoint.TopLeft];
            Vector3          gridOriginMoveVector       = quadTopLeftCornerPoint - cellQuadTopLeftCornerPoint;

            Grid.Translate(gridOriginMoveVector);
        }
Example #4
0
 public static void RenderXZOrientedQuadBorderLines(XZOrientedQuad3D orientedQuad, Color color)
 {
     RenderLinesBetweenPoints(orientedQuad.GetCornerPoints(), color);
 }