我提前为这个长篇大论道歉。我正在研究一个C#函数来统一一个Unity3D项目的网格三角形的方向。我使用Vector3
类型,这是一个由float array [3]
组成的结构。
为了做到这一点,我采取了第一个三角形并检查它是否指向外表或向内。之后,我检查一个adiacent,并检查三角形的索引的顺序。如果我按顺序找到几个索引,我会翻转它们。这里有一个例子:
1 2 3
1 2 4
(1 2
与基本三角形的顺序相同,需要翻转到2 1 4
)
4 2 1
(2 1
的顺序不一样,然后什么都不做)检查返回下一个三角形以检查它是否找到;否则它返回一个空向量。 如果“next”不为null我将“next”三角形设为“current”,我将固定三角形存储在变量中,以避免程序继续检查相同的索引并重新检查检查。
问题是某些变量似乎在指令搞砸了我的条件之前更新了它们的值。
正如您在下面的代码中看到的,我有一个非常复杂的If语句,它试图找出两个三角形的指示是否以相同的顺序存在(不一定是相同的位置),如:
1 2 3 (base)
以下所有可能的结果都需要翻转到
1 2 4 -> 2 1 4
4 1 2 -> 1 4 2
2 4 1 -> 4 2 1
需要在mesh.Index[]
之前减去,因为它似乎使用IndexList
值,而不是mesh.Index
值,我不知道为什么。
我正在使用这个自定义结构来测试Unity之外的程序
public struct SimplifiedMesh
{
//this stores the order of the vertices needed to
//create all the triangles of the mesh.
public int[] Index;
it list all the vertices of the mesh
public Vector3[] Vertex;
};
IndexList
用于存储选中的三角形。起初所有值都是正数,但是当它检查一个时,它会将其指数变为负数。
int[] IndexList = new int[Unimesh.Index.Length]; IndexList = Unimesh.Index;
首先,我用不同的方法检查,以确定面部是向外还是向内
FirstCheck(Unimesh, CentreofMesh, currentIndices);
//this will tell the program this triad is already checked
IndexList[currentIndices[0]] *= -1;
IndexList[currentIndices[0] + 1] *= -1;
IndexList[currentIndices[0] + 2] *= -1;
以下现在是艰难的部分。这里是变量的传说:
currentIndices
数组中最后一个三角形的三个指数的位置。它习惯于找到一个新的;Array[3]
是返回下一个要考虑用于检查的向量的返回变量,如果找不到任何三角形,则返回null。mesh.Index
是目前的指数next
获取网格索引,转到“Flipvector”指向的三个索引并交换前两个,反转它们的顺序这里的代码
mesh.Index[currentIndices[0-1-2]
我找到了解决方案。我是C#的初学者,我不知道数组之间的操作“IndexList = Unimesh.Index”传递引用,而不是单个值。我设法以这种方式解决了这个问题。从:
FlipNormals
至:
static int[] AdiacentFace(SimplifiedMesh mesh, int[] IndexList, int[] currentIndices)
{
int[] next = new int[3];
for (int i = 0; i < IndexList.Length; i += 3)
{
if (IndexList[i] > 0 || IndexList[i + 1] > 0)
{
if
// e restituisce la nuova terna modificata
((IndexList[i] == -mesh.Index[currentIndices[0]] && (IndexList[i + 1] == -mesh.Index[currentIndices[1]] || IndexList[i + 2] == -mesh.Index[currentIndices[2]])) ||
(IndexList[i + 1] == -mesh.Index[currentIndices[0]] && (IndexList[i + 2] == -mesh.Index[currentIndices[1]] || IndexList[i] == -mesh.Index[currentIndices[2]])) ||
(IndexList[i + 2] == -mesh.Index[currentIndices[0]] && (IndexList[i] == -mesh.Index[currentIndices[1]] || IndexList[i + 1] == -mesh.Index[currentIndices[2]])))
{
int[] Flipvector = new int[3];
Flipvector[0] = mesh.Index[i];
Flipvector[1] = mesh.Index[i+1];
Flipvector[2] = mesh.Index[i + 2];
FlipNormals(mesh, Flipvector);
// Elimina la terna per i successivi controlli
IndexList[i] *= -1;
IndexList[i + 1] *= -1;
IndexList[i + 2] *= -1;
// Ritorna gli indici del nuovo vettore
next[0] = i;
next[1] = i + 1;
next[2] = i + 2;
return next;
}
else if
((IndexList[i] == -mesh.Index[currentIndices[0]] && (IndexList[i + 2] == -mesh.Index[currentIndices[1]] || IndexList[i + 1] == -mesh.Index[currentIndices[2]])) ||
(IndexList[i + 1] == -mesh.Index[currentIndices[0]] && (IndexList[i] == -mesh.Index[currentIndices[1]] || IndexList[i + 2] == -mesh.Index[currentIndices[2]])) ||
(IndexList[i + 2] == -mesh.Index[currentIndices[0]] && (IndexList[i + 1] == -mesh.Index[currentIndices[1]] || IndexList[i] == -mesh.Index[currentIndices[2]])))
{
// Elimina la terna per i successivi controlli
IndexList[i] *= -1;
IndexList[i + 1] *= -1;
IndexList[i + 2] *= -1;
// Ritorna gli indici del nuovo vettore
next[0] = i;
next[1] = i + 1;
next[2] = i + 2;
return next;
}
}
}
next = null;
return next;
}
这样我只复制值而不是数组的引用。