Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public enum SaveFormat {
- OBJ,
- QMO
- }
- public enum LoadFormat {
- Auto,
- OBJ,
- QMO
- }
- public static class ImportExport {
- public static string SaveToText (this Mesh mesh, SaveFormat sFormat = SaveFormat.OBJ) {
- StringBuilder text = new StringBuilder ();
- if (sFormat == SaveFormat.OBJ) {
- text.AppendFormat("# ModelisaTools exported obj file\n\no {0}\n\n", mesh.name);
- foreach (Vector3 v in mesh.vertices) {
- text.AppendFormat ("v {0} {1} {2}\n", v.x, v.y, v.z);
- }
- text.Append ("\n");
- foreach (Vector3 u in mesh.uv) {
- text.AppendFormat ("vt {0} {1}\n", u.x, u.y);
- }
- text.Append ("\n");
- foreach (Vector3 n in mesh.normals) {
- text.AppendFormat ("vn {0} {1} {2}\n", n.x, n.y, n.z);
- }
- text.Append ("\ns off\n\n"); // smoothing group, but unity doesn't support it
- for (int i = 0; i < mesh.triangles.Length; i += 3) {
- text.AppendFormat ("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n", mesh.triangles[i]+1, mesh.triangles[i+1]+1, mesh.triangles[i+2]+1);
- }
- }
- if (sFormat == SaveFormat.QMO) {
- throw new NotImplementedException ();
- }
- return text.ToString ();
- }
- // path = the path where the file will be created, WITHOUT THE EXTENSION
- public static void SaveToFile (this Mesh mesh, string path, SaveFormat sFormat = SaveFormat.OBJ, bool overwrite = false) {
- path = path + '.' + sFormat.ToString ().ToLower ();
- try {
- if (!File.Exists (path) || overwrite) {
- File.WriteAllText (path, mesh.SaveToText (sFormat));
- }
- else {
- Debug.LogWarning ("This file already exists, can't save mesh : " + mesh.name + ", or set overwrite to true.\n@" + path);
- }
- }
- catch (Exception ex) {
- Debug.LogWarning ("Exception while saving mesh : " + mesh.name + "\n@ " + path + "\nex: " + ex);
- }
- }
- // path = the path were the mesh will be loaded, WITH THE EXTENSION if lFormat is AUTO, and WITHOUT if lFormat is : OBJ, QMO
- public static void LoadFromFile (this Mesh mesh, string path, LoadFormat lFormat = LoadFormat.Auto) {
- if (lFormat != LoadFormat.Auto) {
- path = path + '.' + lFormat.ToString ().ToLower ();
- }
- try {
- if (File.Exists (path)) {
- string extension = Path.GetExtension (path);
- LoadFormat format;
- if (extension == ".obj") {
- format = LoadFormat.OBJ;
- }
- else if (extension == ".qmo") {
- format = LoadFormat.QMO;
- }
- else {
- format = LoadFormat.Auto;
- }
- mesh.LoadFromStream (File.OpenText (path), format);
- }
- else {
- Debug.LogWarning ("This file doesn't exists, can't load mesh @ " + path);
- }
- }
- catch (Exception ex) {
- Debug.LogWarning ("Exception while loading mesh : " + mesh.name + "\n@ " + path + "\nex: " + ex);
- }
- }
- public static void LoadFromStream (this Mesh mesh, TextReader reader, LoadFormat lFormat) {
- string line;
- int lineNumber = 0;
- string[] datas;
- Vector3 tempVector3;
- Vector2 tempVector2;
- List<Vector3> vertices = new List<Vector3> ();
- List<Vector2> uv = new List<Vector2> ();
- List<Vector3> normals = new List<Vector3> ();
- List<int> triangles = new List<int> ();
- if (lFormat == LoadFormat.Auto) {
- Debug.LogWarning ("Can't load mesh : " + mesh.name + ", format is not supported.");
- return;
- }
- if (lFormat == LoadFormat.OBJ) {
- while ((line = reader.ReadLine()) != null) {
- datas = line.Split (' '); // so datas[0] == o || v || vt || vn || f || ...
- // mesh name
- if (line.StartsWith ("o")) {
- mesh.name = datas[1];
- }
- // vertex
- else if (line.StartsWith ("v ")) {
- try {
- tempVector3.x = float.Parse (datas[1]);
- tempVector3.y = float.Parse (datas[2]);
- tempVector3.z = float.Parse (datas[3]);
- vertices.Add (tempVector3);
- }
- catch (Exception ex) {
- Debug.LogWarning ("PARSE ERROR (vertex) : line " + lineNumber + " while loading mesh : " + mesh.name + "\n" + ex);
- vertices.Add (Vector3.zero);
- }
- }
- // uv - texture coordinate
- else if (line.StartsWith ("vt")) {
- try {
- tempVector2.x = float.Parse (datas[1]);
- tempVector2.y = float.Parse (datas[2]);
- uv.Add (tempVector2);
- }
- catch (Exception ex) {
- Debug.LogWarning ("PARSE ERROR (uv) : line " + lineNumber + " while loading mesh : " + mesh.name + "\n" + ex);
- uv.Add (Vector2.zero);
- }
- }
- // normal
- else if (line.StartsWith ("vn")) {
- try {
- tempVector3.x = float.Parse (datas[1]);
- tempVector3.y = float.Parse (datas[2]);
- tempVector3.z = float.Parse (datas[3]);
- normals.Add (tempVector3);
- }
- catch (Exception ex) {
- Debug.LogWarning ("PARSE ERROR (normal) : line " + lineNumber + " while loading mesh : " + mesh.name + "\n" + ex);
- normals.Add (Vector3.zero);
- }
- }
- // triangle (face)
- else if (line.StartsWith ("f")) {
- try {
- triangles.Add (int.Parse (datas[1].Split ('/')[0]) - 1);
- triangles.Add (int.Parse (datas[2].Split ('/')[0]) - 1);
- triangles.Add (int.Parse (datas[3].Split ('/')[0]) - 1);
- }
- catch (Exception ex) {
- Debug.LogWarning ("PARSE ERROR (triangle) : line " + lineNumber + " while loading mesh : " + mesh.name + "\n" + ex);
- triangles.Add (1);
- triangles.Add (2);
- triangles.Add (3); // beurk beurk mais je vois pas comment faire autrement :/
- }
- }
- // else it's :
- // _ a comment (#...)
- // _ a smoothing group, but unity doesn't support it (s on/s off)
- // _ a unsupported operation (ex : mtl)
- lineNumber++;
- }
- mesh.Clear ();
- mesh.vertices = vertices.ToArray ();
- mesh.uv = uv.ToArray ();
- mesh.normals = normals.ToArray ();
- mesh.triangles = triangles.ToArray ();
- }
- if (lFormat == LoadFormat.QMO) {
- //
- }
- reader.Close ();
- }
- public static void LoadFromText (this Mesh mesh, string text, LoadFormat lFormat) {
- mesh.LoadFromStream (new StringReader (text), lFormat);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement