/// <summary> /// 获取面列表. /// </summary> /// <param name="chars">Chars.</param> private void GetTriangleList(string[] chars) { var indexVectorList = new List <ObjVector3>(); var triangleList = new List <ObjVector3>(); for (int i = 1; i < chars.Length; ++i) { //将每一行按照空格分割后从第一个元素开始 //按照/继续分割可依次获得顶点索引、法线索引和UV索引 var indexs = chars[i].Split('/'); var indexVector = new ObjVector3(0, 0, 0); //顶点索引 indexVector.x = ConvertToInt(indexs[0]); //法线索引 if (indexs.Length > 1) { if (indexs[1] != "") { indexVector.y = ConvertToInt(indexs[1]); } } //UV索引 if (indexs.Length > 2) { if (indexs[2] != "") { indexVector.z = ConvertToInt(indexs[2]); } } //将索引向量加入列表中 indexVectorList.Add(indexVector); } //这里需要研究研究 for (int j = 1; j < indexVectorList.Count - 1; ++j) { //按照0,1,2这样的方式来组成面 triangleList.Add(indexVectorList[0]); triangleList.Add(indexVectorList[j]); triangleList.Add(indexVectorList[j + 1]); } //添加到索引列表 foreach (ObjVector3 item in triangleList) { faceVertexNormalUV.Add(item); } }
/// <summary> /// 合并三角面 /// </summary> private void Combine() { //使用一个字典来存储要合并的索引信息 Dictionary <int, ArrayList> toCambineList = new Dictionary <int, ArrayList>(); for (int i = 0; i < faceVertexNormalUV.Count; i++) { if (faceVertexNormalUV[i] != ObjVector3.zero) { //相同索引的列表 ArrayList SameIndexList = new ArrayList(); SameIndexList.Add(i); for (int j = 0; j < faceVertexNormalUV.Count; j++) { if (faceVertexNormalUV[j] != ObjVector3.zero) { if (i != j) { //如果顶点索引和法线索引相同,说明它们在一个面上 ObjVector3 iTemp = (ObjVector3)faceVertexNormalUV[i]; ObjVector3 jTemp = (ObjVector3)faceVertexNormalUV[j]; if (iTemp.x == jTemp.x && iTemp.y == jTemp.y) { //将索引相同索引列表然后将其重置为零向量 //PS:这是个危险的地方,如果某个索引信息为Vector3.Zero //就会被忽略过去,可是貌似到目前为止没有发现为Vector3.Zero的情况 SameIndexList.Add(j); faceVertexNormalUV[j] = ObjVector3.zero; } } } } //用一个索引来作为字典的键名,这样它可以代替对应列表内所有索引 toCambineList.Add(i, SameIndexList); } } //初始化各个数组 this.VertexArray = new ObjVector3[toCambineList.Count]; this.UVArray = new ObjVector2[toCambineList.Count]; this.NormalArray = new ObjVector3[toCambineList.Count]; this.TriangleArray = new int[faceVertexNormalUV.Count]; //定义遍历字典的计数器 int count = 0; //遍历词典 foreach (KeyValuePair <int, ArrayList> IndexTtem in toCambineList) { //根据索引给面数组赋值 foreach (int item in IndexTtem.Value) { TriangleArray[item] = count; } //当前的顶点、UV、法线索引信息 ObjVector3 VectorTemp = (ObjVector3)faceVertexNormalUV[IndexTtem.Key]; //给顶点数组赋值 VertexArray[count] = (ObjVector3)vertexArrayList[(int)VectorTemp.x - 1]; //给UV数组赋值 if (uvArrayList.Count > 0) { ObjVector3 tVec = (ObjVector3)uvArrayList[(int)VectorTemp.y - 1]; UVArray[count] = new ObjVector2(tVec.x, tVec.y); } //给法线数组赋值 if (normalArrayList.Count > 0) { NormalArray[count] = (ObjVector3)normalArrayList[(int)VectorTemp.z - 1]; } count++; } }