Используя триангуляцию Делоне и Triangl eNet, чтобы сделать случайные острова. Как мне покрасить края? - PullRequest
0 голосов
/ 17 апреля 2020

Я генерирую случайные острова с помощью триангуляции Делани. Я следовал этому уроку: https://straypixels.net/delaunay-triangulation-terrain/

Я понятия не имею, как я могу покрасить края моих островов? Любая помощь? Я порождаю их со случайными точками, а затем в Триангуле eNet я их триангулирую. Мне не нужен код, я просто хотел бы, чтобы меня указывали в правильном направлении.

Код выглядит следующим образом:

  void Start()
{
    _polygon = new Polygon();

    // Loop through and add random points to the polygon
    for (int i = 0; i < _numberOfRandomPoints; i++)
    {
        _polygon.Add(new Vertex(Random.Range(0, _maxXSize), Random.Range(0, _maxYSize)));
    }

    // ConformingDelaunay is false by default; this leads to ugly long polygons at the edges
    // because the algorithm will try to keep the mesh convex
    TriangleNet.Meshing.ConstraintOptions options = new TriangleNet.Meshing.ConstraintOptions() { ConformingDelaunay = true };

    _triNetMesh = (TriangleNet.Mesh)_polygon.Triangulate(options);


    // Sample perlin noise at each generated point to get elevation and store it in
    // the elevations array
    foreach (Vertex vert in _triNetMesh.Vertices) {
        float elevation = 0.0f;
        float amplitude = Mathf.Pow(_persistence, _octaves);
        float frequency = 1.0f;
        float maxVal = 0.0f;

        for (int o = 0; o < _octaves; o++) {
            float sample = (Mathf.PerlinNoise(_seed + (float)vert.x*_sampleSize / (float)_maxXSize * frequency,
                _seed + (float)vert.y*_sampleSize / (float)_maxYSize * frequency) - 0.5f) * amplitude;
            elevation += sample;
            maxVal += amplitude;
            amplitude /= _persistence;
            frequency *= _frequencyBase;
        }

        elevation = elevation / maxVal;
        _elevations.Add(elevation);
    }

    MakeMesh();
}

    public void MakeMesh()
    { 


    // Harcoded triangle limit
    int trianglesInChunk = 10000;

    // Instantiate an enumerator to go over the Triangle.Net triangles - they don't
    // provide any array-like interface for indexing
    IEnumerator<Triangle> triangleEnumerator = _triNetMesh.Triangles.GetEnumerator();

    // Create more than one chunk, if necessary
    for (int chunkStart = 0; chunkStart < _triNetMesh.Triangles.Count; chunkStart += trianglesInChunk) {
        // Vertices in the unity mesh
        List<Vector3> vertices = new List<Vector3>();

        // Per-vertex normals
        List<Vector3> normals = new List<Vector3>();

        // Per-vertex UVs - unused here, but Unity still wants them
        List<Vector2> uvs = new List<Vector2>();

        // Triangles - each triangle is made of three indices in the vertices array
        List<int> triangles = new List<int>();

        // Iterate over all the triangles until we hit the maximum chunk size
        int chunkEnd = chunkStart + trianglesInChunk;
        for (int i = chunkStart; i < chunkEnd; i++) {
            if (!triangleEnumerator.MoveNext()) {
                // If we hit the last triangle before we hit the end of the chunk, stop
                break;
            }

            // Get the current triangle
            Triangle triangle = triangleEnumerator.Current; 

            // For the triangles to be right-side up, they need
            // to be wound in the opposite direction
            Vector3 v0 = GetPoint3D(triangle.vertices[2].id);
            Vector3 v1 = GetPoint3D(triangle.vertices[1].id);
            Vector3 v2 = GetPoint3D(triangle.vertices[0].id);

            // This triangle is made of the next three vertices to be added
            triangles.Add(vertices.Count);
            triangles.Add(vertices.Count + 1);
            triangles.Add(vertices.Count + 2);

            // Add the vertices
            vertices.Add(v0);
            vertices.Add(v1);
            vertices.Add(v2);

            // Compute the normal - flat shaded, so the vertices all have the same normal
            Vector3 normal = Vector3.Cross(v1 - v0, v2 - v0);
            normals.Add(normal);
            normals.Add(normal);
            normals.Add(normal);

            // If you want to texture your terrain, UVs are important,
            // but I just use a flat color so put in dummy coords
            uvs.Add(new Vector2(0.0f, 0.0f));
            uvs.Add(new Vector2(0.0f, 0.0f));
            uvs.Add(new Vector2(0.0f, 0.0f));
        }

        // Create the actual Unity mesh object
        Mesh chunkMesh = new Mesh();
        chunkMesh.vertices = vertices.ToArray();
        chunkMesh.uv = uvs.ToArray();
        chunkMesh.triangles = triangles.ToArray();
        chunkMesh.normals = normals.ToArray();
        chunkMesh.colors = _colours.ToArray();

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...