【发布时间】:2018-11-16 00:39:13
【问题描述】:
由于我最近才开始研究网格、它们是如何工作的、它们做了什么等等,我决定使用我自己的计算来创建一个圆形的网格。不幸的是,这真的非常非常慢!
所以我正在寻找有关改进的提示,只是让它变慢(因为这可能是它会得到的最好的......)
这是我用来生成圆圈的代码:
public static void createCircle(MeshFilter meshFilter, float innerRadius, float outerRadius, Color color, float xPosition = 0, float yPosition = 0, float startDegree = 0, float endDegree = 360, int points = 100)
{
Mesh mesh = meshFilter.mesh;
mesh.Clear();
//These values will result in no (or very ugly in the case of points < 10) circle, so let's safe calculation and just return an empty mesh!
if (startDegree == endDegree || points < 10 || innerRadius >= outerRadius || innerRadius < 0 || outerRadius <= 0)
{
return;
}
//The points for the full circle shall be whatever is given but if its not the full circle we dont need all the points!
points = (int)(Mathf.Abs(endDegree - startDegree) / 360f * points);
//We always need an uneven number of points!
if (points % 2 != 0) { points++; }
Vector3[] vertices = new Vector3[points];
float degreeStepSize = (endDegree - startDegree) * 2 / (points - 3);
float halfRadStepSize = (degreeStepSize) * Mathf.Deg2Rad / 2f;
float startRad = Mathf.Deg2Rad * startDegree;
float endRad = Mathf.Deg2Rad * endDegree;
//Let's save the vector at the beginning and the one on the end to make a perfectly straight line
vertices[0] = new Vector3(Mathf.Sin(startRad) * outerRadius + xPosition, Mathf.Cos(startRad) * outerRadius + yPosition, 0);
vertices[vertices.Length - 1] = new Vector3(Mathf.Sin(endRad) * innerRadius + xPosition, Mathf.Cos(endRad) * innerRadius + yPosition, 0);
for (int i = 1; i < vertices.Length - 1; i++)
{
//Pure coinsidence that saved some calculatons. Half Step Size is the same as what would needed to be calculated here!
float rad = (i - 1) * halfRadStepSize + startRad;
if (i % 2 == 0)
{
vertices[i] = new Vector3(Mathf.Sin(rad) * outerRadius + xPosition, Mathf.Cos(rad) * outerRadius + yPosition, 0);
}
else
{
vertices[i] = new Vector3(Mathf.Sin(rad) * innerRadius + xPosition, Mathf.Cos(rad) * innerRadius + yPosition, 0);
}
}
mesh.vertices = vertices;
int[] tri = new int[(vertices.Length - 2) * 3];
for (int i = 0; i < (vertices.Length - 2); i++)
{
int index = i * 3;
if (i % 2 == 0)
{
tri[index + 0] = i + 0;
tri[index + 1] = i + 2;
tri[index + 2] = i + 1;
}
else
{
tri[index + 0] = i + 0;
tri[index + 1] = i + 1;
tri[index + 2] = i + 2;
}
}
mesh.triangles = tri;
Vector3[] normals = new Vector3[vertices.Length];
Color[] colors = new Color[vertices.Length];
for (int i = 0; i < vertices.Length; i++)
{
normals[i] = Vector3.forward;
colors[i] = color;
}
mesh.normals = normals;
mesh.colors = colors;
meshFilter.mesh = mesh;
}
我知道我“可以只使用 Unity 附带的 LineRenderer,它比您编写的任何东西都要快”,但这不是重点。 我正在尝试理解网格,看看我可以在哪里调整我的代码以提高它的性能。
提前感谢您的帮助!
【问题讨论】:
-
好吧,我不确定我是否理解你的答案,但我也在 Unity 中研究点云和网格。我所做的是起初我使用
Coroutines来提高性能,但是对于像我这样的大数据来说,何时屈服是一个大问题。现在我正在创建一个新线程并在那里创建我的网格,我不知道是否会变慢,因为 Unity 一直以其他方法挂起,但至少 Unity 不再与这个挂起。但是你必须小心 Unity 中的多线程编程。您可以查看this。 -
@AliKanat 感谢您的评论。我实际上对任何事情都持开放态度,基本上是在寻找一些输入、想法和改进技巧……我一定会查看您的链接,非常感谢!
标签: performance unity3d mesh