Simple plane generation

 0 Hello everyone,Im just trying to procedurally generate meshes and got some problems just at the beginning. I just want to create a cube, but to look if i understand everything correct I wanted to test a plane.So I wrote that code: ``````function Start () { var block : GameObject = new GameObject("Block"); var mesh : Mesh = new Mesh (); block.AddComponent(MeshFilter); block.AddComponent(MeshRenderer); var vertices : Vector3[]; var triangles : int[]; vertices[0] = Vector3(0,0,0); vertices[1] = Vector3(1,0,0); vertices[2] = Vector3(1,0,1); vertices[3] = Vector3(0,0,1); var uvs = new Vector2[mesh.vertices.length]; for (var i=0; i

 0 There are several errors: 1- the arrays vertices and triangles were just declared - you must allocate them with the new keyword specifying the desired size (they are built-in arrays, not Array class instances); 2- the triangles are wrong: the two triangles overlap, and have different winding orders, what makes only one triangle visible from each side; 3- you must assign the created mesh to meshFilter.mesh; 4- you're calculating uv coordinates based on mesh.vertices before assigning the vertices to mesh.vertices; 5- you must assign some material to the mesh renderer, or the triangles will be rendered in that lovely pink color; ```function Start () { var block : GameObject = new GameObject("Block"); var mesh : Mesh = new Mesh (); // save a reference to the MeshFilter (you will need it): var meshFilter = block.AddComponent(MeshFilter); block.AddComponent(MeshRenderer); var vertices = new Vector3[4]; // allocate the arrays with new! var triangles = new int[6]; vertices[0] = Vector3(0,0,0); // I changed the vertices order to clockwise just vertices[1] = Vector3(0,0,1); // to help understanding the winding order (the vertices[2] = Vector3(1,0,1); // triangles winding order is what really matters) vertices[3] = Vector3(1,0,0); triangles = [0,1,2,0,2,3]; // the triangles now are in clockwise order! mesh.vertices = vertices; // assign mesh.vertices and mesh.triangles... mesh.triangles = triangles; // prior to use them in the uv calculation! var uvs = new Vector2[mesh.vertices.length]; for (var i=0; i < uvs.Length; i++) { uvs[i] = Vector2(mesh.vertices[i].x, mesh.vertices[i].z); } mesh.uv = uvs; // assign the new uv coordinates... meshFilter.mesh = mesh; // and assign the mesh to MeshFilter.mesh } ``` more ▼ answered Feb 22 '12 at 02:12 AM aldonaletto 41.5k ● 16 ● 42 ● 197 Thanks, helped me alot! Cube is done :) Feb 22 '12 at 09:38 PM Ebil add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x5084
x1364
x300
x289
x34

asked: Feb 22 '12 at 01:04 AM

Seen: 1360 times

Last Updated: Feb 22 '12 at 09:38 PM