Exemple #1
0
        public static void GenerateNoiseMapTexture(GraphicsDevice Device, int width, int height, ref Texture2D noiseTexture, int octaves)
        {
            var data = new float[width * height];

            /// track min and max noise value. Used to normalize the result to the 0 to 1.0 range.
            var min = float.MaxValue;
            var max = float.MinValue;

            /// rebuild the permutation table to get a different noise pattern.
            /// Leave this out if you want to play with changing the number of octaves while
            /// maintaining the same overall pattern.
            Noise2d.Reseed();

            var frequency = 0.5f;
            var amplitude = 1f;

            for (var octave = 0; octave < octaves; octave++)
            {
                /// parallel loop - easy and fast.
                Parallel.For(0
                             , width * height
                             , (offset) =>
                {
                    var i     = offset % width;
                    var j     = offset / width;
                    var noise = Noise2d.Noise(i * frequency * 1f / width, j * frequency * 1f / height);
                    noise     = data[j * width + i] += noise * amplitude;

                    min = Math.Min(min, noise);
                    max = Math.Max(max, noise);
                }
                             );

                frequency *= 2;
                amplitude /= 2;
            }


            if (noiseTexture != null && (noiseTexture.Width != width || noiseTexture.Height != height))
            {
                noiseTexture.Dispose();
                noiseTexture = null;
            }
            if (noiseTexture == null)
            {
                noiseTexture = new Texture2D(Device, width, height, false, SurfaceFormat.Color);
            }

            var colors = data.Select(
                (f) =>
            {
                var norm = (f - min) / (max - min);
                return(new Microsoft.Xna.Framework.Color(norm, norm, norm, 1));
            }
                ).ToArray();

            noiseTexture.SetData(colors);
        }
Exemple #2
0
        public static float[,] GenerateNoiseMap(int width, int height, int octaves, float frequency = 0.5f, float amplitude = 1f)
        {
            var data = new float[width * height];

            /// Track min and max noise value. Used to normalize the result to the 0 to 1.0 range.
            var min = float.MaxValue;
            var max = float.MinValue;

            /// Rebuild the permutation table to get a different noise pattern.
            /// Leave this out if you want to play with changing the number of octaves while
            /// maintaining the same overall pattern.
            //Noise2d.Reseed();

            for (var octave = 0; octave < octaves; octave++)
            {
                /// parallel loop - easy and fast.
                Parallel.For(0
                             , width * height
                             , (offset) =>
                {
                    var i     = offset % width;
                    var j     = offset / width;
                    var noise = Noise2d.Noise(i * frequency * 1f / width, j * frequency * 1f / height);
                    noise     = data[j * width + i] += noise * amplitude;

                    min = Math.Min(min, noise);
                    max = Math.Max(max, noise);
                }
                             );

                frequency *= 2;
                amplitude /= 2;
            }

            var r = new float[width, height];
            var c = 0;

            for (var y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    var norm = (data[c] - min) / (max - min);

                    r[x, y] = norm;

                    //r[x, y] = data[c];

                    c += 1;
                }
            }

            return(r);
        }
Exemple #3
0
        //public static float NoiseN(float x, float y)
        //{
        //    var n = Noise(x, y);
        //    var min = float.MaxValue;
        //    var max = float.MinValue;
        //    var norm = (n - min) / (max - min);
        //    return norm;
        //}

        public static float GenerateNoisePoint(int x, int y, int width, int height, int octaves, float frequency = 0.5f, float amplitude = 1f)
        {
            var min = float.MaxValue;
            var max = float.MinValue;

            float noise = 0;

            for (var octave = 0; octave < octaves; octave++)
            {
                var n = Noise2d.Noise(x * frequency * 1f / width, y * frequency * 1f / height);
                noise += n * amplitude;

                min = Math.Min(min, noise);
                max = Math.Max(max, noise);

                frequency *= 2;
                amplitude /= 2;
            }

            var norm = (noise - min) / (max - min);

            return(norm);
        }