我可以创建一个网格(在清醒中),但我看不到它



这是生成网格的代码部分,如果您想了解更多信息,请询问。 所以在场景中我看不到它,我试图进入播放模式,但什么都没有,如果我进入检查器,我可以看到:4 个垂直,2 个 tris,适量的 tris 和垂直。

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();
}

您需要在游戏对象中使用网格渲染器和网格过滤器才能渲染网格。

假设您的方法ConstructMesh是一个MonoBehavior,并且它附加到要显示网格的同一对象,则可以执行以下操作:

GetComponent<MeshFilter>().mesh = planet;

相关内容

最新更新