37 protected const grad3 = [
38 [1, 1, 0], [-1, 1, 0], [1, -1, 0], [-1, -1, 0],
39 [1, 0, 1], [-1, 0, 1], [1, 0, -1], [-1, 0, -1],
40 [0, 1, 1], [0, -1, 1], [0, 1, -1], [0, -1, -1]
43 protected const F2 = 0.5 * (M_SQRT3 - 1);
44 protected const G2 = (3 - M_SQRT3) / 6;
45 protected const G22 = self::G2 * 2.0 - 1;
46 protected const F3 = 1.0 / 3.0;
47 protected const G3 = 1.0 / 6.0;
49 protected float $offsetX;
50 protected float $offsetZ;
51 protected float $offsetY;
53 protected array $perm = [];
55 public function __construct(
Random $random,
int $octaves,
float $persistence,
float $expansion){
56 parent::__construct($octaves, $persistence, $expansion);
58 $this->offsetX = $random->
nextFloat() * 256;
59 $this->offsetY = $random->
nextFloat() * 256;
60 $this->offsetZ = $random->
nextFloat() * 256;
62 for($i = 0; $i < 512; ++$i){
66 for($i = 0; $i < 256; ++$i){
67 $this->perm[$i] = $random->nextBoundedInt(256);
70 for($i = 0; $i < 256; ++$i){
71 $pos = $random->nextBoundedInt(256 - $i) + $i;
72 $old = $this->perm[$i];
74 $this->perm[$i] = $this->perm[$pos];
75 $this->perm[$pos] = $old;
76 $this->perm[$i + 256] = $this->perm[$i];
91 $s = ($x + $y + $z) * self::F3;
95 $t = ($i + $j + $k) * self::G3;
162 $x1 = $x0 - $i1 + self::G3;
163 $y1 = $y0 - $j1 + self::G3;
164 $z1 = $z0 - $k1 + self::G3;
165 $x2 = $x0 - $i2 + 2.0 * self::G3;
166 $y2 = $y0 - $j2 + 2.0 * self::G3;
167 $z2 = $z0 - $k2 + 2.0 * self::G3;
168 $x3 = $x0 - 1.0 + 3.0 * self::G3;
169 $y3 = $y0 - 1.0 + 3.0 * self::G3;
170 $z3 = $z0 - 1.0 + 3.0 * self::G3;
180 $t0 = 0.6 - $x0 * $x0 - $y0 * $y0 - $z0 * $z0;
182 $gi0 = self::grad3[$this->perm[$ii + $this->perm[$jj + $this->perm[$kk]]] % 12];
183 $n += $t0 * $t0 * $t0 * $t0 * ($gi0[0] * $x0 + $gi0[1] * $y0 + $gi0[2] * $z0);
186 $t1 = 0.6 - $x1 * $x1 - $y1 * $y1 - $z1 * $z1;
188 $gi1 = self::grad3[$this->perm[$ii + $i1 + $this->perm[$jj + $j1 + $this->perm[$kk + $k1]]] % 12];
189 $n += $t1 * $t1 * $t1 * $t1 * ($gi1[0] * $x1 + $gi1[1] * $y1 + $gi1[2] * $z1);
192 $t2 = 0.6 - $x2 * $x2 - $y2 * $y2 - $z2 * $z2;
194 $gi2 = self::grad3[$this->perm[$ii + $i2 + $this->perm[$jj + $j2 + $this->perm[$kk + $k2]]] % 12];
195 $n += $t2 * $t2 * $t2 * $t2 * ($gi2[0] * $x2 + $gi2[1] * $y2 + $gi2[2] * $z2);
198 $t3 = 0.6 - $x3 * $x3 - $y3 * $y3 - $z3 * $z3;
200 $gi3 = self::grad3[$this->perm[$ii + 1 + $this->perm[$jj + 1 + $this->perm[$kk + 1]]] % 12];
201 $n += $t3 * $t3 * $t3 * $t3 * ($gi3[0] * $x3 + $gi3[1] * $y3 + $gi3[2] * $z3);
216 $x += $this->offsetX;
217 $y += $this->offsetY;
220 $s = ($x + $y) * self::F2;
221 $i = (int) ($x + $s);
222 $j = (int) ($y + $s);
223 $t = ($i + $j) * self::G2;
225 $x0 = $x - ($i - $t);
226 $y0 = $y - ($j - $t);
245 $x1 = $x0 - $i1 + self::G2;
246 $y1 = $y0 - $j1 + self::G2;
247 $x2 = $x0 + self::G22;
248 $y2 = $y0 + self::G22;
257 $t0 = 0.5 - $x0 * $x0 - $y0 * $y0;
259 $gi0 = self::grad3[$this->perm[$ii + $this->perm[$jj]] % 12];
260 $n += $t0 * $t0 * $t0 * $t0 * ($gi0[0] * $x0 + $gi0[1] * $y0);
263 $t1 = 0.5 - $x1 * $x1 - $y1 * $y1;
265 $gi1 = self::grad3[$this->perm[$ii + $i1 + $this->perm[$jj + $j1]] % 12];
266 $n += $t1 * $t1 * $t1 * $t1 * ($gi1[0] * $x1 + $gi1[1] * $y1);
269 $t2 = 0.5 - $x2 * $x2 - $y2 * $y2;
271 $gi2 = self::grad3[$this->perm[$ii + 1 + $this->perm[$jj + 1]] % 12];
272 $n += $t2 * $t2 * $t2 * $t2 * ($gi2[0] * $x2 + $gi2[1] * $y2);