예제 #1
0
파일: ExpNoise.cs 프로젝트: tinydeltas/salt
        public float noise(Vector2 p)
        {
            // get shared & individual constants

            // get int base
            int x0 = Mathf.FloorToInt(p.x);
            int y0 = Mathf.FloorToInt(p.y);

            // get displacements
            float dx0 = p.x - x0;
            float dy0 = p.y - y0;

            float dx1 = dx0 - 1f;
            float dy1 = dy0 - 1f;

            x0 &= m;
            y0 &= m;

            // Step 3 & 4: first get p00, p01, p10, and p11
            int x1 = x0 + 1;
            int y1 = y0 + 1;

            int _0 = h [x0];
            int _1 = h [x1];

            int _00 = h [_0 + y0];          // h(p00)
            int _01 = h [_0 + y1];          // h(p01)
            int _10 = h [_1 + y0];          // h(p10)
            int _11 = h [_1 + y1];          // h(p11)

            dx0 = M.Fade(dx0);              // must be in [0, 1] range
            dy0 = M.Fade(dy0);

            p_00 [0] = dx0;
            p_00 [1] = dy0;
            p_01 [0] = dx0;
            p_01 [1] = dy1;
            p_10 [0] = dx1;
            p_10 [1] = dy0;
            p_11 [0] = dx1;
            p_11 [1] = dy1;

            // u = h'(p00)p dot h (p00)
            // here, p is (dx0, dy0)
            // and h'(p00) is h'(h(p00))

            float p1 = Mathf.Lerp(M.Dot(g [h [_00] & gm], e [_00] * p_00[0], e[_00] * p_00[1]),
                                  M.Dot(g [h [_10] & gm], e [_10] * p_10[0], e[_10] * p_10[1]), dx0);

            // v = h'(p01)p  * h(p01)

            // Step 6&8
            // u = (h'(p10)p) dot h (p10)
            // v = h'(p11)p dot h (p11)
            float p2 = Mathf.Lerp(M.Dot(g [h [_01] & gm], e [_01] * p_01[0], e[_01] * p_01[1]),
                                  M.Dot(g [h [_11] & gm], e [_11] * p_11[0], e[_11] * p_11[1]), dx0);

            return(Mathf.Lerp(p1, p2, dy0) * n * 0.5f);
        }
예제 #2
0
 private float assignRandom(float offset = 1f)
 {
     if (useGaussian)
     {
         return(M.Gaussian(offset));
     }
     return(Random.Range(0, offset));
 }
예제 #3
0
        // 2D Perlin noise
        public float noise(Vector2 p)
        {
            // get int base
            int x0 = Mathf.FloorToInt(p.x);
            int y0 = Mathf.FloorToInt(p.y);

            // get displacements
            float dx0 = p.x - x0;
            float dy0 = p.y - y0;

            float dx1 = dx0 - 1f;
            float dy1 = dy0 - 1f;

            // do the masking
            x0 &= m;
            y0 &= m;

            // get next ones over
            int x1 = x0 + 1;
            int y1 = y0 + 1;

            // get first-level displacement on x
            int _0 = h [x0];
            int _1 = h [x1];

            int _00 = h [_0 + y0];
            int _01 = h [_0 + y1];
            int _10 = h [_1 + y0];
            int _11 = h [_1 + y1];

            // smooth the displacements
            dx0 = M.Fade(dx0);
            dy0 = M.Fade(dy0);

            // this is where it diverges from value noise: pick a gradient
            int[] _x00 = g [h [_00] & gm];
            int[] _x10 = g [h [_10] & gm];
            float x00  = M.Dot(_x00, dx0, dy0);                 // u = p dot h[p_00]
            float x10  = M.Dot(_x10, dx1, dy0);                 // v = p dot h[_10]

            float p1 = Mathf.Lerp(x00, x10, dx0);

            int[] _x01 = g [h [_01] & gm];
            int[] _x11 = g [h [_11] & gm];
            float x01  = M.Dot(_x01, dx0, dy1);
            float x11  = M.Dot(_x11, dx1, dy1);

            float p2 = Mathf.Lerp(x01, x11, dx0);

            float t0   = Mathf.Lerp(p1, p2, dy0);
            float prod = t0 * n;

            return(prod * 0.5f);
        }
예제 #4
0
        // 2D noise
        public float noise(Vector2 p)
        {
            // get int version
            int x0 = Mathf.FloorToInt(p.x);
            int y0 = Mathf.FloorToInt(p.y);

            // get starting displacements
            float dx = p.x - x0;
            float dy = p.y - y0;

            x0 &= m;
            y0 &= m;

            // get next ones over
            int x1 = x0 + 1;
            int y1 = y0 + 1;

            // first level hashing on x
            int _0 = h [x0];
            int _1 = h [x1];

            // second level hashing on y
            int _00 = h [_0 + y0];
            int _01 = h [_0 + y1];
            int _10 = h [_1 + y0];
            int _11 = h [_1 + y1];

            // smooth the displacements
            dx = M.Fade(dx);
            dy = M.Fade(dy);

            // interpolate between the relative points on a 2-d plane
            float t1 = Mathf.Lerp(_00, _10, dx);
            float t2 = Mathf.Lerp(_01, _11, dx);
            float z  = Mathf.Lerp(t1, t2, dy);

            // normalize the restult
            float prod = z * (1f / m);

            return(prod - 0.5f);
        }
예제 #5
0
        // 3D noise
        public float _noise(Vector3 p)
        {
            // get constants
            int[] h = Constants.hash;
            int   m = Constants.hashMask;

            int x0 = M.Floor(p.x);
            int y0 = M.Floor(p.y);
            int z0 = M.Floor(p.z);

            // only take the most significant bits of this int
            // maybe not necessary if the side of the square isn't too large?

            // mask tem
            x0 &= m;
            y0 &= m;
            z0 &= m;

            // get the next adjacent point
            int x1 = x0 + 1;
            int y1 = y0 + 1;
            int z1 = z0 + 1;

            // first level hashing on x
            int _0 = h [x0];
            int _1 = h [x1];

            // second level hashing with the appropriate closest coordinate
            int _00 = h [_0 + y0];
            int _10 = h [_1 + y0];
            int _01 = h [_0 + y1];
            int _11 = h [_1 + y1];

            // third level three-dimensional coordinate hashing
            int x000 = h [_00 + z0];
            int x100 = h [_10 + z0];
            int x010 = h [_01 + z0];
            int x110 = h [_11 + z0];
            int x001 = h [_00 + z1];
            int x101 = h [_10 + z1];
            int x011 = h [_01 + z1];
            int x111 = h [_11 + z1];

            // get the smoothed floor displacements
            float dx = M.Fade(p.x - x0);
            float dy = M.Fade(p.y - y0);
            float dz = M.Fade(p.z - z0);

            // Do the interpolation
            float p1 = M.Lerp(x000, x100, dx);
            float p2 = M.Lerp(x010, x110, dx);
            float t0 = M.Lerp(p1, p2, dy);

            float p3 = M.Lerp(x001, x101, dx);
            float p4 = M.Lerp(x011, x111, dx);
            float t1 = M.Lerp(p3, p4, dy);

            float z = M.Lerp(t0, t1, dz);

            // normalize the result
            float prod = z / m;

            return(prod);
        }
예제 #6
0
        // 3D NOISE
        public float _noise(Vector3 p)
        {
            // get constants
            int[] h = Constants.hash;
            int   m = Constants.hashMask;

            int gm = hashMask3D;

            int[][] g = gradients3D;

            // get masked int version
            int x0 = M.Floor(p.x);
            int y0 = M.Floor(p.y);
            int z0 = M.Floor(p.z);

            // get the displacements
            float dx0 = p.x - x0;
            float dy0 = p.y - y0;
            float dz0 = p.z - z0;

            // now do the masking
            x0 &= m;
            y0 &= m;
            z0 &= m;

            // get next ones over
            int x1 = x0 + 1;
            int y1 = y0 + 1;
            int z1 = z0 + 1;

            ;

            // why is this subtracted by 1?
            float dx1 = dx0 - 1f;
            float dy1 = dy0 - 1f;
            float dz1 = dz0 - 1f;

            // first level hashing on x
            int _0 = h [x0];
            int _1 = h [x1];

            // second level hashing on y
            int _00 = h [_0 + y0];
            int _01 = h [_0 + y1];
            int _10 = h [_1 + y0];
            int _11 = h [_1 + y1];

            // third "level" hashing on z
            int[] _000 = g [h [_00 + z0] & gm];
            int[] _001 = g [h [_00 + z1] & gm];
            int[] _010 = g [h [_01 + z0] & gm];
            int[] _011 = g [h [_01 + z1] & gm];
            int[] _100 = g [h [_10 + z0] & gm];
            int[] _101 = g [h [_10 + z1] & gm];
            int[] _110 = g [h [_11 + z0] & gm];
            int[] _111 = g [h [_11 + z1] & gm];

            // do the dot product with displacement vector in order to smooth
            float x000 = M.Dot(_000, dx0, dy0, dz0);
            float x001 = M.Dot(_001, dx0, dy0, dz1);
            float x010 = M.Dot(_010, dx0, dy1, dz0);
            float x011 = M.Dot(_011, dx0, dy1, dz1);
            float x100 = M.Dot(_100, dx1, dy0, dz0);
            float x101 = M.Dot(_101, dx1, dy0, dz1);
            float x110 = M.Dot(_110, dx1, dy1, dz0);
            float x111 = M.Dot(_111, dx1, dy1, dz1);

            // smooth the original displacements
            float dx = M.Fade(dx0);
            float dy = M.Fade(dy0);
            float dz = M.Fade(dz0);

            // Do the interpolation
            float p1 = M.Lerp(x000, x100, dx);
            float p2 = M.Lerp(x010, x110, dx);
            float t0 = M.Lerp(p1, p2, dy);

            float p3 = M.Lerp(x001, x101, dx);
            float p4 = M.Lerp(x011, x111, dx);
            float t1 = M.Lerp(p3, p4, dy);

            float res = M.Lerp(t0, t1, dz);

            return(res);
        }