Advertisement
Guest User

Unity Runtime OBJ Importer

a guest
Sep 1st, 2017
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.94 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System;
  5.  
  6. public class ObjImporter
  7. {
  8.     public static Mesh[] Import(string filePath)
  9.     {
  10.         List<Mesh> meshes = new List<Mesh>();
  11.         Mesh currMesh = null;
  12.  
  13.         using (StreamReader stream = File.OpenText(filePath))
  14.         {
  15.             string line = stream.ReadLine();
  16.  
  17.             List<Vector3> vertices = new List<Vector3>();
  18.             List<Vector3> normals = new List<Vector3>();
  19.             List<Vector2> texCoords = new List<Vector2>();
  20.             List<Vector3> faceData = new List<Vector3>();
  21.  
  22.             while (line != null)
  23.             {
  24.                 string[] comp = line.Split(' ');
  25.  
  26.                 switch (comp[0])
  27.                 {
  28.                     case "g":
  29.                     case "o":
  30.                         if (currMesh != null)
  31.                         {
  32.                             Vector3[] adjVerts = new Vector3[faceData.Count];
  33.                             Vector3[] adjNorms = new Vector3[faceData.Count];
  34.                             Vector2[] adjTexCoords = new Vector2[faceData.Count];
  35.                             int[] tris = new int[(int)(faceData.Count * 1.5f)];
  36.  
  37.                             int t = 0;
  38.                             for (int i = 0; i < tris.Length; i += 6)
  39.                             {
  40.                                 tris[i] = t;
  41.                                 tris[i + 1] = t + 1;
  42.                                 tris[i + 2] = t + 2;
  43.  
  44.                                 tris[i + 3] = t;
  45.                                 tris[i + 4] = t + 2;
  46.                                 tris[i + 5] = t + 3;
  47.                                 t += 4;
  48.                             }
  49.  
  50.                             for (int i = 0; i < faceData.Count; i++)
  51.                             {
  52.                                 Vector3 v = faceData[i];
  53.  
  54.                                 adjVerts[i] = vertices[(int)v.x - 1];
  55.  
  56.                                 if (v.y != 0)
  57.                                     adjTexCoords[i] = texCoords[(int)v.y - 1];
  58.                                 if (v.z != 0)
  59.                                     adjNorms[i] = normals[(int)v.z - 1];
  60.                             }
  61.  
  62.                             currMesh.vertices = adjVerts;
  63.                             currMesh.triangles = tris;
  64.                             currMesh.normals = adjNorms;
  65.                             currMesh.uv = adjTexCoords;
  66.  
  67.                             currMesh.RecalculateBounds();
  68.  
  69.                             meshes.Add(currMesh);
  70.                         }
  71.  
  72.                         currMesh = new Mesh();
  73.                         currMesh.name = comp[1];
  74.  
  75.                         faceData.Clear();
  76.  
  77.                         break;
  78.                     case "v":
  79.                         vertices.Add(new Vector3(Convert.ToSingle(comp[1]), Convert.ToSingle(comp[2]), Convert.ToSingle(comp[3])));
  80.                         break;
  81.                     case "vt":
  82.                     case "vt1":
  83.                     case "vt2":
  84.                         texCoords.Add(new Vector2(Convert.ToSingle(comp[1]), Convert.ToSingle(comp[2])));
  85.                         break;
  86.                     case "vn":
  87.                         normals.Add(new Vector3(Convert.ToSingle(comp[1]), Convert.ToSingle(comp[2]), Convert.ToSingle(comp[3])));
  88.                         break;
  89.                     case "f":
  90.                         for (int i = 1; i < comp.Length; i++)
  91.                         {
  92.                             string[] f = comp[i].Split('/');
  93.                             Vector3 vec = Vector3.zero;
  94.  
  95.                             vec.x = Convert.ToInt32(f[0]);
  96.  
  97.                             if (f.Length > 1)
  98.                             {
  99.                                 if (f[1] != "")
  100.                                     vec.y = Convert.ToInt32(f[1]);
  101.  
  102.                                 if (f.Length > 2)
  103.                                 {
  104.                                     if (f[2] != "")
  105.                                         vec.z = Convert.ToInt32(f[2]);
  106.                                 }
  107.                             }
  108.  
  109.                             faceData.Add(vec);
  110.                         }
  111.                         break;
  112.                 }
  113.  
  114.                 line = stream.ReadLine();
  115.  
  116.                 if (line != null)
  117.                     line = line.Replace("  ", " ");
  118.                 else
  119.                 {
  120.                     //currStreamMesh.vertices = vertices.ToArray();
  121.                     //currStreamMesh.triangles = mTris.ToArray();
  122.                     ////currStreamMesh.normals = mNorms.ToArray();
  123.                     ////currStreamMesh.uv = mUVs.ToArray();
  124.  
  125.                     //currStreamMesh.RecalculateBounds();
  126.                     //currStreamMesh.Optimize();
  127.  
  128.                     //meshes.Add(currStreamMesh);
  129.                 }
  130.             }
  131.         }
  132.  
  133.         return meshes.ToArray();
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement