示例#1
0
        public static NoiseSample Perlin1D(Vector3 point, float frequency)
        {
            point *= frequency;
            int   i0 = Mathf.FloorToInt(point.x);
            float t0 = point.x - i0;
            float t1 = t0 - 1f;

            i0 &= hashMask;
            int i1 = i0 + 1;

            float g0 = gradients1D[hash[i0] & gradientsMask1D];
            float g1 = gradients1D[hash[i1] & gradientsMask1D];

            float v0 = g0 * t0;
            float v1 = g1 * t1;
            float dt = SmoothDerivative(t0);
            float t  = Smooth(t0);

            float a = v0;
            float b = v1 - v0;

            float da = g0;
            float db = g1 - g0;

            NoiseSample result = new NoiseSample();

            result.value = a + b * t;

            result.derivative   = Vector3.zero;
            result.derivative.x = da + db * t + b * dt;
            result.derivative  *= frequency;

            return(result * 2f);
        }
示例#2
0
        public static NoiseSample Value1D(Vector3 point, float frequency)
        {
            point *= frequency;
            int   i0 = Mathf.FloorToInt(point.x);
            float t  = point.x - i0;

            i0 &= hashMask;
            int i1 = i0 + 1;

            int h0 = hash[i0];
            int h1 = hash[i1];

            float dt = SmoothDerivative(t);

            t = Smooth(t);

            float a = h0;
            float b = h1 - h0;

            NoiseSample result = new NoiseSample();

            result.value = a + b * t;

            result.derivative   = Vector3.zero;
            result.derivative.x = b * dt;
            result.derivative  *= frequency;

            return(result * (2f / hashMask) - 1f);
        }
示例#3
0
        public static NoiseSample Simplex1D(Vector3 point, float frequency)
        {
            point *= frequency;
            int         ix     = Mathf.FloorToInt(point.x);
            NoiseSample sample = Simplex1DPart(point, ix);

            sample            += (Simplex1DPart(point, ix + 1));
            sample.derivative *= frequency;
            return(sample * (64f / 27f));
        }
示例#4
0
        public static NoiseSample Perlin2D(Vector3 point, float frequency)
        {
            point *= frequency;
            int   ix0 = Mathf.FloorToInt(point.x);
            int   iy0 = Mathf.FloorToInt(point.y);
            float tx0 = point.x - ix0;
            float ty0 = point.y - iy0;
            float tx1 = tx0 - 1f;
            float ty1 = ty0 - 1f;

            ix0 &= hashMask;
            iy0 &= hashMask;
            int ix1 = ix0 + 1;
            int iy1 = iy0 + 1;

            int     h0  = hash[ix0];
            int     h1  = hash[ix1];
            Vector2 g00 = gradients2D[hash[h0 + iy0] & gradientsMask2D];
            Vector2 g10 = gradients2D[hash[h1 + iy0] & gradientsMask2D];
            Vector2 g01 = gradients2D[hash[h0 + iy1] & gradientsMask2D];
            Vector2 g11 = gradients2D[hash[h1 + iy1] & gradientsMask2D];

            float v00 = Dot(g00, tx0, ty0);
            float v10 = Dot(g10, tx1, ty0);
            float v01 = Dot(g01, tx0, ty1);
            float v11 = Dot(g11, tx1, ty1);

            float dtx = SmoothDerivative(tx0);
            float dty = SmoothDerivative(ty0);
            float tx  = Smooth(tx0);
            float ty  = Smooth(ty0);

            float a = v00;
            float b = v10 - v00;
            float c = v01 - v00;
            float d = v11 - v01 - v10 + v00;

            Vector2 da = g00;
            Vector2 db = g10 - g00;
            Vector2 dc = g01 - g00;
            Vector2 dd = g11 - g01 - g10 + g00;

            NoiseSample result = new NoiseSample();

            result.value         = a + b * tx + (c + d * tx) * ty;
            result.derivative    = da + db * tx + (dc + dd * tx) * ty;
            result.derivative.x += (b + d * ty) * dtx;
            result.derivative.y += (c + d * tx) * dty;
            result.derivative.z  = 0;
            result.derivative   *= frequency;

            return(result * sqr2);
        }
示例#5
0
        private static NoiseSample SimplexValue1DPart(Vector3 point, int ix)
        {
            float       x      = point.x - ix;
            float       f      = 1f - x * x;
            float       f2     = f * f;
            float       f3     = f * f2;
            float       h      = hash[ix & hashMask];
            NoiseSample sample = new NoiseSample();

            sample.value        = h * f3;
            sample.derivative.x = -6f * h * x * f2;
            return(sample);
        }
示例#6
0
        private static NoiseSample Simplex1DPart(Vector3 point, int ix)
        {
            float       x      = point.x - ix;
            float       f      = 1f - x * x;
            float       f2     = f * f;
            float       f3     = f * f2;
            float       g      = gradients1D[hash[ix & hashMask] & gradientsMask1D];
            float       v      = g * x;
            NoiseSample sample = new NoiseSample();

            sample.value        = v * f3;
            sample.derivative.x = g * f3 - 6f * v * x * f2;
            return(sample);
        }
示例#7
0
        public static NoiseSample Sum(NoiseMethod method, Vector3 point, float frequency, int octaves, float lacunarity, float persistance)
        {
            NoiseSample sum       = method(point, frequency);
            float       amplitude = 1f;
            float       range     = 1f;

            for (int o = 1; o < octaves; o++)
            {
                frequency *= lacunarity;
                amplitude *= persistance;
                range     += amplitude;
                sum       += method(point, frequency) * amplitude;
            }
            return(sum * (1f / range));
        }
示例#8
0
        public static NoiseSample Value2D(Vector3 point, float frequency)
        {
            point *= frequency;
            int   ix0 = Mathf.FloorToInt(point.x);
            int   iy0 = Mathf.FloorToInt(point.y);
            float tx  = point.x - ix0;
            float ty  = point.y - iy0;

            ix0 &= hashMask;
            iy0 &= hashMask;
            int ix1 = ix0 + 1;
            int iy1 = iy0 + 1;

            int h0  = hash[ix0];
            int h1  = hash[ix1];
            int h00 = hash[h0 + iy0];
            int h10 = hash[h1 + iy0];
            int h01 = hash[h0 + iy1];
            int h11 = hash[h1 + iy1];

            float dtx = SmoothDerivative(tx);
            float dty = SmoothDerivative(ty);

            tx = Smooth(tx);
            ty = Smooth(ty);

            float a = h00;
            float b = h10 - h00;
            float c = h01 - h00;
            float d = h11 - h01 - h10 + h00;

            NoiseSample result = new NoiseSample();

            result.value = a + b * tx + (c + d * tx) * ty;

            result.derivative   = Vector3.zero;
            result.derivative.x = (b + d * ty) * dtx;
            result.derivative.y = (c + d * tx) * dty;
            result.derivative  *= frequency;

            return(result * (2f / hashMask) - 1f);
        }
示例#9
0
        private static NoiseSample SimplexValue2DPart(Vector3 point, int ix, int iy)
        {
            float       unskew = (ix + iy) * squaresToTriangles;
            float       x      = point.x - ix + unskew;
            float       y      = point.y - iy + unskew;
            float       f      = .5f - x * x - y * y;
            NoiseSample sample = new NoiseSample();

            if (f > 0f)
            {
                float f2   = f * f;
                float f3   = f * f2;
                float h    = hash[hash[ix & hashMask] + iy & hashMask];
                float h6f2 = -6f * h * f2;
                sample.value        = f3 * h;
                sample.derivative.x = h6f2 * x;
                sample.derivative.y = h6f2 * y;
            }
            return(sample);
        }
示例#10
0
        private static NoiseSample Simplex2DPart(Vector3 point, int ix, int iy)
        {
            float       unskew = (ix + iy) * squaresToTriangles;
            float       x      = point.x - ix + unskew;
            float       y      = point.y - iy + unskew;
            float       f      = .5f - x * x - y * y;
            NoiseSample sample = new NoiseSample();

            if (f > 0f)
            {
                float   f2   = f * f;
                float   f3   = f * f2;
                Vector2 g    = gradients2D[hash[hash[ix & hashMask] + iy & hashMask] & gradientsMask2D];
                float   v    = Dot(g, x, y);
                float   v6f2 = -6f * v * f2;
                sample.value        = f3 * v;
                sample.derivative.x = g.x * f3 + v6f2 * x;
                sample.derivative.y = g.y * f3 + v6f2 * y;
            }
            return(sample);
        }
示例#11
0
        public static NoiseSample Simplex2D(Vector3 point, float frequency)
        {
            point *= frequency;
            float       skew   = (point.x + point.y) * trianglesToSquares;
            float       sx     = point.x + skew;
            float       sy     = point.y + skew;
            int         ix     = Mathf.FloorToInt(sx);
            int         iy     = Mathf.FloorToInt(sy);
            NoiseSample sample = Simplex2DPart(point, ix, iy);

            sample += Simplex2DPart(point, ix + 1, iy + 1);
            if (sx - ix >= sy - iy)
            {
                sample += Simplex2DPart(point, ix + 1, iy);
            }
            else
            {
                sample += Simplex2DPart(point, ix, iy + 1);
            }
            sample.derivative *= frequency;
            return(sample * simplexScale2D);
        }
示例#12
0
        private static NoiseSample SimplexValue3DPart(Vector3 point, int ix, int iy, int iz)
        {
            float       unskew = (ix + iy + iz) * (1f / 6f);
            float       x      = point.x - ix + unskew;
            float       y      = point.y - iy + unskew;
            float       z      = point.z - iz + unskew;
            float       f      = 0.5f - x * x - y * y - z * z;
            NoiseSample sample = new NoiseSample();

            if (f > 0f)
            {
                float f2   = f * f;
                float f3   = f * f2;
                float h    = hash[hash[hash[ix & hashMask] + iy & hashMask] + iz & hashMask];
                float h6f2 = -6f * h * f2;
                sample.value        = f3 * h;
                sample.derivative.x = h6f2 * x;
                sample.derivative.y = h6f2 * y;
                sample.derivative.z = h6f2 * z;
            }
            return(sample);
        }
示例#13
0
        private static NoiseSample Simplex3DPart(Vector3 point, int ix, int iy, int iz)
        {
            float       unskew = (ix + iy + iz) * (1f / 6f);
            float       x      = point.x - ix + unskew;
            float       y      = point.y - iy + unskew;
            float       z      = point.z - iz + unskew;
            float       f      = 0.5f - x * x - y * y - z * z;
            NoiseSample sample = new NoiseSample();

            if (f > 0f)
            {
                float   f2   = f * f;
                float   f3   = f * f2;
                Vector3 g    = simplexGradients3D[hash[hash[hash[ix & hashMask] + iy & hashMask] + iz & hashMask] & simplexGradientsMask3D];
                float   v    = Dot(g, x, y, z);
                float   v6f2 = -6f * v * f2;
                sample.value        = f3 * v;
                sample.derivative.x = g.x * f3 + v6f2 * x;
                sample.derivative.y = g.y * f3 + v6f2 * y;
                sample.derivative.z = g.z * f3 + v6f2 * z;
            }
            return(sample);
        }
示例#14
0
        public static NoiseSample Simplex3D(Vector3 point, float frequency)
        {
            point *= frequency;
            float       skew   = (point.x + point.y + point.z) * (1f / 3f);
            float       sx     = point.x + skew;
            float       sy     = point.y + skew;
            float       sz     = point.z + skew;
            int         ix     = Mathf.FloorToInt(sx);
            int         iy     = Mathf.FloorToInt(sy);
            int         iz     = Mathf.FloorToInt(sz);
            NoiseSample sample = Simplex3DPart(point, ix, iy, iz);

            sample += Simplex3DPart(point, ix + 1, iy + 1, iz + 1);

            float x = sx - ix;
            float y = sy - iy;
            float z = sz - iz;

            if (x >= y)
            {
                if (x >= z)
                {
                    sample += Simplex3DPart(point, ix + 1, iy, iz);
                    if (y >= z)
                    {
                        sample += Simplex3DPart(point, ix + 1, iy + 1, iz);
                    }
                    else
                    {
                        sample += Simplex3DPart(point, ix + 1, iy, iz + 1);
                    }
                }
                else
                {
                    sample += Simplex3DPart(point, ix, iy, iz + 1);
                    sample += Simplex3DPart(point, ix + 1, iy, iz + 1);
                }
            }
            else
            {
                if (y >= z)
                {
                    sample += Simplex3DPart(point, ix, iy + 1, iz);
                    if (x >= z)
                    {
                        sample += Simplex3DPart(point, ix + 1, iy + 1, iz);
                    }
                    else
                    {
                        sample += Simplex3DPart(point, ix, iy + 1, iz + 1);
                    }
                }
                else
                {
                    sample += Simplex3DPart(point, ix, iy, iz + 1);
                    sample += Simplex3DPart(point, ix, iy + 1, iz + 1);
                }
            }

            sample.derivative *= frequency;

            return(sample * simplexScale3D);
        }
示例#15
0
        public static NoiseSample Perlin3D(Vector3 point, float frequency)
        {
            point *= frequency;
            int   ix0 = Mathf.FloorToInt(point.x);
            int   iy0 = Mathf.FloorToInt(point.y);
            int   iz0 = Mathf.FloorToInt(point.z);
            float tx0 = point.x - ix0;
            float ty0 = point.y - iy0;
            float tz0 = point.z - iz0;
            float tx1 = tx0 - 1f;
            float ty1 = ty0 - 1f;
            float tz1 = tz0 - 1f;

            ix0 &= hashMask;
            iy0 &= hashMask;
            iz0 &= hashMask;
            int ix1 = ix0 + 1;
            int iy1 = iy0 + 1;
            int iz1 = iz0 + 1;

            int     h0   = hash[ix0];
            int     h1   = hash[ix1];
            int     h00  = hash[h0 + iy0];
            int     h10  = hash[h1 + iy0];
            int     h01  = hash[h0 + iy1];
            int     h11  = hash[h1 + iy1];
            Vector3 g000 = gradients3D[hash[h00 + iz0] & gradientsMask3D];
            Vector3 g001 = gradients3D[hash[h00 + iz1] & gradientsMask3D];
            Vector3 g010 = gradients3D[hash[h01 + iz0] & gradientsMask3D];
            Vector3 g011 = gradients3D[hash[h01 + iz1] & gradientsMask3D];
            Vector3 g100 = gradients3D[hash[h10 + iz0] & gradientsMask3D];
            Vector3 g101 = gradients3D[hash[h10 + iz1] & gradientsMask3D];
            Vector3 g110 = gradients3D[hash[h11 + iz0] & gradientsMask3D];
            Vector3 g111 = gradients3D[hash[h11 + iz1] & gradientsMask3D];

            float v000 = Dot(g000, tx0, ty0, tz0);
            float v001 = Dot(g001, tx0, ty0, tz1);
            float v010 = Dot(g010, tx0, ty1, tz0);
            float v011 = Dot(g011, tx0, ty1, tz1);
            float v100 = Dot(g100, tx1, ty0, tz0);
            float v101 = Dot(g101, tx1, ty0, tz1);
            float v110 = Dot(g110, tx1, ty1, tz0);
            float v111 = Dot(g111, tx1, ty1, tz1);

            float dtx = SmoothDerivative(tx0);
            float dty = SmoothDerivative(ty0);
            float dtz = SmoothDerivative(tz0);
            float tx  = Smooth(tx0);
            float ty  = Smooth(ty0);
            float tz  = Smooth(tz0);

            float a = v000;
            float b = v100 - v000;
            float c = v010 - v000;
            float d = v001 - v000;
            float e = v110 - v010 - v100 + v000;
            float f = v101 - v001 - v010 + v000;
            float g = v011 - v001 - v010 + v000;
            float h = v111 - v011 - v101 + v001 - v001 - v110 + v010 + v100 - v000;

            Vector3 da = g000;
            Vector3 db = g100 - g000;
            Vector3 dc = g010 - g000;
            Vector3 dd = g001 - g000;
            Vector3 de = g110 - g010 - g100 + g000;
            Vector3 df = g101 - g001 - g100 + g000;
            Vector3 dg = g011 - g001 - g010 + g000;
            Vector3 dh = g111 - g011 - g001 + g001 - g110 + g010 + g100 - g000;

            NoiseSample result = new NoiseSample();

            result.value = a + b * tx + (c + e * tx) * ty + (d + f * tx + (g + h * tx) * ty) * tz;

            result.derivative    = da + db * tx + (dc + de * tx) * ty + (dd + df * tx + (dg + dh * tx) * ty) * tz;
            result.derivative.x += (b + e * ty + (f + h * ty) * tz) * dtx;
            result.derivative.y += (c + e * tx + (g + h * tx) * tz) * dty;
            result.derivative.z += (d + f * tx + (g + h * tx) * ty) * dtz;
            result.derivative   *= frequency;

            return(result);
        }
示例#16
0
        public static NoiseSample Value3D(Vector3 point, float frequency)
        {
            point *= frequency;
            int   ix0 = Mathf.FloorToInt(point.x);
            int   iy0 = Mathf.FloorToInt(point.y);
            int   iz0 = Mathf.FloorToInt(point.z);
            float tx  = point.x - ix0;
            float ty  = point.y - iy0;
            float tz  = point.z - iz0;

            ix0 &= hashMask;
            iy0 &= hashMask;
            iz0 &= hashMask;
            int ix1 = ix0 + 1;
            int iy1 = iy0 + 1;
            int iz1 = iz0 + 1;

            int h0   = hash[ix0];
            int h1   = hash[ix1];
            int h00  = hash[h0 + iy0];
            int h10  = hash[h1 + iy0];
            int h01  = hash[h0 + iy1];
            int h11  = hash[h1 + iy1];
            int h000 = hash[h00 + iz0];
            int h001 = hash[h00 + iz1];
            int h010 = hash[h01 + iz0];
            int h011 = hash[h01 + iz1];
            int h100 = hash[h10 + iz0];
            int h101 = hash[h10 + iz1];
            int h110 = hash[h11 + iz0];
            int h111 = hash[h11 + iz1];

            float dtx = SmoothDerivative(tx);
            float dty = SmoothDerivative(ty);
            float dtz = SmoothDerivative(tz);

            tx = Smooth(tx);
            ty = Smooth(ty);
            tz = Smooth(tz);

            float a = h000;
            float b = h100 - h000;
            float c = h010 - h000;
            float d = h001 - h000;
            float e = h110 - h010 - h100 + h000;
            float f = h101 - h001 - h100 + h000;
            float g = h011 - h001 - h010 + h000;
            float h = h111 - h011 - h101 + h001 - h110 + h010 + h100 - h000;

            NoiseSample result = new NoiseSample();

            result.value = a + b * tx + (c + e * tx) * ty + (d + f * tx + (g + h * tx) * ty) * tz;

            result.derivative   = Vector3.zero;
            result.derivative.x = (b + e * ty + (f + h * ty) * tz) * dtx;
            result.derivative.y = (c + e * tx + (g + h * tx) * tz) * dty;
            result.derivative.z = (d + f * tx + (g + h * tx) * ty) * dtz;
            result.derivative  *= frequency;

            return(result * (2f / hashMask) - 1f);
        }