private List <Vector3> CreateRoad(Bounds bounds, int pointCount, Vector2[] clipPolygon) { // get offset, eg when biome mask is used for clipping float xmin = bounds.center.x - bounds.extents.x; float zmin = bounds.center.z - bounds.extents.z; // get 0/0-based bounds for graph processing Bounds graphBounds = new Bounds(bounds.size / 2, bounds.size); DelaunayVoronoiGraph graph = new DelaunayVoronoiGraph(); // algorithm is 0-based graph.GeneratePoints(0, bounds.size.x, bounds.size.z); // initialize with the dimensions which are used in the algorithm // add random points for (int i = 0; i < pointCount; i++) { // get random point starting at [0/0] Vector2 vector = PolygonUtils.GetRandomPointXZ(graphBounds); graph.AddPoint(vector.x, vector.y); } // create the graph using the points graph.CreateGraph(); int cellIndex = Random.Range(0, pointCount - 1); // normalize clip polygon, shift to [0/0] Vector2[] offsetClipPolygon = clipPolygon.Select(item => new Vector2(item.x - xmin, item.y - zmin)).ToArray(); // get cell, clip it at the clip polygon Cell cell = graph.GetVoronoiCell(cellIndex, offsetClipPolygon); if (cell == null) { return(null); } // consider biome mask shift: shift points away from 0/0 if necessary Vector3 position = new Vector3(cell.Centroid.x + xmin, 0, cell.Centroid.y + zmin); // TODO: recalculate, might have changed because of clipping // consider biome mask shift: shift points away from 0/0 if necessary List <Vector3> nodes = cell.Vertices.Select(item => new Vector3(item.x + xmin, 0, item.y + zmin)).ToList(); // apply random shape if requested if (editor.extension.shapeSettings.randomShape) { nodes = ShapeCreator.CreateRandomShape(nodes, // editor.extension.shapeSettings.RandomConvexity, // editor.extension.shapeSettings.keepOriginalPoints, // editor.extension.shapeSettings.RandomPointsCount, // editor.extension.shapeSettings.randomAngle, // editor.extension.shapeSettings.douglasPeuckerReductionTolerance); } return(nodes); }
/// <summary> /// Distributes random points within bounds and creates a convex hull around it. /// </summary> /// <param name="mask"></param> /// <param name="bounds"></param> /// <param name="count"></param> public static List <Vector3> CreateRandomShapeUsingConvexHull(Bounds bounds, int count) { List <Vector2> points = new List <Vector2>(); for (int i = 0; i < count; i++) { points.Add(PolygonUtils.GetRandomPointXZ(bounds)); } List <Vector2> convexHull = PolygonUtility.GetConvexHull(points); List <Vector3> nodes = convexHull.ConvertAll <Vector3>(item => new Vector3(item.x, 0, item.y)); return(nodes); }