Perlin Noise

Hello folks,
I’m beginning explorations into using Perlin noise to generate things procedurally, and I am having trouble getting started.

The result I am looking for is to be able to send x and y float values along with an int seed, and receive a float between -1.0 and 1.0. It’s my understanding so far that this is essentially the basis of what Perlin noise is for, so hopefully I’m heading in the right direction.

I’ve downloaded the example project in the Unity resources section which contains the various examples of how to use Perlin noise. While I have been poking around through the code trying to figure out how I can use it myself, I’m just not getting it. This project contains absolutely no comments or documentation, and my knowledge of how Perlin works is proving too fuzzy to dissect the process. I am hoping you can help me attain the result I describe above, or at least describe how some of the functions in Perlin.cs are used to help me get started.

Thank you,

-Beck

I’ve still been messing with this, trying to get it to work.

I think I understand now how the functions are generating noise. I used the Fractal Texture example as a base and diluted it to give me a flat image each time. However, every time I run the program it generates a different Perlin. I would very much like to be able to set the Seed for the Perlin myself, and so I poked around for the culprit:

public Perlin()
	{
		int i, j, k;
		System.Random rnd = new System.Random();
	
	   for (i = 0 ; i < B ; i++) {
		  p[i] = i;
		  g1[i] = (float)(rnd.Next(B + B) - B) / B;
	
		  for (j = 0 ; j < 2 ; j++)
			 g2[i,j] = (float)(rnd.Next(B + B) - B) / B;
		  normalize2(ref g2[i, 0], ref g2[i, 1]);
	
		  for (j = 0 ; j < 3 ; j++)
			 g3[i,j] = (float)(rnd.Next(B + B) - B) / B;
			 
	
		  normalize3(ref g3[i, 0], ref g3[i, 1], ref g3[i, 2]);
	   }
	
	   while (--i != 0) {
		  k = p[i];
		  p[i] = p[j = rnd.Next(B)];
		  p[j] = k;
	   }
	
	   for (i = 0 ; i < B + 2 ; i++) {
		  p[B + i] = p[i];
		  g1[B + i] = g1[i];
		  for (j = 0 ; j < 2 ; j++)
			 g2[B + i,j] = g2[i,j];
		  for (j = 0 ; j < 3 ; j++)
			 g3[B + i,j] = g3[i,j];
	   }
	}

rnd is used successively throughout this function, and while I do not quite understand the math (Why would you not comment your code for a public release? How do you expect me to use this?) it seems as though this System.Random is what is making the Perlin noise different every time I run the program.

The problem I’m running into now is I have no idea how I could rewrite this to have a given Seed. The code uses the rnd.Next function a lot, and I don’t know what that does or how to replace it.

Does anyone have some insight into how to fix this?

Thanks,
-Beck

The problem was solved, thanks mike. System.Random() can take in an int for the seed, and so modifying the Perlin function to accommodate for that was exactly what I was looking for.

I especially like the single letter variables… So not only has he not commented his code, he used variables which either only made sense to him, or were convenient at the time. :frowning:

See, that’s one great way to encrypt code :wink:

Problem is, you encrypt it for yourself too.

EDIT: Seems it works in Unity using Random.seed: http://unity3d.com/support/documentation/ScriptReference/Random-seed.html

However I’d like to know if it changes the seed only within the scope of the function, or for the entire application. Sounds like the second one could be dangerous if you use Random elsewhere.

Most perlin code looks like this, including the example that Unity provides. Presumably he started from there. Be nice, if you are going to take the time to respond to a post, you might as well be helpful.

Well the way I solved the problem was changing new Random() to new Random(seed). This seems to be giving me the same result every time. Here’s a link to what I’ve been able to do so far using the function: http://www.rseben.com/SM/sealedMinds.html

Excellent.

hi im in need of a terraria like 2D world generator code i looked on youtube found a video but wasent able to copy there code any advice?

pls help me

What’s that have to do with this 7 year old necro thread?