/// <summary> /// Applies a random height distribution to the terrain grid. /// </summary> public void ApplyHighHeightMap(float h, float scale, float offset, float limit, Vector2 seed) { for (int j = 0; j <= Size; j++) { int x = j % 2; GridVertex vert = new GridVertex(x, j); if (HeightMap[vert.U, vert.V] > limit) { Vector2 pos = vert.ToCartesianCoords2D(); HeightMap[x, j] += (SimplexNoise.Generate(seed.X + pos.X * scale, seed.Y + pos.Y * scale) - offset) * h; } } for (int i = 1; i <= Size; i++) { for (int j = 0; j <= Size; j++) { int x = i * 2 + (j % 2); GridVertex vert = new GridVertex(x, j); if (HeightMap[vert.U, vert.V] > limit) { Vector2 pos = vert.ToCartesianCoords2D(); HeightMap[x, j] += (SimplexNoise.Generate(seed.X + pos.X * scale, seed.Y + pos.Y * scale) - offset) * h; HeightMap[x - 1, j] = (HeightMap[x, j] + HeightMap[x - 2, j]) / 2; } } } }
/// <summary> /// Generates a heightmap using SimplexNoise. /// </summary> /// <param name="terWidth">Width of the terrains heightmap</param> /// <param name="terHeight">Height of the terrains heightmap</param> /// <returns>A heightmap as two dimensional float array</returns> private float[,] GenerateSimplex(int terWidth, int terHeight) { // Variable for return value float[,] heightMap = new float[terWidth, terHeight]; int frequency = baseFrequency; float amplitude = baseAmplitude; for (int i = 0; i < octaves; i++) { // Ratio between terrain map and noise map float xRatio = (float)frequency / (float)terWidth; float yRatio = (float)frequency / (float)terHeight; for (int x = 0; x < terWidth; x++) { for (int y = 0; y < terHeight; y++) { // Mapping height map to noise map float xIndex = (x * xRatio); float yIndex = (y * yRatio); float value = SimplexNoise.Generate(xIndex, yIndex); heightMap[x, y] += (value * amplitude); } } frequency *= 2; amplitude /= 2; } heightMap = Normalize(heightMap); return(heightMap); }
public bool CanBeSolid(int seed3, int seed4, int seed5, int x, int y, int z, SimpleBiome biome) { // TODO: better non-simplex code?! double val = SimplexNoise.Generate((double)seed3 + (x / SolidityMapSize), (double)seed4 + (y / SolidityMapSize), (double)seed5 + (z / SolidityMapSize)); return(val < biome.AirDensity()); }
private float GetNoise(float t) { var result = (SimplexNoise.Generate(t) + 1) / 2; Assert.True(result >= 0 && result <= 1); return(result); }
public double GetHeightQuick(int Seed, int seed2, double x, double y) { double lheight = SimplexNoise.Generate((double)seed2 + (x / GlobalHeightMapSize), (double)Seed + (y / GlobalHeightMapSize)) * 50f - 10f; double height = SimplexNoise.Generate((double)Seed + (x / LocalHeightMapSize), (double)seed2 + (y / LocalHeightMapSize)) * 6f - 3f; return(lheight + height); }
/// <summary> /// Adapt a color effect for rendering. /// TODO: Does this logic belong in FreneticGameEngine? /// </summary> /// <param name="vt">The coordinate.</param> /// <param name="tcol">The base 't-color' value.</param> /// <returns>The resultant color.</returns> public Vector4 AdaptColor(Vector3d vt, Color4F tcol) { if (tcol.IA == 0) { if (tcol.IR == 127 && tcol.IG == 0 && tcol.IB == 127) { float r = (float)SimplexNoise.Generate(vt.X / 10f, vt.Y / 10f, vt.Z / 10f); float g = (float)SimplexNoise.Generate((vt.X + 50f) / 10f, (vt.Y + 127f) / 10f, (vt.Z + 10f) / 10f); float b = (float)SimplexNoise.Generate((vt.X - 150f) / 10f, (vt.Y - 65f) / 10f, (vt.Z + 73f) / 10f); return(new Vector4(r, g, b, 1f)); } else if (tcol.IR == 127 && tcol.IG == 0 && tcol.IB == 0) { Random random = new Random((int)(vt.X + vt.Y + vt.Z)); return(new Vector4((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble(), 1f)); } else { return(new Vector4(tcol.R, tcol.G, tcol.G, tcol.A)); } } else { return(new Vector4(tcol.R, tcol.G, tcol.G, tcol.A)); } }
public bool CanBeSolid(int seed3, int seed4, int seed5, int x, int y, int z, SimpleBiome biome) { // TODO: better non-simplex code?! double val = SimplexNoise.Generate((double)seed3 + (x / SolidityMapSize), (double)seed4 + (y / SolidityMapSize), (double)seed5 + (z / SolidityMapSize)); //SysConsole.Output(OutputType.INFO, seed3 + "," + seed4 + "," + seed5 + " -> " + x + ", " + y + ", " + z + " -> " + val); return(val < biome.AirDensity()); }
public override double GetTemperature(int seed2, int seed3, double x, double y) { double tempA = SimplexNoise.Generate(seed2 + (x / TemperatureMapSize), seed3 + (y / TemperatureMapSize)); double tempB = SimplexNoise.Generate(seed3 + (x / TemperatureMapSize), seed2 + (y / TemperatureMapSize)); double temp2A = SimplexNoise.Generate(seed2 + seed3 + (x / TemperatureMapTwoSize), seed3 - seed2 + (y / TemperatureMapTwoSize)); double temp2 = (temp2A * temp2A) * 2.0 - 1.0; return(((tempA - 0.5) * (tempB - 0.5) * 2.0 + 0.5) * 80.0 + 10.0 + temp2 * 40.0); }
public double GetHeightQuick(int Seed, int seed2, int seed3, int seed4, int seed5, double x, double y) { double mheight = SimplexNoise.Generate((double)seed4 + (x / MountainHeightMapSize), (double)seed3 + (y / MountainHeightMapSize)) * 2f - 1f; if (mheight > 0.9) { mheight = (mheight - 0.9) * 7000f; } else if (mheight < -0.9) { mheight = (mheight + 0.9) * 4000f; } double lheight = SimplexNoise.Generate((double)seed2 + (x / GlobalHeightMapSize), (double)Seed + (y / GlobalHeightMapSize)) * 50f - 10f; double height = SimplexNoise.Generate((double)Seed + (x / LocalHeightMapSize), (double)seed2 + (y / LocalHeightMapSize)) * 6f - 3f; return(mheight + lheight + height); }
/// <summary> /// Applies a random material distribution to the terrain grid. /// </summary> public void ApplyMaterialLayer(GridMaterial mat, float scale, Vector2 seed, float chance) { int s2 = Size * 2; for (int i = 1; i < s2; i++) { for (int j = 0; j < Size; j++) { GridFace face = new GridFace(i, j); Vector2 pos = face.ToCartesianCoords2D(); // pos += face.PointsUp() ? new Vector2(0f, 0.333f * 0.866f) : pos += new Vector2(0f, 0.667f * 0.866f); if (SimplexNoise.Generate(seed.X + pos.X * scale, seed.Y + pos.Y * scale) < chance) { Materials[i, j] = mat; } } } }
public Material GetMatType(int seed2, int seed3, int seed4, int seed5, int x, int y, int z) { // TODO: better non-simplex code! double val = SimplexNoise.Generate((double)seed2 + (x / OreMapSize), (double)seed5 + (y / OreMapSize), (double)seed4 + (z / OreMapSize)); if (val < OreMapTolerance) { return(Material.AIR); } bool thick = val > OreMapThickTolerance; double tval = SimplexNoise.Generate((double)seed5 + (x / OreTypeMapSize), (double)seed3 + (y / OreTypeMapSize), (double)seed2 + (z / OreTypeMapSize)); if (thick) { if (tval > 0.66f) { return(Material.TIN_ORE); } else if (tval > 0.33f) { return(Material.COAL_ORE); } else { return(Material.COPPER_ORE); } } else { if (tval > 0.66f) { return(Material.TIN_ORE_SPARSE); } else if (tval > 0.33f) { return(Material.COAL_ORE_SPARSE); } else { return(Material.COPPER_ORE_SPARSE); } } }
/// <summary> /// Applies a random height distribution to the terrain grid. /// </summary> public void ApplyClampedHeightMap(float h, float scale, float offset, float clamp, Vector2 seed) { for (int j = 0; j <= Size; j++) { int x = j % 2; Vector2 pos = new GridVertex(x, j).ToCartesianCoords2D(); HeightMap[x, j] += (Math.Min(SimplexNoise.Generate(seed.X + pos.X * scale, seed.Y + pos.Y * scale), clamp) - offset) * h; } for (int i = 1; i <= Size; i++) { for (int j = 0; j <= Size; j++) { int x = i * 2 + (j % 2); Vector2 pos = new GridVertex(x, j).ToCartesianCoords2D(); HeightMap[x, j] += (Math.Min(SimplexNoise.Generate(seed.X + pos.X * scale, seed.Y + pos.Y * scale), clamp) - offset) * h; HeightMap[x - 1, j] = (HeightMap[x, j] + HeightMap[x - 2, j]) / 2; } } }
public double GetHeightQuick(int Seed, int seed2, int seed3, int seed4, int seed5, double x, double y, List <MountainData> mountains, bool precise) { double oceanheight = SimplexNoise.Generate(seed4 + (x / OceanHeightMapSize), seed3 + (y / OceanHeightMapSize)) * 2f - 1f; if (oceanheight < -0.9) { oceanheight = (oceanheight + 0.9) * 4000f; } // Note: Can use positive values of oceanheight for hills or weirdly shaped mountions optionally... double mheight = 0; for (int i = 0; i < mountains.Count; i++) { mheight = Math.Max(mheight, GetMountainHeightAt(mountains[i], x, y, precise)); } double hheight = SimplexNoise.Generate(seed4 + (x / HillHeightMapSize), seed3 + (y / HillHeightMapSize)) * 2f - 1f; if (hheight > 0.9) { hheight = (hheight - 0.9) * 700f; } else if (hheight < -0.9) { hheight = (hheight + 0.9) * 400f; } double dampener = SimplexNoise.Generate(Seed + (x / FlatHeightMapSIze), seed5 + (y / FlatHeightMapSIze)); if (dampener < 0.75 && dampener > -0.75) { dampener = 1; } else { dampener = 1.0 - (4.0 * (Math.Abs(dampener) - 0.75)); } double lheight = SimplexNoise.Generate(seed2 + (x / GlobalHeightMapSize), Seed + (y / GlobalHeightMapSize)) * 40f - 7f; double height = SimplexNoise.Generate(Seed + (x / LocalHeightMapSize), seed2 + (y / LocalHeightMapSize)) * 5f - 2.5f; lheight = (lheight - 10) * dampener + 10; height *= dampener; return(oceanheight + mheight + hheight + lheight + height); }
/// <summary> /// Applies a material distribution to the terrain grid, based on height. /// </summary> public void ApplyLowMaterialLayer(GridMaterial mat, float scale, Vector2 seed, float clamp, float mul) { int s2 = Size * 2; for (int i = 1; i < s2; i++) { for (int j = 0; j < Size; j++) { GridFace face = new GridFace(i, j); double h = 0; foreach (GridVertex vert in face.Corners()) { h += HeightMap[vert.U, vert.V]; } h *= 0.333f; Vector2 pos = face.ToCartesianCoords2D(); // pos += face.PointsUp() ? new Vector2(0f, 0.333f * 0.866f) : pos += new Vector2(0f, 0.667f * 0.866f); if ((SimplexNoise.Generate(seed.X + pos.X * scale, seed.Y + pos.Y * scale) - 0.5) * mul + h < clamp) { Materials[i, j] = mat; } } } }
public override Chunk Generate(IProgress <string> progress, int chunkX, int chunkY) { Reseed(chunkX, chunkY); var chunk = new Chunk(Width, Height); Fill(chunk, ChunkLayer.Background, _dirtId); Fill(chunk, ChunkLayer.Floor, _grassId); for (var x = 0; x < Width; x++) { for (var y = 0; y < Height; y++) { var value = SimplexNoise.Generate((chunkX * Width + x) / 256.0f, (chunkY * Height + y) / 256.0f); if (value < -0.5) { chunk[ChunkLayer.Floor, x, y] = _waterId; chunk[ChunkLayer.Background, x, y] = _waterId; } else if (value < -0.25) { chunk[ChunkLayer.Floor, x, y] = _sandId; chunk[ChunkLayer.Background, x, y] = _sandId; } else if (value > 0.5) { chunk[ChunkLayer.Blocking, x, y] = _stoneId; chunk[ChunkLayer.Background, x, y] = _dirtId; } } } chunk = GenerateBushes(progress, chunk); return(chunk); }
public override double GetDownfallRate(int seed3, int seed4, double x, double y) { return(SimplexNoise.Generate((double)seed3 + (x / DownfallMapSize), (double)seed4 + (y / DownfallMapSize))); }
public int GetSimplexNoise(int x, int y, int z, float scale, int max) { return(Mathf.FloorToInt((SimplexNoise.Generate(x * scale, y * scale, z * scale) + 1f) * (max / 2f))); }
public override double GetTemperature(int seed2, int seed3, double x, double y) { return(SimplexNoise.Generate((double)seed2 + (x / TemperatureMapSize), (double)seed3 + (y / TemperatureMapSize)) * 100f); }