Exemple #1
0
 public ThreadParams(float ratio, float start, int step, PerlinNoiseSettings settings)
 {
     this.Ratio    = ratio;
     this.Start    = start;
     this.Step     = step;
     this.Settings = settings;
 }
Exemple #2
0
        /// <summary>
        /// Parse <code>PerlinNoiseSettings</code> from a string representation.
        /// </summary>
        /// <param name="s">The string to parse the <code>PerlinNoiseSettings</code> from.</param>
        /// <returns>A <code>PerlinNoiseSettings</code> that the input string represents.</returns>
        public static PerlinNoiseSettings Parse(string s)
        {
            try
            {
                PerlinNoiseSettings settings = new PerlinNoiseSettings();
                string[]            parts    = s.Split(',');

                settings.Intensity     = float.Parse(parts[0], CultureInfo.CreateSpecificCulture("en-GB"));
                settings.Levels        = int.Parse(parts[1]);
                settings.Offset        = int.Parse(parts[2]);
                settings.RangeHandling = (RangeHandling)Enum.Parse(typeof(RangeHandling), parts[3]);
                settings.Highlight     = Color.FromArgb(byte.Parse(parts[4]), byte.Parse(parts[5]), byte.Parse(parts[6]));
                settings.Shadow        = Color.FromArgb(byte.Parse(parts[7]), byte.Parse(parts[8]), byte.Parse(parts[9]));
                settings.Wrap          = bool.Parse(parts[10]);
                settings.ChannelWrap   = bool.Parse(parts[11]);

                return(settings);
            }
            catch (Exception e) { throw new FormatException("The input string is not in the correct format.", e); }
        }
Exemple #3
0
        private byte[] Generate(PerlinNoiseSettings settings)
        {
            Thread[] threads = new Thread[this.threadCount];
            Random   random;

            if (seed != 0)
            {
                random = new Random(seed);
            }
            else
            {
                random = new Random();
            }

            for (int n = 0; n < settings.Levels; n++)
            {
                this.arraySize = (int)Math.Pow(2, settings.Offset + 1);
                float ratio = (float)this.resolution / this.arraySize;

                this.vectors    = new Vector2[arraySize];
                this.hashLookup = new int[arraySize];

                for (int i = 0; i < this.arraySize; i++)
                {
                    this.vectors[i].X = 2 * (float)random.NextDouble() - 1;
                    this.vectors[i].Y = 2 * (float)random.NextDouble() - 1;

                    if (this.vectors[i].Length() < 1)
                    {
                        this.vectors[i].Normalize();
                    }
                    else if (i > 0)
                    {
                        i--;
                    }
                    else
                    {
                        i = 0;
                    }

                    hashLookup[i] = i;
                }

                Scramble(hashLookup, random);

                this.vectorIndices = new int[this.arraySize + 1, this.arraySize + 1];
                for (int x = 0; x < this.arraySize + 1; x++)
                {
                    for (int y = 0; y < this.arraySize + 1; y++)
                    {
                        this.vectorIndices[x, y] = this.hashLookup[(x + this.hashLookup[y % this.arraySize]) % this.arraySize];
                    }
                }

                for (int i = 0; i < this.threadCount; i++)
                {
                    threads[i] = new Thread(new ParameterizedThreadStart(this.FillTexels));
                }
                for (int i = 0; i < this.threadCount; i++)
                {
                    threads[i].Start(new ThreadParams(ratio, i / ratio, this.threadCount, settings));
                }
                for (int i = 0; i < this.threadCount; i++)
                {
                    threads[i].Join();
                }

                settings.Offset++;
            }
            return(texels);
        }