public static OBJData EncodeOBJ(this Mesh lMesh)
{
OBJData oBJData = new OBJData
{
m_Vertices = new List <Vector3>(lMesh.vertices),
m_UVs = new List <Vector2>(lMesh.uv),
m_Normals = new List <Vector3>(lMesh.normals),
m_UV2s = new List <Vector2>(lMesh.uv2),
m_Colors = new List <Color>(lMesh.colors)
};
for (int i = 0; i < lMesh.subMeshCount; i++)
{
int[] triangles = lMesh.GetTriangles(i);
OBJGroup oBJGroup = new OBJGroup(lMesh.name + "_" + i.ToString());
for (int j = 0; j < triangles.Length; j += 3)
{
OBJFace oBJFace = new OBJFace();
oBJFace.AddVertex(new OBJFaceVertex
{
m_VertexIndex = (oBJData.m_Vertices.Count > 0) ? triangles[j] : -1,
m_UVIndex = (oBJData.m_UVs.Count > 0) ? triangles[j] : -1,
m_NormalIndex = (oBJData.m_Normals.Count > 0) ? triangles[j] : -1,
m_UV2Index = (oBJData.m_UV2s.Count > 0) ? triangles[j] : -1,
m_ColorIndex = (oBJData.m_Colors.Count > 0) ? triangles[j] : -1
});
oBJFace.AddVertex(new OBJFaceVertex
{
m_VertexIndex = (oBJData.m_Vertices.Count > 0) ? triangles[j + 1] : -1,
m_UVIndex = (oBJData.m_UVs.Count > 0) ? triangles[j + 1] : -1,
m_NormalIndex = (oBJData.m_Normals.Count > 0) ? triangles[j + 1] : -1,
m_UV2Index = (oBJData.m_UV2s.Count > 0) ? triangles[j + 1] : -1,
m_ColorIndex = (oBJData.m_Colors.Count > 0) ? triangles[j + 1] : -1
});
oBJFace.AddVertex(new OBJFaceVertex
{
m_VertexIndex = (oBJData.m_Vertices.Count > 0) ? triangles[j + 2] : -1,
m_UVIndex = (oBJData.m_UVs.Count > 0) ? triangles[j + 2] : -1,
m_NormalIndex = (oBJData.m_Normals.Count > 0) ? triangles[j + 2] : -1,
m_UV2Index = (oBJData.m_UV2s.Count > 0) ? triangles[j + 2] : -1,
m_ColorIndex = (oBJData.m_Colors.Count > 0) ? triangles[j + 2] : -1
});
oBJGroup.AddFace(oBJFace);
}
oBJData.m_Groups.Add(oBJGroup);
}
return(oBJData);
}