2

I want to import and display an .obj file at runtime in Unity (2017.3.1f1) and found a fixed version (click, direct link to pastebin) of the FastObjImporter from the Unity wiki (click).

Since my .obj file doesn't include normals, I had to make the following change:

if(faceData[i].z >= 1) {
    if(normals.Count != 0) {
        newNormals[i] = normals[faceData[i].z - 1];
    }
}

I also added mesh.RecalculateNormals(); in the beginning.

The problem and reason why I'm opening this question is that Unity doesn't display anything. The scene is pretty simply: The standard directional light and standard camera with a player character as its child.

My code (I have to rotate to align it properly):

Mesh m = FastObjImporter.Instance.ImportFile(path);
Material mat = new Material(Shader.Find("Hidden/Internal-Colored")) { color = new Color(0,0,1) };
GameObject obj = new GameObject();

if(obj.GetComponent<MeshFilter>()==null) { obj.AddComponent<MeshFilter>(); }

if(obj.GetComponent<MeshRenderer>()==null) { obj.AddComponent<MeshRenderer>(); }

obj.GetComponent<MeshFilter>().mesh = m;
obj.GetComponent<MeshRenderer>().material = mat;
obj.transform.Rotate(new Vector3(270,0,0));
obj.transform.Rotate(new Vector3(0,0,180));

I've tried different shaders and models and I'm using the same code to create my own meshes and GameObjects with vertices information I'm reading from a .txt file and everything works fine there - just not here.

I also noticed that the "triangles" list always carries 0 items, even though there are loads of "t ...." entries in my .obj file. If I change "j=1" to "j=0" here, it fills the list:

info += j;
j = 0;
while(j + 2 < info) {

In the end Unity still doesn't display anything, so there has to be something else that's wrong.

Does anyone have a suspicion what it could be and what I can do to make it work?

Neph
  • 1,823
  • 2
  • 31
  • 69
  • 1
    The name of the script is a bit misleading, it is indeed **fast**, but it is also heavily optimized for Blender and not fail-safe for any OBJ, as with this problem. – Dimitar Dec 10 '18 at 17:18
  • Thanks for the info! My .obj files are created by a script, not Blender, so it is possible that some type of special information is missing in them. I didn't manage to get any of the free ones to work, so I'm using the ObjReader ([link](https://assetstore.unity.com/packages/tools/input-management/objreader-152)) at the moment. I don't know how fast it is compared to the others but it works. The only thing I'm not happy about is that loading bigger files can take a second, which makes the app freeze because the asset doesn't use a Coroutine or thread for reading from HDD. – Neph Dec 11 '18 at 09:43
  • I am testing out [Runtime OBJ Importer](https://assetstore.unity.com/packages/tools/modeling/runtime-obj-importer-49547) from the asset store as an alternative to FastObjImporter. So far, it seems to do a very good job – Eldamir Nov 28 '19 at 07:34
  • @Eldamir I'm pretty sure I tested that one too but there was a problem with it (sorry, can't remember what it was exactly), so I didn't end up using it. I noticed that an update was released for it in Feb. 2019, did it also add anything that allows you to import an .obj file in a background thread? – Neph Dec 04 '19 at 14:33
  • @Neph yeah, in the editor it works really well, but I am struggling to make it work with the web player. Seems memory quickly becomes a problem. Can't find a way to stream the file to the obj importer, so there er big buffers, capping my RAM and killing the browser. Will need to look into alternatives – Eldamir Dec 05 '19 at 13:34
  • @Eldamir I'm creating serialized objects of my .obj files, so loading them at runtime is quicker as I haven't found a reader that can load them in a background thread, but I'm experiencing the same problem: RAM is filling up more and more over time, even though I'm clearing all the GameObjects I don't currently need and even call the garbage collector regularely (I know that it's more of a recommendation). It looks like a memory leak to me and I'm not sure if it's caused by my code, serializer or ObjReader. – Neph Dec 05 '19 at 15:27

0 Answers0