- Home /
Procedural Terrain?
Is it possible to generate terrain semi-procedurally or automatically? Not really sure what the proper terminology is...
Basically, I have a massive heightmap that outlines the general landmass. As opposed to carving out details totally by hand or adding a lot of random noise to it or something, would it be possible to draw out things like mountains and generate them procedurally, and save that data? I read about random generation of essentially infinite terrain at runtime, but is it possible to do it just to aid the development process as opposed to doing it at runtime?
Answer by Kos-Dvornik · Nov 22, 2013 at 10:27 AM
This 3 tutorials cover all procedural terrain generation aspects in Unity:
http://kostiantyn-dvornik.blogspot.com/2013/01/unity-4-cool-sweet-terrain-tutorial.html
http://kostiantyn-dvornik.blogspot.com/2013/11/awesome-unity-texturing-terrain-tutorial.html
http://kostiantyn-dvornik.blogspot.com/2014/02/huge-amazing-unity-terrain-tutorial.html
Additional split terrain script:
http://kostiantyn-dvornik.blogspot.com/2013/12/unity-split-terrain-script.html
Automatic grass placement: https://doctrina-kharkov.blogspot.com/2019/08/unity-automatic-grass-terrain.html
Answer by SirGive · May 29, 2011 at 09:10 AM
Yes, and this has been done with Unity. Let me try to find the link. There was a video and everything. It was done really nicely.
A quick google search yielded it. And it is indeed called procedurally generated terrain. Head to page 2 and its at the bottom.
Wow... so Unity basically just about does everything you could want it to, doesn't it?
Answer by HuskyPanda213 · Mar 06, 2014 at 05:55 PM
Kind of late, but if you have terrain toolkit I've made a script to generate random textures/heightmaps using terraintoolkit:
using UnityEngine;
using System.Collections;
public class PerlinTerrain : MonoBehaviour {
//Toolkit instance.
[SerializeField]TerrainToolkit kit;
//Array of textures.
[SerializeField]Texture2D sandTexture;
[SerializeField]Texture2D grassTexture;
[SerializeField]Texture2D rockTexture;
[SerializeField]Texture2D cliffTexture;
void Start() {
//Check if we have the kit assigned;.
if (kit == null) {
//If there is not an instance assigned or on gameObject, return.
if (!GetComponent<TerrainToolkit>())
{
return;
}
//Else assign kit to gameObject's toolkit.
else
{
kit = GetComponent<TerrainToolkit>();
}
}
//Generate the terrain.
Generate();
}
void OnGUI(){
if (GUILayout.Button ("Generate")) {
Generate();
}
}
public void Generate() {
//Generate the perlin terrain.
kit.PerlinGenerator((int)Random.Range(3,6),Random.Range(0.4f,0.9f),Random.Range(2,6), 1f);
//Gives it a less smooth feel.
kit.PerlinGenerator(4,4,4, 0.1f);
//Creates arrays for stops.
float[] slopeStops = new float[2];
float[] heightStops = new float[4];
Texture2D[] terrainTextures = new Texture2D[4];
//Assigns values to the arrays.
slopeStops[0] = 30f; slopeStops[1] = 70f;
heightStops[0] = Random.Range(0.05f, 0.18f);
heightStops[1] = Random.Range(0.19f, 0.49f);
heightStops[2] = Random.Range(0.5f, 0.69f);
heightStops[3] = Random.Range(0.7f, 0.89f);
terrainTextures[0] = cliffTexture;
terrainTextures[1] = sandTexture;
terrainTextures[2] = grassTexture;
terrainTextures[3] = rockTexture;
//Paints the textures.
kit.TextureTerrain(slopeStops, heightStops, terrainTextures);
}
public void Generate(bool doublePerlin) {
//Generate the perlin terrain.
kit.PerlinGenerator((int)Random.Range(3,6),Random.Range(0.4f,0.9f),Random.Range(2,6), 1f);
//Gives it a less smooth feel.
if(!doublePerlin){
kit.PerlinGenerator(4,4,4, 0.1f);
}
//Creates arrays for stops.
float[] slopeStops = new float[2];
float[] heightStops = new float[4];
Texture2D[] terrainTextures = new Texture2D[4];
//Assigns values to the arrays.
slopeStops[0] = 30f; slopeStops[1] = 70f;
heightStops[0] = Random.Range(0.05f, 0.18f);
heightStops[1] = Random.Range(0.19f, 0.49f);
heightStops[2] = Random.Range(0.5f, 0.69f);
heightStops[3] = Random.Range(0.7f, 0.89f);
terrainTextures[0] = cliffTexture;
terrainTextures[1] = sandTexture;
terrainTextures[2] = grassTexture;
terrainTextures[3] = rockTexture;
//Paints the textures.
kit.TextureTerrain(slopeStops, heightStops, terrainTextures);
}
}
thanks Husky, this is interesting. The terrain toolkit doesn't always update my height changes.
I switched the 1st part around to get a smoother terrain
//Generate the perlin terrain.
kit.PerlinGenerator((int)4,Random.Range(0.16f,0.18f),(int)3, 1f);
//Gives it a less smooth feel.
//kit.PerlinGenerator(4,4,4, 0.1f);
I commented out the 'less smooth' part. Now I get a smooth terrain with only the sand, what do I want to change to get all 4 textures on a smooth terrain?
https://sites.google.com/site/terrymorgan1213/tutorials/terrain_toolkit/smoother_but_only_1_tex.jpg
Never $$anonymous$$d, I realized my terrain was 70 units high out of a possible 600, 12%, so I fit all my texture changes in the 1st 12%.
Your answer
![](https://koobas.hobune.stream/wayback/20220612091612im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Connecting Perlin Terrain Tiles 1 Answer
Mesh based on noise 1 Answer
Terrain height map influences ocean wave heights? 2 Answers
How to seamlessly tile terrains 0 Answers
Spawning above the terrain 2 Answers