Example #1
0
        public Array2D <float> GenerateBottomIslandHeightmap(int size)
        {
            PerlinNoiseGenerater noise;

            noise = new PerlinNoiseGenerater();
            float          factor          = 0.1f;
            float          scale           = 10f;
            List <Vector3> positions       = new List <Vector3>();
            List <Color>   colors          = new List <Color>();
            List <Vector3> positionsIsland = new List <Vector3>();
            List <Color>   colorsIsland    = new List <Color>();
            List <Vector3> positionsFinal  = new List <Vector3>();
            List <Color>   colorsFinal     = new List <Color>();
            int            width           = size;
            int            height          = size;
            float          lengthFactor    = 0.2f;
            SimpleTerrain  terrain;
            SimpleTerrain  terrainIsland;
            SimpleTerrain  terrainFinal;

            ProceduralHeigthGenerater gen = new ProceduralHeigthGenerater(8, 0.7f);
            float heigestFBM   = 0;
            float heigestRidge = 0;

            float lowestIsland = 0;
            var   heights      = new Array2D <float>(new Point2(width, height));

            float[,] heightDataRidge  = new float[width, height];
            float[,] heightDataFBM    = new float[width, height];
            float[,] heightDataIsland = new float[width, height];
            float[,] heightDataFinal  = new float[width, height];


            var r = random;

            var octaves = new[]
            {
                //new Vector4(1/2f, 0.5f, (float)r.NextDouble(), (float)r.NextDouble()),
                //new Vector4(1/5f, 2, (float)r.NextDouble(), (float)r.NextDouble()),
                //new Vector4(1/10f, 5f, (float)r.NextDouble(), (float)r.NextDouble()),
                //new Vector4(1f, 1f, (float)r.NextDouble(), (float)r.NextDouble()),
                new Vector4(1 / 2f, 1f, (float)r.NextDouble(), (float)r.NextDouble()),
                new Vector4(1 / 5f, 4, (float)r.NextDouble(), (float)r.NextDouble()),
                new Vector4(1 / 10f, 10f, (float)r.NextDouble(), (float)r.NextDouble()),
            };


            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    noise.NumberOfOctaves = 1;

                    //heights[new Point2(i, j)] += noise.RidgedMF(i * lengthFactor, j * lengthFactor, 0.15f, 8, 2f, 0.7f, 0.8f);//noise.CombinedFractalBrowningAndRidgedMF(i * lengthFactor, j * lengthFactor, 8, 4,5f, 0.5f, 2f, 0.8f, 1f);


                    foreach (var v in octaves)
                    {
                        float   frequency = v.X;
                        float   oScale    = v.Y;
                        Vector2 offset    = new Vector2(v.Z, v.W) * 1000;

                        heights[new Point2(i, j)] += noise.GetPerlineNoise((i + offset.X) * frequency, (j + offset.Y) * frequency) * oScale;
                    }

                    heightDataFBM[i, j]    = noise.GetFractalBrowningNoise(i * lengthFactor, j * lengthFactor, 8, 1.2f, 1.9f, 1.2f);
                    heightDataIsland[i, j] = gen.IslandFactor(i * lengthFactor, j * lengthFactor, new Vector2(width * lengthFactor * 0.5f, height * lengthFactor * 0.5f), width * lengthFactor * 0.42f, width * lengthFactor * 0.4f);//noise.CombinedFractalBrowningAndRidgedMF(i, j, 8, 4, 4, 0.9f, 0.5f, 1.2f, 0.8f)*0.1f+gen.IslandFactor(i, j, new Vector2(width * 0.45f, height * 0.5f),0,width*0.22f)*0.9f  ;

                    if (heigestFBM < heightDataFBM[i, j])
                    {
                        heigestFBM = heightDataFBM[i, j];
                    }
                    if (heigestRidge < heightDataRidge[i, j])
                    {
                        heigestRidge = heightDataRidge[i, j];
                    }

                    if (lowestIsland > heightDataIsland[i, j])
                    {
                        lowestIsland = heightDataIsland[i, j];
                    }
                }
            }


            return(heights);
        }
Example #2
0
        public static void generateTerrain(int chunksX, int chunksY)
        {
            PerlinNoiseGenerater noise;

            noise = new PerlinNoiseGenerater();
            float          factor                  = 0.1f;
            float          scale                   = 1f;
            List <Vector3> positions               = new List <Vector3>();
            List <Vector3> positionsbase           = new List <Vector3>();
            List <Vector3> positionsbaseDifference = new List <Vector3>();

            List <Color> colors     = new List <Color>();
            List <Color> colorsbase = new List <Color>();

            int           width  = chunksX * 16;
            int           height = chunksY * 16;
            SimpleTerrain terrain;
            SimpleTerrain terrainbase;
            SimpleTerrain terrainbaseDiffernce;


            ProceduralHeigthGenerater gen = new ProceduralHeigthGenerater(8, 0.7f);

            float[,] heightData         = new float[width, height];
            float[,] heightDataErrosion = new float[width, height];
            //float[,] heightDataErrosionDiffernce = new float[width, height];



            for (int i = 0; i < (int)(width); i++)
            {
                for (int j = 0; j < height; j++)
                {
                    heightData[i, j] = (noise.GetPerlineNoise(i, j, 8, 0.1f, 0.8f, 0.8f) * 0.8f + noise.GetPerlineNoise(noise.Perturb(i, j, 0.1f, 30).X, noise.Perturb(i, j, 0.1f, 30).Y, 4, 0.2f, 0.5f, 0.5f) * 0.25f) * 70;
                }
            }
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    positionsbase.Add(new Vector3(i, heightData[i, j], j));
                    colorsbase.Add(Color.White);
                }
            }
            //heightDataErrosionDiffernce = heightData
            heightDataErrosion = gen.GenerateHydrolicErrosion(heightData, 50 * width * height, width, height);

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    positions.Add(new Vector3(i, heightDataErrosion[i, j], j));
                    colors.Add(new Color(new Microsoft.Xna.Framework.Vector3(positions[i * width + j].Y - positionsbase[i * width + j].Y, 50, 50)));
                    //positionsbaseDifference.Add(new Vector3(i, positions[i * width + j].Y - positionsbase[i * width + j].Y, j));(
                }
            }


            for (int x = 0; x < chunksX; x++)
            {
                for (int y = 0; y < chunksY; y++)
                {
                    var terr = new VoxelTerrainChunk();
                    terr.Size = new Point3(16, 64, 16);
                    //terr.Size = new Point3(5, 5, 5);
                    terr.WorldPosition = Vector3.Modulate(terr.Size.ToVector3() * terr.NodeSize, new Vector3(x, 0, y));
                    terr.Create();

                    TW.Data.GetSingleton <Datastore>().Persist(terr);

                    for (int tx = 0; tx < terr.Size.X; tx++)
                    {
                        for (int ty = 0; ty < terr.Size.Y / 2; ty++)
                        {
                            for (int tz = 0; tz < terr.Size.Z; tz++)
                            {
                                var heightMapX = tx + (int)terr.WorldPosition.X;
                                var heightMapZ = tz + (int)terr.WorldPosition.Z;
                                if (heightMapX >= width || heightMapZ >= height)
                                {
                                    continue;
                                }
                                if (ty < heightDataErrosion[heightMapX, heightMapZ] / 2f)
                                {
                                    terr.GetVoxelInternal(new Point3(tx, ty, tz)).Filled = true;
                                }
                            }
                        }
                    }
                }
            }
        }