艾特熊
  • 来自蛇山
  • IP属地: 上海
就一屌丝

zb翻译

using UnityEngine; using UnityEditor; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using System; struct ObjMaterial { public string name; public string textureName; } public class EditorObjExporter : ScriptableObject { private static int vertexOffset = 0; private static int normalOffset = 0; private static int uvOffset = 0; //User should probably be able to change this. It is currently left as an excercise for //the reader. private static string targetFolder = "ExportedObj"; private static string MeshToString(MeshFilter mf, Dictionary<string, ObjMaterial> materialList) { Mesh m = mf.sharedMesh; Material[] mats = mf.renderer.sharedMaterials; StringBuilder sb = new StringBuilder(); sb.Append("g ").Append(mf.name).Append(" "); foreach(Vector3 lv in m.vertices) { Vector3 wv = mf.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} ",-wv.x,wv.y,wv.z)); } sb.Append(" "); foreach(Vector3 lv in m.normals) { Vector3 wv = mf.transform.TransformDirection(lv); sb.Append(string.Format("vn {0} {1} {2} ",-wv.x,wv.y,wv.z)); } sb.Append(" "); foreach(Vector3 v in m.uv) { sb.Append(string.Format("vt {0} {1} ",v.x,v.y)); } for (int material=0; material < m.subMeshCount; material ++) { sb.Append(" "); sb.Append("usemtl ").Append(mats[material].name).Append(" "); sb.Append("usemap ").Append(mats[material].name).Append(" "); //See if this material is already in the materiallist. try { ObjMaterial objMaterial = new ObjMaterial(); objMaterial.name = mats[material].name; if (mats[material].mainTexture) objMaterial.textureName = EditorUtility.GetAssetPath(mats[material].mainTexture); else objMaterial.textureName = null; materialList.Add(objMaterial.name, objMaterial); } catch (ArgumentException) { //Already in the dictionary } int[] triangles = m.GetTriangles(material); for (int i=0;i<triangles.Length;i+=3) { //Because we inverted the x-component, we

6