问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

怎样输出为vtobject的文件

发布网友 发布时间:2022-04-23 15:12

我来回答

1个回答

热心网友 时间:2023-10-04 10:49

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));
}
}
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
...时间会发作,很痛的,请问是这样吗?会痛多久? ...已经坏了,如今全身浮肿,尿排不出,医生说就这 我爷爷得了肝癌晚期,腹水,不能手术.听说氩氦超冷刀技术能治.哪里有... 父亲59岁得了肝癌晚期,医生说最多半年时间了,上面还有80多岁的爷爷... 长春跑大连的列车员,怎么区分正式分和临时工呢? 员工刚怀孕就被开除,开除负责人拒不认错,你如何看待该公司的态度?_百 ... 员工在职期间怀孕,老板可以无故迟退吗? 老板开除怀孕女员工违法吗 如何选购蚊帐 什么样的蚊帐最好 搜狗输入法的英文联想跟英文预测有何区别? WIN7 如何打开BIOS里将VT开关 请问电脑主板华硕开启vt以后f10保存不了怎么回事 怎样在麦本本电脑的BIOS中启用虚拟化支持(VT)? 如何开启VT虚拟化 电脑主板华硕开启VT按F10怎么保存了,怎么回事 惠普bios保存不了vt是怎么回事 电脑BIOS 如何 开启VT。 华为手机怎样设置热点无设备连接后10分钟自动关闭? 联想笔记本怎么设置vt最后保存不了 华为手机双卡经常无信号连不上网是怎么回事 请问下这个要按什么键才可以保存并退出,我要开起VT,希望大神些帮下忙? 华为手机无法自动连wifi怎么版 华为nova7手机之前还能道过WLAN自动连接上网,现在不能自动连接上网了,怎么回? 华为手机无法自动连接上WiFi怎么办? 华为手机怎么无法自动连接上WiFi 歌词中有一句“甜甜的野蕨菜”的歌,八十年代以前的,有谁知道它的歌 长字的笔顺怎么写 长的 笔画顺序 长的笔顺怎么写的 vc excel2003导入 电脑上怎么开启搜狗输入法的词语联想功能呢? 我的电脑内存总是出错??谁来救我 搜狗输入法怎么输入英文联想 如何把125转成0x25,0x01 搜狗输入法的英文为什么不能自动联想 搜狗输入法英文模式下怎么启用联想功能 (电脑上)搜狗拼音输入法怎么设置英文联想功能 怎么取消搜狗输入法的联想功能啊? 搜狗输入法小米版怎么开英文联想 电脑搜狗输入法怎么才能跳出英文联想啊? 搜狗输入法怎么设置英文联想输入,百度上找了一下,那些方法都不行,是... 九节虾的鉴别 搜狗输入法怎样设置联想? 九节虾如何辨认,长什么样 搜狗输入法英文单词联想怎么打开? 搜狗输入法怎么切换到英文联想输入??要详细步骤 九节虾怎么养,吃什么? miui自带的搜狗输入法能英文联想吗 新鲜的九节虾装在袋里的水会不会变乌色