private Mesh Construct() { Mesh mesh = new Mesh(new Vertex[vertices.Count], new Face[0]) { Name = name, Comments = comments }; // add vertex position & color for (int i = 0; i < vertices.Count; i++) { var vertex = vertices[i]; var position = vertex.Position; var color = new Color4(vertex.Color.X, vertex.Color.Y, vertex.Color.Z, 1); mesh.Vertices[i] = new Vertex(position, color); } // facegroup (obj) = submesh (BE) foreach (var faceGroup in faceGroups) { var submesh = new Submesh(new Face[faceGroup.Faces.Count]) { Name = faceGroup.Name, Comments = faceGroup.Comments }; for (int i = 0; i < faceGroup.Faces.Count; i++) { // convert faces var objFace = faceGroup.Faces[i]; submesh.Faces[i] = new Face(3); for (int v = 0; v < 3; v++) { ObjFaceVertex faceVertex = objFace.Vertices[v]; ushort vertexId = faceVertex.VertexIndex; submesh.Faces[i].Indices[v] = vertexId; // add missing (indexed / referenced) data to vertex if (normals.Count > 0) { mesh.Vertices[vertexId].Normal = normals[faceVertex.NormalIndex]; } if (uvs.Count > 0) { mesh.Vertices[vertexId].UV = uvs[faceVertex.UVIndex]; } } } mesh.Submeshes.Add(submesh); } return(mesh); }
private void ParseFace(string[] data) { int length = data.Length - 1; var face = new ObjFace(3); currentFaceGroup.Faces.Add(face); for (int i = 0; i < length; i++) { string[] parts = data[i + 1].Split('/'); var vert = new ObjFaceVertex(); vert.VertexIndex = ParseIndex(parts, 0); if (parts.Length > 1 && !string.IsNullOrEmpty(parts[1])) { vert.UVIndex = ParseIndex(parts, 1); } if (parts.Length > 2 && !string.IsNullOrEmpty(parts[2])) { vert.NormalIndex = ParseIndex(parts, 2); } if (i < 3) { face.Vertices[i] = vert; } else { // polygon > 3 vertices -> triangulate: add new face for every additional polygon var newFace = new ObjFace(3); newFace.Vertices[0] = face.Vertices[0]; newFace.Vertices[1] = face.Vertices[2]; newFace.Vertices[2] = vert; face = newFace; currentFaceGroup.Faces.Add(face); } } }
public static void AppendObjTriangle(ObjItem objModel, ObjFaceVertex faceVertex, ref List<float> vertexPositions, ref List<float> vertexNormals, ref List<float> vertexTextureCoordinates, ref List<float> vertexBoneWeights, ref List<float> vertexBoneIndices, ref List<uint> indeces) { if (indeces != null) indeces.Add((uint)(vertexPositions.Count / 3)); if (vertexPositions != null) { var vertexPosition = objModel.Vertices[faceVertex.VertexIndex - 1]; vertexPositions.Add(vertexPosition.X); vertexPositions.Add(vertexPosition.Y); vertexPositions.Add(vertexPosition.Z); } if (vertexNormals != null) { if (objModel.Normals.Count == 0) { vertexNormals.AddRange(new[] { 0.0f, 0.0f, 0.0f }); } else { var vertexNormal = objModel.Normals[faceVertex.NormalIndex - 1]; vertexNormals.Add(vertexNormal.X); vertexNormals.Add(vertexNormal.Y); vertexNormals.Add(vertexNormal.Z); } } if (vertexTextureCoordinates != null) { var vertexTexture = objModel.TextureCoords[faceVertex.TextureIndex - 1]; vertexTextureCoordinates.Add(vertexTexture.X); vertexTextureCoordinates.Add(vertexTexture.Y); } if (vertexBoneIndices != null) vertexBoneIndices.AddRange(new[] { 0.0f, 0.0f, 0.0f, 0.0f }); if (vertexBoneWeights != null) vertexBoneWeights.AddRange(new[] { 0.0f, 0.0f, 0.0f, 0.0f }); }
private static ObjFaceVertex ParseFaceVertex(string vertexString) { var fields = vertexString.Split(new[] { '/' }, StringSplitOptions.None); var vertexIndex = StringConverter.ToInt(fields[0]); var faceVertex = new ObjFaceVertex(vertexIndex, 0, 0); if (fields.Length > 1) { var textureIndex = fields[1].Length == 0 ? 0 : StringConverter.ToInt(fields[1]); faceVertex.TextureIndex = textureIndex; } if (fields.Length > 2) { var normalIndex = fields.Length > 2 && fields[2].Length == 0 ? 0 : StringConverter.ToInt(fields[2]); faceVertex.NormalIndex = normalIndex; } return faceVertex; }