Feb 032012
 

Procedurally generated content is a fairly large movement at this point. The most prominent, recent example is probably Minecraft, which relies heavily upon computer algorithms to generate the world and events in the game. There’s no cubicle farm of artists churning out Minecraft landscapes.

To a large extent, we have Ken Perlin to thank for this. Ken was awarded an Academy Award for the invention of Perlin Noise during the production of the film Tron. His name is now ubiquitous, but unfortunately the algorithm which bears his name is dated. Ken published a paper in 2002 describing the drawbacks of Perlin Noise and introducing a new algorithm. Many references to this algorithm confusingly call it Improved Perlin Noise, but it is actually named Simplex Noise.

It seems many developers start searching for noise functions and don’t know which to use. Thankfully, Simplex Noise is a suitable replacement to Perlin Noise in almost every way: the noise is cleaner, less computationally expensive and easier to implement in hardware (if desired). Unfortunately, with all the confusion on the web it’s hard to find a good implementation and impossible to find documentation.

When I came upon this problem in 2007, the only implementation I could find was written in Java by Stefan Gustavson (Simplex Noise Demystified pdf). I managed to get a nice fast implementation written for my C++ project, but didn’t really write any meaningful documentation. Nevertheless, it must have been the best thing available because I know it’s in use in several places.

Simplex Noise Implementations

I have written C++ and Python implementations of Simplex Noise. In addition to raw 2D, 3D and 4D noise, multi-octave noise functions are also available. There’s an experimental marble texture available and I expect to add a lot more in due time.

C++ Simplex Noise (Google Code)
simplexnoise.h
simplexnoise.cpp
simplextextures.h
simplextextures.cpp

Python Simplex Noise (Google Code)
simplexnoise.py
simplextextures.py
Performance warning: although functional, the Python implementation is currently 100-times slower than the C++ implementation. I will be building a faster Python module, but go ahead and use this version if you’re lazy and/or don’t need to generate more than ~40,000 values per second.

Understanding Simplex Noise and Multi-Octave Noise

If you already understand other types of procedural noise (such as Perlin Noise) then there’s probably not too much to know. On the other hand, you should certainly learn a little more if you’ve never worked with noise before. Having a basic understanding will allow you to write better-performing, better-looking code.

Simplex Noise on wikipedia.

Multi-octave noise is much more desirable for certain situations (such as generating terrain). You’ll be able to take better advantage of it if you understand basically how it works: Explanation of multi-octave noise

Stefan Gustavson’s Simplex Noise Demystified (pdf) explains the benefits of Simplex noise and how it works.

Some plots of the noise distributions are available on the Google Code wiki.

Using Simplex Noise

If you already know what you’re doing it should be easy to determine which functions you need to call. I’ve made a number of convenience functions available and you can certainly produce new ones easily. Each function takes a set of 2D, 3D or 4D coordinates and returns a single noise value. These are the functions available:

raw_noise() returns the value directly from the Simplex noise function.

scaled_raw_noise() scales the return value from [-1, 1] to [loBound, hiBound] based on the bounds you specify.

octave_noise() returns a multi-octave noise value based on multiple passes of Simplex noise.

scaled_octave_noise() returns a multi-octave noise value based on multiple passes of Simplex noise. Scales the return value from [-1, 1] to [loBound, hiBound] based on the bounds you specify.

  12 Responses to “Simplex Noise for C++ and Python”

Comments (12)
  1. Thanks a lot for releasing this code, this will save me a lot of time! thanks :D

  2. Gratz to you. Possibly explain octaves, persistence and scale a bit.

  3. Thanks for posting this – nice implementation, simple & fast. Planning to use it on an upcoming game.

  4. Reading up on Perlin and Simplex noise, so this is helpful, thanks. And in case you are curious, I found this by searching for “Erlang Perlin Noise” which turned up your home page, though unfortunately not for one single blog post ;)

    I always build MUDs as my learning project for new programming languages. I am dusting off my Erlang based MUD I called Simmoa (link above) and starting over in Elixir. I have in 3 days added more functionality than Simmoa had, though in fairness I have 4 more years of Erlang experience since then. Anyway, wanted to say I appreciate this article, and agree with your other article on Erlang being a great server side language for games.

  5. I want to share a little story with you, in case you will ever read this. I’m rather new to programing and like to try new things. So one day i decided to check out what all this randomly generated stuff is about. So for like 3 days i fought through page after page of google, trying to figure out how to do this stuff. Turns out nobody seems to really understand this stuff. It took 95% of that time to figure out what the difference between perlin noise, simplex noise, value noise and fBm even is. Because i wanted to do it right i dismissed most of my findings because they didn’t seem to agree on the method (i specificly looked for perlin noise). Some blendet some static textures together to get quite nice results. But you can’t really regulate anything with that. Then some people came up with the method you have here, but did not use octaves to make “the real thing”. So again i ended up with some weird looking random noise. Then i shifted my googling to fBm and finally found a guy (i think on stackexchange) who explained what all this stuff even is. Because as i said, if you google for anything noise related you get a million results and they are all callign their crap perlin noise. Turns out most are not. But try to figure that shit out if you have no idea to begin with. Now that i kind of knew what i was looking for i narrowed the search down and finally found this article. But that wasn’t the end of my pain. Because i’m only playing around a little bit i’m using C# and XNA – which is pretty much all i know. XNA that is, some C# stuff is still beyond me :P Anyway, i think you can see the problem as this code is in python/c++. So i copied the whole damn code in my project and instantly got about 1000 errors. Well long story short 6 hours, a trillion pulled hairs and 5000 google querrys later i finally got it to work. Well, i didn’t have any errors anymore. But then i had to try it out. I seriously feared for my life. Either it worked and this whole week of hell was finally over or it didn’t and … well… you know, a human can only bear so much :P. So i filled my little texture with the values your code spat out (oh and btw, thanks for that extra scaling method, it’s a pain in the ass to deal with all the different return values people use) and was shocked. It worked. Well, kind of. I got this random static crap again. But then i looked at the code again and it just looked right. You know, when you saw a trillion implementations of such things you just see which are worth your time and which are not. So i changed the parameter a bit and voil√° – there it was. A beautiful cloud. Well in black and white – but still. So now i’ll just have to figure out what values produce what kind of noise and i’m all set… to do absolutly nothing with it and move on to the next thing i’m curios about :D Seriously though, i swear one day i will understand this cryptic code of yours… maybe.

    I still can’t believe that i managed to translate this code to C#, considering that i have no clue about C++. Laugh at me if you will but i’m proud of myself :)

    Anyway, far too long story short:

    Thank you so much for apparently beeing the only guy on the internet who posts code which actually does what he says it would.

    Oh and sorry if my english sucks. It’s 6a.m. here in germany and as you can guess english is not my main language ;)

  6. Nice story, kinda know how you feel. But keep it up, eventually you will learn enough and have enough experience so that you can use most code from these kind of sites without too much hassle. Would be nice to release your c# code as well, some people might be looking for just that, and elliot implemented the algorithms well in my experience.

  7. Yea sure, i’ll post it if you tell me how to do that :P. I’d rather not blow up this comment section with it :D

  8. Okay, so i readded the comments which i deleted for convenience while converting it to C#. Also keep in mind that i personally wouldn’t call myself a programmer, so if this is the worst possible conversion to C# possible – it’s all someone else’s fault :)

    Oh and someone needs to figure this whole FastFloor stuff out for me. I tested it against .Nets Math.Floor in a console app and .Nets version was like 2.4 times faster. Not quite believing that i then tested the simplex noise with it. Meaning the original once (my c# version that is) vs the same version but using Math.Floor. Turns out the fastfloor is slightly faster here, which i do not understand at all. How can it be way slower in one test and slightly faster in another test ?

    Anyway, i’ll just take the liberty to leave the pastebin link here because i kind of hate using email – don’t ask me why.

    http://pastebin.com/fpBtP4wR

  9. I have spent weeks, searching the internet for explanations and examples of noise algorithms. I found many explanations of what they do, but only a few (no good ones) of HOW they WORK (except one post about Worley Noise), and the examples i found were either not working at all, or too specialized on one purpose…
    Now i found this one, and it works! Thank you!
    It just works as it should, its easy to implement, and its well commented. So i might even be able to finally understand how it works. ;)

    I would like to ask for your permission to use your Code in my Project, which might (or might not) become commercial one Day… I am sure i will modify it as soon as i understand how it works, but for now i will just use it as it is.
    You can check out some Information on my Project at: http://magratheadev.blogspot.com/

    So Long, and Thanks for All the Noise ;)

  10. Thanks for the example. I was thinking about porting it to ObjC for use in some Cocoa/Cocoa Touch projects of mine. I’ll be sure to give you credit, is that alright?

    • You are welcome to link to me, but also give credit to the original author of the algorithm (Ken Perlin) and Java implementation (Stefan Gustavson).

 Leave a Reply

(to be displayed above your comment)

(for moderation; your e-mail address will not be made public)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>