diff --git a/src/pocketmine/level/generator/noise/Perlin.php b/src/pocketmine/level/generator/noise/Perlin.php deleted file mode 100644 index 84b2ab2c2e..0000000000 --- a/src/pocketmine/level/generator/noise/Perlin.php +++ /dev/null @@ -1,150 +0,0 @@ -octaves = $octaves; - $this->persistence = $persistence; - $this->expansion = $expansion; - $this->offsetX = $random->nextFloat() * 256; - $this->offsetY = $random->nextFloat() * 256; - $this->offsetZ = $random->nextFloat() * 256; - - for($i = 0; $i < 512; ++$i){ - $this->perm[$i] = 0; - } - - for($i = 0; $i < 256; ++$i){ - $this->perm[$i] = $random->nextBoundedInt(256); - } - - for($i = 0; $i < 256; ++$i){ - $pos = $random->nextBoundedInt(256 - $i) + $i; - $old = $this->perm[$i]; - - $this->perm[$i] = $this->perm[$pos]; - $this->perm[$pos] = $old; - $this->perm[$i + 256] = $this->perm[$i]; - } - - } - - public function getNoise3D($x, $y, $z){ - $x += $this->offsetX; - $y += $this->offsetY; - $z += $this->offsetZ; - - $floorX = (int) $x; - $floorY = (int) $y; - $floorZ = (int) $z; - - $X = $floorX & 0xFF; - $Y = $floorY & 0xFF; - $Z = $floorZ & 0xFF; - - $x -= $floorX; - $y -= $floorY; - $z -= $floorZ; - - //Fade curves - //$fX = self::fade($x); - //$fY = self::fade($y); - //$fZ = self::fade($z); - $fX = $x * $x * $x * ($x * ($x * 6 - 15) + 10); - $fY = $y * $y * $y * ($y * ($y * 6 - 15) + 10); - $fZ = $z * $z * $z * ($z * ($z * 6 - 15) + 10); - - //Cube corners - $A = $this->perm[$X] + $Y; - $B = $this->perm[$X + 1] + $Y; - - $AA = $this->perm[$A] + $Z; - $AB = $this->perm[$A + 1] + $Z; - $BA = $this->perm[$B] + $Z; - $BB = $this->perm[$B + 1] + $Z; - - $AA1 = self::grad($this->perm[$AA], $x, $y, $z); - $BA1 = self::grad($this->perm[$BA], $x - 1, $y, $z); - $AB1 = self::grad($this->perm[$AB], $x, $y - 1, $z); - $BB1 = self::grad($this->perm[$BB], $x - 1, $y - 1, $z); - $AA2 = self::grad($this->perm[$AA + 1], $x, $y, $z - 1); - $BA2 = self::grad($this->perm[$BA + 1], $x - 1, $y, $z - 1); - $AB2 = self::grad($this->perm[$AB + 1], $x, $y - 1, $z - 1); - $BB2 = self::grad($this->perm[$BB + 1], $x - 1, $y - 1, $z - 1); - - $xLerp11 = $AA1 + $fX * ($BA1 - $AA1); - - $zLerp1 = $xLerp11 + $fY * ($AB1 + $fX * ($BB1 - $AB1) - $xLerp11); - - $xLerp21 = $AA2 + $fX * ($BA2 - $AA2); - - return $zLerp1 + $fZ * ($xLerp21 + $fY * ($AB2 + $fX * ($BB2 - $AB2) - $xLerp21) - $zLerp1); - - /* - return self::lerp( - $fZ, - self::lerp( - $fY, - self::lerp( - $fX, - self::grad($this->perm[$AA], $x, $y, $z), - self::grad($this->perm[$BA], $x - 1, $y, $z) - ), - self::lerp( - $fX, - self::grad($this->perm[$AB], $x, $y - 1, $z), - self::grad($this->perm[$BB], $x - 1, $y - 1, $z) - ) - ), - self::lerp( - $fY, - self::lerp( - $fX, - self::grad($this->perm[$AA + 1], $x, $y, $z - 1), - self::grad($this->perm[$BA + 1], $x - 1, $y, $z - 1) - ), - self::lerp( - $fX, - self::grad($this->perm[$AB + 1], $x, $y - 1, $z - 1), - self::grad($this->perm[$BB + 1], $x - 1, $y - 1, $z - 1) - ) - ) - ); - */ - } - - public function getNoise2D($x, $y){ - return $this->getNoise3D($x, $y, 0); - } -} diff --git a/src/pocketmine/level/generator/noise/Simplex.php b/src/pocketmine/level/generator/noise/Simplex.php index d29546f7e0..3e009f288a 100644 --- a/src/pocketmine/level/generator/noise/Simplex.php +++ b/src/pocketmine/level/generator/noise/Simplex.php @@ -32,7 +32,13 @@ use pocketmine\utils\Random; * Stefan Gustavson at * http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf */ -class Simplex extends Perlin{ +class Simplex extends Noise{ + public static $grad3 = [ + [1, 1, 0], [-1, 1, 0], [1, -1, 0], [-1, -1, 0], + [1, 0, 1], [-1, 0, 1], [1, 0, -1], [-1, 0, -1], + [0, 1, 1], [0, -1, 1], [0, 1, -1], [0, -1, -1] + ]; + protected static $SQRT_3; protected static $SQRT_5; protected static $F2; @@ -66,8 +72,32 @@ class Simplex extends Perlin{ public function __construct(Random $random, $octaves, $persistence, $expansion = 1){ - parent::__construct($random, $octaves, $persistence, $expansion); + $this->octaves = $octaves; + $this->persistence = $persistence; + $this->expansion = $expansion; + $this->offsetX = $random->nextFloat() * 256; + $this->offsetY = $random->nextFloat() * 256; + $this->offsetZ = $random->nextFloat() * 256; $this->offsetW = $random->nextFloat() * 256; + + for($i = 0; $i < 512; ++$i){ + $this->perm[$i] = 0; + } + + for($i = 0; $i < 256; ++$i){ + $this->perm[$i] = $random->nextBoundedInt(256); + } + + for($i = 0; $i < 256; ++$i){ + $pos = $random->nextBoundedInt(256 - $i) + $i; + $old = $this->perm[$i]; + + $this->perm[$i] = $this->perm[$pos]; + $this->perm[$pos] = $old; + $this->perm[$i + 256] = $this->perm[$i]; + } + + self::$SQRT_3 = sqrt(3); self::$SQRT_5 = sqrt(5); self::$F2 = 0.5 * (self::$SQRT_3 - 1);