0

What I am attempting to do for about 2 days is importing a mesh in runtime with this: http://wiki.unity3d.com/index.php?title=ObjImporter

In total I found 3 importers, one that I must pay for, one that was probably a modified version of the importer above, and the one I have right now.

to import it I do:

ObjImporter objImporter = new ObjImporter();
Holder.ModelMesh = objImporter.ImportFile("./file.obj");

sadly, I get this error:

IndexOutOfRangeException: Array index is out of range.
ObjImporter.populateMeshStruct (.meshStruct& mesh) (at Assets/OBJReader/ObjImporter.cs:218)
ObjImporter.ImportFile (System.String filePath) (at Assets/OBJReader/ObjImporter.cs:33)

Which points at:

temp.z = System.Convert.ToInt32(brokenBrokenString[2]);

And I totally have no idea why it is doing that... I don't know how to fix it since I did not write that script, so I ask for some help on why it won't work as intended.

Additional information: The file I try to load can be found, as I can change the name to "file.b" in the script and it tells me it cannot be loaded, while when I put .obj it tells no error on that side. I also tried with 2 other files that also failed.

edit I also tried commenting out that line, and had weird results: the model was there but I had some weird shape following my camera for no reason...

Bart
  • 19,692
  • 7
  • 68
  • 77
Po0ka
  • 107
  • 3
  • 9
  • Just for future people reading this, if you have any problem regarding the lightning on the imported meshes, just do that in importfile method: `Mesh mesh = new Mesh();` `mesh.vertices = newVerts;` `mesh.uv = newUVs;` `mesh.normals = newNormals;` `mesh.triangles = newMesh.triangles;` `// Auto-calculate vertex normals from the mesh` `mesh.RecalculateNormals(); //added and it miraculously fixed the lightning issues on the mesh!` `mesh.RecalculateBounds();` `mesh.Optimize();` `return mesh;` – Po0ka Jul 25 '14 at 23:07

2 Answers2

1

I also try to get this importer to work. I just got help with this issue before half an hour, so i thought i share it.

This importer has problems with indexes that contains just two values. One is fine. Three is fine. But when your index area contains face indexes like f 5/1 6/2 2/3 1/4 , then the importer fails with above error.

You have to replace this:

if (brokenBrokenString.Length > 1) //Some .obj files skip UV and normal
{
    if (brokenBrokenString[1] != "") //Some .obj files skip the uv and not the normal
    {
        temp.y = System.Convert.ToInt32(brokenBrokenString[1]);
    }
    temp.z = System.Convert.ToInt32(brokenBrokenString[2]);
}

by this:

if (brokenBrokenString.Length == 2) //Some .obj files skip UV and normal
{
    temp.y = System.Convert.ToInt32(brokenBrokenString[1]);
}

if (brokenBrokenString.Length == 3) //Some .obj files skip UV and normal
{
    if (brokenBrokenString[1] != "") //Some .obj files skip the uv and not the normal
    {
        temp.y = System.Convert.ToInt32(brokenBrokenString[1]);
    }
    temp.z = System.Convert.ToInt32(brokenBrokenString[2]);
}

This fixes the issues with the faceindex values that just contains two values instead of one or three as required.

As a counterpart, could you be so kind to tell me how you import the meshes that are working for you? Because what i am still stuck with is how to proper set it up so that it imports the mesh data. The wiki sucks with its missing descriptions. The error message is gone for me now, but still no mesh data arrives at the object. I use a JS script that is attached to a game object with a mesh filter.

#pragma strict

var mesh1 : MeshFilter;

function Start ()
{
    mesh1= GetComponent(typeof(MeshFilter)) as MeshFilter;
    var objImporter = ObjImporter(); 
    var mesh1 = objImporter.ImportFile("C:/pathtomyfile/myfile.obj");

}

You don't seem to use a meshfilter at all. What exactly are you doing? You create an empty, and attach a C# script then? What exact script? How's the setup? Where's the script attached?

EDIT, nevermind, found a solution.

I create an empty, add a mesh filter to it, add a mesh renderer to it, then add this script here, and it imports the data. Great :)

#pragma strict

function Start () { 

    var mymeshfilter= GetComponent(typeof(MeshFilter)) as MeshFilter;
    var objImporter = ObjImporter(); 
    mymeshfilter.mesh = objImporter.ImportFile("C:/pathtofile/mymesh.obj");
} 

This is UnityJS, but the method should of course also work for C#

Tiles
  • 126
  • 2
0

As work-around you can also export the .obj-file with normals. E.g. in Blender set the option "Write Normals". Though that also makes my test file around 1/3 bigger.

kungfooman
  • 4,473
  • 1
  • 44
  • 33