Пример #1
0
            void GenPointsMasked(Image <Rgba32> buffer)
            {
                var o      = PointSize - 1;
                var random = new Random(Seed);
                var noise  = new RustNoise(MaskNoiseType);

                noise.SetSeed(MaskSeed);
                noise.SetFrequency(MaskFrequency);
                noise.SetPersistence(MaskPersistence);
                noise.SetLacunarity(MaskLacunarity);
                noise.SetOctaves(MaskOctaves);
                noise.SetPeriodX(buffer.Width);
                noise.SetPeriodY(buffer.Height);

                var threshVal = 1 / (1 - MaskThreshold);
                var powFactor = 1 / MaskPower;

                const int MaxPointAttemptCount = 9999;
                int       PointAttemptCount    = 0;

                for (int i = 0; i < PointCount; i++)
                {
                    var relX = random.NextDouble();
                    var relY = random.NextDouble();

                    var x = (int)(relX * buffer.Width);
                    var y = (int)(relY * buffer.Height);

                    // Grab noise at this point.
                    var noiseVal = Math.Min(1, Math.Max(0, (noise.GetNoise(x, y) + 1) / 2));
                    // Threshold
                    noiseVal  = Math.Max(0, noiseVal - MaskThreshold);
                    noiseVal *= threshVal;
                    noiseVal  = Math.Pow(noiseVal, powFactor);

                    var randomThresh = random.NextDouble();
                    if (randomThresh > noiseVal)
                    {
                        if (++PointAttemptCount <= MaxPointAttemptCount)
                        {
                            i--;
                        }
                        continue;
                    }

                    var dist = random.NextDouble();

                    for (int ox = x - o; ox <= x + o; ox++)
                    {
                        for (int oy = y - o; oy <= y + o; oy++)
                        {
                            var color = Util.ColorMix(CloseColor, FarColor, (float)dist).Convert();
                            buffer[Util.SaneMod(ox, buffer.Width), Util.SaneMod(oy, buffer.Height)] = color;
                        }
                    }
                }
            }
Пример #2
0
            public override void Apply(Image <Rgba32> bitmap)
            {
                var noise = new RustNoise(NoiseType);

                noise.SetSeed(Seed);
                noise.SetFrequency(Frequency);
                noise.SetPersistence(Persistence);
                noise.SetLacunarity(Lacunarity);
                noise.SetOctaves(Octaves);
                noise.SetPeriodX(bitmap.Width);
                noise.SetPeriodY(bitmap.Height);
                var threshVal = 1 / (1 - Threshold);
                var powFactor = 1 / Power;

                for (var x = 0; x < bitmap.Width; x++)
                {
                    for (var y = 0; y < bitmap.Height; y++)
                    {
                        // Do noise calculations.
                        var noiseVal = Math.Min(1, Math.Max(0, (noise.GetNoise(x, y) + 1) / 2));

                        // Threshold
                        noiseVal  = Math.Max(0, noiseVal - Threshold);
                        noiseVal *= threshVal;
                        noiseVal  = Math.Pow(noiseVal, powFactor);

                        // Get colors based on noise values.
                        var srcColor = Util.ColorMix(OuterColor, InnerColor, (float)noiseVal);
                        srcColor.A = (float)noiseVal;

                        // Apply blending factors & write back.
                        var dstColor = bitmap[x, y].Convert();
                        bitmap[x, y] = Util.Blend(dstColor, srcColor, DstFactor, SrcFactor).Convert();
                    }
                }
            }