- Home /
Make a high poly count plane with code
Okay, so, I've been searching for this question all day and have yet to get anywhere. With a bunch of aids I've been able to make a low poly plane with code, that has 4 verts and 2 tris, But I want it to be more than that.
I have everything set to an array that can be adjusted on the start of the game, that makes several points in a 2D grid, and I'm trying to make it so that it makes a vertex and a connecting triangle across all the points.. But it just seems to make a stretched out plane of 4 verts and 2 tris... What step am I missing, and is it a simple fix that I just can't find?
Here's the code thus far:
var Landsize : int;
var size : float;
var Land : GameObject;
var scale : float = 1.0;
var power : float = 3.0;
var Adj : float;
var Grid : Vector3[,];
var NewPos : Vector3;
private var Sample : Vector2 = new Vector2(0f,0f);
function Start()
{
var Width : float = Landsize;
var Height : float = Landsize;
Grid = new Vector3[Landsize,Landsize];
Sample = new Vector2(Random.Range(0.0f,size),Random.Range(0.0f,size));
var m : Mesh = new Mesh();
m.name = "Scripted_Plane_New_Mesh";
for(var yCount=0;yCount<Landsize;yCount++)
{
Width -= Landsize;
Height += Adj;
for(var xCount=0;xCount<Landsize;xCount++)
{
Width += Adj;
NewPos += Vector3(Width,0.01,Height);
Grid[yCount,xCount] = NewPos;
}
}
for(var Point : Vector3 in Grid)
{
m.vertices = [Vector3(-Point.x, 0.01, -Point.z), Vector3(Point.x, 0.01, -Point.z), Vector3(Point.x, 0.01, Point.z), Vector3(-Point.x, 0.01, Point.z)];
m.uv = [Vector2 (0, 0), Vector2 (0, 1), Vector2(1, 1), Vector2 (1, 0)];
m.triangles = [0, 1, 2, 0, 2, 3];
m.RecalculateNormals();
}
var obj : GameObject = new GameObject("New_Plane_Fom_Script", MeshRenderer, MeshFilter, MeshCollider);
obj.GetComponent(MeshFilter).mesh = m;
obj.transform.position = Vector3.zero;
obj.transform.tag = "Land";
Land = obj;
MoveIt();
}
"MoveIt();" takes all the vertices from the object that was just spawned from the code and moves it along a Perlin texture, which that part thus far works just fine, but there needs to be a -lot- more vertices for it to make a difference.
You may want to fix your code as a learning exercise, but if you just want to the plane, you can use the CreatePlane editor script from the Unity Wiki.
You can either just make a plane at edit time and attach you Perlin noise code, or you can exa$$anonymous$$e/borrow the guts to help you with your code.
Note that there is a 64$$anonymous$$ limit to the number of vertices in a mesh, so the CreatePlane editor script has width/length limits. You can build something that is 254 x 254 with the script.
Your answer
Follow this Question
Related Questions
Terrain help 0 Answers
Perlin Noise for 3d Voxel Terrain 1 Answer
Can anyone explain this code? 1 Answer
2D Efficient Realtime Terrain Generation 0 Answers
3d Perlin Noise? 3 Answers