这是生成网格的部分代码,如果你想了解更多信息,请问.所以在场景中我看不到它,我试图进入播放模式,但什么都没有,如果我进入检查器,我可以看到。4个顶点,2个三边形,三边形和顶点的数量是正确的。
public void ConstructMesh()
{
Vector3[] vertices = new Vector3[(resolution + 2) * (resolution + 2)];
int[] triangles = new int[(resolution + 1) * (resolution + 1) * 6];
int triIndex = 0;
for (int y = 0; y < resolution; y++)
{
for (int x = 0; x < resolution; x++)
{
int i = x + y * resolution;
Vector2 percent = new Vector2(x, y) / (resolution - 1);
Vector3 pointOnUnitCode = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;
vertices[i] = pointOnUnitCode;
triangles[triIndex] = i;
triangles[triIndex + 1] = i + resolution + 1;
triangles[triIndex + 2] = i + resolution;
triangles[triIndex + 3] = i;
triangles[triIndex + 4] = i + 1;
triangles[triIndex + 5] = i + resolution + 1;
triIndex += 6;
}
}
planet.Clear();
planet.vertices = vertices;
planet.triangles = triangles;
planet.RecalculateNormals();
}
你需要一个MeshRenderer和MeshFilter在GameObject中才能够渲染你的网格。
假设你的方法ConstructMesh是一个MonoBehaviour,并且它被连接到你想要显示网格的同一个对象上,你可以这样做。
GetComponent<MeshFilter>().mesh = planet;