1 using System.Collections.Generic;
12 private const string ExportDirectoryKey =
"_ExportDirectory";
13 private const string ExportDirectoryDefault =
"MeshExport";
14 private const string ExportDialogErrorTitle =
"Export Error";
15 private const string WavefrontFileExtension =
".obj";
17 public static string ExportDirectory
25 if (
string.IsNullOrEmpty(value))
27 value = ExportDirectoryDefault;
34 private static bool MakeExportDirectory()
38 Directory.CreateDirectory(ExportDirectory);
48 [MenuItem(
"Mixed Reality Toolkit/Export/Export Room (.room) To Wavefront (.obj)...")]
51 string selectedFile = EditorUtility.OpenFilePanelWithFilters(
"Select Room File",
MeshSaver.
MeshFolderName,
new string[] {
"Room",
"room" });
52 if (
string.IsNullOrEmpty(selectedFile))
57 string fileName = Path.GetFileNameWithoutExtension(selectedFile);
58 IEnumerable<Mesh> meshes = null;
71 EditorUtility.DisplayDialog(ExportDialogErrorTitle,
"Unable to parse selected file.",
"Ok");
75 SaveMeshesToWavefront(fileName, meshes);
78 System.Diagnostics.Process.Start(ExportDirectory);
81 [MenuItem(
"Mixed Reality Toolkit/Export/Export Selection To Wavefront (.obj)")]
84 Transform[] selectedTransforms = Selection.transforms;
85 if (selectedTransforms.Length <= 0)
87 EditorUtility.DisplayDialog(ExportDialogErrorTitle,
"Please select GameObject(s) within the scene that you want saved.",
"OK");
91 List<MeshFilter> meshFilters =
new List<MeshFilter>(selectedTransforms.Length);
92 for (
int i = 0, iLength = selectedTransforms.Length; i < iLength; ++i)
94 meshFilters.AddRange(selectedTransforms[i].GetComponentsInChildren<MeshFilter>());
97 if (meshFilters.Count == 0)
99 EditorUtility.DisplayDialog(ExportDialogErrorTitle,
"Nothing selected contains a MeshFilter.",
"Ok");
103 SaveMeshFiltersToWavefront(
"Selection", meshFilters);
106 System.Diagnostics.Process.Start(ExportDirectory);
115 if (!MakeExportDirectory())
117 EditorUtility.DisplayDialog(ExportDialogErrorTitle,
"Failed to create export directory.",
"Ok");
121 string filePath = Path.Combine(ExportDirectory, fileName + WavefrontFileExtension);
122 using (StreamWriter stream =
new StreamWriter(filePath))
124 stream.Write(SerializeMeshes(meshes));
134 if (!MakeExportDirectory())
136 EditorUtility.DisplayDialog(ExportDialogErrorTitle,
"Failed to create export directory.",
"Ok");
140 string filePath = Path.Combine(ExportDirectory, fileName + WavefrontFileExtension);
141 using (StreamWriter stream =
new StreamWriter(filePath))
143 stream.Write(SerializeMeshFilters(meshes));
147 private static string SerializeMeshes(IEnumerable<Mesh> meshes)
149 StringWriter stream =
new StringWriter();
151 foreach (var mesh
in meshes)
153 SerializeMesh(mesh, stream, ref offset);
155 return stream.ToString();
158 private static string SerializeMeshFilters(IEnumerable<MeshFilter> meshes)
160 StringWriter stream =
new StringWriter();
162 foreach (var mesh
in meshes)
164 SerializeMeshFilter(mesh, stream, ref offset);
166 return stream.ToString();
175 private static void SerializeMesh(Mesh mesh, TextWriter stream, ref
int offset)
178 foreach (Vector3 vertex
in mesh.vertices)
180 stream.WriteLine(
string.Format(
"v {0} {1} {2}", -vertex.x, vertex.y, vertex.z));
184 foreach (Vector3 normal
in mesh.normals)
186 stream.WriteLine(
string.Format(
"vn {0} {1} {2}", normal.x, normal.y, normal.z));
190 for (
int s = 0, sLength = mesh.subMeshCount; s < sLength; ++s)
192 int[] indices = mesh.GetTriangles(s);
193 for (
int i = 0, iLength = indices.Length - indices.Length % 3; i < iLength; i += 3)
196 stream.WriteLine(
string.Format(
"f {0}//{0} {1}//{1} {2}//{2}",
197 indices[i + 2] + 1 + offset,
198 indices[i + 1] + 1 + offset,
199 indices[i + 0] + 1 + offset));
203 offset += mesh.vertices.Length;
212 private static void SerializeMeshFilter(MeshFilter meshFilter, TextWriter stream, ref
int offset)
214 Mesh mesh = meshFilter.sharedMesh;
217 foreach (Vector3 vertex
in mesh.vertices)
219 Vector3 pos = meshFilter.transform.TransformPoint(vertex);
220 stream.WriteLine(
string.Format(
"v {0} {1} {2}", -pos.x, pos.y, pos.z));
224 foreach (Vector3 meshNormal
in mesh.normals)
226 Vector3 normal = meshFilter.transform.TransformDirection(meshNormal);
227 stream.WriteLine(
string.Format(
"vn {0} {1} {2}", normal.x, normal.y, normal.z));
231 for (
int s = 0, sLength = mesh.subMeshCount; s < sLength; ++s)
233 int[] indices = mesh.GetTriangles(s);
234 for (
int i = 0, iLength = indices.Length - indices.Length % 3; i < iLength; i += 3)
237 stream.WriteLine(
string.Format(
"f {0}//{0} {1}//{1} {2}//{2}",
238 indices[i + 0] + 1 + offset,
239 indices[i + 1] + 1 + offset,
240 indices[i + 2] + 1 + offset));
244 offset += mesh.vertices.Length;