/// <summary>
        /// Filters the NoiseField by applying the Gradient values.
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public NoiseField <Color> Filter(NoiseField <float> field)
        {
            NoiseField <Color> result = new NoiseField <Color>(field.Width, field.Height);

            for (int x = 0; x < field.Width; x++)
            {
                for (int y = 0; y < field.Height; y++)
                {
                    float fieldValue = field.Field[x, y];

                    foreach (ColorPair pair in Colors)
                    {
                        if (pair.InRange(fieldValue))
                        {
                            float colorMultipler = fieldValue / pair.End;

                            result.Field[x, y] =
                                new Color((int)(pair.Color.R * colorMultipler),
                                          (int)(pair.Color.G * colorMultipler),
                                          (int)(pair.Color.B * colorMultipler));
                        }
                    }
                }
            }

            return(result);
        }
Example #2
0
        public NoiseField <Color> Filter(NoiseField <float> field)
        {
            NoiseField <Color> result = new NoiseField <Color>(field.Width, field.Height);

            for (int x = 0; x < field.Width; x++)
            {
                for (int y = 0; y < field.Height; y++)
                {
                    float fieldValue = field.Field[x, y];

                    foreach (ColorPair pair in Colors)
                    {
                        if (pair.InRange(fieldValue))
                        {
                            result.Field[x, y] =
                                new Color((int)(pair.Color.R),
                                          (int)(pair.Color.G),
                                          (int)(pair.Color.B)
                                          );
                        }
                    }
                }
            }
            values = result;

            return(result);
        }
Example #3
0
        public NoiseField <Color> Filter(NoiseField <float> field)
        {
            NoiseField <Color> result = new NoiseField <Color>(field.Width, field.Height);

            //Colors = Colors.Where((x, i) => i % PixelSize == 0).ToList();

            for (int x = 0; x < field.Width / PixelSize; x++)
            {
                for (int y = 0; y < field.Height / PixelSize; y++)
                {
                    float fieldValue = field.Field[x * PixelSize, y *PixelSize];

                    foreach (var pair in Colors)
                    {
                        for (int a = 0; a < PixelSize; a++)
                        {
                            for (int b = 0; b < PixelSize; b++)
                            {
                                if (pair.InRange(fieldValue))
                                {
                                    result.Field[x * PixelSize + a, y *PixelSize + b] =
                                        new Color((int)(pair.Color.R),
                                                  (int)(pair.Color.G),
                                                  (int)(pair.Color.B)
                                                  );
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Takes a field of color data and renders it to a Texture2D.
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public Texture2D Transform(NoiseField <Color> field)
        {
            Color[]   data    = field.Flatten();
            Texture2D texture = new Texture2D(Graphics, field.Width, field.Height);

            texture.SetData <Color>(data);
            return(texture);
        }
        /// <summary>
        /// Performs a transformation on a float field to a Texture2D.  This will render as
        /// a grayscale image.
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public Texture2D Transform(NoiseField <float> field)
        {
            float[] data      = field.Flatten();
            Color[] colorData = new Color[data.Length];

            for (int i = 0; i < data.Length; i++)
            {
                colorData[i] = new Color(data[i], data[i], data[i]);
            }

            Texture2D texture = new Texture2D(Graphics, field.Width, field.Height);

            texture.SetData <Color>(colorData);
            return(texture);
        }
        /// <summary>
        /// Performs the gradient.
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public NoiseField <Color> Filter(NoiseField <float> field)
        {
            NoiseField <Color> result = new NoiseField <Color>(field.Width, field.Height);

            for (int x = 0; x < field.Width; x++)
            {
                for (int y = 0; y < field.Height; y++)
                {
                    float t = field.Field[x, y];
                    float u = StartPercentage - t;
                    t *= StartPercentage;

                    result.Field[x, y] =
                        new Color((int)(u * StartColor.R + t * EndColor.R),
                                  (int)(u * StartColor.G + t * EndColor.G),
                                  (int)(u * StartColor.B + t * EndColor.B));
                }
            }

            return(result);
        }
Example #7
0
        public void GenerateNoiseTexture()
        {
            PerlinNoiseGenerator gen = new PerlinNoiseGenerator();

            gen.OctaveCount   = 7;
            gen.Persistence   = .55f;
            gen.Interpolation = InterpolationAlgorithms.CosineInterpolation;

            perlinNoise = gen.GeneratePerlinNoise(512, 512);

            CustomGradientColorFilter filter      = new CustomGradientColorFilter();
            Texture2DTransformer      transformer = new Texture2DTransformer(graphics.GraphicsDevice);

            filter.AddColorPoint(0.0f, 0.40f, Color.Blue);
            filter.AddColorPoint(0.4f, 0.50f, Color.Yellow);
            filter.AddColorPoint(0.50f, 0.70f, Color.Green);
            filter.AddColorPoint(0.70f, 0.90f, Color.SaddleBrown);
            filter.AddColorPoint(0.90f, 1.00f, Color.White);

            noiseTexture = transformer.Transform(filter.Filter(perlinNoise));
        }
        /// <summary>
        /// Smooths a noise field.
        /// </summary>
        /// <param name="whiteNoise">Noise field to smooth.</param>
        /// <param name="octave">The current octave.</param>
        /// <returns></returns>
        public NoiseField<float> SmoothNoiseField(NoiseField<float> whiteNoise, int octave)
        {
            NoiseField<float> smooth = new NoiseField<float>(whiteNoise.Width, whiteNoise.Height);

            int samplePeriod = 1 << octave;
            float sampleFrequency = 1.0f / samplePeriod;

            for(int x = 0; x < smooth.Width; x++) {
                int sampleX1 = (x / samplePeriod) * samplePeriod;
                int sampleX2 = (sampleX1 + samplePeriod) % smooth.Width;

                float horizontalBlend = (x - sampleX1) * sampleFrequency;

                for(int y = 0; y < smooth.Height; y++) {
                    int sampleY1 = (y / samplePeriod) * samplePeriod;
                    int sampleY2 = (sampleY1 + samplePeriod) % smooth.Height;

                    float verticalBlend = (y - sampleY1) * sampleFrequency;

                    float top = Interpolation(whiteNoise.Field[sampleX1, sampleY1], whiteNoise.Field[sampleX2, sampleY1], horizontalBlend);
                    float bottom = Interpolation(whiteNoise.Field[sampleX1, sampleY2], whiteNoise.Field[sampleX2, sampleY2], horizontalBlend);

                    smooth.Field[x, y] = Interpolation(top, bottom, verticalBlend);
                }
            }

            return smooth;
        }
        /// <summary>
        /// Generates a new Perlin Noise field.
        /// </summary>
        /// <param name="baseNoise">Base noise to use.  Should be white noise.</param>
        /// <param name="octaveCount">Number of octaves to go over (number of fields to average)</param>
        /// <param name="persistence">Persistence factor</param>
        /// <returns></returns>
        public NoiseField<float> PerlinNoiseField(NoiseField<float> baseNoise)
        {
            NoiseField<float>[] smoothNoise = new NoiseField<float>[OctaveCount];

            for(int i = 0; i < OctaveCount; i++) {
                smoothNoise[i] = SmoothNoiseField(baseNoise, i);
            }

            NoiseField<float> perlinNoise = new NoiseField<float>(baseNoise.Width, baseNoise.Height);
            float amplitude = 1.0f;
            float totalAmplitude = 0.0f;

            for(int octave = OctaveCount - 1; octave >= 0; octave--) {
                amplitude *= Persistence;
                totalAmplitude += amplitude;

                for(int x = 0; x < baseNoise.Width; x++) {
                    for(int y = 0; y < baseNoise.Height; y++) {
                        perlinNoise.Field[x, y] += smoothNoise[octave].Field[x, y] * amplitude;
                    }
                }
            }

            // Normalize the fields
            for(int x = 0; x < baseNoise.Width; x++) {
                for(int y = 0; y < baseNoise.Height; y++) {
                    perlinNoise.Field[x, y] /= totalAmplitude;
                }
            }

            return perlinNoise;
        }
        /// <summary>
        /// Generates wa field of white noise (random noise)
        /// </summary>
        /// <param name="width">Width of the field</param>
        /// <param name="height">Height of the field</param>
        /// <param name="random">Random number generator to use.  Can be pre-seeded.</param>
        /// <returns></returns>
        public NoiseField<float> GenerateWhiteNoise(int width, int height, Random random)
        {
            NoiseField<float> field = new NoiseField<float>(width, height);

            for(int x = 0; x < width; x++) {
                for(int y = 0; y < height; y++) {
                    field.Field[x, y] = (float)random.NextDouble() % 1;
                }
            }

            return field;
        }