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?