public static float ValueCoherentNoise1D(float x, long seed, NoiseQuality quality)
        {
            int   num = (!((double)x > 0.0)) ? ((int)x - 1) : ((int)x);
            int   x2  = num + 1;
            float a   = 0f;

            switch (quality)
            {
            case NoiseQuality.Fast:
                a = x - (float)num;
                break;

            case NoiseQuality.Standard:
                a = Libnoise.SCurve3(x - (float)num);
                break;

            case NoiseQuality.Best:
                a = Libnoise.SCurve5(x - (float)num);
                break;
            }
            float n  = ValueNoise1D(num, seed);
            float n2 = ValueNoise1D(x2, seed);

            return(Libnoise.Lerp(n, n2, a));
        }
Пример #2
0
        public float GetValue(float x, float y)
        {
            x *= _frequency;
            y *= _frequency;
            float num = _source2D.GetValue(x, y);

            if (num < 0f)
            {
                num = 0f - num;
            }
            num  = _offset - num;
            num *= num;
            float num2 = num;
            float num3 = 1f;
            int   num4 = 1;

            while ((double)num3 > 0.001 && (float)num4 < _octaveCount)
            {
                x   *= _lacunarity;
                y   *= _lacunarity;
                num3 = Libnoise.Clamp01(num * _gain);
                num  = _source2D.GetValue(x, y);
                if ((double)num < 0.0)
                {
                    num = 0f - num;
                }
                num   = _offset - num;
                num  *= num;
                num  *= num3;
                num2 += num * _spectralWeights[num4];
                num4++;
            }
            return(num2);
        }
Пример #3
0
        /// <summary>
        /// Generates an output value given the coordinates of the specified input value.
        /// </summary>
        /// <param name="x">The input coordinate on the x-axis.</param>
        /// <param name="y">The input coordinate on the y-axis.</param>
        /// <param name="z">The input coordinate on the z-axis.</param>
        /// <returns>The resulting output value.</returns>
        public double GetValue(double x, double y, double z)
        {
            double value = ((IModule3D)_sourceModule).GetValue(x, y, z);

            value = (value + 1.0) / 2.0;
            return(Math.Pow(Libnoise.FastFloor(value), _exponent) * 2.0 - 1.0);
        }
Пример #4
0
        public float GetValue(float x, float y)
        {
            int num  = (!((double)x > 0.0)) ? ((int)x - 1) : ((int)x);
            int num2 = (!((double)y > 0.0)) ? ((int)y - 1) : ((int)y);
            int num3 = num & 0xFF;
            int num4 = num2 & 0xFF;

            x -= (float)num;
            x -= (float)num2;
            float a  = 0f;
            float a2 = 0f;

            switch (_quality)
            {
            case NoiseQuality.Fast:
                a  = x;
                a2 = y;
                break;

            case NoiseQuality.Standard:
                a  = Libnoise.SCurve3(x);
                a2 = Libnoise.SCurve3(y);
                break;

            case NoiseQuality.Best:
                a  = Libnoise.SCurve5(x);
                a2 = Libnoise.SCurve5(y);
                break;
            }
            int num5 = _random[num3] + num4;
            int num6 = _random[num3 + 1] + num4;

            return(Libnoise.Lerp(Libnoise.Lerp(Grad(_random[num5], x, y), Grad(_random[num6], x - 1f, y), a), Libnoise.Lerp(Grad(_random[num5 + 1], x, y - 1f), Grad(_random[num6 + 1], x - 1f, y - 1f), a), a2));
        }
Пример #5
0
        /// <summary>
        /// Returns the output value from the noise module given the
        /// (latitude, longitude) coordinates of the specified input value
        /// located on the surface of the sphere.
        ///
        /// Use a negative latitude if the input value is located on the
        /// southern hemisphere.
        ///
        /// Use a negative longitude if the input value is located on the
        /// western hemisphere.
        /// </summary>
        /// <param name="lat">The latitude of the input value, in degrees</param>
        /// <param name="lon">The longitude of the input value, in degrees</param>
        /// <returns>The output value from the noise module</returns>
        public double GetValue(double lat, double lon)
        {
            double x = 0.0, y = 0.0, z = 0.0;

            Libnoise.LatLonToXYZ(lat, lon, ref x, ref y, ref z);
            return(((IModule3D)PSourceModule).GetValue(x, y, z));
        }
        public float GetValue(float x, float y, float z)
        {
            float value = ((IModule3D)_sourceModule).GetValue(x, y, z);

            value = (value + 1f) / 2f;
            return((float)Math.Pow((double)Libnoise.FastFloor(value), (double)_exponent) * 2f - 1f);
        }
Пример #7
0
        /// <summary>
        /// Returns the output value from the noise module given the
        /// (latitude, longitude) coordinates of the specified input value
        /// located on the surface of the sphere.
        ///
        /// Use a negative latitude if the input value is located on the
        /// southern hemisphere.
        ///
        /// Use a negative longitude if the input value is located on the
        /// western hemisphere.
        /// </summary>
        /// <param name="lat">The latitude of the input value, in degrees</param>
        /// <param name="lon">The longitude of the input value, in degrees</param>
        /// <returns>The output value from the noise module</returns>
        public float GetValue(float lat, float lon)
        {
            float x = 0.0f, y = 0.0f, z = 0.0f;

            Libnoise.LatLonToXYZ(lat, lon, ref x, ref y, ref z);
            return(((IModule3D)PSourceModule).GetValue(x, y, z));
        }
Пример #8
0
        /// <summary>
        /// Generates an output value given the coordinates of the specified input value.
        /// </summary>
        /// <param name="x">The input coordinate on the x-axis.</param>
        /// <param name="y">The input coordinate on the y-axis.</param>
        /// <param name="z">The input coordinate on the z-axis.</param>
        /// <returns>The resulting output value.</returns>
        public float GetValue(float x, float y, float z)
        {
            float value = ((IModule3D)_sourceModule).GetValue(x, y, z);

            value = (value + 1.0f) / 2.0f;
            return((float)Math.Pow(Libnoise.FastFloor(value), _exponent) * 2.0f - 1.0f);
        }
        public float GetValue(float x, float y, float z)
        {
            float value = ((IModule3D)_sourceModule).GetValue(x, y, z);
            int   i;

            for (i = 0; i < _controlPoints.Count && !(value < _controlPoints[i]); i++)
            {
            }
            int num  = Libnoise.Clamp(i - 1, 0, _controlPoints.Count - 1);
            int num2 = Libnoise.Clamp(i, 0, _controlPoints.Count - 1);

            if (num == num2)
            {
                return(_controlPoints[num2]);
            }
            float a    = _controlPoints[num];
            float b    = _controlPoints[num2];
            float num3 = (value - a) / (b - a);

            if (_invert)
            {
                num3 = 1f - num3;
                Libnoise.SwapValues(ref a, ref b);
            }
            num3 *= num3;
            return(Libnoise.Lerp(a, b, num3));
        }
Пример #10
0
        /// <summary>
        /// Generates an output value given the coordinates of the specified input value.
        /// </summary>
        /// <param name="x">The input coordinate on the x-axis.</param>
        /// <param name="y">The input coordinate on the y-axis.</param>
        /// <returns>The resulting output value.</returns>
        public float GetValue(float x, float y)
        {
            int curOctave;

            x *= _frequency;
            y *= _frequency;

            // Initialize value : 1st octave
            float signal = _source2D.GetValue(x, y);

            // get absolute value of signal (this creates the ridges)
            if (signal < 0.0f)
            {
                signal = -signal;
            }

            // invert and translate (note that "offset" should be ~= 1.0)
            signal = _offset - signal;

            // Square the signal to increase the sharpness of the ridges.
            signal *= signal;

            // Add the signal to the output value.
            float value = signal;

            float weight = 1.0f;

            for (curOctave = 1; weight > 0.001 && curOctave < _octaveCount; curOctave++)
            {
                x *= _lacunarity;
                y *= _lacunarity;

                // Weight successive contributions by the previous signal.
                weight = Libnoise.Clamp01(signal * _gain);

                // Get the coherent-noise value.
                signal = _source2D.GetValue(x, y);

                // Make the ridges.
                if (signal < 0.0)
                {
                    signal = -signal;
                }

                signal = _offset - signal;

                // Square the signal to increase the sharpness of the ridges.
                signal *= signal;

                // The weighting from the previous octave is applied to the signal.
                // Larger values have higher weights, producing sharp points along the
                // ridges.
                signal *= weight;

                // Add the signal to the output value.
                value += (signal * _spectralWeights[curOctave]);
            }

            return(value);
        }
Пример #11
0
        }        //end GetValue

        #endregion

        #region ValueNoise1D

        /// <summary>
        /// Generates a value-coherent-noise value from the coordinates of a
        /// one-dimensional input value.
        ///
        /// The return value ranges from -1.0 to +1.0.
        ///
        /// </summary>
        /// <param name="x">The x coordinate of the input value</param>
        /// <param name="seed">The random number seed</param>
        /// <param name="quality">The quality of the coherent-noise</param>
        /// <returns>The generated value-coherent-noise value</returns>
        public static float ValueCoherentNoise1D(float x, long seed, NoiseQuality quality)
        {
            // Create a unit-length line aligned along an integer boundary.
            // This line surrounds the input point.
            int x0 = (x > 0.0 ? (int)x: (int)x - 1);
            int x1 = x0 + 1;

            float xs = 0;

            switch (quality)
            {
            case NoiseQuality.Fast:
                xs = (x - (float)x0);
                break;

            case NoiseQuality.Standard:
                xs = Libnoise.SCurve3(x - (float)x0);
                break;

            case NoiseQuality.Best:
                xs = Libnoise.SCurve5(x - (float)x0);
                break;
            }            //end switch

            // Now calculate the noise values at each point of the line.  To generate
            // the coherent-noise value at the input point, interpolate these two
            // noise values using the S-curve value as the interpolant (trilinear
            // interpolation.)
            float n0, n1;

            n0 = ValueNoise1D(x0, seed);
            n1 = ValueNoise1D(x1, seed);
            return(Libnoise.Lerp(n0, n1, xs));
        }        //end ValueCoherentNoise1D
Пример #12
0
        private void Randomize(int seed)
        {
            _random = new int[RandomSize * 2];

            if (seed != 0)
            {
                // Shuffle the array using the given seed
                // Unpack the seed into 4 bytes then perform a bitwise XOR operation
                // with each byte
                var f = new byte[4];
                Libnoise.UnpackLittleUint32(seed, ref f);

                for (int i = 0; i < Source.Length; i++)
                {
                    _random[i]  = Source[i] ^ f[0];
                    _random[i] ^= f[1];
                    _random[i] ^= f[2];
                    _random[i] ^= f[3];

                    _random[i + RandomSize] = _random[i];
                }
            }
            else
            {
                for (int i = 0; i < RandomSize; i++)
                {
                    _random[i + RandomSize] = _random[i] = Source[i];
                }
            }
        }
        }         //end Render

        #endregion

        #region Internal

        /// <summary>
        /// Calculates the normal vector at a given point on the noise map.
        ///
        /// This method encodes the (x, y, z) components of the normal vector
        /// into the (red, green, blue) channels of the returned color.  In
        /// order to represent the vector as a color, each coordinate of the
        /// normal is mapped from the -1.0 to 1.0 range to the 0 to 255 range.
        ///
        /// The bump height specifies the ratio of spatial resolution to
        /// elevation resolution.  For example, if your noise map has a
        /// spatial resolution of 30 meters and an elevation resolution of one
        /// meter, set the bump height to 1.0 / 30.0.
        ///
        /// The spatial resolution and elevation resolution are determined by
        /// the application.
        /// </summary>
        /// <param name="nc">The height of the given point in the noise map</param>
        /// <param name="nr">The height of the left neighbor</param>
        /// <param name="nu">The height of the up neighbor</param>
        /// <param name="bumpHeight">The bump height</param>
        /// <returns>The normal vector represented as a color</returns>
        IColor CalcNormalColor(float nc, float nr, float nu, float bumpHeight)
        {
            // Calculate the surface normal.
            nc *= bumpHeight;
            nr *= bumpHeight;
            nu *= bumpHeight;

            float ncr = (nc - nr);
            float ncu = (nc - nu);
            float d   = (float)Math.Sqrt((ncu * ncu) + (ncr * ncr) + 1);
            float vxc = (nc - nr) / d;
            float vyc = (nc - nu) / d;
            float vzc = 1.0f / d;

            // Map the normal range from the (-1.0 .. +1.0) range to the (0 .. 255)
            // range.
            byte xc, yc, zc;

            xc = (byte)(Libnoise.FastFloor((vxc + 1.0f) * 127.5f) & 0xff);
            yc = (byte)(Libnoise.FastFloor((vyc + 1.0f) * 127.5f) & 0xff);
            zc = (byte)(Libnoise.FastFloor((vzc + 1.0f) * 127.5f) & 0xff);

            //
            //zc = (byte)((int)((floor)((vzc + 1.0f) * 127.5f)) & 0xff);

            return(new Color(xc, yc, zc, 255));
        }        //end CalcNormalColor
Пример #14
0
        /// <summary>
        /// Calculates the normal vector at a given point on the noise map.
        ///
        /// This method encodes the (x, y, z) components of the normal vector
        /// into the (red, green, blue) channels of the returned color.  In
        /// order to represent the vector as a color, each coordinate of the
        /// normal is mapped from the -1.0 to 1.0 range to the 0 to 255 range.
        ///
        /// The bump height specifies the ratio of spatial resolution to
        /// elevation resolution.  For example, if your noise map has a
        /// spatial resolution of 30 meters and an elevation resolution of one
        /// meter, set the bump height to 1.0 / 30.0.
        ///
        /// The spatial resolution and elevation resolution are determined by
        /// the application.
        /// </summary>
        /// <param name="nc">The height of the given point in the noise map</param>
        /// <param name="nr">The height of the left neighbor</param>
        /// <param name="nu">The height of the up neighbor</param>
        /// <param name="bumpHeight">The bump height</param>
        /// <returns>The normal vector represented as a color</returns>
        private IColor CalcNormalColor(double nc, double nr, double nu, double bumpHeight)
        {
            // Calculate the surface normal.
            nc *= bumpHeight;
            nr *= bumpHeight;
            nu *= bumpHeight;

            double ncr = (nc - nr);
            double ncu = (nc - nu);
            var    d   = Math.Sqrt((ncu * ncu) + (ncr * ncr) + 1);
            double vxc = (nc - nr) / d;
            double vyc = (nc - nu) / d;
            double vzc = 1.0 / d;

            // Map the normal range from the (-1.0 .. +1.0) range to the (0 .. 255)
            // range.
            byte xc, yc, zc;

            xc = (byte)(Libnoise.FastFloor((vxc + 1.0) * 127.5) & 0xff);
            yc = (byte)(Libnoise.FastFloor((vyc + 1.0) * 127.5) & 0xff);
            zc = (byte)(Libnoise.FastFloor((vzc + 1.0) * 127.5) & 0xff);

            //
            //zc = (byte)((int)((floor)((vzc + 1.0) * 127.5)) & 0xff);

            return(new Color(xc, yc, zc, 255));
        }
Пример #15
0
        public IColor GetColor(float position)
        {
            int i;

            for (i = 0; i < _gradientPoints.Count; i++)
            {
                GradientPoint gradientPoint = _gradientPoints[i];
                if (position < gradientPoint.Position)
                {
                    break;
                }
            }
            int num  = Libnoise.Clamp(i - 1, 0, _gradientPoints.Count - 1);
            int num2 = Libnoise.Clamp(i, 0, _gradientPoints.Count - 1);

            if (num == num2)
            {
                GradientPoint gradientPoint2 = _gradientPoints[num2];
                return(gradientPoint2.Color);
            }
            GradientPoint gradientPoint3 = _gradientPoints[num];
            float         position2      = gradientPoint3.Position;
            GradientPoint gradientPoint4 = _gradientPoints[num2];
            float         position3      = gradientPoint4.Position;
            float         num3           = (position - position2) / (position3 - position2);
            GradientPoint gradientPoint5 = _gradientPoints[num];
            IColor        color          = gradientPoint5.Color;
            GradientPoint gradientPoint6 = _gradientPoints[num2];

            return(Color.Lerp(color, gradientPoint6.Color, num3));
        }
Пример #16
0
        /// <summary>
        ///     Generates an output value given the coordinates of the specified input value.
        /// </summary>
        /// <param name="x">The input coordinate on the x-axis.</param>
        /// <returns>The resulting output value.</returns>
        public float GetValue(float x)
        {
            // Fast floor
            var xf = x > 0.0 ? (int)x : (int)x - 1;

            // Compute the cell coordinates
            var cellX = xf & 255;

            // Retrieve the decimal part of the cell
            x -= xf;

            // Smooth the curve
            var u = 0.0f;

            switch (Quality)
            {
            case NoiseQuality.Fast:
                u = x;
                break;

            case NoiseQuality.Standard:
                u = Libnoise.SCurve3(x);
                break;

            case NoiseQuality.Best:
                u = Libnoise.SCurve5(x);
                break;
            }

            return(Libnoise.Lerp(Grad(_random[cellX], x), Grad(_random[cellX + 1], x - 1), u));
        }
Пример #17
0
        /// <summary>
        /// Generates an output value given the coordinates of the specified input value.
        /// </summary>
        /// <param name="x">The input coordinate on the x-axis.</param>
        /// <returns>The resulting output value.</returns>
        public double GetValue(double x)
        {
            // Fast floor
            int xf = (x > 0.0) ? (int)x : (int)x - 1;

            // Compute the cell coordinates
            int X = xf & 255;

            // Retrieve the decimal part of the cell
            x -= xf;

            // Smooth the curve
            double u = 0.0;

            switch (Quality)
            {
            case NoiseQuality.Fast:
                u = x;
                break;

            case NoiseQuality.Standard:
                u = Libnoise.SCurve3(x);
                break;

            case NoiseQuality.Best:
                u = Libnoise.SCurve5(x);
                break;
            }

            return(Libnoise.Lerp(Grad(_random[X], x), Grad(_random[X + 1], x - 1), u));
        }
Пример #18
0
        private IColor CalcDestColor(IColor sourceColor, IColor backgroundColor, float lightValue)
        {
            float n    = (float)(int)sourceColor.Red / 255f;
            float n2   = (float)(int)sourceColor.Green / 255f;
            float n3   = (float)(int)sourceColor.Blue / 255f;
            float a    = (float)(int)sourceColor.Alpha / 255f;
            float n4   = (float)(int)backgroundColor.Red / 255f;
            float n5   = (float)(int)backgroundColor.Green / 255f;
            float n6   = (float)(int)backgroundColor.Blue / 255f;
            float num  = Libnoise.Lerp(n4, n, a);
            float num2 = Libnoise.Lerp(n5, n2, a);
            float num3 = Libnoise.Lerp(n6, n3, a);

            if (_lightEnabled)
            {
                float num4 = lightValue * (float)(int)_lightColor.Red / 255f;
                float num5 = lightValue * (float)(int)_lightColor.Green / 255f;
                float num6 = lightValue * (float)(int)_lightColor.Blue / 255f;
                num  *= num4;
                num2 *= num5;
                num3 *= num6;
            }
            num  = Libnoise.Clamp01(num);
            num2 = Libnoise.Clamp01(num2);
            num3 = Libnoise.Clamp01(num3);
            return(new Color((byte)((uint)(num * 255f) & 0xFF), (byte)((uint)(num2 * 255f) & 0xFF), (byte)((uint)(num3 * 255f) & 0xFF), Math.Max(sourceColor.Alpha, backgroundColor.Alpha)));
        }
Пример #19
0
        /// <summary>
        /// Generates an output value given the coordinates of the specified input value.
        /// </summary>
        /// <param name="x">The input coordinate on the x-axis.</param>
        /// <param name="y">The input coordinate on the y-axis.</param>
        /// <param name="z">The input coordinate on the z-axis.</param>
        /// <returns>The resulting output value.</returns>
        public double GetValue(double x, double y, double z)
        {
            double v0    = ((IModule3D)_leftModule).GetValue(x, y, z);
            double v1    = ((IModule3D)_rightModule).GetValue(x, y, z);
            double alpha = (((IModule3D)_controlModule).GetValue(x, y, z) + 1.0) / 2.0;

            return(Libnoise.Lerp(v0, v1, alpha));
        }
        public float GetValue(float x, float y, float z)
        {
            float value  = ((IModule3D)_leftModule).GetValue(x, y, z);
            float value2 = ((IModule3D)_rightModule).GetValue(x, y, z);
            float a      = (((IModule3D)_controlModule).GetValue(x, y, z) + 1f) / 2f;

            return(Libnoise.Lerp(value, value2, a));
        }
Пример #21
0
        }        //end Blend

        #endregion

        #region IModule3D Members

        /// <summary>
        /// Generates an output value given the coordinates of the specified input value.
        /// </summary>
        /// <param name="x">The input coordinate on the x-axis.</param>
        /// <param name="y">The input coordinate on the y-axis.</param>
        /// <param name="z">The input coordinate on the z-axis.</param>
        /// <returns>The resulting output value.</returns>
        public float GetValue(float x, float y, float z)
        {
            float v0    = ((IModule3D)_leftModule).GetValue(x, y, z);
            float v1    = ((IModule3D)_rightModule).GetValue(x, y, z);
            float alpha = (((IModule3D)_controlModule).GetValue(x, y, z) + 1.0f) / 2.0f;

            return(Libnoise.Lerp(v0, v1, alpha));
        }        //end GetValue
        public override void WriteFile()
        {
            if (_image == null)
            {
                throw new ArgumentException("An image map must be provided");
            }
            int width  = _image.Width;
            int height = _image.Height;
            int num    = CalcWidthByteCount(width);
            int num2   = num * height;

            byte[] array = new byte[num];
            OpenFile();
            byte[] buffer  = new byte[4];
            byte[] buffer2 = new byte[2]
            {
                66,
                77
            };
            try
            {
                _writer.Write(buffer2);
                _writer.Write(Libnoise.UnpackLittleUint32(num2 + 54, ref buffer));
                _writer.Write(Libnoise.UnpackLittleUint32(0, ref buffer));
                _writer.Write(Libnoise.UnpackLittleUint32(54, ref buffer));
                _writer.Write(Libnoise.UnpackLittleUint32(40, ref buffer));
                _writer.Write(Libnoise.UnpackLittleUint32(width, ref buffer));
                _writer.Write(Libnoise.UnpackLittleUint32(height, ref buffer));
                _writer.Write(Libnoise.UnpackLittleUint16(1, ref buffer2));
                _writer.Write(Libnoise.UnpackLittleUint16(24, ref buffer2));
                _writer.Write(Libnoise.UnpackLittleUint32(0, ref buffer));
                _writer.Write(Libnoise.UnpackLittleUint32(num2, ref buffer));
                _writer.Write(Libnoise.UnpackLittleUint32(2834, ref buffer));
                _writer.Write(Libnoise.UnpackLittleUint32(2834, ref buffer));
                _writer.Write(Libnoise.UnpackLittleUint32(0, ref buffer));
                _writer.Write(buffer);
                for (int i = 0; i < height; i++)
                {
                    int num3 = 0;
                    Array.Clear(array, 0, array.Length);
                    for (int j = 0; j < width; j++)
                    {
                        Color value = _image.GetValue(j, i);
                        array[num3++] = value.Blue;
                        array[num3++] = value.Green;
                        array[num3++] = value.Red;
                    }
                    _writer.Write(array);
                }
            }
            catch (Exception innerException)
            {
                throw new IOException("Unknown IO exception", innerException);
            }
            CloseFile();
        }
        public new float GetValue(float x, float y)
        {
            float num  = 0f;
            float num2 = 0f;
            float num3 = 0f;
            float num4 = (x + y) * F2;
            int   num5 = Libnoise.FastFloor(x + num4);
            int   num6 = Libnoise.FastFloor(y + num4);
            float num7 = (float)(num5 + num6) * G2;
            float num8 = x - ((float)num5 - num7);
            float num9 = y - ((float)num6 - num7);
            int   num10;
            int   num11;

            if (num8 > num9)
            {
                num10 = 1;
                num11 = 0;
            }
            else
            {
                num10 = 0;
                num11 = 1;
            }
            float num12 = num8 - (float)num10 + G2;
            float num13 = num9 - (float)num11 + G2;
            float num14 = num8 + G22;
            float num15 = num9 + G22;
            int   num16 = num5 & 0xFF;
            int   num17 = num6 & 0xFF;
            float num18 = 0.5f - num8 * num8 - num9 * num9;

            if (num18 > 0f)
            {
                num18 *= num18;
                int num19 = _random[num16 + _random[num17]] % 12;
                num = num18 * num18 * Dot(_grad3[num19], num8, num9);
            }
            float num20 = 0.5f - num12 * num12 - num13 * num13;

            if (num20 > 0f)
            {
                num20 *= num20;
                int num21 = _random[num16 + num10 + _random[num17 + num11]] % 12;
                num2 = num20 * num20 * Dot(_grad3[num21], num12, num13);
            }
            float num22 = 0.5f - num14 * num14 - num15 * num15;

            if (num22 > 0f)
            {
                num22 *= num22;
                int num23 = _random[num16 + 1 + _random[num17 + 1]] % 12;
                num3 = num22 * num22 * Dot(_grad3[num23], num14, num15);
            }
            return(70f * (num + num2 + num3));
        }
Пример #24
0
        public static IColor Lerp(IColor color0, IColor color1, float t, bool withAlphaChannel)
        {
            IColor color2 = (IColor)Activator.CreateInstance(color0.GetType());

            color2.Red   = Libnoise.Lerp(color0.Red, color1.Red, t);
            color2.Green = Libnoise.Lerp(color0.Green, color1.Green, t);
            color2.Blue  = Libnoise.Lerp(color0.Blue, color1.Blue, t);
            color2.Alpha = (byte)((!withAlphaChannel) ? 255 : Libnoise.Lerp(color0.Alpha, color1.Alpha, t));
            return(color2);
        }
Пример #25
0
        }        //end ImprovedPerlin

        #endregion

        #region Utils

        /// <summary>
        /// Initializes the random values
        ///
        /// </summary>
        /// <param name="seed">The seed used to generate the random values</param>
        protected void Randomize(int seed)
        {
            _random = new int[RANDOM_SIZE * 2];

            if (seed != 0)
            {
                // Shuffle the array using the given seed
                // Unpack the seed into 4 bytes then perform a bitwise XOR operation
                // with each byte
                byte[] F = new byte[4];
                Libnoise.UnpackLittleUint32(seed, ref F);

                for (int i = 0; i < _source.Length; i++)
                {
                    /*
                     * _random[i] =  (F[0] > 0) ? _source[i] ^ F[0] : _source[i];
                     * _random[i] =  (F[1] > 0) ? _source[i] ^ F[1] : _random[i];
                     * _random[i] =  (F[2] > 0) ? _source[i] ^ F[2] : _random[i];
                     * _random[i] =  (F[3] > 0) ? _source[i] ^ F[3] : _random[i];
                     */

                    _random[i]  = _source[i] ^ F[0];
                    _random[i] ^= F[1];
                    _random[i] ^= F[2];
                    _random[i] ^= F[3];

                    _random[i + RANDOM_SIZE] = _random[i];
                }                //end for

#if NOISE_RANDOM_PARANOIA
#warning NOISE_RANDOM_PARANOIA is on

                // Test if _random has unique values, a sorted _random array
                // must have values from 0 to 255
                int[] __sorted = new int[RANDOM_SIZE];
                Array.Copy(_random, __sorted, RANDOM_SIZE);
                Array.Sort(__sorted);

                for (int _j = 0; _j < RANDOM_SIZE; _j++)
                {
                    if (_j != __sorted[_j])
                    {
                        throw new Exception("Unconsistent random value at " + _j + " : " + __sorted[_j]);
                    }    //end if
                }        //end for
#endif
            }            //end if
            else
            {
                for (int i = 0; i < RANDOM_SIZE; i++)
                {
                    _random[i + RANDOM_SIZE] = _random[i] = _source[i];
                } //end for
            }     //end else
        }         //end Randomize
Пример #26
0
        /// <summary>
        /// Initializes the random values
        ///
        /// </summary>
        /// <param name="seed">The seed used to generate the random values</param>
        private void Randomize(int seed)
        {
            _random = new int[RandomSize * 2];

            if (seed != 0)
            {
                // Shuffle the array using the given seed
                // Unpack the seed into 4 bytes then perform a bitwise XOR operation
                // with each byte
                var F = new byte[4];
                Libnoise.UnpackLittleUint32(seed, ref F);

                for (int i = 0; i < Source.Length; i++)
                {
                    /*
                     *                  _random[i] =  (F[0] > 0) ? _source[i] ^ F[0] : _source[i];
                     *                  _random[i] =  (F[1] > 0) ? _source[i] ^ F[1] : _random[i];
                     *                  _random[i] =  (F[2] > 0) ? _source[i] ^ F[2] : _random[i];
                     *                  _random[i] =  (F[3] > 0) ? _source[i] ^ F[3] : _random[i];
                     */

                    _random[i]  = Source[i] ^ F[0];
                    _random[i] ^= F[1];
                    _random[i] ^= F[2];
                    _random[i] ^= F[3];

                    _random[i + RandomSize] = _random[i];
                }

#if NOISE_RANDOM_PARANOIA
#warning NOISE_RANDOM_PARANOIA is on

                // Test if Random has unique values, a sorted Random array
                // must have values from 0 to 255
                var __sorted = new int[RandomSize];
                Array.Copy(Random, __sorted, RandomSize);
                Array.Sort(__sorted);

                for (int _j = 0; _j < RandomSize; _j++)
                {
                    if (_j != __sorted[_j])
                    {
                        throw new Exception("Unconsistent random value at " + _j + " : " + __sorted[_j]);
                    }
                }
#endif
            }
            else
            {
                for (int i = 0; i < RandomSize; i++)
                {
                    _random[i + RandomSize] = _random[i] = Source[i];
                }
            }
        }
Пример #27
0
        }        //end Grad

        #endregion

        #region IModule2D Members

        /// <summary>
        /// Generates an output value given the coordinates of the specified input value.
        /// </summary>
        /// <param name="x">The input coordinate on the x-axis.</param>
        /// <param name="y">The input coordinate on the y-axis.</param>
        /// <returns>The resulting output value.</returns>
        public float GetValue(float x, float y)
        {
            // Fast floor
            int xf = (x > 0.0) ? (int)x: (int)x - 1;
            int yf = (y > 0.0) ? (int)y: (int)y - 1;

            // Compute the cell coordinates
            int X = xf & 255;
            int Y = yf & 255;

            // Retrieve the decimal part of the cell
            x -= (float)xf;
            x -= (float)yf;

            // Smooth the curve
            float u = 0.0f, v = 0.0f;

            switch (_quality)
            {
            case NoiseQuality.Fast:
                u = x;
                v = y;
                break;

            case NoiseQuality.Standard:
                u = Libnoise.SCurve3(x);
                v = Libnoise.SCurve3(y);
                break;

            case NoiseQuality.Best:
                u = Libnoise.SCurve5(x);
                v = Libnoise.SCurve5(y);
                break;
            }            //end switch

            // Fetch some randoms values from the table
            int A = _random[X] + Y;
            int B = _random[X + 1] + Y;

            // Interpolate between directions
            return(Libnoise.Lerp(
                       Libnoise.Lerp(
                           Grad(_random[A], x, y),
                           Grad(_random[B], x - 1, y),
                           u
                           ),

                       Libnoise.Lerp(
                           Grad(_random[A + 1], x, y - 1),
                           Grad(_random[B + 1], x - 1, y - 1),
                           u
                           ),
                       v
                       ));
        }        //end GetValue
Пример #28
0
        /// <summary>
        ///     Generates an output value given the coordinates of the specified input value.
        /// </summary>
        /// <param name="x">The input coordinate on the x-axis.</param>
        /// <param name="y">The input coordinate on the y-axis.</param>
        /// <returns>The resulting output value.</returns>
        public float GetValue(float x, float y)
        {
            // Fast floor
            var xf = x > 0.0 ? (int)x : (int)x - 1;
            var yf = y > 0.0 ? (int)y : (int)y - 1;

            // Compute the cell coordinates
            var cellX = xf & 255;
            var cellY = yf & 255;

            // Retrieve the decimal part of the cell
            x -= xf;
            x -= yf;

            // Smooth the curve
            float u = 0.0f, v = 0.0f;

            switch (Quality)
            {
            case NoiseQuality.Fast:
                u = x;
                v = y;
                break;

            case NoiseQuality.Standard:
                u = Libnoise.SCurve3(x);
                v = Libnoise.SCurve3(y);
                break;

            case NoiseQuality.Best:
                u = Libnoise.SCurve5(x);
                v = Libnoise.SCurve5(y);
                break;
            }

            // Fetch some randoms values from the table
            var cellA = _random[cellX] + cellY;
            var cellB = _random[cellX + 1] + cellY;

            // Interpolate between directions
            return(Libnoise.Lerp(
                       Libnoise.Lerp(
                           Grad(_random[cellA], x, y),
                           Grad(_random[cellB], x - 1, y),
                           u
                           ),
                       Libnoise.Lerp(
                           Grad(_random[cellA + 1], x, y - 1),
                           Grad(_random[cellB + 1], x - 1, y - 1),
                           u
                           ),
                       v
                       ));
        }
Пример #29
0
        /// <summary>
        /// Performs linear interpolation between two colors only with rgb channels.
        /// </summary>
        /// <param name="color0">The first color.</param>
        /// <param name="color1">The second color.</param>
        /// <param name="t">the amount to interpolate between the two colors.</param>
        /// <param name="withAlphaChannel">Flag indicates if this method also interpolate alpha channel.</param>
        /// <returns>The interpolated color, with the same type of color0.</returns>
        public static IColor Lerp(IColor color0, IColor color1, double t, bool withAlphaChannel = true)
        {
            var color = (IColor)Activator.CreateInstance(color0.GetType());

            color.Red   = Libnoise.Lerp(color0.Red, color1.Red, t);
            color.Green = Libnoise.Lerp(color0.Green, color1.Green, t);
            color.Blue  = Libnoise.Lerp(color0.Blue, color1.Blue, t);
            color.Alpha = (withAlphaChannel) ? Libnoise.Lerp(color0.Alpha, color1.Alpha, t) : (byte)255;

            return(color);
        }
Пример #30
0
        }        //end Equals

        #endregion

        #region Utilities

        /// <summary>
        /// Performs linear interpolation between two colors only with rgb channels
        /// </summary>
        /// <param name="color0">The first color</param>
        /// <param name="color1">The second color</param>
        /// <param name="t">the amount to interpolate between the two colors</param>
        /// <param name="withAlphaChannel">Flag indicates if this method also interpolate alpha channel</param>
        /// <returns>The interpolated color, with the same type of color0</returns>
        public static IColor Lerp(IColor color0, IColor color1, float t, bool withAlphaChannel)
        {
            IColor color = (IColor)System.Activator.CreateInstance(color0.GetType());

            color.Red   = Libnoise.Lerp(color0.Red, color1.Red, t);
            color.Green = Libnoise.Lerp(color0.Green, color1.Green, t);
            color.Blue  = Libnoise.Lerp(color0.Blue, color1.Blue, t);
            color.Alpha = (withAlphaChannel) ? Libnoise.Lerp(color0.Alpha, color1.Alpha, t) : (byte)255;

            return(color);
        }        //end Lerp