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 '';
  echo '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
  echo '';
  $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 '';

Download: classperlinphp.zip

Comments

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

    Comment by stelt — 02.03.2008 @ 15:08

  2. AquilaX You can found sample output here.

    Comment by AquilaX — 02.03.2008 @ 16:11

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

    Comment by Christian — 26.12.2008 @ 16:02

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

    Comment by AquilaX — 26.12.2008 @ 19:05

  5. Derek 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.

    Comment by Derek — 19.07.2009 @ 18:21

  6. AquilaX Derek: That's because the loop sizes are $xl = 100; $yl = 40; and the SVG's dimensions are 400x400px. Just make both equal.

    Comment by AquilaX — 19.07.2009 @ 18:39

  7. Dylan 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

    Comment by Dylan — 23.07.2010 @ 08:55

  8. Joseph Yep, same here, only the two top rows are getting random data. After that, it evens out to a solid color.

    Comment by Joseph — 17.07.2011 @ 14:30

comments powered by Disqus