Skip to content

Instantly share code, notes, and snippets.

@kkestell
Created December 10, 2015 22:13
Show Gist options
  • Save kkestell/28e3dc925b0dae8c083e to your computer and use it in GitHub Desktop.
Save kkestell/28e3dc925b0dae8c083e to your computer and use it in GitHub Desktop.
using System;
using LibNoise;
using LibNoise.Builder;
using LibNoise.Filter;
using LibNoise.Modifier;
using LibNoise.Primitive;
using OpenTK;
namespace Erebus
{
internal class HeightmapGenerator
{
private readonly int size;
private readonly int seed;
private readonly int octaves;
private readonly float frequency;
private readonly float lacunarity;
private readonly NoiseQuality quality;
private readonly ScaleBias scale;
private readonly PrimitiveModule primitive;
private readonly FilterModule filter;
private NoiseMapBuilder projection;
public HeightmapGenerator(int size)
{
this.size = size;
seed = 0;
octaves = 2;
frequency = 0.01f;
lacunarity = 2.0f;
quality = NoiseQuality.Standard;
primitive = new ImprovedPerlin();
primitive.Quality = quality;
primitive.Seed = seed;
filter = new RidgedMultiFractal();
filter.Frequency = frequency;
filter.Lacunarity = lacunarity;
filter.OctaveCount = octaves;
filter.Primitive3D = (IModule3D)primitive;
scale = new ScaleBias(filter, 1f, 0);
}
public HeightmapTile GenerateTile(int offsetX, int offsetY)
{
var noiseMap = new NoiseMap();
projection = new NoiseMapBuilderPlane(offsetX * size, (offsetX * size) + size, offsetY * size, (offsetY * size) + size, true);
projection.SetSize(size, size);
projection.SourceModule = filter;
projection.NoiseMap = noiseMap;
projection.Build();
var tileData = new double[size * size];
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
tileData[y * size + x] = projection.NoiseMap.GetValue(x, y);
}
}
return new HeightmapTile(tileData, new Vector2(offsetX, offsetY));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment