using System; using Generators; using UnityEngine; public class MapRenderer : MonoBehaviour { public Renderer mapRenderer; public IGenerator generator; public Vector2 Offset; public float Scale; [Range(0, 1)] public float Decay; [Range(0, 4)] public float Lacunarity; [Range(0, 1)] public float Threshold; public void DrawMap(int width, int height) { var texture = new Texture2D(width, height); var colors = new Color[width * height]; var position = new Vector2(0.0f, 0.0f); generator = new PerlinNoiseGenerator { Scale = Scale, Offset = Offset, Decay = Decay, Lacunarity = Lacunarity }; generator = new IslandGenerator(generator) { Threshold = Threshold }; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { position.x = x - width / 2; position.y = y - height / 2; colors[y * width + x] = Color.Lerp(Color.black, Color.white, (float)generator.GetValue(position)); } } texture.SetPixels(colors); texture.Apply(); mapRenderer.sharedMaterial.mainTexture = texture; mapRenderer.transform.localScale = new Vector3(width / 100.0f, 1, height / 100.0f); } }