2using System.Collections;
3using System.Collections.Generic;
18 static string exportPath =
"teszt.1.obj";
19 private static string lastExportFolder;
20 private static string versionString =
"v2.0";
21 static string MaterialToString(Material m)
23 StringBuilder sb =
new StringBuilder();
24 sb.AppendLine(
"newmtl " + m.name);
26 if (m.HasProperty(
"_Color"))
28 sb.AppendLine(
"Kd " + m.color.r.ToString() +
" " + m.color.g.ToString() +
" " + m.color.b.ToString());
32 sb.AppendLine(
"Tr " + (1f - m.color.a).ToString());
33 sb.AppendLine(
"d " + m.color.a.ToString());
37 if (m.HasProperty(
"_SpecColor"))
39 Color sc = m.GetColor(
"_SpecColor");
40 sb.AppendLine(
"Ks " + sc.r.ToString() +
" " + sc.g.ToString() +
" " + sc.b.ToString());
46 string exResult = TryExportTexture(
"_MainTex", m);
47 if (exResult !=
"false")
49 sb.AppendLine(
"map_Kd " + exResult);
53 exResult = TryExportTexture(
"_SpecMap", m);
54 if (exResult !=
"false")
56 sb.AppendLine(
"map_Ks " + exResult);
60 exResult = TryExportTexture(
"_BumpMap", m);
61 if (exResult !=
"false")
63 sb.AppendLine(
"map_Bump " + exResult);
67 sb.AppendLine(
"illum 2");
71 static string TryExportTexture(
string propertyName, Material m)
73 if (m.HasProperty(propertyName))
75 Texture t = m.GetTexture(propertyName);
81 static Vector3 MultiplyVec3s(Vector3 v1, Vector3 v2)
83 return new Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
86 static Vector3 RotateAroundPoint(Vector3 point, Vector3 pivot, Quaternion angle)
88 return angle * (point - pivot) + pivot;
91 private static string ConstructOBJString(
int index)
93 string idxString = index.ToString();
94 return idxString +
"/" + idxString +
"/" + idxString;
101 Dictionary<string, bool> materialCache =
new Dictionary<string, bool>();
102 var exportFileInfo =
new FileInfo(exportPath);
103 lastExportFolder = exportFileInfo.Directory.FullName;
104 string baseFileName = Path.GetFileNameWithoutExtension(exportPath);
106 MeshFilter[] sceneMeshes;
108 sceneMeshes = go.GetComponentsInChildren<MeshFilter>().ToArray();
111 StringBuilder sb =
new StringBuilder();
112 StringBuilder sbMaterials =
new StringBuilder();
113 sb.AppendLine(
"# Export of " + go.name);
114 sb.AppendLine(
"# from Aaro4130 OBJ Exporter " + versionString);
117 sb.AppendLine(
"mtllib " + baseFileName +
".mtl");
120 float maxExportProgress = (float)(sceneMeshes.Length + 1);
122 for (
int i = 0; i < sceneMeshes.Length; i++)
124 string meshName = sceneMeshes[i].gameObject.name;
125 float progress = (float)(i + 1) / maxExportProgress;
126 MeshFilter mf = sceneMeshes[i];
127 MeshRenderer mr = sceneMeshes[i].gameObject.GetComponent<MeshRenderer>();
130 string exportName = meshName;
133 exportName +=
"_" + i;
136 sb.AppendLine(
"g " + exportName);
141 Material[] mats = mr.sharedMaterials;
142 for (
int j = 0; j < mats.Length; j++)
144 Material m = mats[j];
145 if (!materialCache.ContainsKey(m.name))
147 materialCache[m.name] =
true;
148 sbMaterials.Append(MaterialToString(m));
149 sbMaterials.AppendLine();
155 Mesh msh = mf.sharedMesh;
156 int faceOrder = (int)Mathf.Clamp((mf.gameObject.transform.lossyScale.x * mf.gameObject.transform.lossyScale.z), -1, 1);
158 foreach (Vector3 vx
in msh.vertices)
163 v = MultiplyVec3s(v, mf.gameObject.transform.lossyScale);
168 v = RotateAroundPoint(v, Vector3.zero, mf.gameObject.transform.rotation);
173 v += mf.gameObject.transform.position;
177 sb.AppendLine(
"v " + v.x +
" " + v.y +
" " + v.z);
180 foreach (Vector3 vx
in msh.normals)
185 v = MultiplyVec3s(v, mf.gameObject.transform.lossyScale.normalized);
190 v = RotateAroundPoint(v, Vector3.zero, mf.gameObject.transform.rotation);
194 sb.AppendLine(
"vn " + v.x +
" " + v.y +
" " + v.z);
197 foreach (Vector2 v
in msh.uv)
199 sb.AppendLine(
"vt " + v.x +
" " + v.y);
202 for (
int j = 0; j < msh.subMeshCount; j++)
204 if (mr !=
null && j < mr.sharedMaterials.Length)
206 string matName = mr.sharedMaterials[j].name;
207 sb.AppendLine(
"usemtl " + matName);
211 sb.AppendLine(
"usemtl " + meshName +
"_sm" + j);
214 int[] tris = msh.GetTriangles(j);
215 for (
int t = 0; t < tris.Length; t += 3)
217 int idx2 = tris[t] + 1 + lastIndex;
218 int idx1 = tris[t + 1] + 1 + lastIndex;
219 int idx0 = tris[t + 2] + 1 + lastIndex;
222 sb.AppendLine(
"f " + ConstructOBJString(idx2) +
" " + ConstructOBJString(idx1) +
" " + ConstructOBJString(idx0));
226 sb.AppendLine(
"f " + ConstructOBJString(idx0) +
" " + ConstructOBJString(idx1) +
" " + ConstructOBJString(idx2));
231 lastIndex += msh.vertices.Length;
237 File.WriteAllText(exportPath, sb.ToString());
240 File.WriteAllText(exportFileInfo.Directory.FullName +
"\\" + baseFileName +
".mtl", sbMaterials.ToString());
244 return sb.ToString();
static bool applyPosition
static bool exportTextures
static string MeshToText(GameObject go, bool wtf)
static bool generateMaterials
static bool autoMarkTexReadable
static bool applyRotation
static bool objNameAddIdNum