Ejemplo n.º 1
0
    void RegenerateLines()
    {
        foreach (var line in lines)
        {
            Destroy(line);
        }
        lines.Clear();

        var triangles = BowyerWatson.Triangulate(points);

        var graph = new HashSet <Edge>();

        foreach (var triangle in triangles)
        {
            graph.UnionWith(triangle.edges);
        }

        var tree = Kruskal.MinimumSpanningTree(graph);

        foreach (var edge in tree)
        {
            Vector3 p1           = new Vector3(edge.a.x, edge.a.y);
            Vector3 p2           = new Vector3(edge.b.x, edge.b.y);
            var     line         = new GameObject();
            var     lineRenderer = line.AddComponent <LineRenderer>();
            lineRenderer.material = lineMaterial;
            lineRenderer.SetPosition(0, p1);
            lineRenderer.SetPosition(1, p2);
            lines.Add(line);
        }
    }
Ejemplo n.º 2
0
    public WorleyNoise.CellProfile GetCellProfile(NativeArray <WorleyNoise.CellData> nineCells, WorleyNoise.CellData cell)
    {
        this.cellPosition = cell.position;

        this.triangles = bowyerWatson.Triangulate(nineCells);

        SortTrianglesClockwise();

        this.cellVertices    = new NativeList <float3>(Allocator.Temp);
        this.adjacentCells   = new NativeList <WorleyNoise.CellDataX2>(Allocator.Temp);
        this.vertexRotations = new NativeList <float>(Allocator.Temp);
        GetCellVerticesAndAdjacentCells();

        WorldToLocalVertexPositions();

        var cellProfile = new WorleyNoise.CellProfile();

        cellProfile.data            = cell;
        cellProfile.vertices        = new NineValues <float3>(cellVertices);
        cellProfile.adjacentCells   = new NineValues <WorleyNoise.CellDataX2>(adjacentCells);
        cellProfile.vertexRotations = new NineValues <float>(vertexRotations);

        triangles.Dispose();
        cellVertices.Dispose();
        adjacentCells.Dispose();

        return(cellProfile);
    }
    void PointTesselation()
    {
        int    scale = 7;
        var    pointsToTriangulate = new List <PositionWrapper>();
        float3 up    = new float3(0, 0, 0.5f);
        float3 right = new float3(0.5f, 0, 0);

        int    centerRingLength = plot.radianOffset[gridSelect.y].Length;
        float2 center           = plot.radianOffset[gridSelect.y][gridSelect.x];
        float3 center3          = new float3(center.x, 0, center.y) * scale;
        float3 centerWorld      = plot.worldOffset[gridSelect.y][gridSelect.x];

        pointsToTriangulate.Add(new PositionWrapper(centerWorld, center3));

        //Debug.DrawLine(center3 + up, center3 - up, Color.green);
        //Debug.DrawLine(center3 + right, center3 - right, Color.green);
        for (int i = 0; i < adjacent.Count; i++)
        {
            float2 radians    = plot.radianOffset[adjacent[i].y][adjacent[i].x];
            float3 radians3   = new float3(radians.x, 0, radians.y) * scale;
            float3 world      = plot.worldOffset[adjacent[i].y][adjacent[i].x];
            int    ringLength = plot.radianOffset[adjacent[i].y].Length;

            //float2 normalised = math.unlerp(0, ringLength, radians);
            //float2 interpolated = radians;// = math.lerp(0, centerRingLength, normalised);
            //float3 interpolated3 = new float3(interpolated.x, 0, interpolated.y) * scale;
            //pointsToTriangulate.Add(new PositionWrapper(interpolated3));

            pointsToTriangulate.Add(new PositionWrapper(world, radians3));

            //Debug.DrawLine(radians3 + up, radians3 - up, Color.red);
            //Debug.DrawLine(radians3 + right, radians3 - right, Color.red);
        }

        NativeArray <PositionWrapper> triangulateArray = new NativeArray <PositionWrapper>(pointsToTriangulate.Count, Allocator.Persistent);

        for (int i = 0; i < triangulateArray.Length; i++)
        {
            triangulateArray[i] = pointsToTriangulate[i];
        }

        var triangles = bowyerWatson.Triangulate(triangulateArray);

        for (int i = 0; i < triangles.Length; i++)
        {
            DrawTriangle(triangles[i], Color.white);
        }

        triangulateArray.Dispose();
        triangles.Dispose();
    }