Esempio n. 1
0
    public static float noise3Perlin(float x, float y, float z) {
      float t = x + 10000.0f;
      int bx0 = (int) t & 0xFF;
      int bx1 = bx0 + 1 & 0xFF;
      float rx0 = t - (int) t;
      float rx1 = rx0 - 1.0f;

      t = y + 10000.0f;
      int by0 = (int) t & 0xFF;
      int by1 = by0 + 1 & 0xFF;
      float ry0 = t - (int) t;
      float ry1 = ry0 - 1.0f;

      t = z + 10000.0f;
      int bz0 = (int) t & 0xFF;
      int bz1 = bz0 + 1 & 0xFF;
      float rz0 = t - (int) t;
      float rz1 = rz0 - 1.0f;

      int i = p[bx0];
      int j = p[bx1];

      int b00 = p[i + by0];
      int b10 = p[j + by0];
      int b01 = p[i + by1];
      int b11 = p[j + by1];

      float sx = NoiseMath.surve(rx0);
      float sy = NoiseMath.surve(ry0);
      float sz = NoiseMath.surve(rz0);

      float[] q = g[b00 + bz0];
      float u = NoiseMath.at(rx0, ry0, rz0, q);
      q = g[b10 + bz0];
      float v = NoiseMath.at(rx1, ry0, rz0, q);
      float a = NoiseMath.lerp(sx, u, v);

      q = g[b01 + bz0];
      u = NoiseMath.at(rx0, ry1, rz0, q);
      q = g[b11 + bz0];
      v = NoiseMath.at(rx1, ry1, rz0, q);
      float b = NoiseMath.lerp(sx, u, v);

      float c = NoiseMath.lerp(sy, a, b);

      q = g[b00 + bz1];
      u = NoiseMath.at(rx0, ry0, rz1, q);
      q = g[b10 + bz1];
      v = NoiseMath.at(rx1, ry0, rz1, q);
      a = NoiseMath.lerp(sx, u, v);

      q = g[b01 + bz1];
      u = NoiseMath.at(rx0, ry1, rz1, q);
      q = g[b11 + bz1];
      v = NoiseMath.at(rx1, ry1, rz1, q);
      b = NoiseMath.lerp(sx, u, v);

      float d = NoiseMath.lerp(sy, a, b);
      return 1.5f * NoiseMath.lerp(sz, c, d);
    }
Esempio n. 2
0
 // instead of adding another permutation array, just use hash table defined above
 public static float newPerlin(float x, float y, float z) {
   int A, AA, AB, B, BA, BB;
   float floorX = (float) Math.floor(x),
       floorY = (float) Math.floor(y),
       floorZ = (float) Math.floor(z);
   int intX = (int) floorX & 0xFF, intY = (int) floorY & 0xFF, intZ = (int) floorZ & 0xFF;
   x -= floorX;
   y -= floorY;
   z -= floorZ;
   // computing fading curves
   floorX = NoiseMath.npfade(x);
   floorY = NoiseMath.npfade(y);
   floorZ = NoiseMath.npfade(z);
   A = hash[intX] + intY;
   AA = hash[A] + intZ;
   AB = hash[A + 1] + intZ;
   B = hash[intX + 1] + intY;
   BA = hash[B] + intZ;
   BB = hash[B + 1] + intZ;
   return NoiseMath.lerp(
       floorZ,
       NoiseMath.lerp(
           floorY,
           NoiseMath.lerp(
               floorX, NoiseMath.grad(hash[AA], x, y, z), NoiseMath.grad(hash[BA], x - 1, y, z)),
           NoiseMath.lerp(
               floorX,
               NoiseMath.grad(hash[AB], x, y - 1, z),
               NoiseMath.grad(hash[BB], x - 1, y - 1, z))),
       NoiseMath.lerp(
           floorY,
           NoiseMath.lerp(
               floorX,
               NoiseMath.grad(hash[AA + 1], x, y, z - 1),
               NoiseMath.grad(hash[BA + 1], x - 1, y, z - 1)),
           NoiseMath.lerp(
               floorX,
               NoiseMath.grad(hash[AB + 1], x, y - 1, z - 1),
               NoiseMath.grad(hash[BB + 1], x - 1, y - 1, z - 1))));
 }