-
Notifications
You must be signed in to change notification settings - Fork 32
/
ObjExporterScript.cs
162 lines (131 loc) · 3.54 KB
/
ObjExporterScript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
using System.Text;
public class ObjExporterScript
{
private static int StartIndex = 0;
public static void Start()
{
StartIndex = 0;
}
public static void End()
{
StartIndex = 0;
}
public static string MeshToString(MeshFilter mf, Transform t)
{
Vector3 s = t.localScale;
Vector3 p = t.localPosition;
Quaternion r = t.localRotation;
int numVertices = 0;
Mesh m = mf.sharedMesh;
if (!m)
{
return "####Error####";
}
Material[] mats = mf.GetComponent<Renderer>().sharedMaterials;
StringBuilder sb = new StringBuilder();
foreach(Vector3 vv in m.vertices)
{
Vector3 v = t.TransformPoint(vv);
numVertices++;
sb.Append(string.Format("v {0} {1} {2}\n",v.x,v.y,-v.z));
}
sb.Append("\n");
foreach(Vector3 nn in m.normals)
{
Vector3 v = r * nn;
sb.Append(string.Format("vn {0} {1} {2}\n",-v.x,-v.y,v.z));
}
sb.Append("\n");
foreach(Vector2 v in m.uv)
{
sb.Append(string.Format("vt {0} {1}\n",v.x,v.y));
}
for (int material=0; material < m.subMeshCount; material ++)
{
sb.Append("\n");
sb.Append("usemtl ").Append(mats[material].name).Append("\n");
sb.Append("usemap ").Append(mats[material].name).Append("\n");
int[] triangles = m.GetTriangles(material);
for (int i=0;i<triangles.Length;i+=3) {
sb.Append(string.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n",
triangles[i]+1+StartIndex, triangles[i+1]+1+StartIndex, triangles[i+2]+1+StartIndex));
}
}
StartIndex += numVertices;
return sb.ToString();
}
}
public class ObjExporter : ScriptableObject
{
[MenuItem ("File/Export/Wavefront OBJ")]
static void DoExportWSubmeshes()
{
DoExport(true);
}
[MenuItem ("File/Export/Wavefront OBJ (No Submeshes)")]
static void DoExportWOSubmeshes()
{
DoExport(false);
}
static void DoExport(bool makeSubmeshes)
{
if (Selection.gameObjects.Length == 0)
{
Debug.Log("Didn't Export Any Meshes; Nothing was selected!");
return;
}
string meshName = Selection.gameObjects[0].name;
string fileName = EditorUtility.SaveFilePanel("Export .obj file", "", meshName, "obj");
ObjExporterScript.Start();
StringBuilder meshString = new StringBuilder();
meshString.Append("#" + meshName + ".obj"
+ "\n#" + System.DateTime.Now.ToLongDateString()
+ "\n#" + System.DateTime.Now.ToLongTimeString()
+ "\n#-------"
+ "\n\n");
Transform t = Selection.gameObjects[0].transform;
Vector3 originalPosition = t.position;
t.position = Vector3.zero;
if (!makeSubmeshes)
{
meshString.Append("g ").Append(t.name).Append("\n");
}
meshString.Append(processTransform(t, makeSubmeshes));
WriteToFile(meshString.ToString(),fileName);
t.position = originalPosition;
ObjExporterScript.End();
Debug.Log("Exported Mesh: " + fileName);
}
static string processTransform(Transform t, bool makeSubmeshes)
{
StringBuilder meshString = new StringBuilder();
meshString.Append("#" + t.name
+ "\n#-------"
+ "\n");
if (makeSubmeshes)
{
meshString.Append("g ").Append(t.name).Append("\n");
}
MeshFilter mf = t.GetComponent<MeshFilter>();
if (mf)
{
meshString.Append(ObjExporterScript.MeshToString(mf, t));
}
for(int i = 0; i < t.childCount; i++)
{
meshString.Append(processTransform(t.GetChild(i), makeSubmeshes));
}
return meshString.ToString();
}
static void WriteToFile(string s, string filename)
{
using (StreamWriter sw = new StreamWriter(filename))
{
sw.Write(s);
}
}
}