原插件代码中只有MeshFilter
的Obj导出代码;由于项目需求,需要将SkinnedMeshRenderer
导出为Obj文件,故在原代码的基础上,扩展出了SkinnedMeshRenderer
的Obj导出功能。对原代码有兴趣的可以自行进入参考链接2。
下载链接:修改后MeshFilter和SkinnedMeshRenderer的Obj导出功能
将下载的插件导入项目之后,就是文件夹Editor下的3个C#脚本文件。如下图,
下面对代码进行简单分析纪要:
EditorObjExporter<T, R>
、MeshFilter
的子类EditorObjExporter_MeshFilter
和SkinnedMeshRenderer
的子类EditorObjExporter_SkinnedMeshRenderer
。MeshFilter
和SkinnedMeshRenderer
的整个过程代码都一样只有在获取Mesh
网格和Material[]
材质时有所不同。static
静态函数,尤其是3个触发函数:ExportSelectionToSeparate
、ExportWholeSelectionToSingle
、ExportEachSelectionToSingle
。string MeshToString(MeshFilter t, Dictionary<string, ObjMaterial> materialList)
,用于子类分别获取Mesh
网格和Material[]
材质,所以该函数使用泛型类继承的方式;同时父类的static void MeshToFile(T t, string folder, string filename)
和static void MeshesToFile(T[] ts, string folder, string filename)
两个静态函数都需要调用该方法,但因为MeshToString
是非静态函数,使用单例对象调用是最方便简单的。sw.Write(GetInstance().MeshToString(t, materialList));//静态类中使用单例对象调用函数
Selection.GetTransforms(SelectionMode.Editable | SelectionMode.ExcludePrefab);
函数获取当前选择的Transform
对象数组;再根据泛型的每个子类不同的需求遍历该数组,并获取对应的组件;最后根据获取的组件数组保存成Obj文件。示例代码:protected static void ExportSelectionToSeparate()
{
if (!CreateTargetFolder())
return;
Transform[] selection = Selection.GetTransforms(SelectionMode.Editable | SelectionMode.ExcludePrefab);
if (selection.Length == 0)
{
EditorUtility.DisplayDialog("No source object selected!", "Please select one or more target objects", "");
return;
}
int exportedObjects = 0;
for (int i = 0; i < selection.Length; i++)
{
Component[] components = selection[i].GetComponentsInChildren(typeof(T));
for (int m = 0; m < components.Length; m++)
{
exportedObjects++;
MeshToFile((T)components[m], targetFolder, selection[i].name + "_" + i + "_" + m);
}
}
if (exportedObjects > 0)
EditorUtility.DisplayDialog("Objects exported", "Exported " + exportedObjects + " objects", "");
else
EditorUtility.DisplayDialog("Objects not exported", "Make sure at least some of your selected objects have mesh filters!", "");
}
MeshToString
函数将组件中获取的Mesh
网格转换成String
字符串:先顶点数据循环,再法线数据循环,然后是UV数据循环,最后材质数据循环的同时储存材质对象字典。函数代码:protected static string MeshToString(Mesh mesh, Material[] materials, string name, Transform transform, Dictionary<string, ObjMaterial> materialList)
{
StringBuilder sb = new StringBuilder();
sb.Append("g ").Append(name).Append("\n");//数据起始
foreach (Vector3 lv in mesh.vertices)
{//顶点数据循环输入
Vector3 wv = transform.TransformPoint(lv);
//This is sort of ugly - inverting x-component since we're in
//a different coordinate system than "everyone" is "used to".
sb.Append(string.Format("v {0} {1} {2}\n", -wv.x, wv.y, wv.z));
}
sb.Append("\n");//空格
foreach (Vector3 lv in mesh.normals)
{//法线数据循环输入
Vector3 wv = transform.TransformDirection(lv);
sb.Append(string.Format("vn {0} {1} {2}\n", -wv.x, wv.y, wv.z));
}
sb.Append("\n");//空格
foreach (Vector3 v in mesh.uv)
{//UV数据循环输入
sb.Append(string.Format("vt {0} {1}\n", v.x, v.y));
}
for (int material = 0; material < mesh.subMeshCount; material++)
{//材质数据循环输入
sb.Append("\n");//空格
sb.Append("usemtl ").Append(materials[material].name).Append("\n");
sb.Append("usemap ").Append(materials[material].name).Append("\n");
try
{//See if this material is already in the materiallist.看看这个字典是否已经在字典中
ObjMaterial objMaterial = new ObjMaterial
{
name = materials[material].name
};
if (materials[material].mainTexture)
objMaterial.textureName = AssetDatabase.GetAssetPath(materials[material].mainTexture);//另一种方式EditorUtility.GetAssetPath(mats[material].mainTexture)
else
objMaterial.textureName = null;
materialList.Add(objMaterial.name, objMaterial);
}
catch (ArgumentException)
{//已经在字典中
//Already in the dictionary
}
int[] triangles = mesh.GetTriangles(material);
for (int i = 0; i < triangles.Length; i += 3)
{//Because we inverted the x-component, we also needed to alter the 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));
}
}
vertexOffset += mesh.vertices.Length;
normalOffset += mesh.normals.Length;
uvOffset += mesh.uv.Length;
return sb.ToString();
}
MeshToFile
和MeshesToFile
函数是将Mesh
网格转换的String
字符串保存到文件中,其中的最后一步MaterialsToFile
是遍历材质对象字典保存到文件中。MenuItem
修饰。示例:[MenuItem("Custom/Export Obj/MeshFilter/导出所有选择的网格过滤器以分离的Obj形式")]//Export all MeshFilters in selection to separate Objs
protected static void ExportSelectionToSeparate_MF()
{
ExportSelectionToSeparate();
}
Mesh
组件选择对应的选项:MeshFilter
或SkinnedMeshRenderer
。
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删