Page 1 of 1

Making some noise! (Simplex style)

Posted: Fri Oct 28, 2011 1:21 am
by Nergal
Hello Everyone!

I have become a bit fascinated with procedural generation lately and decided to try my hand at some terrain generation. After doing some googling (google knows everything you know... :crazy: ) I decided I'd start with using a simplex noise filter. Luckily someone had already translate the original example written by Ken Perlin in Java to Lua. It was in the form of a module however. Also they where including the LuaBit functions for their bitwise operations.

In any case LevyBreak posted his original translation here: http://www.facepunch.com/threads/834088

I took this, stripped out the 3D and 4D operations and converted it so that it built a table and returned it. It made it easier to use in LOVE, as I think loading the module was causing some goofiness in the loader.

MapGen I based on another Java script which was written by Wade Kallhoff http://mavdisk.mnsu.edu/kallhw/

The original source can be found here: http://mavdisk.mnsu.edu/kallhw/world/world.txt

I also modified LuaBit (because the way it was built using do <stuff> end) was causing issues with it working in LOVE as well: Attempt to index global "bit" a nil value... wat?! :shock: ) and included that so that we didn't need to get messy with the functions in the Simplex Noise library.

In any case, I was originally disappointed with the results, mainly because I had never knowingly seen the results of perlin/simplex noise filters before. After doing some googling trying to figure out what I could have possibly done wrong, I discovered that this is the way the filter actually turns out. Silly me.

So I was happy again.

Just a pre-warning however, this is still extremely simple. I just wanted to share what I had so far. ^^
MapGen.love
(6.3 KiB) Downloaded 476 times
The only variables you can actually modify, currently, is the noiseScale and tileScale. By default the tileScale is set to 10x10 pixels. The noiseScale is set to 0.1 by default, I currently have it being set in the function call to 0.05.

In any case, I hope to make this more useful as I work on it, and eventually turn it into a full blown library for LOVE. So if anyone has any suggestions, critiques, comments or contributions feel free to share!

Some future features I already have in mind:
- Ability to fine tune noise detail and add random seed capability
- Add the ability to pre-gen a map using noise and apply other filters on top of it. For instance, pre-gen using diamond-square fractal noise and modify it by simplex (which would probably turn out much better looking maps) and allow you to fine tune how the filter is applied.
- Add the ability to generate a Lua table in a format which can be used by Advanced Tiled Loader http://love2d.org/wiki/Advanced_Tiled_Loader (because I like it)

In any case, I found this to be a very interesting learning experience so far, and easier than I had originally anticipated it to be. I hope this helps someone else out as well. :cool:

*Edit: Durr... helps if I upload the file. lol

Re: Making some noise! (Simplex style)

Posted: Fri Oct 28, 2011 1:34 am
by josefnpat
This is some nice code to get ported over to lua. I bet there's lots of lovers that wouldn't mind being able to use this and fine tune it to use in their own applications.

Re: Making some noise! (Simplex style)

Posted: Fri Oct 28, 2011 1:49 am
by slime
Sweet! There's also a nice LuaJIT implementation of simplex noise for those who use LuaJIT with love (although luajit breaks slightly with the upcoming version of love...), it's many times faster than a pure Lua implementation could ever be.

http://staffwww.itn.liu.se/~stegu/simpl ... /Noise.lua

Re: Making some noise! (Simplex style)

Posted: Fri Oct 28, 2011 2:12 am
by Nergal
Hey, that looks pretty good slime.

It actually looks like it would run in straight Lua as well however... unless I'm mistaken. Going over it, it actually looks quite a bit better than the implementation I'm using. I'm going to test it out and see.

I agree LuaJIT is definitely a better approach for speed. Try generating a 500x500 tile map with my example for instance, and you'll see there is a significant speed hit. Of course rendering 5000 rectangles is also a hit in and of itself. I'll probably include the ability to simply render the entire thing as an image at some point.

Re: Making some noise! (Simplex style)

Posted: Fri Oct 28, 2011 2:16 am
by slime
A huge portion of the speed increase from it is because it uses LuaJIT's FFI to use C datatypes, so it can't be ported straight to pure Lua.

Re: Making some noise! (Simplex style)

Posted: Fri Oct 28, 2011 2:23 am
by Nergal
slime wrote:A huge portion of the speed increase from it is because it uses LuaJIT's FFI to use C datatypes, so it can't be ported straight to pure Lua.
I completely missed that. Thanks for pointing that out so I didn't waist a bunch of time trying to get it to work. :)