I have been converting the obj exporter script from C# to JavaScript and modifying it a lot. Well the first thing I added was for it to scan an empty game object for children and put the children together into one mesh. It works great except the 2nd gameobject it adds always seems to be a combination of the itself and the 1st game object. The top picture is a box inside a *.obj file with a cylinder and the bottom picture is a box in its own *.obj file. Here is my code:
//Badly converted from C# by medsouz
import System.IO;
var MeshFolder : Transform;
function MeshToString(Thingy : Transform) {
var sbackup = "#Exported from medLAB\n";
var sb = "";
for(var Child : Transform in Thingy){
sb = sb+"\n";
m = Child.gameObject.GetComponent(MeshFilter).mesh;
mats = Child.gameObject.GetComponent(MeshFilter).renderer.sharedMaterials;
sb = sb + ("g ")+(Child.name)+("\n");
for(var v in m.vertices) {
sb = sb + (String.Format("v {0} {1} {2}\n",v.x,v.y,v.z));
}
sb = sb + ("\n");
for(var v in m.normals) {
sb = sb + (String.Format("vn {0} {1} {2}\n",v.x,v.y,v.z));
}
sb = sb + ("\n");
for(var v in m.uv) {
sb = sb + (String.Format("vt {0} {1}\n",v.x,v.y));
}
for (material=0; material < m.subMeshCount; material ++) {
sb = sb + ("\n");
sb = sb + ("usemtl ")+(mats[material].name)+("\n");
sb = sb + ("usemap ")+(mats[material].name)+("\n");
triangles = m.GetTriangles(material);
for (i=0;i<triangles.Length;i+=3) {
sb = sb + (String.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n",
triangles[i]+1, triangles[i+1]+1, triangles[i+2]+1));
}
}
sbackup = sbackup + sb;
sb = "";
}
print(sbackup.ToString());
return sbackup.ToString();
}
function CreateMeshFile(meshstring,filename){
var fileinfo = ("Assets/"+filename.ToString()+".obj");
sr = new StreamWriter(fileinfo);
sr.Write(meshstring);
sr.Close();
}
CreateMeshFile(MeshToString(MeshFolder),MeshFolder.name);//omg it works...
I think the two meshes are being combined somehow… :?



Yep its definatly being combined, this is what happens if a cube then a capsule is *.obj’d…

In the linefor(var Child : Transform in Thingy){
has you exporting OBJ for all child objects into a single OBJ file. Are you passing in some shared parent object to convert?
I am putting all the objects into one file.
I believe all the vertex coordinates of an object are relative. So as you have child objects you may need to incorporate their offset in the world?
I did that yesterday while trying to fix this problem, all it did was prevent the objects from only being in the center regardless of their position.
I really need help with this…
When you get the triangles listing, that’s referencing indexed vertices for that particular object. So if your first cube has 8 vertices, the triangles array will have values ranging from 0 to 7 (the 8 vertices). If you add another cube, its triangle listing will also contain values ranging from 0 to 7. Add some other random mesh with 10000 vertices, its triangle listing will have values ranging from 0 to 9999.
Herein lies the problem. The OBJ format’s vertex indices increase throughout the file. So when you keep outputting vertex index 1, 2, 3, 4, regardless of what object/group its for in the file, they will all reference the very first (or second, third, fourth, etc.) vertex defined in the file, regardless of what group that vertex belonged to.
Same goes for the normals and UV coordinates.
You need to increment your indices as you go along:
var sbackup = "#Exported from medLAB\n";
var sb = "";
var counter = 0;
for(var Child : Transform in Thingy){
...
...
triangles = m.GetTriangles(material);
for (i=0;i<triangles.Length;i+=3) {
sb = sb + (String.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n",
triangles[i]+1 + counter, triangles[i+1]+1 + counter, triangles[i+2]+1 + counter));
}
counter += triangles.Length;
...
...
}
Ideally, you should go through and collect shared vertices, normals, and UV coordinates rather than duplicated data.
Um… What?:shock: I don’t know much about OBJ files I just know enough to convert it from C# to JavaScript. :?
Looking at your code, I’m betting your OBJ looks something like this (note I’m writing the OBJ specification off the top of my head):
g firstObject
v 0.00000,0.00000,0.00000
v 0,.000005,.000000.00000
v 5.00000,0.00000,0.00000
f 1/2/3
g secondObject
v 1.00000,1.00000,1.00000
v 5.00000,10.00000,3.00000
v 2.00000,8.00000,3.00000
f 1/2/3
Notice how both face polygon refers to vertices 1, 2, and 3. You need your second face to reference vertices 4, 5, 6 instead.