public static float[,] GenerateNoiseMapMatrix(HeightSettings heightSettings) { (int dimensionLength, int seed, float scale, int octaves, float persistence, float lacunarity, Vector2 offset) = heightSettings.ToTuple(); float[,] noiseMap = new float[dimensionLength, dimensionLength]; System.Random prng = new System.Random(seed); Vector2[] octaveOffsets = new Vector2[octaves]; float maxPossibleHeight = 0; float amplitude = 1; float frequency = 1; for (int i = 0; i < octaves; i++) { float offsetX = prng.Next(-100000, 100000) + offset.x; float offsetY = prng.Next(-100000, 100000) - offset.y; octaveOffsets[i] = new Vector2(offsetX, offsetY); maxPossibleHeight += amplitude; amplitude *= persistence; } float maxLocalNoiseHeight = float.MinValue; float minLocalNoiseHeight = float.MaxValue; float halfWidth = dimensionLength / 2f; float halfHeight = dimensionLength / 2f; for (int y = 0; y < dimensionLength; y++) { for (int x = 0; x < dimensionLength; x++) { amplitude = 1; frequency = 1; float noiseHeight = 0; for (int i = 0; i < octaves; i++) { float sampleX = (x - halfWidth + octaveOffsets[i].x) / scale * frequency; float sampleY = (y - halfHeight + octaveOffsets[i].y) / scale * frequency; float perlinValue = Mathf.PerlinNoise(sampleX, sampleY) * 2 - 1; noiseHeight += perlinValue * amplitude; amplitude *= persistence; frequency *= lacunarity; } if (noiseHeight > maxLocalNoiseHeight) { maxLocalNoiseHeight = noiseHeight; } if (noiseHeight < minLocalNoiseHeight) { minLocalNoiseHeight = noiseHeight; } noiseMap[x, y] = noiseHeight; /*if (settings.normalizeMode == NormalizeMode.Global) { * float normalizedHeight = (noiseMap [x, y] + 1) / (maxPossibleHeight / 0.9f); * noiseMap [x, y] = Mathf.Clamp (normalizedHeight, 0, int.MaxValue); * }*/ } } //if (settings.normalizeMode == NormalizeMode.Local) { for (int y = 0; y < dimensionLength; y++) { for (int x = 0; x < dimensionLength; x++) { noiseMap[x, y] = Mathf.InverseLerp(minLocalNoiseHeight, maxLocalNoiseHeight, noiseMap[x, y]); } } //} return(noiseMap); }
public void LoadWorldResources() { m_HeightSettings = Resources.Load <HeightSettings>($"Worlds/{world.ToString()}/{world.ToString()}Height"); m_TerrainSettings = Resources.Load <TerrainSettings>($"Worlds/{world.ToString()}/{world.ToString()}Terrain"); m_FoliageSettings = Resources.Load <FoliageSettings>($"Worlds/{world.ToString()}/{world.ToString()}Foliage"); }