unity导出.obj模型文件
生活随笔
收集整理的這篇文章主要介紹了
unity导出.obj模型文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
unity導出.obj模型文件
最近使用realworldterrain生成真實地形遇到一個問題,就是該地形的坐標軸沒有在中心位置,這樣在旋轉縮放操作時候就有各種問題,效果不好,于是想到先導出該地形為obj文件,然后在Max中修改其坐標軸位置。
一、將地形導出為obj文件
在unity中新建js腳本ExportTerrain.js,且必須放在Assets文件夾下的Editor文件夾中。(js文件不要用vs編譯,要用自帶的mono編譯器。)
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 UVsfor (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 trianglefor (y = 0; y < h-1; y++) {for (x = 0; x < w-1; x++) {// For each grid cell output two trianglestPolys[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 quadfor (y = 0; y < h-1; y++) {for (x = 0; x < w-1; x++) {// For each grid cell output one quadtPolys[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 .objtry {var sw = new StreamWriter(fileName);sw.WriteLine("# Unity terrain OBJ File");// Write verticesSystem.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 UVsfor (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 trianglesfor (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 quadsfor (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));}} }然后在菜單欄中會多出一項“Terrain”,選中場景中的地形,然后點擊該菜單欄,就可以導出了,導出的地形文件為obj類型,將obj導入max中,選擇“僅影響軸”即可改變坐標軸位置。
總結
以上是生活随笔為你收集整理的unity导出.obj模型文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 移相全桥DCDC峰值电流控制PCMC和电
- 下一篇: PHP 获取格式化的日期和时间