/// <summary> /// This function generates a heightmap and returns it as a System.Drawing.Image /// </summary> /// <param name="imageSize">The size of the image. Square sizes work best</param> /// <param name="borderSize"></param> /// <param name="octaveCount">The higher the octave, the 'busier' it is. Values 1-6 work well.</param> /// <param name="frequency">Increasing frequencies adds detail but also makes the features smaller. 0.5 to 5 work well.</param> /// <param name="persistence">Increasing persistence adds roughness. 0.25 to 0.75 work well.</param> /// <returns></returns> public static Image GeneratePerlinImage(int imageSize, double borderSize = 2, int octaveCount = 4, double frequency = 2, double persistence = 0.4) { //Console.WriteLine("Press Enter to Start Perlin Generator"); //Console.ReadLine(); Console.WriteLine("Generating Perlin Heightmap..."); //seamless so bumpmap looks good heightMapBuilder = new PlanarNoiseMapBuilder((uint)imageSize, (uint)imageSize, borderSize, perlin, xLower, xLower+dist, yLower, yLower+dist, true); heightMap = heightMapBuilder.Build(); GradientColour colors = new GradientColour(); colors.AddGradientPoint(-1.0F, Color.Black); colors.AddGradientPoint(0, Color.Gray); colors.AddGradientPoint(1.0F, Color.White); perlin.OctaveCount = (uint)octaveCount; perlin.Frequency = frequency; perlin.Persistence = persistence; imgBuilder = new ImageBuilder(heightMap, colors); finalImage = imgBuilder.Render(); Console.WriteLine("Perlin Heightmap Generated"); return finalImage; }
public override NoiseMap Build() { NoiseMap map = new NoiseMap(); map.SetSize(_destinationWidth, _destinationHeight); Plane plane = new Plane(_source); double xExtent = _xUpperBound - _xLowerBound; double zExtent = _zUpperBound - _zLowerBound; double xDelta = xExtent / (double)_destinationWidth; double zDelta = zExtent / (double)_destinationHeight; double xCur = _xLowerBound; double zCur = _zLowerBound; for (uint z = 0; z < _destinationHeight; z++) { xCur = _xLowerBound; for (uint x = 0; x < _destinationWidth; x++) { double finalValue; if (!_seamless) { finalValue = plane.GetValue(xCur, zCur); } else { double swValue, seValue, nwValue, neValue; swValue = plane.GetValue(xCur, zCur); seValue = plane.GetValue(xCur + xExtent, zCur); nwValue = plane.GetValue(xCur, zCur + zExtent); neValue = plane.GetValue(xCur + xExtent, zCur + zExtent); double xBlend = 1.0 - ((xCur - _xLowerBound) / xExtent); double zBlend = 1.0 - ((zCur - _zLowerBound) / zExtent); double z0 = Interpolation.LinearInterpolate(swValue, seValue, xBlend); double z1 = Interpolation.LinearInterpolate(nwValue, neValue, xBlend); finalValue = Interpolation.LinearInterpolate(z0, z1, zBlend); } map.SetPoint(x, z, finalValue); xCur += xDelta; } zCur += zDelta; } return map; }
public ImageBuilder(NoiseMap map, GradientColour colours) { _map = map; _colour = colours; }
public static Texture GeneratePlanetTexture(Vector2u texSize) { var imgSize = texSize; perlin = new Perlin(random.Next(2, 3), 0.2, NoiseQuality.Best, 4, 0.7, random.Next(0, 1024)); ridgedMulti = new RidgedMulti(random.NextDouble() * 2, 0.3, 2, NoiseQuality.Best, random.Next(0, 1024)); voronoi = new Voronoi(0.1, random.NextDouble() * 2, true, random.Next(0, 1024)); selectModule = new Select(1.0, 1.0, 0.0); selectModule.SetSourceModule(0, perlin); selectModule.SetSourceModule(1, ridgedMulti); selectModule.SetSourceModule(2, voronoi); heightMapBuilder = new PlanarNoiseMapBuilder(imgSize.X, imgSize.Y, 0, selectModule, 1, 5, 1, 5, true); heightMap = heightMapBuilder.Build(); var texColors = new GradientColour(); texColors.AddGradientPoint(-1, GenerateProceduralColor()); texColors.AddGradientPoint(-0.2 + random.NextDouble() * 0.4, GenerateProceduralColor()); texColors.AddGradientPoint(1, GenerateProceduralColor()); var renderer = new ImageBuilder(heightMap, texColors); var renderedImg = renderer.Render(); var img = new Bitmap(renderedImg); var sfmlImg = new SFML.Graphics.Image(imgSize.X, imgSize.Y); for (uint x = 0; x < imgSize.X; x++) { for (uint y = 0; y < imgSize.Y; y++) { var col = img.GetPixel((int)x, (int)y); sfmlImg.SetPixel(x, y, new Color(col.R, col.G, col.B, col.A)); } } var returnTex = new Texture(sfmlImg); return returnTex; }