将地形Terrain导出为obj文件
因为做网游的寻路导航功能,我用的是Recast Navigation,插件为unity3d_nav_critterai,通过这个插件能导出.navmesh导航文件给后端使用,后端也可以用Recast Navigation来进行导航,这样前后端的寻路导航就能保持一致了。还有一种方式就是导出地形的obj文件给后端使用也可以,前端使用自带的导航组件,因为写Recast Navigation的作者也写了u
·
因为做网游的寻路导航功能,我用的是Recast Navigation,插件为unity3d_nav_critterai,通过这个插件能导出.navmesh导航文件给后端使用,后端也可以用Recast Navigation来进行导航,这样前后端的寻路导航就能保持一致了。
还有一种方式就是导出地形的obj文件给后端使用也可以,前端使用自带的导航组件,因为写Recast Navigation的作者也写了unity自带导航组件,所以两者理论上寻路效果一致(自己猜的)。
使用下面的脚本,放入Assets/Editor目录下,然后选中场景中的地形,通过菜单进行导出。
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
enum SaveFormat { Triangles, Quads }
enum SaveResolution { Full, Half, Quarter, Eighth, Sixteenth }
public class ExportTerrain : EditorWindow
{
SaveFormat saveFormat = SaveFormat.Triangles;
SaveResolution saveResolution = SaveResolution.Half;
static TerrainData terrain;
static Vector3 terrainPos;
int tCount;
int counter;
int totalCount;
[MenuItem("Tools/Terrain/Export To Obj")]
static void Init()
{
terrain = null;
Terrain terrainObject = Selection.activeObject as Terrain;
if (!terrainObject)
{
terrainObject = Terrain.activeTerrain;
}
if (terrainObject)
{
terrain = terrainObject.terrainData;
terrainPos = terrainObject.transform.position;
}
EditorWindow.GetWindow(typeof(ExportTerrain)).Show();
}
private void OnGUI()
{
if (!terrain)
{
GUILayout.Label("No terrain found");
if (GUILayout.Button("Cancel"))
{
EditorWindow.GetWindow(typeof(ExportTerrain)).Close();
}
return;
}
saveFormat = (SaveFormat)EditorGUILayout.EnumPopup("Export Format", saveFormat);
saveResolution = (SaveResolution)EditorGUILayout.EnumPopup("Resolution", saveResolution);
if (GUILayout.Button("Export"))
{
Export();
}
}
private void Export()
{
string fileName = EditorUtility.SaveFilePanel("Export .obj file", "", "Terrain", "obj");
var w = terrain.heightmapResolution;
var h = terrain.heightmapResolution;
var meshScale = terrain.size;
int tRes = (int)Mathf.Pow(2, (int)saveResolution);
meshScale = new Vector3(meshScale.x / (w - 1) * tRes, meshScale.y, meshScale.z / (h - 1) * tRes);
var uvScale = new Vector2(1.0f / (w - 1), 1.0f / (h - 1));
var tData = terrain.GetHeights(0, 0, w, h);
w = (int)((w - 1) / tRes + 1);
h = (int)((h - 1) / tRes + 1);
var tVertices = new Vector3[w * h];
var tUV = new Vector2[w * h];
int[] tPolys = new int[0];
if (saveFormat == SaveFormat.Triangles)
{
tPolys = new int[(w - 1) * (h - 1) * 6];
}
else
{
tPolys = new int[(w - 1) * (h - 1) * 4];
}
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
tVertices[y * w + x] = Vector3.Scale(meshScale, new Vector3(x, tData[x * tRes, y * tRes], y)) + terrainPos;
tUV[y * w + x] = Vector2.Scale(new Vector2(x * tRes, y * tRes), uvScale);
}
}
var index = 0;
if (saveFormat == SaveFormat.Triangles)
{
for (int y = 0; y < h - 1; y++)
{
for (int x = 0; x < w - 1; x++)
{
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
{
for (int y = 0; y < h - 1; y++)
{
for (int 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;
}
}
}
System.IO.StreamWriter sw = null;
try
{
sw = new System.IO.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;
System.Text.StringBuilder sb = null;
for (int i = 0; i < tVertices.Length; i++)
{
UpdateProgress();
sb = new System.Text.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 (int i = 0; i < tUV.Length; i++)
{
UpdateProgress();
sb = new System.Text.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 (int i = 0; i < tPolys.Length; i += 3)
{
UpdateProgress();
sb = new System.Text.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 (int i = 0; i < tPolys.Length; i += 4)
{
UpdateProgress();
sb = new System.Text.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 (System.Exception err)
{
Debug.Log("Error saving file: " + err.Message);
}
sw.Close();
terrain = null;
EditorUtility.ClearProgressBar();
EditorWindow.GetWindow(typeof(ExportTerrain)).Close();
}
private void UpdateProgress()
{
if (counter++ == 1000)
{
counter = 0;
EditorUtility.DisplayProgressBar("Saving...", "", Mathf.InverseLerp(0, totalCount, ++tCount));
}
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)