Ejemplo n.º 1
0
        private Bitmap NoiseTexture(int octaves, double persistence, double initFrequency, double initAmplitude)
        {
            Bitmap     finalNoise     = new Bitmap(Heightmap.Instance.Width, Heightmap.Instance.Height, PixelFormat.Format24bppRgb);
            BitmapData finalDataNoise = finalNoise.LockBits(new Rectangle(0, 0, finalNoise.Width, finalNoise.Height),
                                                            ImageLockMode.WriteOnly, finalNoise.PixelFormat);

            byte[] finalRawData = new byte[finalDataNoise.Height * finalDataNoise.Stride];
            Marshal.Copy(finalDataNoise.Scan0, finalRawData, 0, finalDataNoise.Height * finalDataNoise.Stride);
            // write noise
            AForge.Math.PerlinNoise noise = new AForge.Math.PerlinNoise(octaves, persistence, initFrequency, initAmplitude);
            int height = finalDataNoise.Height, width = finalDataNoise.Width, stride = finalDataNoise.Stride;

            Parallel.For(0, height, i =>
            {
                for (int j = 0; j < width; j++)
                {
                    var value = (byte)(Math.Max(0.0f, Math.Min(1.0f, (float)noise.Function2D(i, j) * 0.5f + 0.5f)) * 255);
                    value     = (byte)((value < 0) ? 0 : (value > 255) ? 255 : value);
                    finalRawData[i * stride + j * 3]     = value;
                    finalRawData[i * stride + j * 3 + 1] = value;
                    finalRawData[i * stride + j * 3 + 2] = value;
                }
            });
            Marshal.Copy(finalRawData, 0, finalDataNoise.Scan0, finalRawData.Length);
            finalNoise.UnlockBits(finalDataNoise);
            return(finalNoise);
        }
Ejemplo n.º 2
0
        public void PerlinNoise(int octaves, double persistence, double initFrequency, double initAmplitude, bool threaded)
        {
            LockForWriting();

            // reserve for texture values
            if (_dimensionsChanged)
            {
                MapValues = new byte[_height * _stride];
            }

            // not ready for writing
            if (MapValues == null || !_bitsLocked)
            {
                return;
            }

            // subdivide work betweek threads
            if (threaded)
            {
                // thread work subdivitions
                int subDivs = (int)Math.Ceiling((double)_height / (Environment.ProcessorCount - 1));
                // perlin function
                Perlin = new AForge.Math.PerlinNoise(octaves, persistence, initFrequency, initAmplitude);

                // assign work to threads
                for (int i = 0; i < Environment.ProcessorCount - 1; i++)
                {
                    int startIndex = i * subDivs;
                    int endIndex   = Math.Min(startIndex + subDivs, _height);
                    // fire up threads
                    _perlinWorker[i] = new Thread(() => CalculateSection(startIndex, endIndex));
                    _perlinWorker[i].Start();
                }

                // wait for all threads to finish
                for (int i = 0; i < Environment.ProcessorCount - 1; i++)
                {
                    _perlinWorker[i].Join();
                }
            }
            else
            {
                // perlin function
                Perlin = new AForge.Math.PerlinNoise(octaves, persistence, initFrequency, initAmplitude);

                // assign perlin noise value per pixel
                for (int i = 0; i < _height; i++)
                {
                    for (int j = 0; j < _width; j++)
                    {
                        var value = Math.Max(0.0f, Math.Min(1.0, (double)Perlin.Function2D((double)i, (double)j) * 0.5 + 0.5));
                        // from 0.0 - 1.0 to 0 - 255 byte data
                        value = value * 255;
                        // assign values at rgb positions
                        this[i, j] = (byte)value;
                    }
                }
            }

            CopyToTexture();
            Unlock();
        }