pocketmine\level\generator\noise\Simplex::getNoise3D PHP Method

getNoise3D() public method

public getNoise3D ( $x, $y, $z )
    public function getNoise3D($x, $y, $z)
    {
        $x += $this->offsetX;
        $y += $this->offsetY;
        $z += $this->offsetZ;
        // Skew the input space to determine which simplex cell we're in
        $s = ($x + $y + $z) * self::$F3;
        // Very nice and simple skew factor for 3D
        $i = (int) ($x + $s);
        $j = (int) ($y + $s);
        $k = (int) ($z + $s);
        $t = ($i + $j + $k) * self::$G3;
        // Unskew the cell origin back to (x,y,z) space
        $x0 = $x - ($i - $t);
        // The x,y,z distances from the cell origin
        $y0 = $y - ($j - $t);
        $z0 = $z - ($k - $t);
        // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
        // Determine which simplex we are in.
        if ($x0 >= $y0) {
            if ($y0 >= $z0) {
                $i1 = 1;
                $j1 = 0;
                $k1 = 0;
                $i2 = 1;
                $j2 = 1;
                $k2 = 0;
            } elseif ($x0 >= $z0) {
                $i1 = 1;
                $j1 = 0;
                $k1 = 0;
                $i2 = 1;
                $j2 = 0;
                $k2 = 1;
            } else {
                $i1 = 0;
                $j1 = 0;
                $k1 = 1;
                $i2 = 1;
                $j2 = 0;
                $k2 = 1;
            }
            // Z X Y order
        } else {
            // x0<y0
            if ($y0 < $z0) {
                $i1 = 0;
                $j1 = 0;
                $k1 = 1;
                $i2 = 0;
                $j2 = 1;
                $k2 = 1;
            } elseif ($x0 < $z0) {
                $i1 = 0;
                $j1 = 1;
                $k1 = 0;
                $i2 = 0;
                $j2 = 1;
                $k2 = 1;
            } else {
                $i1 = 0;
                $j1 = 1;
                $k1 = 0;
                $i2 = 1;
                $j2 = 1;
                $k2 = 0;
            }
            // Y X Z order
        }
        // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
        // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
        // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
        // c = 1/6.
        $x1 = $x0 - $i1 + self::$G3;
        // Offsets for second corner in (x,y,z) coords
        $y1 = $y0 - $j1 + self::$G3;
        $z1 = $z0 - $k1 + self::$G3;
        $x2 = $x0 - $i2 + 2.0 * self::$G3;
        // Offsets for third corner in (x,y,z) coords
        $y2 = $y0 - $j2 + 2.0 * self::$G3;
        $z2 = $z0 - $k2 + 2.0 * self::$G3;
        $x3 = $x0 - 1.0 + 3.0 * self::$G3;
        // Offsets for last corner in (x,y,z) coords
        $y3 = $y0 - 1.0 + 3.0 * self::$G3;
        $z3 = $z0 - 1.0 + 3.0 * self::$G3;
        // Work out the hashed gradient indices of the four simplex corners
        $ii = $i & 255;
        $jj = $j & 255;
        $kk = $k & 255;
        $n = 0;
        // Calculate the contribution from the four corners
        $t0 = 0.6 - $x0 * $x0 - $y0 * $y0 - $z0 * $z0;
        if ($t0 > 0) {
            $gi0 = self::$grad3[$this->perm[$ii + $this->perm[$jj + $this->perm[$kk]]] % 12];
            $n += $t0 * $t0 * $t0 * $t0 * ($gi0[0] * $x0 + $gi0[1] * $y0 + $gi0[2] * $z0);
        }
        $t1 = 0.6 - $x1 * $x1 - $y1 * $y1 - $z1 * $z1;
        if ($t1 > 0) {
            $gi1 = self::$grad3[$this->perm[$ii + $i1 + $this->perm[$jj + $j1 + $this->perm[$kk + $k1]]] % 12];
            $n += $t1 * $t1 * $t1 * $t1 * ($gi1[0] * $x1 + $gi1[1] * $y1 + $gi1[2] * $z1);
        }
        $t2 = 0.6 - $x2 * $x2 - $y2 * $y2 - $z2 * $z2;
        if ($t2 > 0) {
            $gi2 = self::$grad3[$this->perm[$ii + $i2 + $this->perm[$jj + $j2 + $this->perm[$kk + $k2]]] % 12];
            $n += $t2 * $t2 * $t2 * $t2 * ($gi2[0] * $x2 + $gi2[1] * $y2 + $gi2[2] * $z2);
        }
        $t3 = 0.6 - $x3 * $x3 - $y3 * $y3 - $z3 * $z3;
        if ($t3 > 0) {
            $gi3 = self::$grad3[$this->perm[$ii + 1 + $this->perm[$jj + 1 + $this->perm[$kk + 1]]] % 12];
            $n += $t3 * $t3 * $t3 * $t3 * ($gi3[0] * $x3 + $gi3[1] * $y3 + $gi3[2] * $z3);
        }
        // Add contributions from each corner to get the noise value.
        // The result is scaled to stay just inside [-1,1]
        return 32.0 * $n;
    }