Esempio n. 1
0
        public void terrainGenerator()
        {
            float scale = 0.0052f;
            int   size  = 500;

            Noise.Seed            = new Random().Next(int.MinValue, int.MaxValue);
            this.generationThread = new Thread(() => {
                float[,] map = Noise.Calc2D(size, size, scale);

                for (int c = 0; c < ((map.GetLength(0) + map.GetLength(1)) / 16); c++)
                {
                    for (int x = c; c + x < map.GetLength(0) / 16; x++)
                    {
                        for (int y = c; c + y < map.GetLength(1) / 16; y++)
                        {
                            double noise = Math.Abs(map[x, y]);
                            if (noise >= 0)
                            {
                                int waterHeight = 2;
                                //Material m = (int)noise <= waterHeight ? (Block)new Wool(new Color(46, 124, 248, 150)) : new Grass();
                                Material m = (int)noise <= waterHeight ? Material.WOOL : Material.GRASS;
                                this.setBlock(new Location(x, y, (int)noise <= waterHeight ? waterHeight : noise), m);
                            }
                        }
                    }
                }
            });
            this.generationThread.Priority     = ThreadPriority.Highest;
            this.generationThread.IsBackground = true;
            this.generationThread.Start();
        }
Esempio n. 2
0
        public void GenerateLandshaftMap()
        {
            //сид для шума
            Noise.Seed = rnd.Next();

            //двухмерный массив с шумом
            float[,] noise = Noise.Calc2D(700, 700, 0.01f);

            Noise.Seed      = rnd.Next();
            float[,] noise2 = Noise.Calc2D(700, 700, 0.01f);

            //перебор массива
            for (int y = 0; y < 500; y++)
            {
                for (int x = 0; x < 500; x++)
                {
                    //получаем точку с шума
                    float pixelNoise = (noise[y, x] + noise2[y, x]) / 4;
                    pixelNoise /= 2.5f;

                    Color currentPixelColor = GetPixel(x, y);

                    //каналы цвета
                    //добавляем яркости взависимости от значения точки из шума
                    int R = currentPixelColor.R + (int)pixelNoise > 255 ? 255 : currentPixelColor.R + (int)pixelNoise;
                    int G = currentPixelColor.G + (int)pixelNoise > 255 ? 255 : currentPixelColor.G + (int)pixelNoise;
                    int B = currentPixelColor.B + (int)pixelNoise > 255 ? 255 : currentPixelColor.B + (int)pixelNoise;

                    currentPixelColor = Color.FromArgb(R, G, B);

                    DrawPixel(currentPixelColor, x, y);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Randomly generate vertices position value (Noise)
        /// </summary>
        private void LoadHeightData()
        {
            Random rng = new Random();

            heightData = new float[terrainWidth, terrainLength];
            float fOff = 0.2f;

            //float hOff = 0.02f;
            Console.WriteLine("Noise range " + noiseRange);
            float frequency = noiseRange / terrainWidth;

            float[,] noises = Noise.Calc2D(terrainWidth, terrainLength, frequency);
            for (int i = 0; i < terrainLength; i++)
            {
                for (int j = 0; j < terrainWidth; j++)
                {
                    heightData[j, i] = noises[j, i] / 75 + fOff;
                    if (heightData[j, i] > MaxHeight)
                    {
                        MaxHeight = heightData[j, i];
                    }
                }
                // fOff += 0.1f;
                // hOff += 0.01f;
            }
        }
Esempio n. 4
0
        /// <inheritdoc />
        public void GenerateVoxels(Vector3Int worldPosition, ref Voxel[,,] voxels)
        {
            Noise.Seed    = VoxelTerrainManager.Current.WorldSeed;
            Noise.OffsetX = worldPosition.X;
            Noise.OffsetY = worldPosition.Z;
            Noise.OffsetZ = worldPosition.Y;

            // Calculate noise
            var noise2D = Noise.Calc2D(VoxelTerrainChunk.Width, VoxelTerrainChunk.Length, 0.025f);

            const float baseLevel = 16.0f;
            const float hillLevel = 12.0f;

            for (var y = 0; y < VoxelTerrainChunk.Height; y++)
            {
                for (var x = 0; x < VoxelTerrainChunk.Width; x++)
                {
                    for (var z = 0; z < VoxelTerrainChunk.Length; z++)
                    {
                        // Calculate world space voxel Y/height
                        var voxelHeight = worldPosition.Y + y;

                        // Calculate noise value in range [0...1]
                        var noiseValue = noise2D[x, z] / 256.0f;

                        // Calculate terrain height TODO: This can be optimized, use cached values after first XZ iteration
                        var terrainHeight = noiseValue * hillLevel;
                        terrainHeight += baseLevel;

                        // Calculate distance to the surface
                        var surfaceDistance = terrainHeight - voxelHeight;

                        // Check if we are still under surface, if not, then continue to the next voxel.
                        if (surfaceDistance < 0)
                        {
                            continue;
                        }

                        // Select which block should we used based on distance from surface
                        ushort voxelId;
                        if (surfaceDistance > 6)
                        {
                            voxelId = 1; // We are deep, use stone.
                        }
                        else if (surfaceDistance > 2)
                        {
                            voxelId = 2; // Well a lil bit under surface, use dirt.
                        }
                        else
                        {
                            voxelId = 4; // Otherwise, use sand.
                        }
                        voxels[x, y, z] = new Voxel(voxelId);
                    }
                }
            }
        }
Esempio n. 5
0
        public static void AddNoise(TiledMap map, float intensity, float scale)
        {
            float[,] noise = Noise.Calc2D(map.width, map.length, scale);

            for (int i = 0; i < map.width; i++)
            {
                for (int j = 0; j < map.length; j++)
                {
                    map.tiles[i, j].height += noise[i, j] * intensity;
                }
            }
        }
Esempio n. 6
0
		public void GenerateNoisePng()
		{
			Texture2D noiseTexture = new Texture2D(Settings.ChunkCubicSize.x, Settings.ChunkCubicSize.y);

			Noise noiseGenerator = new Noise(Settings.Seed);

			float[,] noise = noiseGenerator.Calc2D(Settings.ChunkCubicSize.x, Settings.ChunkCubicSize.y, Settings.NoiseScale);

			//float[,] noise = new float[Settings.ChunkCubicSize.x, Settings.ChunkCubicSize.y];

			/*for (int i = 0; i < Settings.ChunkCubicSize.x; i++)
			{
				for (int j = 0; j < Settings.ChunkCubicSize.y; j++)
				{
					noise[i, j] = TestNoise.Noise.Simplex2D(new Vector3(i, j, 0), Settings.NoiseScale).value;
				}
			}*/

			for (int i = 0; i < Settings.ChunkCubicSize.x; i++)
			{
				for (int j = 0; j < Settings.ChunkCubicSize.y; j++)
				{
					float normalizedNoise = (noise[i, j] + 1f) / 2f;
					Color color = new Color(normalizedNoise, normalizedNoise, normalizedNoise, 1f);

					noiseTexture.SetPixel(i, j, color);
				}
			}

			byte[] bytes = noiseTexture.EncodeToPNG();
			string filename = "Assets/noise.png";
			System.IO.File.WriteAllBytes(filename, bytes);

			Texture2D resultTexture = new Texture2D(Settings.ChunkCubicSize.x, Settings.ChunkCubicSize.y);

			for (int i = 0; i < Settings.ChunkCubicSize.x; i++)
			{
				for (int j = 0; j < Settings.ChunkCubicSize.y; j++)
				{
					float normalizedNoise = (noise[i, j] + 1f) / 2f;

					Color color = normalizedNoise <= Settings.RandomHollowCellsPercent ? HollowColor : CaveColor;

					resultTexture.SetPixel(i, j, color);
				}
			}

			bytes = resultTexture.EncodeToPNG();
			filename = "Assets/noise_result.png";
			System.IO.File.WriteAllBytes(filename, bytes);
		}
        /// <summary>
        /// Generates the land.
        /// </summary>
        /// <param name="width">The width of the generated world.</param>
        /// <param name="height">The height of the generated world.</param>
        /// <param name="rdm">The random number generator.</param>
        /// <returns>The generated world.</returns>
        public static GeneratedWorld Generate(int width, int height, Random rdm)
        {
            GeneratedWorld world = new GeneratedWorld(width, height);

            Noise.Seed             = rdm.Next();
            float[,] elevationMap  = Noise.Calc2D(width, height, 0.05f);
            Noise.Seed             = rdm.Next();
            float[,] rainfallMap   = Noise.Calc2D(width, height, 0.05f);
            Noise.Seed             = rdm.Next();
            float[,] drainageMap   = Noise.Calc2D(width, height, 0.05f);
            Noise.Seed             = rdm.Next();
            float[,] tempertureMap = Noise.Calc2D(width, height, 0.05f);
            Noise.Seed             = rdm.Next();
            float[,] evilMap       = Noise.Calc2D(width, height, 0.05f);
            Noise.Seed             = rdm.Next();
            float[,] savageMap     = Noise.Calc2D(width, height, 0.05f);
            Noise.Seed             = rdm.Next();
            float[,] materialsMap  = Noise.Calc2D(width, height, 0.05f);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int elevation  = (int)(ElevationMask(elevationMap[x, y], x, y, width, height) / 255f * 400f);
                    int rainfall   = (int)(rainfallMap[x, y] / 255f * 100f);
                    int drainage   = (int)(drainageMap[x, y] / 255f * 100f);
                    int temperture = (int)TempertureMask(tempertureMap[x, y], y, height);
                    int evil       = (int)(evilMap[x, y] / 255f * 100f);
                    int savagery   = (int)(savageMap[x, y] / 255f * 100f);
                    int materials  = (int)(materialsMap[x, y] / 255f * 100f);

                    world.Grid[x, y] = new GeneratedSquare()
                    {
                        Elevation   = elevation,
                        Rainfall    = rainfall,
                        Drainage    = drainage,
                        Temperature = temperture,
                        Evil        = evil,
                        Savagery    = savagery,
                        Materials   = materials,
                        Water       = elevation < 100,
                    };
                }
            }

            // Convert to ocean on both sides of the world.
            ConvertToOcean(world, 0, height / 2);
            ConvertToOcean(world, width, height / 2);

            return(world);
        }
Esempio n. 8
0
        private void AddTerrain(int width, int height)
        {
            SetCurrentStep("--- Adding terrain");
            float[,] fheights = new float[width, height];
            float[,] heights  = new float[width, height];
            Noise.Seed        = /*399;*/ new Random().Next(1, 10000);
            fheights          = Noise.Calc2D(width, height, 0.009f);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    heights[x, y] = fheights[x, y] / 10;
                }
            }
            //TODO: terug aanpassen

            tcph.AddCommandToQueue(Commands.CreateTerrain(width, height, heights));
        }
Esempio n. 9
0
 public static float[,] GeneratePerlinNoiseMap(int width, int height, int seed)
 {
     Noise.Seed        = seed;
     float[,] noiseMap = Noise.Calc2D(width, height, .2f);
     return(noiseMap);
 }