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);
    }
Пример #2
0
    public virtual void Generate()
    {
        UnityEngine.Random.InitState(0);

        elevations = new List <float>();

        float[] seed = new float[octaves];

        for (int i = 0; i < octaves; i++)
        {
            seed[i] = Random.Range(0.0f, 100.0f);
        }

        PoissonDiscSampler sampler = new PoissonDiscSampler(xsize, ysize, minPointRadius);

        Polygon polygon = new Polygon();

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

        // Add some randomly sampled points
        for (int i = 0; i < randomPoints; i++)
        {
            polygon.Add(new Vertex(Random.Range(0.0f, xsize), Random.Range(0.0f, ysize)));
        }

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

        bin = new TriangleBin(mesh, xsize, ysize, minPointRadius * 2.0f);

        // Sample perlin noise to get elevations
        foreach (Vertex vert in mesh.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[o] + (float)vert.x * sampleSize / (float)xsize * frequency,
                                                  seed[o] + (float)vert.y * sampleSize / (float)ysize * frequency) - 0.5f) * amplitude;
                elevation += sample;
                maxVal    += amplitude;
                amplitude /= persistence;
                frequency *= frequencyBase;
            }

            elevation = elevation / maxVal;
            elevations.Add(elevation * elevationScale);
        }

        MakeMesh();

        ScatterDetailMeshes();
    }