I just made a script to create a mesh via code. So my Problem now is that I only know how to create a Mesh with triangles, but there should be a way to use squares instead of triangles. The last time I worked on this I had a prefab that I manipulated... the prefab was made in 3DSMAX and had no triangles, just like I modelled it. So i would be really grateful if someone got a clue on this.
Here is my Code:
//MESH-SETUP
MeshFilter vertMeshFilter = vertics.GetComponents<MeshFilter>()[0];
Mesh vertMesh = new Mesh();
vertMeshFilter.mesh = vertMesh;
//VERTS
Vector3[] vertices = new Vector3[4]
{
new Vector3(0f, 0.0f, 0.0f),
new Vector3(width, 0.0f, 0.0f),
new Vector3(0.0f, height, 0.0f),
new Vector3(width, height, 0.0f)
};
//TRIANGLES
int[] tri = new int[6];
tri[0] = 0;
tri[1] = 2;
tri[2] = 1;
tri[3] = 2;
tri[4] = 3;
tri[5] = 1;
//NORMALS
Vector3[] normals = new Vector3[4];
normals[0] = Vector3.forward;
normals[1] = Vector3.forward;
normals[2] = Vector3.forward;
normals[3] = Vector3.forward;
//UV
Vector2[] uvs = new Vector2[4];
uvs[0] = new Vector2(0, 0);
uvs[1] = new Vector2(1, 0);
uvs[2] = new Vector2(0, 1);
uvs[3] = new Vector2(1, 1);
//ASSIGN
vertMesh.vertices = vertices;
vertMesh.triangles = tri;
vertMesh.normals = normals;
vertMesh.uv = uvs;
The solution is to split the original square ( the ones you have ) into two triangles and construct the mesh with these triangles. It could worth some attention to keep the vertex order in a manner the two resulting triangles "faces" the same as the starting square.
From the image above, the original square is v0,v1,v2,v3, the yielded triangles will be: v0,v1,v2
and v2,v3,v0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.