Perlin noise generator in PHP

Perlin noiseGenerating Perlin noise requires heavy calculation and PHP (in my opinion) is not the best language for this task but still it can be useful.
The algorithm is an adaptation form this pseudo code.
The class seems to work but I'm not if it works 100% correctly. The tricky part was to deal with the types because PHP tends to convert large integers to float, and the algorithm uses bitwise operators on some stages.

This class is just for testing purposes it consumes a lot of CPU cycles and I hardly recommend to avoid using it in live environment. Try to keep the cycles low as well as the octaves number close to 1.

Sample SVG result: noise.svg.
Example usage (generate SVG map of terrain):

 
  require("class.perlin.php");
  $per = new Perlin;
 
  $xl = 100;
  $yl = 40;
 
  header("Content-type: image/svg+xml");
  echo '<?xml version="1.0" standalone="no"?>';
  echo '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
  echo '<svg width="400" height="400"
  xmlns="http://www.w3.org/2000/svg" version="1.1">';
  $size = 3;
  for ($x = 0; $x < $xl; $x++){
    for ($y = 0; $y < $yl; $y++){
      $n = $per->perlinNoise2d($x, $y)*255;
      $c = '#986121';
      if ($n < 50){
        $c = '#0000ff';
      }
      if ($n > 200){
        $c = '#FFFFFF';
      }
      echo "<rect x=\"".($x*$size)."\" y=\"".($y*$size)."\"
            width=\"$size\" height=\"$size\" fill=\"$c\"/>\n";
    }
  }
  echo '</svg>';
 

Download: classperlinphp.zip

Comments 7

  1. stelt wrote:

    Why not show the actual resulting SVG here? (as opposed to the raster)

    Posted 02 Mar 2008 at 15:08
  2. AquilaX wrote:

    You can found sample output here.

    Posted 02 Mar 2008 at 16:11
  3. Christian wrote:

    I have tried using your class and i get everything in a single color except for some pixels on the top of the image :/

    Posted 26 Dec 2008 at 16:02
  4. AquilaX wrote:

    Play with $persistence and $octaves variables also $r1, $r2, $r3 gives different result depending on the values.

    Posted 26 Dec 2008 at 19:05
  5. Derek wrote:

    I’m sorry but I don’t think this class works. Especially not well enough to generate the image you have displayed. It does some variation near the left side and the rest is always 1 solid color. Even changing the variables to all imaginable combination it still results in the same type of thing.

    I’d really love a version that worked but this was posted awhile ago so I doubt I’ll get a response here.

    Posted 19 Jul 2009 at 18:21
  6. AquilaX wrote:

    Derek:
    That’s because the loop sizes are
    $xl = 100;
    $yl = 40;
    and the SVG’s dimensions are 400×400px.

    Just make both equal.

    Posted 19 Jul 2009 at 18:39
  7. Dylan wrote:

    Hmm it seems this doesn’t work. There is minimal randomness produced… try copying/pasting all your own code into it’s own area and see for yourself. I also ported this to javascript and am getting weird results… the noise doesn’t spread over the entire area, but eventually maxes out at -1

    Posted 23 Jul 2010 at 8:55

Post a Comment

Your email is never published nor shared. Required fields are marked *