回答: unity3D导出obj的方法如下: 从terrAIn菜单下选择export to obj... ,在分辨率窗口,选择要四边形还是三角形网格结构。同样也可以选择要导出地形的分辨率,有高中低等等.点击export,选择要保存的位置和文件名.obj文件将被导出.要注意如果选择大面积的full地形导出,最终的obj文件将非常大,而且也要导出时间较长。 下面为exportterrAIn.js脚本 import system.io;import system.text;enum saveFormat {triangles, quads}enum saveresolution {full, half, quarter, eighth, sixteenth}class exportterrAIn extends editorWindow { var saveFormat = saveFormat.triangles; var saveresolution = saveresolution.half; static var terrAIn : terrAIndata; static var terrAInpos : vector3; var tcount : int; var counter : int; var totalcount : int; @menUItem ("terrAIn/export to obj...") static function init () { terrAIn = null; var terrAInobject : terrAIn = selection.activeobject as terrAIn; if (!terrAInobject) { terrAInobject = terrAIn.activeterrAIn;} if (terrAInobject) { terrAIn = terrAInobject.terrAIndata; terrAInpos = terrAInobject.transForm.position;} editorWindow.getWindow(exportterrAIn).show(); } function ongUI () { if (!terrAIn) { gUIlayout.label("no terrAIn found"); if (gUIlayout.button("cancel")) {editorWindow.getWindow(exportterrAIn).close(); } return; } saveFormat = editorgUIlayout.enumpopup("export Format", saveFormat); saveresolution = editorgUIlayout.enumpopup("resolution", saveresolution); if (gUIlayout.button("export")) {export(); } } function export () { var filename = editorutility.savefilepanel("export .obj file", "", "terrAIn", "obj"); var w = terrAIn.heightmapwIDth; var h = terrAIn.heightmapheight; var meshscale = terrAIn.size; var tres = mathf.pow(2, parseint(saveresolution)); meshscale = vector3(meshscale.x/(w-1)*tres, meshscale.y, meshscale.z/(h-1)*tres); var uvscale = vector2(1.0/(w-1), 1.0/(h-1)); var tdata = terrAIn.getheights(0, 0, w, h); w = (w-1) / tres + 1; h = (h-1) / tres + 1; var tvertices = new vector3[w * h]; var tuv = new vector2[w * h]; if (saveFormat == saveFormat.triangles) { var tpolys = new int[(w-1) * (h-1) * 6];} else { tpolys = new int[(w-1) * (h-1) * 4];} // bUIld vertices and uvs For (y = 0; y < h; y++) { For (x = 0; x < w; x++) { tvertices[y*w + x] = vector3.scale(meshscale, vector3(x, tdata[x*tres,y*tres], y)) + terrAInpos; tuv[y*w + x] = vector2.scale(vector2(x*tres, y*tres), uvscale);} } var index = 0; if (saveFormat == saveFormat.triangles) { // bUIld triangle indices: 3 indices into vertex array For each triangle For (y = 0; y < h-1; y++) { For (x = 0; x < w-1; x++) { // For each grID cell output two triangles tpolys[index++] = (y * w) + x; tpolys[index++] = ((y+1) * w) + x; tpolys[index++] = (y * w) + x + 1; tpolys[index++] = ((y+1) * w) + x; tpolys[index++] = ((y+1) * w) + x + 1; tpolys[index++] = (y * w) + x + 1;} } } else { // bUIld quad indices: 4 indices into vertex array For each quad For (y = 0; y < h-1; y++) { For (x = 0; x < w-1; x++) { // For each grID cell output one quad tpolys[index++] = (y * w) + x; tpolys[index++] = ((y+1) * w) + x; tpolys[index++] = ((y+1) * w) + x + 1; tpolys[index++] = (y * w) + x + 1;} } } // export to .obj try { var SW = new streamwriter(filename); SW.writeline("# unity terrAIn obj file"); // write vertices system.threading.thread.currentthread.currentculture = new system.globalization.cultureinfo("en-us"); counter = tcount = 0; totalcount = (tvertices.length*2 + (saveFormat == saveFormat.triangles? tpolys.length/3 : tpolys.length/4)) / 1000; For (i = 0; i < tvertices.length; i++) {updateProgress(); var sb = stringbUIlder("v ", 20); // stringbUIlder stuff is done this way becAuse it's faster than using the "{0} {1} {2}"etc. Format // which is important when you're exporting hUGe terrAIns. sb.append(tvertices[i].x.tostring()).append(" "). append(tvertices[i].y.tostring()).append(" ").append(tvertices[i].z.tostring()); SW.writeline(sb); } // write uvs For (i = 0; i < tuv.length; i++) {updateProgress(); sb = stringbUIlder("vt ", 22); sb.append(tuv[i].x.tostring()).append(" ").append(tuv[i].y.tostring()); SW.writeline(sb); } if (saveFormat == saveFormat.triangles) { // write triangles For (i = 0; i < tpolys.length; i += 3) {updateProgress(); sb = stringbUIlder("f ", 43); sb.append(tpolys[i]+1).append("/").append(tpolys[i]+1).append(" "). append(tpolys[i+1]+1).append("/").append(tpolys[i+1]+1).append(" ").append(tpolys[i+2]+1).append("/").append(tpolys[i+2]+1); SW.writeline(sb); } } else { // write quads For (i = 0; i < tpolys.length; i += 4) {updateProgress(); sb = stringbUIlder("f ", 57); sb.append(tpolys[i]+1).append("/").append(tpolys[i]+1).append(" "). append(tpolys[i+1]+1).append("/").append(tpolys[i+1]+1).append(" "). append(tpolys[i+2]+1).append("/").append(tpolys[i+2]+1).append(" ").append(tpolys[i+3]+1).append("/").append(tpolys[i+3]+1); SW.writeline(sb); } } } catch (err) { debUG.log("error saving file: " + err.message);} SW.close(); terrAIn = null;editorutility.clearProgressbar(); editorWindow.getWindow(exportterrAIn).close(); } function updateProgress () { if (counter++ == 1000) { counter = 0; editorutility.displayProgressbar("saving...", "", mathf.inverselerp(0, totalcount, ++tcount));} }
|