#Problem reading .obj file

1 messages · Page 1 of 1 (latest)

gaunt sentinel
#

um guy i have a poblem with exporting a mesh. I have code a code like this

        foreach (Vector3 vertex in mesh.vertices)
            objData += "v " + vertex.x + " " + vertex.y + " " + vertex.z + "\n";
        foreach (Vector3 normal in mesh.normals)
            objData += "vn " + normal.x + " " + normal.y + " " + normal.z + "\n";
        foreach (Vector2 uv in mesh.uv)
            objData += "vt " + uv.x + " " + uv.y + "\n";
        for (int i = 0; i < mesh.triangles.Length; i += 3)
            objData += "f " + (mesh.triangles[i] + 1) + " " + (mesh.triangles[i + 1] + 1) + " " + (mesh.triangles[i + 2] + 1) + "\n";```

And the export file have all the value i want but when i drop it to a Unity Project it say it have no Normal and when i check it in 3D Viewer it have no UV too (but there are vertex and triangle). Do you guy know why ?
gaunt sentinel
#

well i found the working code


        for (int i = 0; i < mesh.vertices.Length; i++)
        {
            objData += "v " + mesh.vertices[i].x + " " + mesh.vertices[i].y + " " + mesh.vertices[i].z + "\n";
            objData += "vn " + mesh.normals[i].x + " " + mesh.normals[i].y + " " + mesh.normals[i].z + "\n";
            objData += "vt " + mesh.uv[i].x + " " + mesh.uv[i].y + "\n";
        }

        int[] triangles = mesh.triangles;
        for (int i = 0; i < triangles.Length; i += 3)
        {
            int vertexIndex1 = triangles[i] + 1;
            int vertexIndex2 = triangles[i + 1] + 1;
            int vertexIndex3 = triangles[i + 2] + 1;

            objData += "f " + vertexIndex1 + "/" + vertexIndex1 + "/" + vertexIndex1 + " "
                              + vertexIndex2 + "/" + vertexIndex2 + "/" + vertexIndex2 + " "
                              + vertexIndex3 + "/" + vertexIndex3 + "/" + vertexIndex3 + "\n";
        }