Perlin Noise Mesh

Hello all. I have started working on terrain generation in unity. So far, I have a small script based off of this post by WillJ: http://forum.unity3d.com/threads/151608-Random-Terrain-Generator. This works well, and creates a block-based terrain, like something from Minecraft. However, I want to make a mesh instead of using blocks. I looked at this page of the documentation, but I’m not sure how to use it for what I need: http://docs.unity3d.com/Documentation/ScriptReference/Mesh.html.

This is the block-based script I have:

private var rotation: Quaternion;
rotation.eulerAngles=Vector3(0,0,0);

private var startpos=Vector3(20,20,20);

var obj:GameObject;
var player:GameObject;

function Start(){
	
	for(var xx:float=0; xx<75; xx++){
		for(var yy:float=0; yy<75; yy++){
			var perlin1=Mathf.PerlinNoise(xx/15,100);
			var perlin2=Mathf.PerlinNoise(yy/15,100);
			
			Instantiate(obj,Vector3(yy,Mathf.Round(perlin1*25*perlin2),xx),rotation);
		}
	}
	
	Instantiate(player,startpos,rotation);
	
}

I guess what I’m asking, is how to use the perlin noise in the code above along with the mesh functions to make a random terrain. Now, I’m not asking anyone to write a script for me, but if someone could at least point me in the right direction with using the mesh functions along with perlin noise, I’d really appreciate it.

Thanks

Learn the Mesh class; you can download the procedural mesh examples to start with.

–Eric

Instead of Instantiating obj at that Vector3 just put the Vector3 in an array and feed the final array to a Mesh instance. You’ll also need to give it a triangles array at the very least - of which there are examples of triangulation and procedural mesh generation floating around.

Also note that you could write Quaternion.identity instead of eulerAngles = new Vector3(,0,0,0) to represent “no rotation” :slight_smile:

Thanks for both the replies. I like the sound of your idea KelsoMRK. I’ll definitely try that out when I get a chance. Thanks for the tip about Quaternion.identity too :slight_smile:

Well, I tried… and failed. Below is a link to a picture of the code, and an error I got:

http://gyazo.com/38fe5bec98d747be47fcd00c79ccb55c

I tried what you said, and put each vector into an array, and put those arrays into a Vector3 for the mesh.vertices. But I now found out you can’t put arrays into a Vector3?.. Also I have no clue how to do the triangles now that my attempt at putting arrays into a vector3 didn’t work out. Below I posted the actual code as well, as humiliating as it is :smile:

private var startpos=Vector3(20,20,20);

var obj:GameObject;
var player:GameObject;

function Start(){
	
	for(var xx:float=0; xx<75; xx++){
		for(var yy:float=0; yy<75; yy++){
			var perlin1=Mathf#PerlinNoise(xx/15,100);
			var perlin2=Mathf#PerlinNoise(yy/15,100);
			
			
			//Instantiate(obj,Vector3(yy,Mathf.Round(perlin1*25*perlin2),xx)Quaternion.identity);
			
			var mesh:Mesh=GetComponent(MeshFilter).mesh;
			
			var vertsx:int[]=new int[yy];
			var vertsy:int[]=new int[Mathf.Round(perlin1*25*perlin2)];
			var vertsz:int[]=new int[xx];
			
			mesh.vertices=Vector3(vertsx,vertsy,vertsz);
			
			//trianglulation...?
			
		}
	}
	
	Instantiate(player,startpos,Quaternion.identity);
	
}

Any help on fixing this would be great. Thanks

mesh.vertices is an array of Vector3’s not a Vector3 of arrays (that’s not even a thing:))

Be honest… am i the dumbest person alive?! :slight_smile: I’ll check it out. Thanks!

edit

Well I though of an error the last thing I wrote had, and now have this:

private var startpos=Vector3(20,20,20);

private var width=75;
private var height=75;

var obj:GameObject;
var player:GameObject;

function Start(){
	
	for(var xx:float=0; xx<width; xx++){
		for(var yy:float=0; yy<height; yy++){
			var perlin1=Mathf.PerlinNoise(xx/15,100);
			var perlin2=Mathf.PerlinNoise(yy/15,100);
			
			
			//Instantiate(obj,Vector3(yy,Mathf.Round(perlin1*25*perlin2),xx),Quaternion.identity);
			
			var mesh:Mesh=GetComponent(MeshFilter).mesh;
			
			var vertices:Vector3[]=new Vector3[width*height];
			
			for(var i=0; i<width; i++){
				vertices[i]=new Vector3(yy,Mathf.Round(perlin1*25*perlin2),0);
				for(var j=0; j<height; j++){
					vertices[i]=new Vector3(0,Mathf.Round(perlin1*25*perlin2),xx);
				}
			}
			
			mesh.vertices=vertices;
			
			//trianglulation...?
			
		}
	}
	
	//Instantiate(player,startpos,Quaternion.identity);
	
}

Whenever you get a chance, have a look and tell me what you think, or if I made another horrible error.
Thanks