PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
noise/Generator.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 
28 namespace pocketmine\level\generator\noise;
29 
30 
31 abstract class Generator{
32  protected $perm = [];
33  protected $offsetX = 0;
34  protected $offsetY = 0;
35  protected $offsetZ = 0;
36  protected $octaves = 8;
37  protected $frequency;
38  protected $amplitude;
39 
40  public static function floor($x){
41  return $x >= 0 ? (int) $x : (int) ($x - 1);
42  }
43 
44  public static function fade($x){
45  return $x ** 3 * ($x * ($x * 6 - 15) + 10);
46  }
47 
48  public static function lerp($x, $y, $z){
49  return $y + $x * ($z - $y);
50  }
51 
52  public static function grad($hash, $x, $y, $z){
53  $hash &= 15;
54  $u = $hash < 8 ? $x : $y;
55  $v = $hash < 4 ? $y : (($hash === 12 or $hash === 14) ? $x : $z);
56 
57  return (($hash & 1) === 0 ? $u : -$u) + (($hash & 2) === 0 ? $v : -$v);
58  }
59 
60  abstract public function getNoise2D($x, $z);
61 
62  abstract public function getNoise3D($x, $y, $z);
63 
64  public function noise2D($x, $z, $normalized = false){
65  $result = 0;
66  $amp = 1;
67  $freq = 1;
68  $max = 0;
69 
70  for($i = 0; $i < $this->octaves; ++$i){
71  $result += $this->getNoise2D($x * $freq, $z * $freq) * $amp;
72  $max += $amp;
73  $freq *= $this->frequency;
74  $amp *= $this->amplitude;
75  }
76  if($normalized === true){
77  $result /= $max;
78  }
79 
80  return $result;
81  }
82 
83  public function noise3D($x, $y, $z, $normalized = false){
84  $result = 0;
85  $amp = 1;
86  $freq = 1;
87  $max = 0;
88 
89  for($i = 0; $i < $this->octaves; ++$i){
90  $result += $this->getNoise3D($x * $freq, $y * $freq, $z * $freq) * $amp;
91  $max += $amp;
92  $freq *= $this->frequency;
93  $amp *= $this->amplitude;
94  }
95  if($normalized === true){
96  $result /= $max;
97  }
98 
99  return $result;
100  }
101 
102  public function setOffset($x, $y, $z){
103  $this->offsetX = $x;
104  $this->offsetY = $y;
105  $this->offsetZ = $z;
106  }
107 }