private int VertexIndexFromFaceElement(ObjParser.Types.Face face, int elementIndex) { int faceVertexIndex = face.VertexIndexList[elementIndex]; int faceTextureVertexIndex = face.TextureVertexIndexList[elementIndex]; int vertexIndex; ObjParser.Types.Vertex vertex = obj.VertexList[faceVertexIndex - 1]; if (faceIndex.ContainsKey(faceVertexIndex, faceTextureVertexIndex)) { vertexIndex = faceIndex.VertexIndex(faceVertexIndex, faceTextureVertexIndex); } else { vertices.Add(new Vector3((float)vertex.X, (float)vertex.Y, (float)vertex.Z) + loadObjectOffset); if (faceTextureVertexIndex != 0) { ObjParser.Types.TextureVertex textureVertex = obj.TextureList[faceTextureVertexIndex - 1]; uvs.Add(new Vector2((float)textureVertex.X, (float)textureVertex.Y)); } vertexIndex = vertices.Count - 1; faceIndex.Add(faceVertexIndex, faceTextureVertexIndex, vertexIndex); } return(vertexIndex); }
private List <ObjParser.Types.TextureVertex> ObjTextureVerticesOfListVector2(Vector2[] uvs) { List <ObjParser.Types.TextureVertex> textureVertexList = new List <ObjParser.Types.TextureVertex>(); int i = 0; foreach (Vector2 uv in uvs) { ObjParser.Types.TextureVertex newVertex = new ObjParser.Types.TextureVertex(); newVertex.X = uv.x; newVertex.Y = uv.y; newVertex.Index = i; textureVertexList.Add(newVertex); i++; } return(textureVertexList); }
public SyncMesh BuildMesh(Obj curObj) { SyncMesh syncmesh = new SyncMesh(new SyncId("MeshId_" + curObj.Name), "Mesh_" + curObj.Name); // Faceの定義に従う。無駄は多くなる syncmesh.SubMeshes = new List <SyncSubMesh>(); SyncSubMesh submesh = new SyncSubMesh(); for (int j = 0; j < curObj.FaceList.Count; j++) { ObjParser.Types.Face face = curObj.FaceList[j]; for (int i = 0; i < face.VertexIndexList.Length; i++) { //idx int vertexIdx = face.VertexIndexList[i] - 1; int uvIdx = face.TextureVertexIndexList[i] - 1; int normalIdx = face.NormalIndexList[i] - 1; //Console.WriteLine(string.Format("index[{0}] vert[{1}] uv[{2}] normal[{3}]",i, vertexIdx+1, uvIdx+1, normalIdx+1)); //ObjParser.Types.Vertex vertex = curObj.VertexList[vertexIdx]; //ObjParser.Types.TextureVertex uv = curObj.TextureList[uvIdx]; //ObjParser.Types.VertexNormal normal = curObj.VertexNormalList[normalIdx]; ObjParser.Types.Vertex vertex = obj.VertexList[vertexIdx]; ObjParser.Types.TextureVertex uv = obj.TextureList[uvIdx]; ObjParser.Types.VertexNormal normal = obj.VertexNormalList[normalIdx]; //頂点 // OpenGL->DirectX変換のため、Xは反転する syncmesh.Vertices.Add(new Vector3(vertex.X * -1.0f, vertex.Y, vertex.Z)); // 2021.5.26 X軸を反転 syncmesh.Uvs.Add(new Vector2(uv.X, uv.Y)); syncmesh.Normals.Add(new Vector3(normal.X * -1.0f, normal.Y, normal.Z)); // 2021.5.26 X軸を反転 //syncmesh.Normals.Add(new Vector3(0,0,1)); //Console.WriteLine(string.Format("vert={0}\t{1}\t{2}", (float)vertex.X, (float)vertex.Y, (float)vertex.Z)); //index //submesh.Triangles.Add(i + j * face.VertexIndexList.Length); // OpenGL->DirectX変換のため、逆順でセットする submesh.Triangles.Add((face.VertexIndexList.Length - 1 - i) + j * face.VertexIndexList.Length); } } syncmesh.SubMeshes.Add(submesh); return(syncmesh); }