Use single Perlin noise

This commit is contained in:
Shoghi Cervantes 2014-02-12 13:25:40 +01:00
parent 1bc54dbc44
commit 4e820ab89f
5 changed files with 69 additions and 36 deletions

View File

@ -38,27 +38,23 @@ class NormalGenerator implements LevelGenerator{
private $noiseGen5;
private $noiseGen6;
private $noiseArray = array();
private $noise;
public function __construct(array $options = array()){
$this->noiseGen1 = new PerlinOctaveGenerator($rand, 16);
$this->noiseGen2 = new PerlinOctaveGenerator($rand, 16);
$this->noiseGen3 = new PerlinOctaveGenerator($rand, 8);
$this->noiseGen4 = new PerlinOctaveGenerator($rand, 4);
$this->noiseGen5 = new PerlinOctaveGenerator($rand, 10);
$this->noiseGen6 = new PerlinOctaveGenerator($rand, 16);
}
public function init(Level $level, Random $random){
$this->level = $level;
$this->random = $random;
$this->random->setSeed($this->level->getSeed());
$perlin = new PerlinNoiseGenerator($this->random, array(
"frequency" => 1,
"octaves" => 8,
"persistence" => 0.3,
"scale" => 1,
));
$this->deviations = $perlin->fillNoiseArray(256, 256);
$this->noise = new NoiseGeneratorPerlin($this->random);
$this->noiseGen1 = new PerlinOctaveGenerator($this->random, 16);
$this->noiseGen2 = new PerlinOctaveGenerator($this->random, 16);
$this->noiseGen3 = new PerlinOctaveGenerator($this->random, 8);
$this->noiseGen4 = new PerlinOctaveGenerator($this->random, 4);
$this->noiseGen5 = new PerlinOctaveGenerator($this->random, 10);
$this->noiseGen6 = new PerlinOctaveGenerator($this->random, 16);
$ores = new OrePopulator();
$ores->setOreTypes(array(
@ -75,9 +71,10 @@ class NormalGenerator implements LevelGenerator{
}
public function generateChunk($chunkX, $chunkZ){
//$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
$byte0 = 4;
$this->noiseArray = $this->initializeNoiseArray($chunkX * $byte0, 0, $chunkZ * $byte0, $byte0 + 1, ($this->worldHeight / 8) + 1, $byte0 + 1);
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
//$this->noiseArray = $this->initializeNoiseArray($chunkX * $byte0, 0, $chunkZ * $byte0, $byte0 + 1, ($this->worldHeight / 8) + 1, $byte0 + 1);
$chunks = array();
for($chunkY = 0; $chunkY < 8; ++$chunkY){
@ -86,7 +83,7 @@ class NormalGenerator implements LevelGenerator{
$endY = $startY + 16;
for($z = 0; $z < 16; ++$z){
for($x = 0; $x < 16; ++$x){
$height = (int) ($this->baseHeight + $this->deviations[$z + ($chunkZ << 4)][$x + ($chunkX << 4)]);
$height = (int) ($this->worldHeight + $this->noise->noise3D($x + ($chunkX << 4), 0, $z + ($chunkZ << 4), 4, 0.5, 24));
for($y = $startY; $y < $endY; ++$y){
$diff = $height - $y;
if($y <= 4 and ($y === 0 or $this->random->nextFloat() < 0.75)){

View File

@ -22,12 +22,12 @@
abstract class NoiseGenerator{
protected $perm = array();
protected $offsetX;
protected $offsetY;
protected $offsetZ;
protected $offsetX = 0;
protected $offsetY = 0;
protected $offsetZ = 0;
public static function floor($x){
return $x >= 0 ? (int) $x : (int) $x - 1;
return $x >= 0 ? (int) $x : (int) ($x - 1);
}
public static function fade($x){
@ -64,4 +64,11 @@ abstract class NoiseGenerator{
}
return $result;
}
public function setOffset($x, $y, $z){
$this->offsetX = $x;
$this->offsetY = $y;
$this->offsetZ = $z;
}
}

View File

@ -23,7 +23,7 @@
require_once("NoiseGenerator.php");
/***REM_END***/
class NoiseGeneratorPerlin extends NoiseGenerator[
class NoiseGeneratorPerlin extends NoiseGenerator{
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],
@ -37,19 +37,20 @@ class NoiseGeneratorPerlin extends NoiseGenerator[
$this->offsetZ = $random->nextFloat() * 256;
for($i = 0; $i < 512; ++$i){
$this->perm[$i} = 0;
$this->perm[$i] = 0;
}
for($i = 0; $i < 256; ++$i){
$this->perm[$i} = $random->nextRange(0, 255);
$this->perm[$i] = $random->nextRange(0, 255);
}
for($i = 0; $i < 256; ++$i)[
$pos = $random->nextRange(0, 255 - $i) + $i;
$old = $this->perm[$i};
$this->perm[$i} = $this->perm[$pos};
$this->perm[$pos} = $old;
$this->perm[$i + 256} = $this->perm[$i};
for($i = 0; $i < 256; ++$i){
$pos = $random->nextRange(0, 255 - $i) + $i;
$old = $this->perm[$i];
$this->perm[$i] = $this->perm[$pos];
$this->perm[$pos] = $old;
$this->perm[$i + 256] = $this->perm[$i];
}
}
@ -84,8 +85,8 @@ class NoiseGeneratorPerlin extends NoiseGenerator[
$BA = $this->perm[$B] + $Z;
$BB = $this->perm[$B + 1] + $Z;
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),
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),

View File

@ -102,7 +102,7 @@ abstract class OctaveGenerator{
return $result;
}
public function generateNoiseOctaves($x, $y, $z, $frequency, $amplitude){
/*public function generateNoiseOctaves($x, $y, $z, $frequency, $amplitude){
}
}*/
}

View File

@ -31,5 +31,33 @@ class PerlinOctaveGenerator extends OctaveGenerator{
}
}
public function
/*public function generateNoiseOctaves($x, $y, $z, $sizeX, $sizeY, $sizeZ, $fX, $fY, $fZ){
$adouble = array_fill(0, $sizeX * $sizeY * $sizeZ, 0.0);
$d3 = 1.0;
foreach($this->octaves as $octave){
$dX = $x * $d3 * $fX;
$dY = $y * $d3 * $fY;
$dZ = $x * $d3 * $fZ;
$x1 = NoiseGenerator::floor($dX);
$z1 = NoiseGenerator::floor($dZ);
$dX -= $x1;
$dZ -= $z1;
$x1 %= 16777216;
$z1 %= 16777216;
//$x1 &= 0xFFFFFF;
//$z1 &= 0xFFFFFF;
$dX += $x1;
$dZ += $z1;
$octave->populateNoiseArray($adouble, $dX, $dY, $dZ, $sizeX, $sizeY, $sizeZ, $fX * $d3, $fY * $d3, $fZ * $d3, $d3);
$d3 *= 0.5;
}
return $adouble;
}*/
}