- Home /
Perlin Terrain Generation?
I'm having trouble finding a tutorial on this but would i use Perlin noise or any other noise function to generate a terrain mesh?
Answer by grahnzz · Jan 10, 2015 at 03:45 AM
http://lmgtfy.com/?q=perlin+noise+unity+terrain
There's a lot of resources on this on the web. But perlin noise or any other coherent noise function is nothing more than a random number generator that produces values that can be predicted assuming you are using the same parameters, that is why for example minecraft can build the same level for you using just one integer as the seed.
This noise can then be applied to a mesh, typically this would be done along the world Y-axis only. If you want to use the unity Terrain system for a more performant solution that should be possible too; by applying the noise to a heightmap, i'm not sure how you would do this at runtime though. I have a feeling that unity's terrain system isn't designed to update its mesh dynamically.
I will leave you with some code from http://docs.unity3d.com/ScriptReference/Mesh.html
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Update() {
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
Vector3[] normals = mesh.normals;
int i = 0;
while (i < vertices.Length) {
vertices[i] += normals[i] * Mathf.Sin(Time.time);
i++;
}
mesh.vertices = vertices;
}
}
this code goes through all vertices in the mesh and extrudes them along the normal of the vertex by a multiplier of Mathf.Sin(). Try swapping that Mathf.Sin() with a Mathf.PerlinNoise(vertices[i].x, vertices[i].y)
If you like the idea of procedurally building your terrain, but the terrain doesn't need to be built during the runtime of your game. Take a look @ http://www.world-machine.com/ World machine is a tool that helps you build terrain with the help of noise nodes and other mathematical functions
thank you i should have clarified though i wasnt actually trying to generate terrain a t runtime or flat terrain for that matter but planetary terrain so i can make random planets i just didnt know how to manipulate the verts.
Answer by Ericool · Jan 10, 2015 at 03:39 AM
Or you could find a texture generated by Perlin Noise look for extension raw and the kind of texture is a terrain heightmap. So then use the terrain editor to generate it for you it is easier than generating yourself the terrain with an algorithm that is already known . Also your terrain wont be different than if I use the terrain editor. Just saying if you want to save some time. Good Luck.
But if you really want to do it , how it works it that the intensity of a pixel(in the texture) is the height ratio of a vertex or a block of vertices. So you would have to do a double for loop and adding a vertices at location [i][j] and the vertices[i][j].position = GetPixel().alpha * 10.0f; Something like that for sure. Once the mesh is done you must save it as an asset. Using SaveAssets().
A year or two ago I have done a terain mesh in c++ and it takes time..but the algorithm is easy.
Your answer
![](https://koobas.hobune.stream/wayback/20220613180023im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
help with terrains 1 Answer
Unity Terrain looking strange and dark 0 Answers
Access terrains "Alphamap 0" from within code 0 Answers
Terrain Collider doesn't update/ how to create a new terrain Collider? 2 Answers
Multiple terrain with different values 0 Answers