How to generate Perlin Noise in Java?

So, yesterday while I was in bed, I couldn’t fall asleep. So I figured out the way that the S-curve works, and I played with it a little while today. I managed to make my perlin noise work! It would need some improvement though. I’m still using java.Random class. If I wasn’t using it, maybe performance would improve. What I have done differently from my first attempt, is now I generate random direction values that are length of 1. Also, I swapped the interpolation values a little. First, I calculate interpolated value between SV and TU using xd00.y as my weight. Then I interpolate based on xd00.x weight. This magically worked, but I don’t know why, but my noise looks kinda crappy. Not in sense of quality, but Simplex Noise looks a lot better for some reason. Anyway, here is the code. If there is some kind of expert here, please do tell if this is actually Perlin’s Noise correct algorithm.

It is pretty long code… Thought I better put in to pastebin, because it was pretty hard to read in post.
http://pastebin.java-gaming.org/3b0691d8474

I documented this code a little bit. I followed this tutorial to make it. Read it, because I think code is confusing for someone who doesn’t know what is going on.
http://webstaff.itn.liu.se/~stegu/TNM022-2005/perlinnoiselinks/perlin-noise-math-faq.html#algorithm

Here are some images generated using the code above.
Low frequency.

High frequency.

High frequency, high contrast.

I would really like for someone to explain to me how to generate random numbers from -1 to 1 for x, y based on some kind of random seed. I think it would speed up the algorithm even further.


RANDOM STUFF BELOW WHY DID I PUT IT THERE.


Random random = new Random();
Link randomLink = random.nextLink();

randomLink = http://www.twitch.tv/notch/b/302867302
Lol I love to watch Notch coding. <3 that keyboard sound.

Well based on the title alone I would say you do it like you do in every other language. No special java’isms.

When resizing images for fake/pseudo Perlin noise, make sure to turn on bi-linear filtering.

Here’s how you can implement your own Perlin Noise:

  1. Go watch this Twitch livestream here.
  2. Go to the time frame: 3:25:00. (Or seek the video to 3 hours 25 minutes in.)
  3. Watch the developer create a Perlin Noise, with a little bit of commentary, IN ACTION!!

Hey its Notch again! Thanks for the link.

Attempt to search the forum now and then. Look at the local wiki page. (required yet again repeat: this is not perlin noise)

I guess this should be only called “height map”.
What I’m thinking you’re trying to imply, is that Perlin Noise is a height map, which is generated by a specific technique, which was developed by Perlin.
Then I will call this Chris Noise.

No, this is just a heightmap generated using what seems to be some sort of averaged noise, although I can’t tell very well. Its not Perlin Noise.

Also, any image can be used as a heightmap, it doesn’t matter what kind of noise algorithm you use.

So how is Perlin Noise different from this height map?

First off, Perlin Noise is an algorithm, not a heightmap. You can generate other besides height maps with noise algorithms.

The difference? Perlin Noise would probably produce much ‘cooler’ height maps. Using Perlin Noise you can generate different types of images, including cloud like heightmaps, which makes for realistic terrain in games. The one you have right now would be very ‘spiky’, and not really realistic at all.

Your noise effect has an odd grid pattern built in. But it does look nicely noisy and all that. And the implementation looks pretty fast, too, which could be a very useful tradeoff. My head-cold’s a little too painful right now to look at your code more closely and try and figure out if the noise is “gradient” or “value” noise.

There is a GREAT article explaining the mathematics behind Ken Perlin’s gradient noise:

http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf

Or you could check out the lecture by KP himself:
http://www.noisemachine.com/talk1/index.html

The wiki page Roquen was referring to on our site brings all this up and more and is well worth checking out.

The Perlin algorithm’s are cool to a large degree because of the level of control they give the programmer to sculpt various textures. I’m not sure if “value” noise can be so easily sculpted. (The concept of octaves in Perlin noise is very useful. Can value noise bands be created at multiples of a given frequency as easily as with Perlin noise?)

I have a tool up (java applet) that will probably become unavailable at the end of the year thanks to new signing requirements. It has a gallery of various effects.

Ah, there is a link that allows the download of the jar. That is probably a better way to run it, anyway.

Clicking on the gallery image (Menubar: View/Gallery) will set up the tool with the parameters for that image, and allows you to edit/tweak the settings and see what happens. There is also a demo-template to help explain how the settings in the app tie into the algorithm. But I don’t go into explaining the algorithm itself. Gustavson is my favorite for that.

We had a code generator working, but I broke it when I ‘improved’ the way gradients are used as the subject of noise modulation, and it hasn’t been rebuilt.

[quote]Your noise effect has an odd grid pattern built in. But it does look nicely noisy and all that. And the implementation looks pretty fast, too, which could be a very useful tradeoff. My head-cold’s a little too painful right now to look at your code more closely and try and figure out if the noise is “gradient” or “value” noise.
[/quote]
This is actually Notch’s noise effect algorithm, taken from Twitch livestream. I’m interested in your quote, especially in:

  • How did you “see” that it has an odd grid pattern? From a few noise map patterns, I see no differences. I probably lack experiences, henceforth.
  • You say the implementation looks pretty fast, and it could be a useful tradeoff. What tradeoff are you thinking about?

It seems to be comprised of squares approximately 1x1mm in size on my screen. But not exactly that, more like I’m looking through a semi-opaque glass with a grid pebbling on it, with obvious vertical and horizontal gradations occurring at that frequency. Maybe this has to do with the resolution of this algorithm.

The Simplex implementation I’ve been using tends to lead more towards hexagons and triangles. And they are not as obvious.

The tradeoff I was referring to was between performance and the graphical effect. Perlin’s algorithms are pretty efficient, but they still have a significant cost in terms of cpu. Something that can give “good enough” noise that takes less cpu than Perlin noise could be useful in some contexts.

I used this code and quickly came up with a simple little demo. I basically just re-implemented TheCodingUniverse’s heightmap tutorial with random terrain generation.

Here is a jar and source(look in the world file for usage, look in the noise package for implementation) if you want to test it out.

Umm… I don’t think you understand the problem here. This image was done in 64x64 or 128x128 resolution. That is why it is “blocky”. What I mean is that those squares are individual pixels, stretched on 512x512 image.

Here is a smooth looking image:

And my implementation is really slow. Here is my current code. It allows to adjust the size of these noise areas. You can make a first heightmap really small, and then stretch it onto huge height-map and resample it a lot of times (64 and more) and it will give you a really smooth height-map with huge noise areas.
Here is an example:

if I want to generate a 512x512 smooth image with “huge” noise areas, it takes about 5 seconds… It is really slow… I will need to learn Perlin / Simplex noise for proper use.

It’s a value noise variant. 5 sec for 512x512…crazy. Look at the wiki page. http://www.java-gaming.org/topics/noise-bandpassed-white/27071/view.html

@trollwarrior1 Yes, the smooth image looks more like what I’d expect. Sorry to hear the implementation is so slow. Definitely check out Roquen’s link, and consider using Gustavson’s Simplex implementation (I cited earlier–it is linked in his article).

@Longarmx – Pretty cool! The StackOverflow link shows that the base implementation is Gustavson’s.

Speaking of base implementations, what is Notch’s algorithm’s base implementation? It looks like the diamond-square method, but I’m not confident enough to say it’s the correct answer. :frowning:

No, its not diamond square, its far simpler and way less CPU intensive. I believe its gradient noise, but someone else already said it could also be value noise. I don’t know much about this certain algorithm type, so I could not tell you.

Sigh. Reading comprehension is a useful skill.

On a different topic. Never ever use java.util.Random.

This is really slow and stuff. Can’t you read the code plox?

First, I create an array of doubles, which I fill with random doubles from -1 to 1.


		values = new double[width * height];

		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				values[x + y * width] = random.nextDouble() * 2 - 1;
			}
		}

Then I just “blend” nearby pixels together.


for (int y = 0; y < height; y++) {
				for (int x = 0; x < height; x++) {
					double a = getSample(x, y);
					double b = getSample(x + 1, y);
					double c = getSample(x + 1, y + 1);
					double d = getSample(x, y + 1);
					double e = getSample(x - 1, y + 1);
					double f = getSample(x - 1, y);
					double g = getSample(x - 1, y - 1);
					double h = getSample(x, y - 1);
					double j = getSample(x + 1, y - 1);

					double z = (a + b + c + d + e + f + g + h + j) / 9 * mult;

					if (z > 1) z = 1;
					if (z < -1) z = -1;

					setSample(x, y, z);

				}
			}

Blending pixels only 1 time doesn’t do the trick, so you need to blend them a few times over and over again. After blending them a few times, you get something that looks like Perlin Noise. Just read the code plox. It is pretty simple…