61 public static function bilinearLerp($x, $y, $q00, $q01, $q10, $q11, $x1, $x2, $y1, $y2){
62 $dx1 = (($x2 - $x) / ($x2 - $x1));
63 $dx2 = (($x - $x1) / ($x2 - $x1));
65 return (($y2 - $y) / ($y2 - $y1)) * (
66 $dx1 * $q00 + $dx2 * $q10
67 ) + (($y - $y1) / ($y2 - $y1)) * (
68 $dx1 * $q01 + $dx2 * $q11
93 public static function trilinearLerp($x, $y, $z, $q000, $q001, $q010, $q011, $q100, $q101, $q110, $q111, $x1, $x2, $y1, $y2, $z1, $z2){
94 $dx1 = (($x2 - $x) / ($x2 - $x1));
95 $dx2 = (($x - $x1) / ($x2 - $x1));
96 $dy1 = (($y2 - $y) / ($y2 - $y1));
97 $dy2 = (($y - $y1) / ($y2 - $y1));
99 return (($z2 - $z) / ($z2 - $z1)) * (
101 $dx1 * $q000 + $dx2 * $q100
103 $dx1 * $q001 + $dx2 * $q101
105 ) + (($z - $z1) / ($z2 - $z1)) * (
107 $dx1 * $q010 + $dx2 * $q110
109 $dx1 * $q011 + $dx2 * $q111
175 public function noise3D($x, $y, $z, $normalized =
false){
181 $x *= $this->expansion;
182 $y *= $this->expansion;
183 $z *= $this->expansion;
185 for($i = 0; $i < $this->octaves; ++$i){
186 $result += $this->
getNoise3D($x * $freq, $y * $freq, $z * $freq) * $amp;
189 $amp *= $this->persistence;
192 if($normalized ===
true){
203 public function getFastNoise1D(
int $xSize,
int $samplingRate,
int $x,
int $y,
int $z) : \SplFixedArray{
204 if($samplingRate === 0){
205 throw new \InvalidArgumentException(
"samplingRate cannot be 0");
207 if($xSize % $samplingRate !== 0){
208 throw new \InvalidArgumentException(
"xSize % samplingRate must return 0");
212 $noiseArray = new \SplFixedArray($xSize + 1);
214 for($xx = 0; $xx <= $xSize; $xx += $samplingRate){
215 $noiseArray[$xx] = $this->
noise3D($xx + $x, $y, $z);
218 for($xx = 0; $xx < $xSize; ++$xx){
219 if($xx % $samplingRate !== 0){
220 $nx = (int) ($xx / $samplingRate) * $samplingRate;
224 x2: $nx + $samplingRate,
225 q0: $noiseArray[$nx],
226 q1: $noiseArray[$nx + $samplingRate]
238 public function getFastNoise2D(
int $xSize,
int $zSize,
int $samplingRate,
int $x,
int $y,
int $z) : \SplFixedArray{
239 assert($samplingRate !== 0, new \InvalidArgumentException(
"samplingRate cannot be 0"));
241 assert($xSize % $samplingRate === 0,
new \InvalidArgumentException(
"xSize % samplingRate must return 0"));
242 assert($zSize % $samplingRate === 0,
new \InvalidArgumentException(
"zSize % samplingRate must return 0"));
245 $noiseArray = new \SplFixedArray($xSize + 1);
247 for($xx = 0; $xx <= $xSize; $xx += $samplingRate){
248 $noiseArray[$xx] = new \SplFixedArray($zSize + 1);
249 for($zz = 0; $zz <= $zSize; $zz += $samplingRate){
250 $noiseArray[$xx][$zz] = $this->noise3D($x + $xx, $y, $z + $zz);
254 for($xx = 0; $xx < $xSize; ++$xx){
255 if($xx % $samplingRate !== 0){
256 $noiseArray[$xx] = new \SplFixedArray($zSize + 1);
259 for($zz = 0; $zz < $zSize; ++$zz){
260 if($xx % $samplingRate !== 0 || $zz % $samplingRate !== 0){
261 $nx = (int) ($xx / $samplingRate) * $samplingRate;
262 $nz = (int) ($zz / $samplingRate) * $samplingRate;
266 q00: $noiseArray[$nx][$nz],
267 q01: $noiseArray[$nx][$nz + $samplingRate],
268 q10: $noiseArray[$nx + $samplingRate][$nz],
269 q11: $noiseArray[$nx + $samplingRate][$nz + $samplingRate],
271 x2: $nx + $samplingRate,
273 y2: $nz + $samplingRate
285 public function getFastNoise3D(
int $xSize,
int $ySize,
int $zSize,
int $xSamplingRate,
int $ySamplingRate,
int $zSamplingRate,
int $x,
int $y,
int $z) : array{
287 assert($xSamplingRate !== 0, new \InvalidArgumentException(
"xSamplingRate cannot be 0"));
288 assert($zSamplingRate !== 0,
new \InvalidArgumentException(
"zSamplingRate cannot be 0"));
289 assert($ySamplingRate !== 0,
new \InvalidArgumentException(
"ySamplingRate cannot be 0"));
291 assert($xSize % $xSamplingRate === 0,
new \InvalidArgumentException(
"xSize % xSamplingRate must return 0"));
292 assert($zSize % $zSamplingRate === 0,
new \InvalidArgumentException(
"zSize % zSamplingRate must return 0"));
293 assert($ySize % $ySamplingRate === 0,
new \InvalidArgumentException(
"ySize % ySamplingRate must return 0"));
295 $noiseArray = array_fill(0, $xSize + 1, array_fill(0, $zSize + 1, []));
297 for($xx = 0; $xx <= $xSize; $xx += $xSamplingRate){
298 for($zz = 0; $zz <= $zSize; $zz += $zSamplingRate){
299 for($yy = 0; $yy <= $ySize; $yy += $ySamplingRate){
300 $noiseArray[$xx][$zz][$yy] = $this->noise3D($x + $xx, $y + $yy, $z + $zz,
true);
316 for($xx = 0; $xx < $xSize; ++$xx){
317 $nx = (int) ($xx / $xSamplingRate) * $xSamplingRate;
318 $nnx = $nx + $xSamplingRate;
320 $dx1 = (($nnx - $xx) / ($nnx - $nx));
321 $dx2 = (($xx - $nx) / ($nnx - $nx));
323 for($zz = 0; $zz < $zSize; ++$zz){
324 $nz = (int) ($zz / $zSamplingRate) * $zSamplingRate;
325 $nnz = $nz + $zSamplingRate;
327 $dz1 = ($nnz - $zz) / ($nnz - $nz);
328 $dz2 = ($zz - $nz) / ($nnz - $nz);
330 for($yy = 0; $yy < $ySize; ++$yy){
331 if($xx % $xSamplingRate !== 0 || $zz % $zSamplingRate !== 0 || $yy % $ySamplingRate !== 0){
332 $ny = (int) ($yy / $ySamplingRate) * $ySamplingRate;
333 $nny = $ny + $ySamplingRate;
335 $dy1 = (($nny - $yy) / ($nny - $ny));
336 $dy2 = (($yy - $ny) / ($nny - $ny));
338 $noiseArray[$xx][$zz][$yy] = $dz1 * (
340 $dx1 * $noiseArray[$nx][$nz][$ny] + $dx2 * $noiseArray[$nnx][$nz][$ny]
342 $dx1 * $noiseArray[$nx][$nz][$nny] + $dx2 * $noiseArray[$nnx][$nz][$nny]
346 $dx1 * $noiseArray[$nx][$nnz][$ny] + $dx2 * $noiseArray[$nnx][$nnz][$ny]
348 $dx1 * $noiseArray[$nx][$nnz][$nny] + $dx2 * $noiseArray[$nnx][$nnz][$nny]