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?
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.
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?
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?