创建没有三角形的网格

时间:2015-02-02 16:11:00

标签: c# unity3d mesh

我刚制作了一个脚本来通过代码创建网格。所以现在我的问题是我只知道如何使用三角形创建网格,但应该有一种方法来使用正方形而不是三角形。我最后一次研究这个时,我制作了一个预制件,我操作了......预制件是用3DSMAX制作的,没有三角形,就像我建模一样。如果有人对此有所了解,我将非常感激。

这是我的代码:

//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;

1 个答案:

答案 0 :(得分:2)

解决方案是将原始正方形(您拥有的正方形)分割成两个三角形,并使用这些三角形构造网格。值得注意的是保持顶点顺序的方式是两个结果三角形&#34;面对&#34;和起始广场一样。

enter image description here

从上图中,原始正方形是v0,v1,v2,v3,得到的三角形将是: v0,v1,v2v2,v3,v0

相关问题