PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
Perlin.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\level\generator\noise;
23 
25 
26 class Perlin extends Generator{
27  public static $grad3 = [
28  [1, 1, 0], [-1, 1, 0], [1, -1, 0], [-1, -1, 0],
29  [1, 0, 1], [-1, 0, 1], [1, 0, -1], [-1, 0, -1],
30  [0, 1, 1], [0, -1, 1], [0, 1, -1], [0, -1, -1]
31  ];
32 
33 
34  public function __construct(Random $random, $octaves, $frequency, $amplitude){
35  $this->octaves = $octaves;
36  $this->frequency = $frequency;
37  $this->amplitude = $amplitude;
38  $this->offsetX = $random->nextFloat() * 256;
39  $this->offsetY = $random->nextFloat() * 256;
40  $this->offsetZ = $random->nextFloat() * 256;
41 
42  for($i = 0; $i < 512; ++$i){
43  $this->perm[$i] = 0;
44  }
45 
46  for($i = 0; $i < 256; ++$i){
47  $this->perm[$i] = $random->nextRange(0, 255);
48  }
49 
50  for($i = 0; $i < 256; ++$i){
51  $pos = $random->nextRange(0, 255 - $i) + $i;
52  $old = $this->perm[$i];
53 
54  $this->perm[$i] = $this->perm[$pos];
55  $this->perm[$pos] = $old;
56  $this->perm[$i + 256] = $this->perm[$i];
57  }
58 
59  }
60 
61  public function getNoise3D($x, $y, $z){
62  $x += $this->offsetX;
63  $y += $this->offsetY;
64  $z += $this->offsetZ;
65 
66  $floorX = (int) floor($x);
67  $floorY = (int) floor($y);
68  $floorZ = (int) floor($z);
69 
70  $X = $floorX & 0xFF;
71  $Y = $floorY & 0xFF;
72  $Z = $floorZ & 0xFF;
73 
74  $x -= $floorX;
75  $y -= $floorY;
76  $z -= $floorZ;
77 
78  //Fade curves
79  $fX = self::fade($x);
80  $fY = self::fade($y);
81  $fZ = self::fade($z);
82 
83  //Cube corners
84  $A = $this->perm[$X] + $Y;
85  $AA = $this->perm[$A] + $Z;
86  $AB = $this->perm[$A + 1] + $Z;
87  $B = $this->perm[$X + 1] + $Y;
88  $BA = $this->perm[$B] + $Z;
89  $BB = $this->perm[$B + 1] + $Z;
90 
91  return self::lerp($fZ, self::lerp($fY, self::lerp($fX, self::grad($this->perm[$AA], $x, $y, $z),
92  self::grad($this->perm[$BA], $x - 1, $y, $z)),
93  self::lerp($fX, self::grad($this->perm[$AB], $x, $y - 1, $z),
94  self::grad($this->perm[$BB], $x - 1, $y - 1, $z))),
95  self::lerp($fY, self::lerp($fX, self::grad($this->perm[$AA + 1], $x, $y, $z - 1),
96  self::grad($this->perm[$BA + 1], $x - 1, $y, $z - 1)),
97  self::lerp($fX, self::grad($this->perm[$AB + 1], $x, $y - 1, $z - 1),
98  self::grad($this->perm[$BB + 1], $x - 1, $y - 1, $z - 1))));
99  }
100 
101  public function getNoise2D($x, $y){
102  return $this->getNoise3D($x, $y, 0);
103  }
104 }
nextRange($start=0, $end=0x7fffffff)
Definition: Random.php:112