public IEnumerator PerlinNoise1DTerrainAnimation(int nOctaves, int perlinSeed, float fBias) { if (_map == null) { yield break; } int maxOctavesCount = 0; int worldWidth = _map.GetMapSize().x; while (worldWidth != 0) { worldWidth /= 2; maxOctavesCount++; } if (nOctaves > maxOctavesCount) { nOctaves = maxOctavesCount; } float defaultChange = 1.0f / _map.GetMapSize().y; PerlinNoise pn = new PerlinNoise(perlinSeed); int nFrames = 250; float[,] perlinArray = new float[_map.GetMapSize().x, nFrames]; pn.Get2DPerlinNoise(_map.GetMapSize().x, nFrames, nOctaves, fBias, ref perlinArray); for (int i = 0; i < nFrames; i++) { for (int x = 0; x < _map.GetMapSize().x; x++) { int height = (int)(perlinArray[x, i] * _map.GetMapSize().y); for (int y = 0; y < height; y++) { GameObject cell = _map.GetCellAt(new Vector2Int(x, y)).GetGameObject(); float f = defaultChange * y; Color heightColor = new Color(f, f, f, 1); cell.GetComponent <SpriteRenderer>().color = heightColor; } for (int y = height; y < _map.GetMapSize().y; y++) { GameObject cell = _map.GetCellAt(new Vector2Int(x, y)).GetGameObject(); cell.GetComponent <SpriteRenderer>().color = Color.white; } } yield return(null); } }
private static void GenerateTerrainWithPerlinNoise(ref Tile[,] grid, float seaLevel = 0.33f) { PerlinNoise pn = new PerlinNoise(perlinSeed); int nOctaves = pn.CalculateMaxOctavesCount(Utils.MapSize); float fBias = 2f; float[,] perlinArray = new float[Utils.MapSize, Utils.MapSize]; pn.Get2DPerlinNoise(Utils.MapSize, Utils.MapSize, nOctaves, fBias, ref perlinArray); //Min and max values that perlin array has float minP = 1; float maxP = 0; //calculating max and min values of perlin array for (int x = 0; x < Utils.MapSize; x++) { for (int y = 0; y < Utils.MapSize; y++) { float v = perlinArray[x, y]; minP = Mathf.Min(v, minP); maxP = Mathf.Max(v, maxP); } } float elevationRange = maxP - minP; float sea = minP + elevationRange * seaLevel; float sand = sea + (maxP - sea) * 0.2f; for (int x = 0; x < Utils.MapSize; x++) { for (int y = 0; y < Utils.MapSize; y++) { float height = perlinArray[x, y]; string dataName = "tile"; if (height < sea) { dataName = "water tile"; } else if (height < sand) { dataName = "sand tile"; } else { dataName = "grass tile"; } grid[x, y] = Factory.CreateData <Tile>(dataName, new Vector2Int(x, y)); } } }
public void PerlinNoise2DTerrainGeneration(int nOctaves, int perlinSeed, float fBias, float scale) { if (_map == null) { return; } int maxOctavesCount = 0; int scaledX = (int)(_map.GetMapSize().x * 1); int scaledY = (int)(_map.GetMapSize().y * 1); int worldWidth = scaledX; while (worldWidth != 0) { worldWidth /= 2; maxOctavesCount++; } if (nOctaves > maxOctavesCount) { nOctaves = maxOctavesCount; } PerlinNoise pn = new PerlinNoise(perlinSeed); float[,] perlinArray = new float[scaledX, scaledY]; pn.Get2DPerlinNoise(scaledX, scaledY, nOctaves, fBias, ref perlinArray); for (int x = 0; x < _map.GetMapSize().x; x++) { for (int y = 0; y < _map.GetMapSize().y; y++) { float height = perlinArray[x, y] * scale; GameObject cell = _map.GetCellAt(new Vector2Int(x, y)).GetGameObject(); Color heightColor = new Color(height, height, height, 1); cell.GetComponent <SpriteRenderer>().color = heightColor; } } }
public void Generate2DPerlinTerrain(int nOctaves, int perlinSeed, float fBias, float seaLevel) { if (_map == null) { return; } PerlinNoise pn = new PerlinNoise(perlinSeed); float[,] perlinArray = new float[_map.GetMapSize().x, _map.GetMapSize().y]; pn.Get2DPerlinNoise(_map.GetMapSize().x, _map.GetMapSize().y, nOctaves, fBias, ref perlinArray); float minP = 1; float maxP = 0; for (int x = 0; x < _map.GetMapSize().x; x++) { for (int y = 0; y < _map.GetMapSize().y; y++) { float v = perlinArray[x, y]; minP = Mathf.Min(v, minP); maxP = Mathf.Max(v, maxP); } } float elevationRange = maxP - minP; float sea = minP + elevationRange * seaLevel; float A = maxP - sea; float ground = sea + A * 0.8f; int waterCells = 0; int groundCells = 0; int mountainCells = 0; for (int x = 0; x < _map.GetMapSize().x; x++) { for (int y = 0; y < _map.GetMapSize().y; y++) { float height = perlinArray[x, y]; GameObject cell = _map.GetCellAt(new Vector2Int(x, y)).GetGameObject(); if (height < sea) { float maxHeight = sea; float percent = height / maxHeight; float resultR = seaColorMin.r + percent * (seaColorMax.r - seaColorMin.r); float resultG = seaColorMin.g + percent * (seaColorMax.g - seaColorMin.g); float resultB = seaColorMin.b + percent * (seaColorMax.b - seaColorMin.b); Color heightColor = new Color(resultR, resultG, resultB, 1); cell.GetComponent <SpriteRenderer>().color = heightColor; waterCells++; } else if (height < ground) { float maxHeight = ground - sea; float percent = (height - sea) / maxHeight; float resultR = groundColorMin.r + percent * (groundColorMax.r - groundColorMin.r); float resultG = groundColorMin.g + percent * (groundColorMax.g - groundColorMin.g); float resultB = groundColorMin.b + percent * (groundColorMax.b - groundColorMin.b); Color heightColor = new Color(resultR, resultG, resultB, 1); cell.GetComponent <SpriteRenderer>().color = heightColor; groundCells++; } else { float maxHeight = 1 - ground; float percent = (height - ground) / maxHeight; float resultR = mountainColorMin.r + percent * (mountainColorMax.r - mountainColorMin.r); float resultG = mountainColorMin.g + percent * (mountainColorMax.g - mountainColorMin.g); float resultB = mountainColorMin.b + percent * (mountainColorMax.b - mountainColorMin.b); Color heightColor = new Color(resultR, resultG, resultB, 1); cell.GetComponent <SpriteRenderer>().color = heightColor; mountainCells++; } } } }