unsafe IOReadResult BuildMeshes_Simple(ReadOptions options, IMeshBuilder builder) { if (vPositions.Length == 0) { return(new IOReadResult(IOCode.GarbageDataError, "No vertices in file")); } if (vTriangles.Length == 0) { return(new IOReadResult(IOCode.GarbageDataError, "No triangles in file")); } // [TODO] support non-per-vertex normals/colors bool bHaveNormals = (vNormals.Length == vPositions.Length); bool bHaveColors = (vColors.Length == vPositions.Length); bool bHaveUVs = (vUVs.Length / 2 == vPositions.Length / 3); int nVertices = vPositions.Length / 3; int[] mapV = new int[nVertices]; int meshID = builder.AppendNewMesh(bHaveNormals, bHaveColors, bHaveUVs, false); for (int k = 0; k < nVertices; ++k) { vtx_key vk = new vtx_key() { vi = k, ci = k, ni = k, ui = k }; mapV[k] = append_vertex(builder, vk, bHaveNormals, bHaveColors, bHaveUVs); } // [TODO] this doesn't handle missing vertices... for (int k = 0; k < vTriangles.Length; ++k) { append_triangle(builder, k, mapV); } if (UsedMaterials.Count == 1) // [RMS] should not be in here otherwise { int material_id = UsedMaterials.Keys.First(); string sMatName = UsedMaterials[material_id]; OBJMaterial useMat = Materials[sMatName]; int matID = builder.BuildMaterial(useMat); builder.AssignMaterial(matID, meshID); } return(new IOReadResult(IOCode.Ok, "")); }
IOReadResult BuildMeshes_ByMaterial(ReadOptions options, IMeshBuilder builder) { if (vPositions.Length == 0) { return(new IOReadResult(IOCode.GarbageDataError, "No vertices in file")); } if (vTriangles.Length == 0) { return(new IOReadResult(IOCode.GarbageDataError, "No triangles in file")); } bool bHaveNormals = (vNormals.Length > 0); bool bHaveColors = (vColors.Length > 0); bool bHaveUVs = (vUVs.Length > 0); var usedMaterialIDs = new List <int>(UsedMaterials.Keys); usedMaterialIDs.Add(Triangle.InvalidMaterialID); foreach (int material_id in usedMaterialIDs) { int matID = Triangle.InvalidMaterialID; if (material_id != Triangle.InvalidMaterialID) { string sMatName = UsedMaterials[material_id]; OBJMaterial useMat = Materials[sMatName]; matID = builder.BuildMaterial(useMat); } bool bMatHaveUVs = (material_id == Triangle.InvalidMaterialID) ? false : bHaveUVs; // don't append mesh until we actually see triangles int meshID = -1; var mapV = new Dictionary <Index3i, int>(); for (int k = 0; k < vTriangles.Length; ++k) { Triangle t = vTriangles[k]; if (t.nMaterialID == material_id) { if (meshID == -1) { meshID = builder.AppendNewMesh(bHaveNormals, bHaveColors, bMatHaveUVs, false); } var t2 = new Triangle(); for (int j = 0; j < 3; ++j) { var vk = new Index3i( t.vIndices[j] - 1, t.vNormals[j] - 1, t.vUVs[j] - 1); int use_vtx = -1; if (mapV.ContainsKey(vk) == false) { use_vtx = append_vertex(builder, vk, bHaveNormals, bHaveColors, bMatHaveUVs); mapV[vk] = use_vtx; } else { use_vtx = mapV[vk]; } t2.vIndices[j] = use_vtx; } append_triangle(builder, t2); } } if (matID != Triangle.InvalidMaterialID) { builder.AssignMaterial(matID, meshID); } } return(new IOReadResult(IOCode.Ok, "")); }