226 lines
4.7 KiB
C#
226 lines
4.7 KiB
C#
using System.Collections.Generic;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
|
|
public class MegaCacheObjImporter
|
|
{
|
|
private class MegaCacheOBJFace
|
|
{
|
|
public int[] v = new int[4];
|
|
|
|
public int[] uv = new int[4];
|
|
|
|
public int[] n = new int[4];
|
|
|
|
public bool quad;
|
|
|
|
public int smthgrp;
|
|
|
|
public int mtl;
|
|
}
|
|
|
|
private class MegaCacheOBJMesh
|
|
{
|
|
public List<Vector3> vertices = new List<Vector3>();
|
|
|
|
public List<Vector3> normals = new List<Vector3>();
|
|
|
|
public List<Vector2> uv = new List<Vector2>();
|
|
|
|
public List<Vector2> uv1 = new List<Vector2>();
|
|
|
|
public List<Vector2> uv2 = new List<Vector2>();
|
|
|
|
public List<MegaCacheOBJFace> faces = new List<MegaCacheOBJFace>();
|
|
}
|
|
|
|
private static List<MegaCacheOBJMtl> mtls = new List<MegaCacheOBJMtl>();
|
|
|
|
private static List<MegaCacheFace> faces = new List<MegaCacheFace>();
|
|
|
|
private static int currentmtl;
|
|
|
|
private static int smthgrp;
|
|
|
|
private static int offset = 0;
|
|
|
|
private static MegaCacheOBJMtl loadmtl;
|
|
|
|
private static string importpath;
|
|
|
|
public static void Init()
|
|
{
|
|
mtls.Clear();
|
|
}
|
|
|
|
public static int NumMtls()
|
|
{
|
|
return mtls.Count;
|
|
}
|
|
|
|
public static MegaCacheOBJMtl GetMtl(int i)
|
|
{
|
|
return mtls[i];
|
|
}
|
|
|
|
public static Mesh ImportFile(string filePath, float scale, bool adjust, bool tangents, bool loadmtls, bool optimize, bool recalcnormals)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public static string ReadLine(string input)
|
|
{
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
while (offset < input.Length)
|
|
{
|
|
int num = input[offset++];
|
|
if (num == 13 || num == 10)
|
|
{
|
|
while (offset < input.Length)
|
|
{
|
|
int num2 = input[offset++];
|
|
if (num2 != 10)
|
|
{
|
|
offset--;
|
|
break;
|
|
}
|
|
}
|
|
return stringBuilder.ToString();
|
|
}
|
|
stringBuilder.Append((char)num);
|
|
}
|
|
if (stringBuilder.Length > 0)
|
|
{
|
|
return stringBuilder.ToString();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static void populateMeshStructNew(string entireText, ref MegaCacheOBJMesh mesh, bool loadmtls)
|
|
{
|
|
offset = 0;
|
|
string text = ReadLine(entireText);
|
|
char[] separator = new char[1] { ' ' };
|
|
char[] separator2 = new char[1] { '/' };
|
|
while (text != null)
|
|
{
|
|
text = text.Trim();
|
|
string[] array = text.Split(separator, 50);
|
|
switch (array[0])
|
|
{
|
|
case "usemtl":
|
|
array[1] = array[1].Replace(':', '_');
|
|
if (loadmtls)
|
|
{
|
|
currentmtl = GetMtlID(array[1]);
|
|
}
|
|
else
|
|
{
|
|
currentmtl = 0;
|
|
}
|
|
break;
|
|
case "mtllib":
|
|
if (loadmtls)
|
|
{
|
|
string filename = importpath + "/" + array[1];
|
|
LoadMtlLib(filename);
|
|
}
|
|
break;
|
|
case "v":
|
|
mesh.vertices.Add(new Vector3(float.Parse(array[1]), float.Parse(array[2]), float.Parse(array[3])));
|
|
break;
|
|
case "vt":
|
|
mesh.uv.Add(new Vector2(float.Parse(array[1]), float.Parse(array[2])));
|
|
break;
|
|
case "vt1":
|
|
mesh.uv1.Add(new Vector2(float.Parse(array[1]), float.Parse(array[2])));
|
|
break;
|
|
case "vt2":
|
|
mesh.uv2.Add(new Vector2(float.Parse(array[1]), float.Parse(array[2])));
|
|
break;
|
|
case "vn":
|
|
mesh.normals.Add(new Vector3(float.Parse(array[1]), float.Parse(array[2]), float.Parse(array[3])));
|
|
break;
|
|
case "f":
|
|
{
|
|
int i = 1;
|
|
MegaCacheOBJFace megaCacheOBJFace = new MegaCacheOBJFace();
|
|
megaCacheOBJFace.mtl = currentmtl;
|
|
megaCacheOBJFace.smthgrp = smthgrp;
|
|
for (; i < array.Length && (string.Empty + array[i]).Length > 0; i++)
|
|
{
|
|
string[] array2 = array[i].Split(separator2, 3);
|
|
if (i == 4)
|
|
{
|
|
megaCacheOBJFace.quad = true;
|
|
}
|
|
megaCacheOBJFace.v[i - 1] = int.Parse(array2[0]) - 1;
|
|
if (array2.Length > 1)
|
|
{
|
|
if (array2[1] != string.Empty)
|
|
{
|
|
megaCacheOBJFace.uv[i - 1] = int.Parse(array2[1]) - 1;
|
|
}
|
|
if (array2.Length > 2)
|
|
{
|
|
megaCacheOBJFace.n[i - 1] = int.Parse(array2[2]) - 1;
|
|
}
|
|
}
|
|
}
|
|
mesh.faces.Add(megaCacheOBJFace);
|
|
break;
|
|
}
|
|
}
|
|
text = ReadLine(entireText);
|
|
if (text != null)
|
|
{
|
|
text = text.Replace(" ", " ");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void ImportMtl(string filePath)
|
|
{
|
|
LoadMtlLib(filePath);
|
|
}
|
|
|
|
private static MegaCacheOBJMtl HaveMaterial(string name)
|
|
{
|
|
for (int i = 0; i < mtls.Count; i++)
|
|
{
|
|
if (mtls[i].name == name)
|
|
{
|
|
return mtls[i];
|
|
}
|
|
}
|
|
MegaCacheOBJMtl megaCacheOBJMtl = new MegaCacheOBJMtl();
|
|
megaCacheOBJMtl.Ka = Color.white;
|
|
megaCacheOBJMtl.Kd = Color.white;
|
|
megaCacheOBJMtl.Ks = Color.white;
|
|
megaCacheOBJMtl.Ke = Color.white;
|
|
megaCacheOBJMtl.name = name;
|
|
mtls.Add(megaCacheOBJMtl);
|
|
return megaCacheOBJMtl;
|
|
}
|
|
|
|
private static int GetMtlID(string name)
|
|
{
|
|
if (mtls.Count > 0)
|
|
{
|
|
for (int i = 0; i < mtls.Count; i++)
|
|
{
|
|
if (mtls[i].name == name)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
Debug.Log("Missing Material " + name);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
private static void LoadMtlLib(string filename)
|
|
{
|
|
}
|
|
}
|