コード例 #1
0
    public virtual void Generate(int worldSeed)
    {
        int chunkSeed = Generator.GetPerIslandSeed(this);

        creatingChunkStopwatch = System.Diagnostics.Stopwatch.StartNew();

        random       = new Random(chunkSeed);
        elevations   = new List <float>();
        islandColors = new List <Color>();

        CreatingPoolTask = delegate {
            PoissonDiscSampler sampler = new PoissonDiscSampler(Size, Size, minPointRadius);

            Polygon polygon = new Polygon();

            //Add uniformly-spaced points
            foreach (Vector2 sample in sampler.Samples(chunkSeed))
            {
                polygon.Add(new Vertex((double)sample.x, (double)sample.y));
            }

            //add points at corners so chunk will be always square shaped
            polygon.Add(new Vertex(0, 0));
            polygon.Add(new Vertex(0, Size));
            polygon.Add(new Vertex(Size, 0));
            polygon.Add(new Vertex(Size, Size));

            //Add some randomly sampled points
            for (int i = 0; i < randomPoints - 4; i++)
            {
                polygon.Add(new Vertex(random.Range(0.0f, Size), random.Range(0.0f, Size)));
            }

            TriangleNet.Meshing.ConstraintOptions options = new TriangleNet.Meshing.ConstraintOptions()
            {
                ConformingDelaunay = true
            };
            mesh = (TriangleNet.Mesh)polygon.Triangulate(options);

            // Sample perlin noise to get elevations
            foreach (Vertex vert in mesh.Vertices)
            {
                float height = Generator.GetTerrainHeight((float)vert.x, (float)vert.y, this);
                Color color  = Generator.GetTerrainColor((float)vert.x, (float)vert.y, height, this);

                elevations.Add(height);
                islandColors.Add(color);
            }

            CreateMeshRequest = true;

            //let this be always the last piece of code here
            try {
                bin = new TriangleBin(mesh, Size, Size, minPointRadius * 2.0f);
            } catch (Exception e) {
                Debug.Log("triangulation failed!");
            }
        };
        CustomThreadPool.AddTask(CreatingPoolTask);
    }