コード例 #1
0
ファイル: Perlin.cs プロジェクト: dvdbrink/Procedural
        public static double Get(Seed seed, double x)
        {
            // Find unit grid cell containing point
            var X = FastFloor(x);

            // Get relative xyz coordinates of point within that cell
            x -= X;

            // Wrap the integer cells at 255 (smaller integer period can be introduced here)
            X &= 255;

            // Calculate a set of eight hashed gradient indices
            var gi0 = p[X]%12;
            var gi1 = p[X + 1]%12;

            // The gradients of each corner are now:
            // g0 = grad3[gi0];
            // g1 = grad3[gi1];

            // Calculate noise contributions from each of the four corners
            var n0 = Dot(Grad3[gi0], x);
            var n1 = Dot(Grad3[gi1], x - 1);

            // Compute the fade curve value for each of x, y
            var u = Fade(x);

            // Interpolate along x the contributions from each of the corners
            var nx = Lerp(n0, n1, u);

            return nx;
        }
コード例 #2
0
ファイル: OpenSimplex.cs プロジェクト: dvdbrink/Procedural
        public static double Get(Seed seed, double x, double y)
        {
            const double zero = 0d;
            const double one = 1d;
            const double two = 2d;

            // Place input coordinates onto grid.
            var stretchOffset = (x + y)*StretchConstant_2D;
            var xs = x + stretchOffset;
            var ys = y + stretchOffset;

            // Floor to get grid coordinates of rhombus (stretched square) super-cell origin.
            var xsFloor = Math.FastFloor(xs);
            var ysFloor = Math.FastFloor(ys);

            // Skew out to get actual coordinates of rhombus origin. We'll need these later.
            var squishOffset = (xsFloor + ysFloor)*SquishConstant_2D;
            var xFloor = xsFloor + squishOffset;
            var yFloor = ysFloor + squishOffset;

            // Compute grid coordinates relative to rhombus origin.
            var xsFrac = xs - xsFloor;
            var ysFrac = ys - ysFloor;

            // Sum those together to get a value that determines which region we're in.
            var fracSum = xsFrac + ysFrac;

            // Positions relative to origin point.
            var dx0 = x - xFloor;
            var dy0 = y - yFloor;

            var value = zero;

            // Contribution (1,0)
            var dx1 = dx0 - one - SquishConstant_2D;
            var dy1 = dy0 - zero - SquishConstant_2D;
            value += Gradient.Get(seed, xsFloor + 1, ysFloor, dx1, dy1);

            // Contribution (0,1)
            var dx2 = dx0 - zero - SquishConstant_2D;
            var dy2 = dy0 - one - SquishConstant_2D;
            value += Gradient.Get(seed, xsFloor, ysFloor + 1, dx2, dy2);

            // Check if we're inside the triangle (2-Simplex) at (1,1)
            if (fracSum > one)
            {
                xsFloor++;
                ysFloor++;
                dx0 = dx0 - one - two*SquishConstant_2D;
                dy0 = dy0 - one - two*SquishConstant_2D;
            }

            // Contribution (0,0) or (1,1)
            value += Gradient.Get(seed, xsFloor, ysFloor, dx0, dy0);

            return value/NormConstant_2D;
        }
コード例 #3
0
        public ThreadedChunkGenerator(Seed seed)
        {
            _in = new Queue<IChunk>();
            _out = new Queue<IChunk>();

            _seed = seed;
            _b = new Brownian(3, 0.01, 2, 0.8);

            var thread = new Thread(Run)
            {
                Priority = ThreadPriority.BelowNormal,
                IsBackground = true
            };
            thread.Start();
        }
コード例 #4
0
ファイル: Brownian.cs プロジェクト: dvdbrink/Procedural
        public double Get(Seed seed, double x, double y, double z, Func<Seed, double, double, double, double> noise)
        {
            var frequency = _startFrequency;
            double amplitude = 1;
            double result = 0;

            for (var octave = 0; octave < _octaves; ++octave)
            {
                result += noise(seed, x*frequency, y*frequency, z*frequency)*amplitude;
                amplitude *= _persistence;
                frequency *= _lacunarity;
            }

            return result;
        }
コード例 #5
0
ファイル: OpenSimplexOld.cs プロジェクト: dvdbrink/Procedural
 private double extrapolate(Seed seed, int xsb, int ysb, int zsb, double dx, double dy, double dz)
 {
     var index = seed.Get(xsb, ysb, zsb);
     var a = gradients3D[index] * dx
         + gradients3D[index + 1] * dy
         + gradients3D[index + 2] * dz;
     return a;
 }
コード例 #6
0
ファイル: OpenSimplexOld.cs プロジェクト: dvdbrink/Procedural
 private double extrapolate(Seed seed, int xsb, int ysb, double dx, double dy)
 {
     var seedIndex = seed.Get(xsb, ysb) & 0x0E;
     return gradients2D[seedIndex] * dx
         + gradients2D[seedIndex + 1] * dy;
 }
コード例 #7
0
ファイル: OpenSimplexOld.cs プロジェクト: dvdbrink/Procedural
        //3D OpenSimplex Noise.
        public double eval(Seed seed, double x, double y, double z)
        {
            //Place input coordinates on simplectic honeycomb.
            double stretchOffset = (x + y + z) * STRETCH_CONSTANT_3D;
            double xs = x + stretchOffset;
            double ys = y + stretchOffset;
            double zs = z + stretchOffset;

            //Floor to get simplectic honeycomb coordinates of rhombohedron (stretched cube) super-cell origin.
            int xsb = fastFloor(xs);
            int ysb = fastFloor(ys);
            int zsb = fastFloor(zs);

            //Skew out to get actual coordinates of rhombohedron origin. We'll need these later.
            double squishOffset = (xsb + ysb + zsb) * SQUISH_CONSTANT_3D;
            double xb = xsb + squishOffset;
            double yb = ysb + squishOffset;
            double zb = zsb + squishOffset;

            //Compute simplectic honeycomb coordinates relative to rhombohedral origin.
            double xins = xs - xsb;
            double yins = ys - ysb;
            double zins = zs - zsb;

            //Sum those together to get a value that determines which region we're in.
            double inSum = xins + yins + zins;

            //Positions relative to origin point.
            double dx0 = x - xb;
            double dy0 = y - yb;
            double dz0 = z - zb;

            //We'll be defining these inside the next block and using them afterwards.
            double dx_ext0, dy_ext0, dz_ext0;
            double dx_ext1, dy_ext1, dz_ext1;
            int xsv_ext0, ysv_ext0, zsv_ext0;
            int xsv_ext1, ysv_ext1, zsv_ext1;

            double value = 0;
            if (inSum <= 1)
            { //We're inside the tetrahedron (3-Simplex) at (0,0,0)
                //Determine which two of (0,0,1), (0,1,0), (1,0,0) are closest.
                byte aPoint = 0x01;
                double aScore = xins;
                byte bPoint = 0x02;
                double bScore = yins;
                if (aScore >= bScore && zins > bScore)
                {
                    bScore = zins;
                    bPoint = 0x04;
                }
                else if (aScore < bScore && zins > aScore)
                {
                    aScore = zins;
                    aPoint = 0x04;
                }

                //Now we determine the two lattice points not part of the tetrahedron that may contribute.
                //This depends on the closest two tetrahedral vertices, including (0,0,0)
                double wins = 1 - inSum;
                if (wins > aScore || wins > bScore)
                { //(0,0,0) is one of the closest two tetrahedral vertices.
                    byte c = (bScore > aScore ? bPoint : aPoint); //Our other closest vertex is the closest out of a and b.

                    if ((c & 0x01) == 0)
                    {
                        xsv_ext0 = xsb - 1;
                        xsv_ext1 = xsb;
                        dx_ext0 = dx0 + 1;
                        dx_ext1 = dx0;
                    }
                    else
                    {
                        xsv_ext0 = xsv_ext1 = xsb + 1;
                        dx_ext0 = dx_ext1 = dx0 - 1;
                    }

                    if ((c & 0x02) == 0)
                    {
                        ysv_ext0 = ysv_ext1 = ysb;
                        dy_ext0 = dy_ext1 = dy0;
                        if ((c & 0x01) == 0)
                        {
                            ysv_ext1 -= 1;
                            dy_ext1 += 1;
                        }
                        else
                        {
                            ysv_ext0 -= 1;
                            dy_ext0 += 1;
                        }
                    }
                    else
                    {
                        ysv_ext0 = ysv_ext1 = ysb + 1;
                        dy_ext0 = dy_ext1 = dy0 - 1;
                    }

                    if ((c & 0x04) == 0)
                    {
                        zsv_ext0 = zsb;
                        zsv_ext1 = zsb - 1;
                        dz_ext0 = dz0;
                        dz_ext1 = dz0 + 1;
                    }
                    else
                    {
                        zsv_ext0 = zsv_ext1 = zsb + 1;
                        dz_ext0 = dz_ext1 = dz0 - 1;
                    }
                }
                else
                { //(0,0,0) is not one of the closest two tetrahedral vertices.
                    byte c = (byte)(aPoint | bPoint); //Our two extra vertices are determined by the closest two.

                    if ((c & 0x01) == 0)
                    {
                        xsv_ext0 = xsb;
                        xsv_ext1 = xsb - 1;
                        dx_ext0 = dx0 - 2 * SQUISH_CONSTANT_3D;
                        dx_ext1 = dx0 + 1 - SQUISH_CONSTANT_3D;
                    }
                    else
                    {
                        xsv_ext0 = xsv_ext1 = xsb + 1;
                        dx_ext0 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D;
                        dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D;
                    }

                    if ((c & 0x02) == 0)
                    {
                        ysv_ext0 = ysb;
                        ysv_ext1 = ysb - 1;
                        dy_ext0 = dy0 - 2 * SQUISH_CONSTANT_3D;
                        dy_ext1 = dy0 + 1 - SQUISH_CONSTANT_3D;
                    }
                    else
                    {
                        ysv_ext0 = ysv_ext1 = ysb + 1;
                        dy_ext0 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D;
                        dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D;
                    }

                    if ((c & 0x04) == 0)
                    {
                        zsv_ext0 = zsb;
                        zsv_ext1 = zsb - 1;
                        dz_ext0 = dz0 - 2 * SQUISH_CONSTANT_3D;
                        dz_ext1 = dz0 + 1 - SQUISH_CONSTANT_3D;
                    }
                    else
                    {
                        zsv_ext0 = zsv_ext1 = zsb + 1;
                        dz_ext0 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D;
                        dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D;
                    }
                }

                //Contribution (0,0,0)
                double attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0;
                if (attn0 > 0)
                {
                    attn0 *= attn0;
                    value += attn0 * attn0 * extrapolate(seed, xsb + 0, ysb + 0, zsb + 0, dx0, dy0, dz0);
                }

                //Contribution (1,0,0)
                double dx1 = dx0 - 1 - SQUISH_CONSTANT_3D;
                double dy1 = dy0 - 0 - SQUISH_CONSTANT_3D;
                double dz1 = dz0 - 0 - SQUISH_CONSTANT_3D;
                double attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1;
                if (attn1 > 0)
                {
                    attn1 *= attn1;
                    value += attn1 * attn1 * extrapolate(seed, xsb + 1, ysb + 0, zsb + 0, dx1, dy1, dz1);
                }

                //Contribution (0,1,0)
                double dx2 = dx0 - 0 - SQUISH_CONSTANT_3D;
                double dy2 = dy0 - 1 - SQUISH_CONSTANT_3D;
                double dz2 = dz1;
                double attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2;
                if (attn2 > 0)
                {
                    attn2 *= attn2;
                    value += attn2 * attn2 * extrapolate(seed, xsb + 0, ysb + 1, zsb + 0, dx2, dy2, dz2);
                }

                //Contribution (0,0,1)
                double dx3 = dx2;
                double dy3 = dy1;
                double dz3 = dz0 - 1 - SQUISH_CONSTANT_3D;
                double attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3;
                if (attn3 > 0)
                {
                    attn3 *= attn3;
                    value += attn3 * attn3 * extrapolate(seed, xsb + 0, ysb + 0, zsb + 1, dx3, dy3, dz3);
                }
            }
            else if (inSum >= 2)
            { //We're inside the tetrahedron (3-Simplex) at (1,1,1)
                //Determine which two tetrahedral vertices are the closest, out of (1,1,0), (1,0,1), (0,1,1) but not (1,1,1).
                byte aPoint = 0x06;
                double aScore = xins;
                byte bPoint = 0x05;
                double bScore = yins;
                if (aScore <= bScore && zins < bScore)
                {
                    bScore = zins;
                    bPoint = 0x03;
                }
                else if (aScore > bScore && zins < aScore)
                {
                    aScore = zins;
                    aPoint = 0x03;
                }

                //Now we determine the two lattice points not part of the tetrahedron that may contribute.
                //This depends on the closest two tetrahedral vertices, including (1,1,1)
                double wins = 3 - inSum;
                if (wins < aScore || wins < bScore)
                { //(1,1,1) is one of the closest two tetrahedral vertices.
                    byte c = (bScore < aScore ? bPoint : aPoint); //Our other closest vertex is the closest out of a and b.

                    if ((c & 0x01) != 0)
                    {
                        xsv_ext0 = xsb + 2;
                        xsv_ext1 = xsb + 1;
                        dx_ext0 = dx0 - 2 - 3 * SQUISH_CONSTANT_3D;
                        dx_ext1 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D;
                    }
                    else
                    {
                        xsv_ext0 = xsv_ext1 = xsb;
                        dx_ext0 = dx_ext1 = dx0 - 3 * SQUISH_CONSTANT_3D;
                    }

                    if ((c & 0x02) != 0)
                    {
                        ysv_ext0 = ysv_ext1 = ysb + 1;
                        dy_ext0 = dy_ext1 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D;
                        if ((c & 0x01) != 0)
                        {
                            ysv_ext1 += 1;
                            dy_ext1 -= 1;
                        }
                        else
                        {
                            ysv_ext0 += 1;
                            dy_ext0 -= 1;
                        }
                    }
                    else
                    {
                        ysv_ext0 = ysv_ext1 = ysb;
                        dy_ext0 = dy_ext1 = dy0 - 3 * SQUISH_CONSTANT_3D;
                    }

                    if ((c & 0x04) != 0)
                    {
                        zsv_ext0 = zsb + 1;
                        zsv_ext1 = zsb + 2;
                        dz_ext0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D;
                        dz_ext1 = dz0 - 2 - 3 * SQUISH_CONSTANT_3D;
                    }
                    else
                    {
                        zsv_ext0 = zsv_ext1 = zsb;
                        dz_ext0 = dz_ext1 = dz0 - 3 * SQUISH_CONSTANT_3D;
                    }
                }
                else
                { //(1,1,1) is not one of the closest two tetrahedral vertices.
                    byte c = (byte)(aPoint & bPoint); //Our two extra vertices are determined by the closest two.

                    if ((c & 0x01) != 0)
                    {
                        xsv_ext0 = xsb + 1;
                        xsv_ext1 = xsb + 2;
                        dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D;
                        dx_ext1 = dx0 - 2 - 2 * SQUISH_CONSTANT_3D;
                    }
                    else
                    {
                        xsv_ext0 = xsv_ext1 = xsb;
                        dx_ext0 = dx0 - SQUISH_CONSTANT_3D;
                        dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D;
                    }

                    if ((c & 0x02) != 0)
                    {
                        ysv_ext0 = ysb + 1;
                        ysv_ext1 = ysb + 2;
                        dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D;
                        dy_ext1 = dy0 - 2 - 2 * SQUISH_CONSTANT_3D;
                    }
                    else
                    {
                        ysv_ext0 = ysv_ext1 = ysb;
                        dy_ext0 = dy0 - SQUISH_CONSTANT_3D;
                        dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D;
                    }

                    if ((c & 0x04) != 0)
                    {
                        zsv_ext0 = zsb + 1;
                        zsv_ext1 = zsb + 2;
                        dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D;
                        dz_ext1 = dz0 - 2 - 2 * SQUISH_CONSTANT_3D;
                    }
                    else
                    {
                        zsv_ext0 = zsv_ext1 = zsb;
                        dz_ext0 = dz0 - SQUISH_CONSTANT_3D;
                        dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D;
                    }
                }

                //Contribution (1,1,0)
                double dx3 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D;
                double dy3 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D;
                double dz3 = dz0 - 0 - 2 * SQUISH_CONSTANT_3D;
                double attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3;
                if (attn3 > 0)
                {
                    attn3 *= attn3;
                    value += attn3 * attn3 * extrapolate(seed, xsb + 1, ysb + 1, zsb + 0, dx3, dy3, dz3);
                }

                //Contribution (1,0,1)
                double dx2 = dx3;
                double dy2 = dy0 - 0 - 2 * SQUISH_CONSTANT_3D;
                double dz2 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D;
                double attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2;
                if (attn2 > 0)
                {
                    attn2 *= attn2;
                    value += attn2 * attn2 * extrapolate(seed, xsb + 1, ysb + 0, zsb + 1, dx2, dy2, dz2);
                }

                //Contribution (0,1,1)
                double dx1 = dx0 - 0 - 2 * SQUISH_CONSTANT_3D;
                double dy1 = dy3;
                double dz1 = dz2;
                double attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1;
                if (attn1 > 0)
                {
                    attn1 *= attn1;
                    value += attn1 * attn1 * extrapolate(seed, xsb + 0, ysb + 1, zsb + 1, dx1, dy1, dz1);
                }

                //Contribution (1,1,1)
                dx0 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D;
                dy0 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D;
                dz0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D;
                double attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0;
                if (attn0 > 0)
                {
                    attn0 *= attn0;
                    value += attn0 * attn0 * extrapolate(seed, xsb + 1, ysb + 1, zsb + 1, dx0, dy0, dz0);
                }
            }
            else
            { //We're inside the octahedron (Rectified 3-Simplex) in between.
                double aScore;
                byte aPoint;
                bool aIsFurtherSide;
                double bScore;
                byte bPoint;
                bool bIsFurtherSide;

                //Decide between point (0,0,1) and (1,1,0) as closest
                double p1 = xins + yins;
                if (p1 > 1)
                {
                    aScore = p1 - 1;
                    aPoint = 0x03;
                    aIsFurtherSide = true;
                }
                else
                {
                    aScore = 1 - p1;
                    aPoint = 0x04;
                    aIsFurtherSide = false;
                }

                //Decide between point (0,1,0) and (1,0,1) as closest
                double p2 = xins + zins;
                if (p2 > 1)
                {
                    bScore = p2 - 1;
                    bPoint = 0x05;
                    bIsFurtherSide = true;
                }
                else
                {
                    bScore = 1 - p2;
                    bPoint = 0x02;
                    bIsFurtherSide = false;
                }

                //The closest out of the two (1,0,0) and (0,1,1) will replace the furthest out of the two decided above, if closer.
                double p3 = yins + zins;
                if (p3 > 1)
                {
                    double score = p3 - 1;
                    if (aScore <= bScore && aScore < score)
                    {
                        aScore = score;
                        aPoint = 0x06;
                        aIsFurtherSide = true;
                    }
                    else if (aScore > bScore && bScore < score)
                    {
                        bScore = score;
                        bPoint = 0x06;
                        bIsFurtherSide = true;
                    }
                }
                else
                {
                    double score = 1 - p3;
                    if (aScore <= bScore && aScore < score)
                    {
                        aScore = score;
                        aPoint = 0x01;
                        aIsFurtherSide = false;
                    }
                    else if (aScore > bScore && bScore < score)
                    {
                        bScore = score;
                        bPoint = 0x01;
                        bIsFurtherSide = false;
                    }
                }

                //Where each of the two closest points are determines how the extra two vertices are calculated.
                if (aIsFurtherSide == bIsFurtherSide)
                {
                    if (aIsFurtherSide)
                    { //Both closest points on (1,1,1) side

                        //One of the two extra points is (1,1,1)
                        dx_ext0 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D;
                        dy_ext0 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D;
                        dz_ext0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D;
                        xsv_ext0 = xsb + 1;
                        ysv_ext0 = ysb + 1;
                        zsv_ext0 = zsb + 1;

                        //Other extra point is based on the shared axis.
                        byte c = (byte)(aPoint & bPoint);
                        if ((c & 0x01) != 0)
                        {
                            dx_ext1 = dx0 - 2 - 2 * SQUISH_CONSTANT_3D;
                            dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D;
                            dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D;
                            xsv_ext1 = xsb + 2;
                            ysv_ext1 = ysb;
                            zsv_ext1 = zsb;
                        }
                        else if ((c & 0x02) != 0)
                        {
                            dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D;
                            dy_ext1 = dy0 - 2 - 2 * SQUISH_CONSTANT_3D;
                            dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D;
                            xsv_ext1 = xsb;
                            ysv_ext1 = ysb + 2;
                            zsv_ext1 = zsb;
                        }
                        else
                        {
                            dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D;
                            dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D;
                            dz_ext1 = dz0 - 2 - 2 * SQUISH_CONSTANT_3D;
                            xsv_ext1 = xsb;
                            ysv_ext1 = ysb;
                            zsv_ext1 = zsb + 2;
                        }
                    }
                    else
                    {//Both closest points on (0,0,0) side

                        //One of the two extra points is (0,0,0)
                        dx_ext0 = dx0;
                        dy_ext0 = dy0;
                        dz_ext0 = dz0;
                        xsv_ext0 = xsb;
                        ysv_ext0 = ysb;
                        zsv_ext0 = zsb;

                        //Other extra point is based on the omitted axis.
                        byte c = (byte)(aPoint | bPoint);
                        if ((c & 0x01) == 0)
                        {
                            dx_ext1 = dx0 + 1 - SQUISH_CONSTANT_3D;
                            dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D;
                            dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D;
                            xsv_ext1 = xsb - 1;
                            ysv_ext1 = ysb + 1;
                            zsv_ext1 = zsb + 1;
                        }
                        else if ((c & 0x02) == 0)
                        {
                            dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D;
                            dy_ext1 = dy0 + 1 - SQUISH_CONSTANT_3D;
                            dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D;
                            xsv_ext1 = xsb + 1;
                            ysv_ext1 = ysb - 1;
                            zsv_ext1 = zsb + 1;
                        }
                        else
                        {
                            dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D;
                            dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D;
                            dz_ext1 = dz0 + 1 - SQUISH_CONSTANT_3D;
                            xsv_ext1 = xsb + 1;
                            ysv_ext1 = ysb + 1;
                            zsv_ext1 = zsb - 1;
                        }
                    }
                }
                else
                { //One point on (0,0,0) side, one point on (1,1,1) side
                    byte c1, c2;
                    if (aIsFurtherSide)
                    {
                        c1 = aPoint;
                        c2 = bPoint;
                    }
                    else
                    {
                        c1 = bPoint;
                        c2 = aPoint;
                    }

                    //One contribution is a permutation of (1,1,-1)
                    if ((c1 & 0x01) == 0)
                    {
                        dx_ext0 = dx0 + 1 - SQUISH_CONSTANT_3D;
                        dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D;
                        dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D;
                        xsv_ext0 = xsb - 1;
                        ysv_ext0 = ysb + 1;
                        zsv_ext0 = zsb + 1;
                    }
                    else if ((c1 & 0x02) == 0)
                    {
                        dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D;
                        dy_ext0 = dy0 + 1 - SQUISH_CONSTANT_3D;
                        dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D;
                        xsv_ext0 = xsb + 1;
                        ysv_ext0 = ysb - 1;
                        zsv_ext0 = zsb + 1;
                    }
                    else
                    {
                        dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D;
                        dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D;
                        dz_ext0 = dz0 + 1 - SQUISH_CONSTANT_3D;
                        xsv_ext0 = xsb + 1;
                        ysv_ext0 = ysb + 1;
                        zsv_ext0 = zsb - 1;
                    }

                    //One contribution is a permutation of (0,0,2)
                    dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D;
                    dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D;
                    dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D;
                    xsv_ext1 = xsb;
                    ysv_ext1 = ysb;
                    zsv_ext1 = zsb;
                    if ((c2 & 0x01) != 0)
                    {
                        dx_ext1 -= 2;
                        xsv_ext1 += 2;
                    }
                    else if ((c2 & 0x02) != 0)
                    {
                        dy_ext1 -= 2;
                        ysv_ext1 += 2;
                    }
                    else
                    {
                        dz_ext1 -= 2;
                        zsv_ext1 += 2;
                    }
                }

                //Contribution (1,0,0)
                double dx1 = dx0 - 1 - SQUISH_CONSTANT_3D;
                double dy1 = dy0 - 0 - SQUISH_CONSTANT_3D;
                double dz1 = dz0 - 0 - SQUISH_CONSTANT_3D;
                double attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1;
                if (attn1 > 0)
                {
                    attn1 *= attn1;
                    value += attn1 * attn1 * extrapolate(seed, xsb + 1, ysb + 0, zsb + 0, dx1, dy1, dz1);
                }

                //Contribution (0,1,0)
                double dx2 = dx0 - 0 - SQUISH_CONSTANT_3D;
                double dy2 = dy0 - 1 - SQUISH_CONSTANT_3D;
                double dz2 = dz1;
                double attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2;
                if (attn2 > 0)
                {
                    attn2 *= attn2;
                    value += attn2 * attn2 * extrapolate(seed, xsb + 0, ysb + 1, zsb + 0, dx2, dy2, dz2);
                }

                //Contribution (0,0,1)
                double dx3 = dx2;
                double dy3 = dy1;
                double dz3 = dz0 - 1 - SQUISH_CONSTANT_3D;
                double attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3;
                if (attn3 > 0)
                {
                    attn3 *= attn3;
                    value += attn3 * attn3 * extrapolate(seed, xsb + 0, ysb + 0, zsb + 1, dx3, dy3, dz3);
                }

                //Contribution (1,1,0)
                double dx4 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D;
                double dy4 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D;
                double dz4 = dz0 - 0 - 2 * SQUISH_CONSTANT_3D;
                double attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4;
                if (attn4 > 0)
                {
                    attn4 *= attn4;
                    value += attn4 * attn4 * extrapolate(seed, xsb + 1, ysb + 1, zsb + 0, dx4, dy4, dz4);
                }

                //Contribution (1,0,1)
                double dx5 = dx4;
                double dy5 = dy0 - 0 - 2 * SQUISH_CONSTANT_3D;
                double dz5 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D;
                double attn5 = 2 - dx5 * dx5 - dy5 * dy5 - dz5 * dz5;
                if (attn5 > 0)
                {
                    attn5 *= attn5;
                    value += attn5 * attn5 * extrapolate(seed, xsb + 1, ysb + 0, zsb + 1, dx5, dy5, dz5);
                }

                //Contribution (0,1,1)
                double dx6 = dx0 - 0 - 2 * SQUISH_CONSTANT_3D;
                double dy6 = dy4;
                double dz6 = dz5;
                double attn6 = 2 - dx6 * dx6 - dy6 * dy6 - dz6 * dz6;
                if (attn6 > 0)
                {
                    attn6 *= attn6;
                    value += attn6 * attn6 * extrapolate(seed, xsb + 0, ysb + 1, zsb + 1, dx6, dy6, dz6);
                }
            }

            //First extra vertex
            double attn_ext0 = 2 - dx_ext0 * dx_ext0 - dy_ext0 * dy_ext0 - dz_ext0 * dz_ext0;
            if (attn_ext0 > 0)
            {
                attn_ext0 *= attn_ext0;
                value += attn_ext0 * attn_ext0 * extrapolate(seed, xsv_ext0, ysv_ext0, zsv_ext0, dx_ext0, dy_ext0, dz_ext0);
            }

            //Second extra vertex
            double attn_ext1 = 2 - dx_ext1 * dx_ext1 - dy_ext1 * dy_ext1 - dz_ext1 * dz_ext1;
            if (attn_ext1 > 0)
            {
                attn_ext1 *= attn_ext1;
                value += attn_ext1 * attn_ext1 * extrapolate(seed, xsv_ext1, ysv_ext1, zsv_ext1, dx_ext1, dy_ext1, dz_ext1);
            }

            return value / NORM_CONSTANT_3D;
        }
コード例 #8
0
ファイル: OpenSimplexOld.cs プロジェクト: dvdbrink/Procedural
        //2D OpenSimplex Noise.
        public double eval(Seed seed, double x, double y)
        {
            //Place input coordinates onto grid.
            double stretchOffset = (x + y) * STRETCH_CONSTANT_2D;
            double xs = x + stretchOffset;
            double ys = y + stretchOffset;

            //Floor to get grid coordinates of rhombus (stretched square) super-cell origin.
            int xsb = fastFloor(xs);
            int ysb = fastFloor(ys);

            //Skew out to get actual coordinates of rhombus origin. We'll need these later.
            double squishOffset = (xsb + ysb) * SQUISH_CONSTANT_2D;
            double xb = xsb + squishOffset;
            double yb = ysb + squishOffset;

            //Compute grid coordinates relative to rhombus origin.
            double xins = xs - xsb;
            double yins = ys - ysb;

            //Sum those together to get a value that determines which region we're in.
            double inSum = xins + yins;

            //Positions relative to origin point.
            double dx0 = x - xb;
            double dy0 = y - yb;

            //We'll be defining these inside the next block and using them afterwards.
            double dx_ext, dy_ext;
            int xsv_ext, ysv_ext;

            double value = 0;

            //Contribution (1,0)
            double dx1 = dx0 - 1 - SQUISH_CONSTANT_2D;
            double dy1 = dy0 - 0 - SQUISH_CONSTANT_2D;
            double attn1 = 2 - dx1 * dx1 - dy1 * dy1;
            if (attn1 > 0)
            {
                attn1 *= attn1;
                value += attn1 * attn1 * extrapolate(seed, xsb + 1, ysb + 0, dx1, dy1);
            }

            //Contribution (0,1)
            double dx2 = dx0 - 0 - SQUISH_CONSTANT_2D;
            double dy2 = dy0 - 1 - SQUISH_CONSTANT_2D;
            double attn2 = 2 - dx2 * dx2 - dy2 * dy2;
            if (attn2 > 0)
            {
                attn2 *= attn2;
                value += attn2 * attn2 * extrapolate(seed, xsb + 0, ysb + 1, dx2, dy2);
            }

            if (inSum <= 1)
            { //We're inside the triangle (2-Simplex) at (0,0)
                double zins = 1 - inSum;
                if (zins > xins || zins > yins)
                { //(0,0) is one of the closest two triangular vertices
                    if (xins > yins)
                    {
                        xsv_ext = xsb + 1;
                        ysv_ext = ysb - 1;
                        dx_ext = dx0 - 1;
                        dy_ext = dy0 + 1;
                    }
                    else
                    {
                        xsv_ext = xsb - 1;
                        ysv_ext = ysb + 1;
                        dx_ext = dx0 + 1;
                        dy_ext = dy0 - 1;
                    }
                }
                else
                { //(1,0) and (0,1) are the closest two vertices.
                    xsv_ext = xsb + 1;
                    ysv_ext = ysb + 1;
                    dx_ext = dx0 - 1 - 2 * SQUISH_CONSTANT_2D;
                    dy_ext = dy0 - 1 - 2 * SQUISH_CONSTANT_2D;
                }
            }
            else
            { //We're inside the triangle (2-Simplex) at (1,1)
                double zins = 2 - inSum;
                if (zins < xins || zins < yins)
                { //(0,0) is one of the closest two triangular vertices
                    if (xins > yins)
                    {
                        xsv_ext = xsb + 2;
                        ysv_ext = ysb + 0;
                        dx_ext = dx0 - 2 - 2 * SQUISH_CONSTANT_2D;
                        dy_ext = dy0 + 0 - 2 * SQUISH_CONSTANT_2D;
                    }
                    else
                    {
                        xsv_ext = xsb + 0;
                        ysv_ext = ysb + 2;
                        dx_ext = dx0 + 0 - 2 * SQUISH_CONSTANT_2D;
                        dy_ext = dy0 - 2 - 2 * SQUISH_CONSTANT_2D;
                    }
                }
                else
                { //(1,0) and (0,1) are the closest two vertices.
                    dx_ext = dx0;
                    dy_ext = dy0;
                    xsv_ext = xsb;
                    ysv_ext = ysb;
                }
                xsb += 1;
                ysb += 1;
                dx0 = dx0 - 1 - 2 * SQUISH_CONSTANT_2D;
                dy0 = dy0 - 1 - 2 * SQUISH_CONSTANT_2D;
            }

            //Contribution (0,0) or (1,1)
            double attn0 = 2 - dx0 * dx0 - dy0 * dy0;
            if (attn0 > 0)
            {
                attn0 *= attn0;
                value += attn0 * attn0 * extrapolate(seed, xsb, ysb, dx0, dy0);
            }

            //Extra Vertex
            double attn_ext = 2 - dx_ext * dx_ext - dy_ext * dy_ext;
            if (attn_ext > 0)
            {
                attn_ext *= attn_ext;
                value += attn_ext * attn_ext * extrapolate(seed, xsv_ext, ysv_ext, dx_ext, dy_ext);
            }

            return value / NORM_CONSTANT_2D;
        }
コード例 #9
0
ファイル: MeshChunkFactory.cs プロジェクト: dvdbrink/Grapple
 public IChunkGenerator CreateChunkGenerator(Seed seed)
 {
     return new ThreadedChunkGenerator(seed);
 }
コード例 #10
0
ファイル: OpenSimplex.cs プロジェクト: dvdbrink/Procedural
        public static double Get(Seed seed, double x, double y, double z)
        {
            const double zero = 0d;
            const double one = 1d;
            const double two = 2d;
            const double three = 3d;

            // Place input coordinates on simplectic honeycomb.
            var stretchOffset = (x + y + z)*StretchConstant_3D;
            var xs = x + stretchOffset;
            var ys = y + stretchOffset;
            var zs = z + stretchOffset;

            // Floor to get simplectic honeycomb coordinates of rhombohedron (stretched cube) super-cell origin.
            var xsFloor = Math.FastFloor(xs);
            var ysFloor = Math.FastFloor(ys);
            var zsFloor = Math.FastFloor(zs);

            // Skew out to get actual coordinates of rhombohedron origin. We'll need these later.
            var squishOffset = (xsFloor + ysFloor + zsFloor)*SquishConstant_3D;
            var xFloor = xsFloor + squishOffset;
            var yFloor = ysFloor + squishOffset;
            var zFloor = zsFloor + squishOffset;

            // Compute simplectic honeycomb coordinates relative to rhombohedral origin.
            var xsFrac = xs - xsFloor;
            var ysFrac = ys - ysFloor;
            var zsFrac = zs - zsFloor;

            // Sum those together to get a value that determines which region we're in.
            var fracSum = xsFrac + ysFrac + zsFrac;

            // Positions relative to origin point.
            var dx0 = x - xFloor;
            var dy0 = y - yFloor;
            var dz0 = z - zFloor;

            var value = zero;

            // Check if we're inside the tetrahedron (3-Simplex) at (0,0,0)
            if (fracSum <= one)
            {
                // Contribution (0,0,0)
                value += Gradient.Get(seed, xsFloor, ysFloor, zsFloor, dx0, dy0, dz0);

                // Contribution (1,0,0)
                var dx1 = dx0 - one - SquishConstant_3D;
                var dy1 = dy0 - zero - SquishConstant_3D;
                var dz1 = dz0 - zero - SquishConstant_3D;
                value += Gradient.Get(seed, xsFloor + 1, ysFloor, zsFloor, dx1, dy1, dz1);

                // Contribution (0,1,0)
                var dx2 = dx0 - zero - SquishConstant_3D;
                var dy2 = dy0 - one - SquishConstant_3D;
                var dz2 = dz1;
                value += Gradient.Get(seed, xsFloor, ysFloor + 1, zsFloor, dx2, dy2, dz2);

                // Contribution (0,0,1)
                var dx3 = dx2;
                var dy3 = dy1;
                var dz3 = dz0 - one - SquishConstant_3D;
                value += Gradient.Get(seed, xsFloor, ysFloor, zsFloor + 1, dx3, dy3, dz3);
            }
            // Check if we're inside the tetrahedron (3-Simplex) at (1,1,1)
            else if (fracSum >= two)
            {
                // Contribution (1,1,0)
                var dx3 = dx0 - one - two*SquishConstant_3D;
                var dy3 = dy0 - one - two*SquishConstant_3D;
                var dz3 = dz0 - zero - two*SquishConstant_3D;
                value += Gradient.Get(seed, xsFloor + 1, ysFloor + 1, zsFloor, dx3, dy3, dz3);

                // Contribution (1,0,1)
                var dx2 = dx3;
                var dy2 = dy0 - zero - two*SquishConstant_3D;
                var dz2 = dz0 - one - two*SquishConstant_3D;
                value += Gradient.Get(seed, xsFloor + 1, ysFloor, zsFloor + 1, dx2, dy2, dz2);

                // Contribution (0,1,1)
                var dx1 = dx0 - zero - two*SquishConstant_3D;
                var dy1 = dy3;
                var dz1 = dz2;
                value += Gradient.Get(seed, xsFloor, ysFloor + 1, zsFloor + 1, dx1, dy1, dz1);

                // Contribution (1,1,1)
                dx0 = dx0 - one - three*SquishConstant_3D;
                dy0 = dy0 - one - three*SquishConstant_3D;
                dz0 = dz0 - one - three*SquishConstant_3D;
                value += Gradient.Get(seed, xsFloor + 1, ysFloor + 1, zsFloor + 1, dx0, dy0, dz0);
            }
            // Check if we're inside the octahedron (Rectified 3-Simplex) in between.
            else
            {
                // Contribution (1,0,0)
                var dx1 = dx0 - one - SquishConstant_3D;
                var dy1 = dy0 - zero - SquishConstant_3D;
                var dz1 = dz0 - zero - SquishConstant_3D;
                value += Gradient.Get(seed, xsFloor + 1, ysFloor, zsFloor, dx1, dy1, dz1);

                // Contribution (0,1,0)
                var dx2 = dx0 - zero - SquishConstant_3D;
                var dy2 = dy0 - one - SquishConstant_3D;
                var dz2 = dz1;
                value += Gradient.Get(seed, xsFloor, ysFloor + 1, zsFloor, dx2, dy2, dz2);

                // Contribution (0,0,1)
                var dx3 = dx2;
                var dy3 = dy1;
                var dz3 = dz0 - one - SquishConstant_3D;
                value += Gradient.Get(seed, xsFloor, ysFloor, zsFloor + 1, dx3, dy3, dz3);

                // Contribution (1,1,0)
                var dx4 = dx0 - one - two*SquishConstant_3D;
                var dy4 = dy0 - one - two*SquishConstant_3D;
                var dz4 = dz0 - zero - two*SquishConstant_3D;
                value += Gradient.Get(seed, xsFloor + 1, ysFloor + 1, zsFloor, dx4, dy4, dz4);

                // Contribution (1,0,1)
                var dx5 = dx4;
                var dy5 = dy0 - zero - two*SquishConstant_3D;
                var dz5 = dz0 - one - two*SquishConstant_3D;
                value += Gradient.Get(seed, xsFloor + 1, ysFloor, zsFloor + 1, dx5, dy5, dz5);

                // Contribution (0,1,1)
                var dx6 = dx0 - zero - two*SquishConstant_3D;
                var dy6 = dy4;
                var dz6 = dz5;
                value += Gradient.Get(seed, xsFloor, ysFloor + 1, zsFloor + 1, dx6, dy6, dz6);
            }

            return value/NormConstant_3D;
        }
コード例 #11
0
ファイル: OpenSimplex.cs プロジェクト: dvdbrink/Procedural
        private const double StretchConstant_4D = -0.138196601125011; //(1/Math.Sqrt(4+1)-1)/4;

        #endregion Fields

        #region Methods

        public static double Get(Seed seed, double x)
        {
            throw new NotImplementedException();
        }
コード例 #12
0
ファイル: VoxelTerrain.cs プロジェクト: dvdbrink/Procedural
 private void InitializeChunkGenerators(uint chunkGeneratorCount)
 {
     _chunkGeneratorCount = chunkGeneratorCount;
     _nextChunkGeneratorIndex = 0;
     _chunkGenerators = new IChunkGenerator[_chunkGeneratorCount];
     var seed = new Seed(CryptoRand.NextInt64());
     for (var i = 0; i < _chunkGeneratorCount; ++i)
     {
         _chunkGenerators[i] = _chunkFactory.CreateChunkGenerator(seed);
     }
 }
コード例 #13
0
ファイル: Perlin.cs プロジェクト: dvdbrink/Procedural
        public static double Get(Seed seed, double x, double y, double z)
        {
            // Find unit grid cell containing point
            var X = FastFloor(x);
            var Y = FastFloor(y);
            var Z = FastFloor(z);

            // Get relative xyz coordinates of point within that cell
            x -= X;
            y -= Y;
            z -= Z;

            // Wrap the integer cells at 255 (smaller integer period can be introduced here)
            X &= 255;
            Y &= 255;
            Z &= 255;

            // Calculate a set of eight hashed gradient indices
            var gi000 = p[X + p[Y + p[Z]]]%12;
            var gi001 = p[X + p[Y + p[Z + 1]]]%12;
            var gi010 = p[X + p[Y + 1 + p[Z]]]%12;
            var gi011 = p[X + p[Y + 1 + p[Z + 1]]]%12;
            var gi100 = p[X + 1 + p[Y + p[Z]]]%12;
            var gi101 = p[X + 1 + p[Y + p[Z + 1]]]%12;
            var gi110 = p[X + 1 + p[Y + 1 + p[Z]]]%12;
            var gi111 = p[X + 1 + p[Y + 1 + p[Z + 1]]]%12;

            // The gradients of each corner are now:
            // g000 = grad3[gi000];
            // g001 = grad3[gi001];
            // g010 = grad3[gi010];
            // g011 = grad3[gi011];
            // g100 = grad3[gi100];
            // g101 = grad3[gi101];
            // g110 = grad3[gi110];
            // g111 = grad3[gi111];

            // Calculate noise contributions from each of the eight corners
            var n000 = Dot(Grad3[gi000], x, y, z);
            var n100 = Dot(Grad3[gi100], x - 1, y, z);
            var n010 = Dot(Grad3[gi010], x, y - 1, z);
            var n110 = Dot(Grad3[gi110], x - 1, y - 1, z);
            var n001 = Dot(Grad3[gi001], x, y, z - 1);
            var n101 = Dot(Grad3[gi101], x - 1, y, z - 1);
            var n011 = Dot(Grad3[gi011], x, y - 1, z - 1);
            var n111 = Dot(Grad3[gi111], x - 1, y - 1, z - 1);

            // Compute the fade curve value for each of x, y, z
            var u = Fade(x);
            var v = Fade(y);
            var w = Fade(z);

            // Interpolate along x the contributions from each of the corners
            var nx00 = Lerp(n000, n100, u);
            var nx01 = Lerp(n001, n101, u);
            var nx10 = Lerp(n010, n110, u);
            var nx11 = Lerp(n011, n111, u);

            // Interpolate the four results along y
            var nxy0 = Lerp(nx00, nx10, v);
            var nxy1 = Lerp(nx01, nx11, v);

            // Interpolate the two last results along z
            var nxyz = Lerp(nxy0, nxy1, w);

            return nxyz;
        }