- Home /
Tileable perlin noise terrain
I have been trying to make an infinite terrain over the last couple of weeks but I can't make it work, here is my current code:
function GenerateTile(nw : int, nh : int) {
if (GameObject.Find("Chunk "+nw+","+nh)) {
return;
}
var freq : float = Frequency;
var td = new TerrainData();
td.heightmapResolution = 513;
td.size = new Vector3(500,200,500);
var ChunkPosX : float = td.size.x * nw;
var ChunkPosY : float = td.size.z * nh;
var t = Terrain.CreateTerrainGameObject(td);
var hw : float = Terrain.activeTerrain.terrainData.heightmapWidth;
var hh : float = Terrain.activeTerrain.terrainData.heightmapHeight;
var heights = new float[hw, hh];
for (var x = 0; x < hw; x++) {
for (var y = 0; y < hh; y++) {
// World Pos Divided by heightmap height
var xCoord = ((ChunkPosX + x) / (Mathf.PI * scale));
var yCoord = ((ChunkPosY + y) / (Mathf.PI * scale));
if (x == 0 && y == 0) {
Debug.Log(xCoord);
Debug.Log(yCoord);
}
heights[x,y] = Mathf.PerlinNoise(xCoord, yCoord);
}
}
Terrain.activeTerrain.terrainData.SetHeights(0,0,heights);
t.transform.position = new Vector3(ChunkPosX, 0,ChunkPosY);
t.name = "Chunk "+nw+","+nh;
}
What's not working about it? The more specific you are, the easier it will be to help.
When I try to generate a tile it is the same every time, even though the nw and nh is changing.
When generating random terrain if you don't change the seed before you generate it, you will always have the same randoms come out in the same order - thus you'll end up with the same terrain.
Generate yourself a seed based upon system time or something?
I meant that every chunk will look the same, They all have the same hills and such. Example: if I generate chunk (1,0) and (0,0) They look exactly the same. Same with (23, 57) and (53, 89) They always look the same.
Perlin values are between -1 and +1, are you multipying your perlin call by a scalar to actually add some variation? They will all look extremely close in height otherwise.
edit: I lied: in unity they are between 0 and 1.
Answer by knoakes · Aug 08, 2018 at 04:21 PM
Does this video help you... https://www.youtube.com/watch?v=dycHQFEz8VI&t=1176s