Example #1
0
        private void OnDrawGizmos()
        {
            if (!isEnabled)
            {
                return;
            }

            //Generate the random sites
            List <Vector2> randomSites = new List <Vector2>();

            //Generate random numbers with a seed
            Random.InitState(seed);

            int max = mapRadius;
            int min = -mapRadius;

            for (int i = 0; i < numberOfPoints; i++)
            {
                int randomX = Random.Range(min, max);
                int randomZ = Random.Range(min, max);

                randomSites.Add(new Vector2(randomX, randomZ));
            }


            //Points outside of the screen for voronoi which has some cells that are infinite
            float bigSize = mapRadius * 5f;

            //Star shape which will give a better result when a cell is infinite large
            //When using other shapes, some of the infinite cells misses triangles
            randomSites.Add(new Vector2(0f, bigSize));
            randomSites.Add(new Vector2(0f, -bigSize));
            randomSites.Add(new Vector2(bigSize, 0f));
            randomSites.Add(new Vector2(-bigSize, 0f));

            //Generate the voronoi diagram
            var(vertices, triangles) = Delaunay.GenerateTriangulation(randomSites);
            var cells = Delaunay.GenerateVoronoiCells((vertices, triangles));

            //Display the voronoi diagram
            LevelDataTester.DrawShapes(cells.Values.ToArray(), Vector3.zero);
            if (showDelaunayDiagam)
            {
                LevelDataTester.DrawShapes(triangles.Values.ToArray(), Vector3.forward, true);
            }

            //Display the sites
            Gizmos.color = Color.white;

            for (int i = 0; i < randomSites.Count; i++)
            {
                float radius = 0.2f;

                Gizmos.DrawSphere(randomSites[i], radius);
            }
        }
Example #2
0
        /// <summary>
        /// Make a new biome map for a given level:
        /// </summary>
        /// <param name="seed"></param>
        /// <param name="biomeTypes"></param>
        protected BiomeMap(Level level, int numberOfVoronoiPoints = 250)
        {
            /// init randomness
            seed  = level.seed;
            noise = new FastNoise(seed);

            /// Generate the biome map using a voronoi diagram with a random set of points
            //Generate the random sites
            List <Vector2> randomBiomeCenters = new List <Vector2>();
            int            max     = level.chunkBounds.x * Chunk.Diameter;
            int            bigSize = max * 5;

            Random.InitState(seed);
            for (int i = 0; i < numberOfVoronoiPoints; i++)
            {
                int randomX = Random.Range(0, max);
                int randomZ = Random.Range(0, max);
                randomBiomeCenters.Add(new Vector2(randomX, randomZ));
            }

            // add a star around the random points to clean them up
            randomBiomeCenters.Add(new Vector2(0f, bigSize));
            randomBiomeCenters.Add(new Vector2(0f, -bigSize));
            randomBiomeCenters.Add(new Vector2(bigSize, 0f));
            randomBiomeCenters.Add(new Vector2(-bigSize, 0f));

            // generate the cells based on the centers
            Dictionary <Vertex, Polygon> voronoiCells = Delaunay.GenerateVoronoiCells(
                Delaunay.GenerateTriangulation(randomBiomeCenters)
                );

            // generate the biomes from the voronoi cells.
            foreach (Polygon voronoiCell in voronoiCells.Values)
            {
                assignBiomeToVoronoiCell(voronoiCell);
            }
        }