object - how to order triangles when exporting obj -
i'm trying export mesh obj unity , found 2 script way export triangles quite different , understand reason each 1 of them:
first:
foreach(vector3 lv in m.vertices) { vector3 wv = mf.transform.transformpoint(lv); //this sort of ugly - inverting x-component since we're in //a different coordinate system "everyone" "used to". sb.append(string.format("v {0} {1} {2}\n",-wv.x,wv.y,wv.z)); } foreach(vector3 lv in m.normals) { vector3 wv = mf.transform.transformdirection(lv); sb.append} (int i=0;i<triangles.length;i+=3) { //because inverted x-component, needed alter triangle winding. sb.append(string.format("f {1}/{1}/{1} {0}/{0}/{0} {2}/{2}/{2}\n", triangles[i]+1 + vertexoffset, triangles[i+1]+1 + normaloffset, triangles[i+2]+1 + uvoffset)); }
(i kept comments), http://wiki.unity3d.com/index.php?title=objexporter
this first method, , second below:
foreach(vector3 vv in m.vertices) { vector3 v = t.transformpoint(vv); numvertices++; sb.append(string.format("v {0} {1} {2}\n",v.x,v.y,-v.z)); } sb.append("\n"); foreach(vector3 nn in m.normals) { vector3 v = r * nn; sb.append(string.format("vn {0} {1} {2}\n",-v.x,-v.y,v.z)); } (int i=0;i<triangles.length;i+=3) { sb.append(string.format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n", triangles[i]+1+startindex, triangles[i+1]+1+startindex, triangles[i+2]+1+startindex)); }
link: http://wiki.unity3d.com/index.php?title=exportobj
the first method works , second 1 aswell it's rotated 180degres in axis. thing don't understand why need reorder triangle components on first method , still works , why normal components sign in second method doesn't match position components sign still works
i don't understand why need reorder triangle components
many renderers use ordering of triangle points indicate side of polygon "front" or "back" of triangle. that's important if back-face culling enabled.
you can try mixing clockwise , counter-clockwise triangles on mesh; unity's default behavior cull faces , you'll end seeing bunch of holes!
flipping axis or 2 common when importing/exporting geometry data. convenient if every file format agreed on sort of thing, don't.
Comments
Post a Comment