private void Generate2DPerlinNoise(int seed) { PerlinNoiseGenerater noiseGenerator = new PerlinNoiseGenerater(seed, PerlinNoiseGenerater.Continum.X, _length, _width, _octaves, _lacunarity, _persistance); Color[] colors = new Color[_size * _size]; for (int y = 0; y < _size; y++) { for (int x = 0; x < _size; x++) { colors[x + y * _size] = Color.Lerp(Color.white, Color.black, noiseGenerator.SampleAt(x / (float)_size, y / (float)_size)); } } if (!_renderer) { _renderer = GameObject.CreatePrimitive(PrimitiveType.Plane).GetComponent <MeshRenderer>(); } Texture2D texture = new Texture2D(_size, _size); texture.SetPixels(colors); texture.Apply(); _renderer.transform.position = Vector3.zero; _renderer.transform.localRotation = Quaternion.Euler(-90, 0, 0); _renderer.transform.localScale = new Vector3(1000, 1, 1000); _renderer.sharedMaterial.mainTexture = texture; }
private HexGrid <float> GenerateHeightMap(int length, int width, int continents, float seaLevel, float frequency, int octaves) { HexGrid <float> heightMap = new HexGrid <float>(length, width); PerlinNoiseGenerater oceanGenerator = new PerlinNoiseGenerater(rng.GenerateRandomInt(), PerlinNoiseGenerater.Continum.X, 2 + continents, 2 + continents); PerlinNoiseGenerater heightGenerator = new PerlinNoiseGenerater(rng.GenerateRandomInt(), PerlinNoiseGenerater.Continum.X, Mathf.CeilToInt(length * frequency), Mathf.CeilToInt(width * frequency), octaves, 2, 0.5f); float maxX = Mathf.Sqrt(3) * _tileSize * (length - 1); float maxZ = 1.5f * _tileSize * (width - 1); heightMap.For(delegate(int x, int y) { Vector3 worldCoordinates = WorldMap.ToWorldCoordinates(x, y, _tileSize); float randomValue = oceanGenerator.SampleAt(Mathf.InverseLerp(0, maxX, worldCoordinates.x), Mathf.InverseLerp(0, maxZ, worldCoordinates.z), 0, 100); heightMap[x, y] = (randomValue > 45 - seaLevel && randomValue < 55 + seaLevel ? -1 : 1) * heightGenerator.SampleAt(Mathf.InverseLerp(0, Mathf.Sqrt(3) * _tileSize * (length - 1), worldCoordinates.x), Mathf.InverseLerp(0, 1.5f * _tileSize * (width - 1), worldCoordinates.z), 0, 100); }); return(heightMap); }
private HexGrid <float> GenerateBiomeMap(int length, int width, float temperature, float surfaceFrequency, int surfaceOctaves, float forestFrequency, int forestOctaves) { HexGrid <float> biomeMap = new HexGrid <float>(length, width); float maxX = Mathf.Sqrt(3) * _tileSize * (length - 1); float maxZ = 1.5f * _tileSize * (width - 1); PerlinNoiseGenerater surfaceGenerator = new PerlinNoiseGenerater(rng.GenerateRandomInt(), PerlinNoiseGenerater.Continum.X, Mathf.CeilToInt(length * surfaceFrequency), Mathf.CeilToInt(width * surfaceFrequency), surfaceOctaves, 2, 0.5f); PerlinNoiseGenerater forestGenerator = new PerlinNoiseGenerater(rng.GenerateRandomInt(), PerlinNoiseGenerater.Continum.X, Mathf.CeilToInt(length * forestFrequency), Mathf.CeilToInt(width * forestFrequency), forestOctaves, 2, 0.5f); biomeMap.For(delegate(int x, int y) { Vector3 worldCoordinates = WorldMap.ToWorldCoordinates(x, y, _tileSize); float latitude = Math.Abs(WorldMap.ToGeographicCoordinates(x, y, length, width).y * 2f / width); float randomValue = surfaceGenerator.SampleAt(Mathf.InverseLerp(0, maxX, worldCoordinates.x), Mathf.InverseLerp(0, maxZ, worldCoordinates.z), 0, 100); biomeMap[x, y] = randomValue > Mathf.Lerp(50 - temperature, 80, MathUtility.CubicCurve2(latitude)) ? 0 : (randomValue < Mathf.Lerp(20 - 2 * temperature, 100, MathUtility.CubicCurve1(latitude)) ? -1 : 1) * forestGenerator.SampleAt(Mathf.InverseLerp(0, maxX, worldCoordinates.x), Mathf.InverseLerp(0, maxZ, worldCoordinates.z), 0, 100); }); return(biomeMap); }
//private void DrawMapTile(WorldMapTile tile, List<Vector3> vertices, List<Color> colors, List<Vector2> uvs, List<int> indices, float tileSize) //{ // DrawHexagonAt(WorldMap.ToWorldCoordinates(tile.axialCoordinates, tileSize), vertices, colors, uvs, indices, tileSize); //} private void DrawHexagonAt(Vector3 center, PerlinNoiseGenerater heightMap, List <Vector3> vertices, List <Color> colors, List <Vector2> uvs, List <int> indices, float size) { int centerIndex = vertices.Count; float centerH = heightMap.SampleAt(Mathf.InverseLerp(0, 4000, center.x), Mathf.InverseLerp(0, 2000, center.z)); vertices.Add(Elevate(center, centerH)); Color c = GetSurfaceColor(center, centerH); //Debug.Log(c); colors.Add(c); uvs.Add(new Vector2(center.x / 100, center.z / 100)); for (int i = 0; i < 6; i++) { Vector3 position = center + size / 2 * new Vector3(Mathf.Sin(i * DEG_60), 0, Mathf.Cos(i * DEG_60)); float h = heightMap.SampleAt(Mathf.InverseLerp(0, 4000, position.x), Mathf.InverseLerp(0, 2000, position.z)); vertices.Add(Elevate(position, h)); colors.Add(GetSurfaceColor(position, h)); uvs.Add(new Vector2(position.x / 100, position.z / 100)); indices.Add(centerIndex); indices.Add(centerIndex + 1 + i); indices.Add(centerIndex + 1 + (i + 1) % 6); } for (int i = 0; i < 6; i++) { Vector3 position1 = center + size * new Vector3(Mathf.Sin(i * DEG_60), 0, Mathf.Cos(i * DEG_60)); Vector3 position2 = center + size * Mathf.Cos(DEG_30) * new Vector3(Mathf.Sin(DEG_30 + i * DEG_60), 0, Mathf.Cos(DEG_30 + i * DEG_60)); float h1 = heightMap.SampleAt(Mathf.InverseLerp(0, 4000, position1.x), Mathf.InverseLerp(0, 2000, position1.z)); float h2 = heightMap.SampleAt(Mathf.InverseLerp(0, 4000, position2.x), Mathf.InverseLerp(0, 2000, position2.z)); vertices.Add(Elevate(position1, h1)); vertices.Add(Elevate(position2, h2)); colors.Add(GetSurfaceColor(position1, h1)); colors.Add(GetSurfaceColor(position2, h2)); uvs.Add(new Vector2(position1.x / 100, position1.z / 100)); uvs.Add(new Vector2(position2.x / 100, position2.z / 100)); indices.Add(centerIndex + 7 + i * 2 + 1); indices.Add(centerIndex + 1 + i); indices.Add(centerIndex + 7 + i * 2); indices.Add(centerIndex + 7 + i * 2 + 1); indices.Add(centerIndex + 1 + (i + 1) % 6); indices.Add(centerIndex + 1 + i); indices.Add(centerIndex + 7 + i * 2 + 1); indices.Add(centerIndex + 7 + (i * 2 + 2) % 12); indices.Add(centerIndex + 1 + (i + 1) % 6); } //for (int i = 0; i < 6; i++) //{ // Vector3 position1 = center + size * new Vector3(Mathf.Sin(i * DEG_60), 0, Mathf.Cos(i * DEG_60)); // Vector3 position2 = center + size * Mathf.Cos(DEG_30) / Mathf.Cos(DEG_10) * new Vector3(Mathf.Sin(DEG_20 + i * DEG_60), 0, Mathf.Cos(DEG_20 + i * DEG_60)); // Vector3 position3 = center + size * Mathf.Cos(DEG_30) / Mathf.Cos(DEG_10) * new Vector3(Mathf.Sin(DEG_40 + i * DEG_60), 0, Mathf.Cos(DEG_40 + i * DEG_60)); // vertices.Add(AddHeight(position1, heightMap)); // vertices.Add(AddHeight(position2, heightMap)); // vertices.Add(AddHeight(position3, heightMap)); // //if (tile.biome == 0) // // colors.Add(new Color(1, 1, 1)); // //else // // colors.Add(new Color(0, 0, 0)); // uvs.Add(new Vector2(position1.x / 100, position1.z / 100)); // uvs.Add(new Vector2(position2.x / 100, position2.z / 100)); // uvs.Add(new Vector2(position3.x / 100, position3.z / 100)); // indices.Add(centerIndex + 19 + i * 3 + 1); // indices.Add(centerIndex + 7 + i * 2); // indices.Add(centerIndex + 19 + i * 3); // indices.Add(centerIndex + 19 + i * 3 + 1); // indices.Add(centerIndex + 7 + i * 2 + 1); // indices.Add(centerIndex + 7 + i * 2); // indices.Add(centerIndex + 19 + i * 3 + 1); // indices.Add(centerIndex + 19 + i * 3 + 2); // indices.Add(centerIndex + 7 + i * 2 + 1); // indices.Add(centerIndex + 19 + i * 3 + 2); // indices.Add(centerIndex + 7 + (i * 2 + 2) % 12); // indices.Add(centerIndex + 7 + i * 2 + 1); // indices.Add(centerIndex + 19 + i * 3 + 2); // indices.Add(centerIndex + 19 + (i * 3 + 3) % 18); // indices.Add(centerIndex + 7 + (i * 2 + 2) % 12); //} }