74 using System.Collections.Generic;
75 using System.Globalization;
77 using System.Text.RegularExpressions;
84 public class OBJ : MonoBehaviour {
87 private TextReader
sr;
92 private const string O =
"o";
93 private const string G =
"g";
94 private const string V =
"v";
95 private const string VT =
"vt";
96 private const string VN =
"vn";
97 private const string F =
"f";
98 private const string MTL =
"mtllib";
99 private const string UML =
"usemtl";
102 private const string NML =
"newmtl";
103 private const string NS =
"Ns";
104 private const string KA =
"Ka";
105 private const string KD =
"Kd";
106 private const string KS =
"Ks";
107 private const string D =
"d";
108 private const string TR =
"Tr";
109 private const string ILLUM =
"illum";
124 public OBJ(TextReader reader) : this()
130 public OBJ(TextReader reader, TextReader mtl_reader) : this(reader)
136 public OBJ(
string path) : this()
138 sr =
new StreamReader(path);
143 public OBJ(
string path,
string mtl_path) : this(path)
145 mtl_reader =
new StreamReader(mtl_path);
153 text = sr.ReadToEnd();
166 text = mtl_reader.ReadToEnd();
190 string[] lines = data.Split(
"\n".ToCharArray());
192 for(
int i = 0; i < lines.Length; i++) {
195 if(l.IndexOf(
"#") != -1) l = l.Substring(0, l.IndexOf(
"#"));
196 string[] p = l.Split(
" ".ToCharArray());
212 buffer.
PushUV(
new Vector2(
cf(p[1]),
cf(p[2]) ));
218 Vector3 norm =
new Vector3( -
cf(p[1]),
cf(p[2]),
cf(p[3]) );
223 for(
int j = 1; j < p.Length; j++) {
224 string[] c = p[j].Trim().Split(
"/".ToCharArray());
228 if(c.Length > 1 && c[1] !=
"") fi.
vu =
ci(c[1])-1;
229 if(c.Length > 2 && c[2] !=
"") fi.
vn =
ci(c[2])-1;
235 if(mtl_reader != null)
236 mtllib = p[1].Trim();
239 if(mtl_reader != null)
264 private float cf(
string v) {
265 return float.Parse(v);
268 private int ci(
string v) {
275 return mtllib != null;
294 string[] lines = data.Split(
"\n".ToCharArray());
296 materialData =
new List<MaterialData>();
299 for(
int i = 0; i < lines.Length; i++) {
302 if(l.IndexOf(
"#") != -1) l = l.Substring(0, l.IndexOf(
"#"));
303 string[] p = l.Split(
" ".ToCharArray());
308 current.name = p[1].Trim();
309 materialData.Add(current);
312 current.ambient =
gc(p);
315 current.diffuse =
gc(p);
318 current.specular =
gc(p);
321 current.shininess =
cf(p[1]) / 1000;
325 current.alpha =
cf(p[1]);
328 current.diffuseTexPath = p[1].Trim();
331 current.illumType =
ci(p[1]);
345 m =
new Material(Shader.Find(
"Bumped Specular cut"));
347 m.SetColor(
"_Color", Color.black);
351 m =
new Material(Shader.Find(
"Diffuse"));
354 m.SetColor(
"_Color", md.
diffuse);
361 private Color
gc(
string[] p) {
362 return new Color(
cf(p[1]),
cf(p[2]),
cf(p[3]) );
366 Dictionary<string, Material> materials =
new Dictionary<string, Material>();
372 Debug.Log(
"Obj import :: MATERIAL read");
377 Debug.Log(
"Obj import :: NO MATERIAL read");
378 m =
new Material(Shader.Find(
"Mat Cap Cut"));
379 materials.Add(
"default", m);
380 m.SetTexture(
"_MatCap", (Texture)Resources.Load(
"lit_spheres/divers/daphz1"));
383 GameObject[] ms =
new GameObject[buffer.
numObjects];
386 GameObject go =
new GameObject();
387 go.name=
"SurfaceOBJ";
390 go.AddComponent(typeof(MeshFilter));
391 go.AddComponent(typeof(MeshRenderer));
void PushObject(string name)
void PushGroup(string name)
void SetGeometryData(string data)
void PushNormal(Vector3 v)
OBJ(TextReader reader, TextReader mtl_reader)
void PushMaterialName(string name)
void SetMaterialData(string data)
void PopulateMeshes(GameObject[] gs, Dictionary< string, Material > mats)
void PushFace(FaceIndices f)
GameObject getParentGameObject()
Material GetMaterial(MaterialData md)
void PushVertex(Vector3 v)
static SurfaceManager getSurfaceManager()
OBJ(string path, string mtl_path)
List< MaterialData > materialData
void addSurface(GameObject surface)
static bool surfaceInitialized