public Form1() { InitializeComponent(); Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height); Gradient ground_gradient = new Gradient(0, 0, 0, 1); #region lowlands Fractal lowland_shape_fractal = new Fractal(FractalType.BILLOW, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 2, 0.25, null); AutoCorrect lowland_autocorrect = new AutoCorrect(lowland_shape_fractal, 0, 1); ScaleOffset lowland_scale = new ScaleOffset(0.125, -0.45, lowland_autocorrect); ScaleDomain lowland_y_scale = new ScaleDomain(lowland_scale, null, 0); TranslatedDomain lowland_terrain = new TranslatedDomain(ground_gradient, null, lowland_y_scale); #endregion #region highlands Fractal highland_shape_fractal = new Fractal(FractalType.FBM, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 4, 2, null); AutoCorrect highland_autocorrect = new AutoCorrect(highland_shape_fractal, -1, 1); ScaleOffset highland_scale = new ScaleOffset(0.25, 0, highland_autocorrect); ScaleDomain highland_y_scale = new ScaleDomain(highland_scale, null, 0); TranslatedDomain highland_terrain = new TranslatedDomain(ground_gradient, null, highland_y_scale); #endregion #region mountains Fractal mountain_shape_fractal = new Fractal(FractalType.RIDGEDMULTI, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 8, 1, null); AutoCorrect mountain_autocorrect = new AutoCorrect(mountain_shape_fractal, -1, 1); ScaleOffset mountain_scale = new ScaleOffset(0.3, 0.15, mountain_autocorrect); ScaleDomain mountain_y_scale = new ScaleDomain(mountain_scale, null, 0.15); TranslatedDomain mountain_terrain = new TranslatedDomain(ground_gradient, null, mountain_y_scale); #endregion #region terrain Fractal terrain_type_fractal = new Fractal(FractalType.FBM, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 3, 0.125, null); AutoCorrect terrain_autocorrect = new AutoCorrect(terrain_type_fractal, 0, 1); ScaleDomain terrain_type_y_scale = new ScaleDomain(terrain_autocorrect, null, 0); Cache terrain_type_cache = new Cache(terrain_type_y_scale); Select highland_mountain_select = new Select(terrain_type_cache, highland_terrain, mountain_terrain, 0.55, 0.2); Select highland_lowland_select = new Select(terrain_type_cache, lowland_terrain, highland_mountain_select, 0.25, 0.15); Cache highland_lowland_select_cache = new Cache(highland_lowland_select); Select ground_select = new Select(highland_lowland_select_cache, 0, 1, 0.5, null); #endregion #region caves Fractal cave_shape = new Fractal(FractalType.RIDGEDMULTI, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 1, 4, null); Bias cave_attenuate_bias = new Bias(highland_lowland_select_cache, 0.65); Combiner cave_shape_attenuate = new Combiner(CombinerTypes.MULT, cave_shape, cave_attenuate_bias); Fractal cave_perturb_fractal = new Fractal(FractalType.FBM, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 6, 3, null); ScaleOffset cave_perturb_scale = new ScaleOffset(0.5, 0, cave_perturb_fractal); TranslatedDomain cave_perturb = new TranslatedDomain(cave_shape_attenuate, cave_perturb_scale, null); Select cave_select = new Select(cave_perturb, 1, 0, 0.75, 0); #endregion Combiner gound_cave_multiply = new Combiner(CombinerTypes.MULT, cave_select, ground_select); //EXAMPLE 1 //Gradient ground_gradient = new Gradient(0, 0, 0, 1); //Fractal ground_shape_fractal = new Fractal(FractalType.FBM, // BasisTypes.GRADIENT, // InterpTypes.QUINTIC, // 6, 2, null); //ScaleOffset ground_scale = new Accidental.ScaleOffset(0.5, 0, ground_shape_fractal); //ScaleDomain ground_scale_y = new ScaleDomain(null, 0, ground_scale); //TranslatedDomain ground_perturb = new TranslatedDomain(ground_gradient, null, ground_scale_y); //Fractal ground_overhang_fractal = new Fractal(FractalType.FBM, // BasisTypes.GRADIENT, // InterpTypes.QUINTIC, // 6, 2, 23434); //ScaleOffset ground_overhang_scale = new ScaleOffset(0.2, 0, ground_overhang_fractal); //TranslatedDomain ground_overhang_perturb = new TranslatedDomain(ground_perturb, ground_overhang_scale, null); //Select ground_select = new Select(ground_overhang_perturb, 0, 1, 0.5, null); //EXAMPLE 2 //Fractal cave_shape = new Accidental.Fractal(FractalType.RIDGEDMULTI, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 1, 2, 4533); //Select cave_select = new Accidental.Select(cave_shape, 1, 0, 0.6, 0); //Fractal cave_perturb_fractal = new Fractal(FractalType.FBM, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 6, 3, null); //ScaleOffset cave_perturb_scale = new ScaleOffset(0.25, 0, cave_perturb_fractal); //TranslatedDomain cave_perturb = new TranslatedDomain(cave_select, cave_perturb_scale, null); SMappingRanges ranges = new SMappingRanges(); //finally update our image for (int x = 0; x < bitmap.Width; x++) { for(int y = 0; y < bitmap.Height; y++) { double p = (double)x / (double)bitmap.Width; double q = (double)y / (double)bitmap.Height; double nx, ny = 0.0; nx = ranges.mapx0 + p * (ranges.mapx1 - ranges.mapx0); ny = ranges.mapy0 + q * (ranges.mapy1 - ranges.mapy0); double val = gound_cave_multiply.Get(nx * 6, ny * 3); bitmap.SetPixel(x, y, Color.Black.Lerp(Color.White, val)); } } pictureBox1.Image = bitmap; }
private unsafe void BtnGenerateLabyrinthClick(object sender, EventArgs e) { Cursor = Cursors.WaitCursor; FractalType type = (FractalType) cbLabyrinthType.SelectedItem; BasisTypes basis = (BasisTypes) cbLabyrinthBasis.SelectedItem; InterpTypes interp = (InterpTypes) cbLabyrinthInterp.SelectedItem; int? octaves = !string.IsNullOrEmpty(tbLabyrinthOctaves.Text) ? int.Parse(tbLabyrinthOctaves.Text) : (int?) null; double? frequency = !string.IsNullOrEmpty(tbLabyrinthFrequency.Text) ? double.Parse(tbLabyrinthFrequency.Text) : (double?) null; double? angle = !string.IsNullOrEmpty(tbLabyrinthAngle.Text) ? double.Parse(tbLabyrinthAngle.Text) : (double?) null; double? lacunarity = !string.IsNullOrEmpty(tbLabyrinthLacunarity.Text) ? double.Parse(tbLabyrinthLacunarity.Text) : (double?) null; _labOldSeed = cbLabyrinthRnd.Checked ? (uint?) Environment.TickCount : _labOldSeed; ModuleBase moduleBase = new Fractal(type, basis, interp, octaves, frequency, _labOldSeed, angle, lacunarity); if (chkLabyrinthAC.Checked) { double acLow = double.Parse(tbLabyrinthACLow.Text); double acHigh = double.Parse(tbLabyrinthACHigh.Text); CombinerTypes combType = (CombinerTypes) cbLabyrinthACCombine.SelectedItem; AutoCorrect correct = new AutoCorrect(moduleBase, acLow, acHigh); moduleBase = new Combiner(combType, correct, moduleBase); } // Bias bias = new Bias(moduleBase, 0.01); // Gradient gradient = new Gradient(0, 0, 50, 100); // moduleBase = new TranslatedDomain(moduleBase, gradient, bias); if (chkLabyrinthSel.Checked) { double selLow = double.Parse(tbLabyrinthSelLow.Text); double selHigh = double.Parse(tbLabyrinthSelHigh.Text); double selThreshold = double.Parse(tbLabyrinthSelThreshold.Text); double? selFalloff = !string.IsNullOrEmpty(tbLabyrinthSelFalloff.Text) ? double.Parse(tbLabyrinthSelFalloff.Text) : (double?) null; moduleBase = new Select(moduleBase, selLow, selHigh, selThreshold, selFalloff); } if (pbLabyrinth.Image != null) { pbLabyrinth.Image.Dispose(); } ushort width = 500, height = 500; Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb); BitmapData data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bitmap.PixelFormat); byte* pRoughMap = (byte*)Memory.HeapAlloc(width * height); Parallel.For(0, height, y => { int* row = (int*) data.Scan0 + (y*data.Stride)/4; Parallel.For(0, width, x => { double p = (double) x/width; double q = (double) y/height; double val = moduleBase.Get(p, q); pRoughMap[y * width + x] = (byte)Math.Abs(val - 1); Color color = Color.Black.Lerp(Color.White, val); row[x] = color.ToArgb(); }); }); using (ServerMap map = new ServerMap(width, height, 0, pRoughMap)) { map.SaveToFile("RK.save"); } Memory.HeapFree(pRoughMap); bitmap.UnlockBits(data); pbLabyrinth.Image = bitmap; Cursor = DefaultCursor; }