![](/img/trans.png)
[英]How to export vertex color baked from Grasshopper,Rhino3d’ analyses in game engines like Unity, Unreal or directly in the webXr in real time?
[英]Unity Game Engine Crashing When Trying To Update The Vertex Positions Of A Mesh In Real Time Using A Script
所以在统一中,我创建了一个成功生成由三角形组成的平面网格的脚本。 该平面具有400个顶点(20×20网格),其中361个正方形由2个三角形组成,每个三角形包括3个顶点(2166个索引)。 如前所述,顶点,索引和法线在start()函数中设置,顶点被加载到称为顶点的vector3数组中,索引被加载到单个浮点数组中,法线被加载到vector3数组中。 然后将它们分配给Start()函数中的网格(表示平面),如下所示:
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
在update()函数中,调用一个函数来计算平面网格中每个顶点的新位置(400):
void Update ()
{
updateMesh ();
mesh.vertices = vertices;
}
使用updateMesh函数如下所示:
void updateMesh()
{
//This function will update the position of each vertex in the mesh
for (float i = 0f; i<1f; i=+0.05f) {
for (float j = 0f; j<1f; j+=0.05f) {
pos = (int)((i * 20) + (j*20));
vertices[pos] = updatePt(j, i);
}
}
}
更新新顶点后,索引和法线保持不变,但必须将新计算的顶点位置加载到网格对象上。 这也可以在Update()函数中尝试,如上所示。
但是,只要按下播放按钮,发动机就会崩溃并且我不确定为什么 - 重新计算功能是否可能在单位渲染之前没有完成其循环? (我已经减少了网格中顶点的数量,因为当网格有10000个顶点(100x100网格)时会出现同样的问题)。 或者是因为我在Update()函数中没有正确执行某些操作?
我认为这可能是该行的错字
for (float i = 0f; i<1f; i=+0.05f) {
它应该是+ = not = +。
它可能陷入无限循环,因为我永远不会增加。
我只想指出,使用整数迭代有效的2D数组并使用整数数学完全计算最终数组索引会好得多。 例如
如果依赖浮点数学,则可以生成19.99之类的索引。 然后将其舍入以索引数组。 不太理想! 希望这可以帮助。
static int meshWidth = 20;
static int meshHeight = 20;
void updateMesh()
{
for (int i = 0; i< meshHeight; i++) {
for (int j = 0; j < meshWidth; j++)
{
int pos = i * meshWidth + j;
// Also change updatePt func to receive two ints...
vertices[pos] = updatePt(j, i);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.