Noisefunction

I’m trying to program a noise generator. Here is the code:

	var terrain : Terrain;
	var tileSize: float;

	function Start () {

	    var xRes = terrain.terrainData.heightmapWidth;

	    var yRes = terrain.terrainData.heightmapHeight;

	    var heights = terrain.terrainData.GetHeights(0, 0, xRes, yRes);


		
		var max:float =0; 
		var noise : float[,] =new float[xRes+100,yRes+100];
		
		var div:float=1.3;
		var maxss:float=10;
		for (var sqsize=xRes;sqsize>=1;sqsize=sqsize/2){
		var locsqsize: float;
		
		 maxss--;
		print(maxss);
		startx=Random.Range(-100,0);
		starty=Random.Range(-100,0);
		for (x=0+startx;x<xRes;x=x+sqsize){
		for (y=0+starty;y<yRes;y=y+sqsize){

			groupvalue=Random.Range(0.0000001f,maxss);
			
			for(var x2=x;x2<sqsize+x&x2<xRes+100;x2++){
			if (x2>0){
			for(var y2=y;y2<sqsize+y&y2<yRes+100;y2++){
			if (y2>0){
				noise[x2,y2]=noise[x2,y2]+groupvalue;
				
				if (noise[x2,y2]>max){
				max=noise[x2,y2];
				
				}
				}}
			}
			}
		}
		}
	}
	


	for( x=0;x<xRes;x++){
	for( y=0;y<yRes;y++){
		heights[x,y]=noise[x,y]/max;
	}
	}
	print(heights[250,200]+"max="+max);
	 terrain.terrainData.SetHeights (0,0,heights);

}

Now this works on itself however it generates very square heightmap. So I’m looking to create a less squire terrain and more realistic. Does somebody around here know how to further improve this function?

So does anybody have any advice I have already tried to generate a 1d noise function and use it to displace the noise however that looked very odd.

From what I can tell from your code you are using the Random.Range() function to return a random height for each vert in the terrain?

Random.Range() returns whats called ‘white noise’ and would be unsuitable for a terrains height. What you need is some thing call ‘gradient noise’. The most well know is perlin noise but there are many others.

There is built-in perlin noise in unity found in Mathf.PerlinNoise().

You will need to ‘layer’ a few samples of this function at different frequencies to create fractal noise that is best for terrain. This can be quite costly in terms of performance.

Not sure what 1D noise function you used but a terrain is 2D so it would look odd. You need a 2D noise function. The builtin unity function is 2D.

What I’m basically doing is assign squares over the height map then increase each of them by a different Random.Range(). After that I half the size of my square and repeat the procedure. The 1D displacement I used was a noise function for 1D that was always slightly higher or lower then it’s previous settings then I took the x value of that many squares higher then I asked for (in order to reduce the odd square look of my map it didn’t work).

But anyway what are the in and output variables of the Mathf.PerlinNoise()? How do I chance it’s settings?

I’m currently trying this:

	for( y=0;y<yRes;y++){
	heights[x/xRes,y/yRes]=Mathf.PerlinNoise(x,y);
	}}

But that isn’t working.

The out put is a number between 0 and 1 that can be use for your terrain height. Just scale it to more suitable level.

The input would be the verts 2D position in space, ie the x and y values.

There are also some perlin noise plugins around for unity that maybe better for terrain generation than the buitin function.

It seems to only generate a single number or is something wrong with this code

	for( x=0;x<50;x=x+10){
	for( y=0;y<50;y=y+10){
	print(Mathf.PerlinNoise(x,y));
}
}

It only generates the number 0.4652731 .
Which I highly doubt is what it is supposed to do or shouldn’t I be giving it whole numbers?

Never mind I got it functional!
if anyone is interested:

	function Start () {

	    var xRes = terrain.terrainData.heightmapWidth;

	    var yRes = terrain.terrainData.heightmapHeight;

	    var heights = terrain.terrainData.GetHeights(0, 0, xRes, yRes);


	var fxRes : float=xRes;
	var fyRes : float =yRes;
		for( x=0.0;x<xRes;x++){
	for( y=0.0f;y<yRes;y++){
		heights[x,y]=Mathf.PerlinNoise(x/fxRes,y/fyRes);
	}}
			for( x=0.0;x<xRes;x++){
	for( y=0.0f;y<yRes;y++){
		
	}}

Sorry, forgot to add its best to scale down the x and y values if they are whole numbers.

You could use any value to scale, it didnt have to be your width and height values. This scaling factor is whats know as the noise’s frequency.

At any rate a really big thank you:smile:

Now I need to somehow combine the heightdata to game world coordinates. Currently I have this:

var relx=Random.Range(0,xRes);
		var rely=Random.Range(0,yRes);
		var xpos=transform.position.x+relx;
		var zpos=transform.position.y+rely;
		var ypos=transform.position.z+heights[relx,rely]*rel;
		
		Instantiate (tree1, Vector3(xpos, ypos, zpos), Quaternion.identity);
		}
		print(heights[0,165]);

Which is meant to spawn trees at random positions on the map however I need to find the correct value for rel as with rel=1 it spawns them all the way beneath the map. Real=850 seems to result in trees sort of hanging above and below the map. So how do I fix the position?

Use LibNoise.NET