- Home /
Cube terrain with perlin noise
hi, iv made a script (JS) that generate a 20x20 terrain of cubes, with random hills using perlin noise.
but i have a few problems to solve and questions to ask:
when i generate the terrain again it gives me the exact same terrain, how would i make it automatically give me something else?
how would i make this infinite? how would i make it so when the player goes forward into the world the world would keep up with him? but still look normal (can i make something with chunks?)
how can i implement a seed system ware when you enter a seed you will get the same terrain every time?
sorry if it's a lot of questions...
the script: var size:Vector2 = new Vector2(20, 20); var height:float = 10.0f; var noiseStrength:float = 10.0f;
private var perlinNoise;
private var root:GameObject;
function OnGUI(){
if(GUI.Button (Rect(10, 10, 100, 30), "Generate")){
Generate();
}
}
function PerlinNoise(x:float, y:float){
var noise:float = Mathf.PerlinNoise(x / noiseStrength, y/ noiseStrength);
return noise * height;
}
function Generate(){
Destroy(GameObject.Find("Terrain"));
root = new GameObject("Terrain");
root.transform.position = new Vector3(size.x/2, 0, size.y/2);
for(var i:int = 0; i <= size.x; i++){
for(var p:int = 0; p <= size.y; p++){
var box:GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
box.transform.position = new Vector3( i, PerlinNoise( i, p ), p);
box.transform.parent = root.transform;
}
}
root.transform.position = Vector3.zero;
print(PerlinNoise);
}
Answer by ExtremePowers · Sep 30, 2014 at 07:20 PM
For the result being the same all the time, add a seed to the x and y Like this: var noise:float = Mathf.PerlinNoise((Seed + x) / noiseStrength, (Seed + y) / noiseStrength);
To save the seed try
var Seed = 0;
function Start() {
if (PlayerPrefs.HasKey("Seed")) {
Seed = PlayerPrefs.GetInt("Seed");
}
}
function OnGUI() {
Seed = GUI.TextField(Rect(0, 50, 100,30), Seed.ToString());
if (GUI.Button(Rect(0,0,100,30), "Save seed") {
PlayerPrefs.SetInt("Seed", Seed);
PlayerPrefs.Save();
}
}