static byte[] RandomFillMap(WavyIslandMapGenerationOptions options) { var perlinSeed = new Random(options.Seed).Next(); var perlinXOffset = perlinSeed & 0xFF; var perlinYOffset = perlinSeed >> 15; var noiseData = new byte[options.Width * options.Height]; for (var y = 0; y < options.Height; y++) { for (var x = 0; x < options.Width; x++) { if (y == 0) { noiseData[y * options.Width + x] = 255; } else { var perlin = Mathf.PerlinNoise( perlinXOffset + options.PerlinScale * (x / 1.0f), perlinYOffset + options.PerlinScale * (y / 1.0f)); // apply "turbulence" to the perlin noise perlin = Mathf.Abs(perlin - 0.5f) * 2; noiseData[y * options.Width + x] = (byte)(perlin * 255); } } } return(noiseData); }
private IEnumerator GenerateMap() { var options = new WavyIslandMapGenerationOptions { Seed = Seed, PerlinScale = PerlinScale, PerlinThreshold = PerlinThreshold, SmoothPasses = SmoothPasses, DilatePasses = DilatePasses, Width = Width, Height = Height }; if (UseRandomSeed) { options.Seed = Time.time.GetHashCode(); } var generation = GeneratesWavyIslandMaps.GenerateMap(options); while (generation.MoveNext()) { // if we needed to bring back the generation algorithm some logic could go here yield return(_map = generation.Current); } ClearChildren(); AddCollisionMesh(); AddDisplayMesh(); yield break; }
public static IEnumerator <MapData> GenerateMap(WavyIslandMapGenerationOptions options) { Debug.Log("GeneratesWavyIslandMaps GenerateMap start with seed " + options.Seed); var timer = Stopwatch.StartNew(); var noiseData = RandomFillMap(options); yield return(null); var fillTime = timer.Lap(); // TODO: display noise map/bitmap somehow? var bitmap = ThresholdMap(noiseData, options); var tmpMap = new Bitmap(options.Width, options.Height, new byte[options.Width * options.Height]); var thresholdTime = timer.Lap(); yield return(null); PickIslands(ref bitmap, ref tmpMap); var islandsTime = timer.Lap(); yield return(null); for (var i = 0; i < options.DilatePasses; i++) { Dilate(ref bitmap, ref tmpMap, options); yield return(null); } var dilateTime = timer.Lap(); for (var i = 0; i < options.SmoothPasses; i++) { Smooth(ref bitmap, ref tmpMap, options); yield return(null); } var smoothTime = timer.Lap(); var map = ToMapData(bitmap); var toMapDataTime = timer.Lap(); Debug.LogFormat( "GeneratesWavyIslandMaps GenerateMap done fillTime {0} thresholdTime {1} islandsTime {2} dilateTime {3} smoothTime {4} toMapDataTime {5}", fillTime, thresholdTime, islandsTime, dilateTime, smoothTime, toMapDataTime); yield return(map); }
static Bitmap ThresholdMap(byte[] noiseData, WavyIslandMapGenerationOptions options) { var threshold = (byte)(options.PerlinThreshold * 255); var bitArray = new byte[options.Width * options.Height]; var thresholdMap = new Bitmap(options.Width, options.Height, bitArray); for (int i = 0; i < options.Width * options.Height; i++) { thresholdMap.Set(i, noiseData[i] > threshold); } return(thresholdMap); }
private static void Dilate(ref Bitmap map, ref Bitmap tmpMap, WavyIslandMapGenerationOptions options) { map.CellArray.CopyTo(tmpMap.CellArray, 0); for (var y = 0; y < options.Height; y++) { for (var x = 0; x < options.Width; x++) { var cell = map.CellArray[y * map.Width + x]; if ((cell & Bitmap.LiveBit) == 0 && (cell & Bitmap.NeighbourCountBits) > 1) { tmpMap.Set(x, y, true); } } } Swap(ref map, ref tmpMap); }