ObjSaver.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Text;
  5. namespace LunarCatsStudio.SuperCombiner
  6. {
  7. /// <summary>
  8. /// This class is responsible for saving meshes in .obj file
  9. /// </summary>
  10. public class ObjSaver {
  11. // Used for exporting to .obj
  12. public static int _StartIndex = 0;
  13. public static void SaveObjFile(GameObject obj, bool makeSubmeshes, string floderPath)
  14. {
  15. string meshName = obj.name;
  16. string fileName = floderPath + "/" + meshName + ".obj";
  17. _StartIndex = 0;
  18. StringBuilder meshString = new StringBuilder();
  19. meshString.Append("#" + meshName + ".obj"
  20. + "\n#" + System.DateTime.Now.ToLongDateString()
  21. + "\n#" + System.DateTime.Now.ToLongTimeString()
  22. + "\n#-------"
  23. + "\n\n");
  24. Transform t = obj.transform;
  25. Vector3 originalPosition = t.position;
  26. t.position = Vector3.zero;
  27. if (!makeSubmeshes) {
  28. meshString.Append("g ").Append(t.name).Append("\n");
  29. }
  30. meshString.Append(processTransform(t, makeSubmeshes));
  31. WriteToFile(meshString.ToString(),fileName);
  32. t.position = originalPosition;
  33. _StartIndex = 0;
  34. }
  35. static void WriteToFile(string s, string filename)
  36. {
  37. using (StreamWriter sw = new StreamWriter(filename))
  38. {
  39. sw.Write(s);
  40. }
  41. }
  42. static string processTransform(Transform t, bool makeSubmeshes)
  43. {
  44. StringBuilder meshString = new StringBuilder();
  45. meshString.Append("#" + t.name
  46. + "\n#-------"
  47. + "\n");
  48. if (makeSubmeshes)
  49. {
  50. meshString.Append("g ").Append(t.name).Append("\n");
  51. }
  52. MeshFilter mf = t.GetComponent<MeshFilter>();
  53. SkinnedMeshRenderer skinned = t.GetComponent<SkinnedMeshRenderer>();
  54. if (mf)
  55. {
  56. meshString.Append(MeshToString(mf.sharedMesh, mf.GetComponent<Renderer>().sharedMaterials, t));
  57. }
  58. if (skinned)
  59. {
  60. meshString.Append(MeshToString(skinned.sharedMesh, skinned.sharedMaterials , t));
  61. }
  62. for(int i = 0; i < t.childCount; i++)
  63. {
  64. meshString.Append(processTransform(t.GetChild(i), makeSubmeshes));
  65. }
  66. return meshString.ToString();
  67. }
  68. public static string MeshToString(Mesh m, Material[] mats, Transform t)
  69. {
  70. /*Vector3 s = t.localScale;
  71. Vector3 p = t.localPosition;*/
  72. Quaternion r = t.localRotation;
  73. int numVertices = 0;
  74. if (!m)
  75. {
  76. return "####Error####";
  77. }
  78. StringBuilder sb = new StringBuilder();
  79. foreach(Vector3 vv in m.vertices)
  80. {
  81. Vector3 v = t.TransformPoint(vv);
  82. numVertices++;
  83. // inverting x-component since we're in a different coordinate system than "everyone" is "used to".
  84. sb.Append(string.Format("v {0} {1} {2}\n",v.x,v.y,-v.z));
  85. //sb.Append(string.Format("v {0} {1} {2}\n",v.x,v.y,v.z));
  86. }
  87. sb.Append("\n");
  88. foreach(Vector3 nn in m.normals)
  89. {
  90. Vector3 v = r * nn;
  91. sb.Append(string.Format("vn {0} {1} {2}\n",-v.x,-v.y,v.z));
  92. //sb.Append(string.Format("vn {0} {1} {2}\n",v.x,v.y,v.z));
  93. }
  94. sb.Append("\n");
  95. foreach(Vector3 v in m.uv)
  96. {
  97. sb.Append(string.Format("vt {0} {1}\n",v.x,v.y));
  98. }
  99. for (int material=0; material < m.subMeshCount; material ++)
  100. {
  101. sb.Append("\n");
  102. sb.Append("usemtl ").Append(mats[material].name).Append("\n");
  103. sb.Append("usemap ").Append(mats[material].name).Append("\n");
  104. int[] triangles = m.GetTriangles(material);
  105. for (int i=0;i<triangles.Length;i+=3) {
  106. //sb.Append(string.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n",
  107. //Because we inverted the x-component, we also needed to alter the triangle winding.
  108. sb.Append(string.Format("f {1}/{1}/{1} {0}/{0}/{0} {2}/{2}/{2}\n",
  109. triangles[i]+1+_StartIndex, triangles[i+1]+1+_StartIndex, triangles[i+2]+1+_StartIndex));
  110. }
  111. }
  112. _StartIndex += numVertices;
  113. return sb.ToString();
  114. }
  115. }
  116. }