forked from solidDoWant/Planetbase-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OBJLoader.cs
451 lines (414 loc) · 18.3 KB
/
OBJLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
(C) 2015 AARO4130
DO NOT USE PARTS OF, OR THE ENTIRE SCRIPT, AND CLAIM AS YOUR OWN WORK
*/
//Credit for this entire file, less a few minor changes, goes to AARO4130 (https://forum.unity.com/threads/free-runtime-obj-loader.365884/)
using System;
using UnityEngine;
using System.Collections.Generic;
using System.IO;
namespace PlanetbaseFramework
{
public class ObjLoader
{
public static bool SplitByMaterial = false;
public static string[] SearchPaths = { "", "%FileName%_Textures" + Path.DirectorySeparatorChar };
//structures
struct ObjFace
{
public string MaterialName;
public string MeshName;
public int[] Indexes;
}
public static Vector3 ParseVectorFromCMPS(string[] cmps)
{
float x = float.Parse(cmps[1]);
float y = float.Parse(cmps[2]);
if (cmps.Length == 4)
{
float z = float.Parse(cmps[3]);
return new Vector3(x, y, z);
}
return new Vector2(x, y);
}
public static Color ParseColorFromCMPS(string[] cmps, float scalar = 1.0f)
{
float Kr = float.Parse(cmps[1]) * scalar;
float Kg = float.Parse(cmps[2]) * scalar;
float Kb = float.Parse(cmps[3]) * scalar;
return new Color(Kr, Kg, Kb);
}
public static string OBJGetFilePath(string path, string basePath, string fileName)
{
foreach (string sp in SearchPaths)
{
string s = sp.Replace("%FileName%", fileName);
if (File.Exists(basePath + s + path))
{
return basePath + s + path;
}
else if (File.Exists(path))
{
return path;
}
}
return null;
}
public static Material[] LoadMTLFile(string fn, List<Texture2D> loadedTextures)
{
Material currentMaterial = null;
List<Material> matlList = new List<Material>();
FileInfo mtlFileInfo = new FileInfo(fn);
string baseFileName = Path.GetFileNameWithoutExtension(fn);
string mtlFileDirectory = mtlFileInfo.Directory.FullName + Path.DirectorySeparatorChar;
foreach (string ln in File.ReadAllLines(fn))
{
string l = ln.Trim().Replace(" ", " ");
string[] cmps = l.Split(' ');
string data = l.Remove(0, l.IndexOf(' ') + 1);
if (cmps[0] == "newmtl")
{
if (currentMaterial != null)
{
matlList.Add(currentMaterial);
}
currentMaterial = new Material(Shader.Find("Standard"));
currentMaterial.name = data;
}
else if (cmps[0] == "Kd")
{
currentMaterial.SetColor("_Color", ParseColorFromCMPS(cmps));
}
else if (cmps[0] == "map_Kd")
{
//TEXTURE
Texture2D texture = loadedTextures.FindObjectByFilepath(data);
if (texture != null)
{
currentMaterial.SetTexture("_MainTex", texture);
}
else
{
Debug.Log("Could not load texture \"" + Path.GetFileName(data) + "\" for material \"" + fn);
}
}
else if (cmps[0] == "map_Bump")
{
Texture2D texture = loadedTextures.FindObjectByFilepath(data);
if (texture != null)
{
currentMaterial.SetTexture("_MainTex", texture);
currentMaterial.EnableKeyword("_NORMALMAP");
}
else
{
Debug.Log("Could not load texture \"" + Path.GetFileName(data) + "\" for material \"" + fn);
}
}
else if (cmps[0] == "Ks")
{
currentMaterial.SetColor("_SpecColor", ParseColorFromCMPS(cmps));
}
else if (cmps[0] == "Ka")
{
currentMaterial.SetColor("_EmissionColor", ParseColorFromCMPS(cmps, 0.05f));
currentMaterial.EnableKeyword("_EMISSION");
}
else if (cmps[0] == "d")
{
float visibility = float.Parse(cmps[1]);
if (visibility < 1)
{
Color temp = currentMaterial.color;
temp.a = visibility;
currentMaterial.SetColor("_Color", temp);
//TRANSPARENCY ENABLER
currentMaterial.SetFloat("_Mode", 3);
currentMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
currentMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
currentMaterial.SetInt("_ZWrite", 0);
currentMaterial.DisableKeyword("_ALPHATEST_ON");
currentMaterial.EnableKeyword("_ALPHABLEND_ON");
currentMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
currentMaterial.renderQueue = 3000;
}
}
else if (cmps[0] == "Ns")
{
float Ns = float.Parse(cmps[1]);
Ns = (Ns / 1000);
currentMaterial.SetFloat("_Glossiness", Ns);
}
}
if (currentMaterial != null)
{
matlList.Add(currentMaterial);
}
return matlList.ToArray();
}
public static GameObject LoadOBJFile(string FilePath, List<Texture2D> LoadedTextures)
{
string meshName = Path.GetFileNameWithoutExtension(FilePath);
bool hasNormals = false;
//OBJ LISTS
List<Vector3> vertices = new List<Vector3>();
List<Vector3> normals = new List<Vector3>();
List<Vector2> uvs = new List<Vector2>();
//UMESH LISTS
List<Vector3> uvertices = new List<Vector3>();
List<Vector3> unormals = new List<Vector3>();
List<Vector2> uuvs = new List<Vector2>();
//MESH CONSTRUCTION
List<string> materialNames = new List<string>();
List<string> objectNames = new List<string>();
Dictionary<string, int> hashtable = new Dictionary<string, int>();
List<ObjFace> faceList = new List<ObjFace>();
string cmaterial = "";
string cmesh = "default";
//CACHE
Material[] materialCache = null;
//save this info for later
FileInfo OBJFileInfo = new FileInfo(FilePath);
foreach (string ln in File.ReadAllLines(FilePath))
{
if (ln.Length > 0 && ln[0] != '#')
{
string l = ln.Trim().Replace(" ", " ");
string[] cmps = l.Split(' ');
string data = l.Remove(0, l.IndexOf(' ') + 1);
if (cmps[0] == "mtllib")
{
//load cache
string pth = OBJGetFilePath(data, OBJFileInfo.Directory.FullName + Path.DirectorySeparatorChar, meshName);
if (pth != null)
materialCache = LoadMTLFile(pth, LoadedTextures);
}
else if ((cmps[0] == "g" || cmps[0] == "o") && SplitByMaterial == false)
{
cmesh = data;
if (!objectNames.Contains(cmesh))
{
objectNames.Add(cmesh);
}
}
else if (cmps[0] == "usemtl")
{
cmaterial = data;
if (!materialNames.Contains(cmaterial))
{
materialNames.Add(cmaterial);
}
if (SplitByMaterial)
{
if (!objectNames.Contains(cmaterial))
{
objectNames.Add(cmaterial);
}
}
}
else if (cmps[0] == "v")
{
//VERTEX
vertices.Add(ParseVectorFromCMPS(cmps));
}
else if (cmps[0] == "vn")
{
//VERTEX NORMAL
normals.Add(ParseVectorFromCMPS(cmps));
}
else if (cmps[0] == "vt")
{
//VERTEX UV
uvs.Add(ParseVectorFromCMPS(cmps));
}
else if (cmps[0] == "f")
{
int[] indexes = new int[cmps.Length - 1];
for (int i = 1; i < cmps.Length; i++)
{
string felement = cmps[i];
int vertexIndex = -1;
int normalIndex = -1;
int uvIndex = -1;
if (felement.Contains("//"))
{
//doubleslash, no UVS.
string[] elementComps = felement.Split('/');
vertexIndex = int.Parse(elementComps[0]) - 1;
normalIndex = int.Parse(elementComps[2]) - 1;
}
else if (felement.Split('/').Length == 3)
{
//contains everything
string[] elementComps = felement.Split('/');
vertexIndex = int.Parse(elementComps[0]) - 1;
uvIndex = int.Parse(elementComps[1]) - 1;
normalIndex = int.Parse(elementComps[2]) - 1;
}
else if (!felement.Contains("/"))
{
//just vertex inedx
vertexIndex = int.Parse(felement) - 1;
}
else
{
//vertex and uv
string[] elementComps = felement.Split('/');
vertexIndex = int.Parse(elementComps[0]) - 1;
uvIndex = int.Parse(elementComps[1]) - 1;
}
string hashEntry = vertexIndex + "|" + normalIndex + "|" + uvIndex;
if (hashtable.ContainsKey(hashEntry))
{
indexes[i - 1] = hashtable[hashEntry];
}
else
{
//create a new hash entry
indexes[i - 1] = hashtable.Count;
hashtable[hashEntry] = hashtable.Count;
uvertices.Add(vertices[vertexIndex]);
if (normalIndex < 0 || (normalIndex > (normals.Count - 1)))
{
unormals.Add(Vector3.zero);
}
else
{
hasNormals = true;
unormals.Add(normals[normalIndex]);
}
if (uvIndex < 0 || (uvIndex > (uvs.Count - 1)))
{
uuvs.Add(Vector2.zero);
}
else
{
uuvs.Add(uvs[uvIndex]);
}
}
}
if (indexes.Length < 5 && indexes.Length >= 3)
{
ObjFace f1 = new ObjFace();
f1.MaterialName = cmaterial;
f1.Indexes = new int[] { indexes[0], indexes[1], indexes[2] };
f1.MeshName = (SplitByMaterial) ? cmaterial : cmesh;
faceList.Add(f1);
if (indexes.Length > 3)
{
ObjFace f2 = new ObjFace();
f2.MaterialName = cmaterial;
f2.MeshName = (SplitByMaterial) ? cmaterial : cmesh;
f2.Indexes = new int[] { indexes[2], indexes[3], indexes[0] };
faceList.Add(f2);
}
}
}
}
}
if (objectNames.Count == 0)
objectNames.Add("default");
//build objects
GameObject parentObject = new GameObject(meshName);
foreach (string obj in objectNames)
{
GameObject subObject = new GameObject(obj);
subObject.transform.parent = parentObject.transform;
subObject.transform.localScale = new Vector3(-1, 1, 1); //IIRC I did this to correct for some rotation issues? TODO review this
//Create mesh
Mesh m = new Mesh();
m.name = obj;
//LISTS FOR REORDERING
List<Vector3> processedVertices = new List<Vector3>();
List<Vector3> processedNormals = new List<Vector3>();
List<Vector2> processedUVs = new List<Vector2>();
List<int[]> processedIndexes = new List<int[]>();
Dictionary<int, int> remapTable = new Dictionary<int, int>();
//POPULATE MESH
List<string> meshMaterialNames = new List<string>();
List<ObjFace> ofaces = faceList.FindAll(x => x.MeshName == obj);
foreach (string mn in materialNames)
{
ObjFace[] faces = ofaces.FindAll(x => x.MaterialName == mn).ToArray();
if (faces.Length > 0)
{
int[] indexes = new int[0];
foreach (ObjFace f in faces)
{
int l = indexes.Length;
Array.Resize(ref indexes, l + f.Indexes.Length);
Array.Copy(f.Indexes, 0, indexes, l, f.Indexes.Length);
}
meshMaterialNames.Add(mn);
if (m.subMeshCount != meshMaterialNames.Count)
m.subMeshCount = meshMaterialNames.Count;
for (int i = 0; i < indexes.Length; i++)
{
int idx = indexes[i];
//build remap table
if (remapTable.ContainsKey(idx))
{
//ezpz
indexes[i] = remapTable[idx];
}
else
{
processedVertices.Add(uvertices[idx]);
processedNormals.Add(unormals[idx]);
processedUVs.Add(uuvs[idx]);
remapTable[idx] = processedVertices.Count - 1;
indexes[i] = remapTable[idx];
}
}
processedIndexes.Add(indexes);
}
else
{
}
}
//apply stuff
m.vertices = processedVertices.ToArray();
m.normals = processedNormals.ToArray();
m.uv = processedUVs.ToArray();
for (int i = 0; i < processedIndexes.Count; i++)
{
m.SetTriangles(processedIndexes[i], i);
}
if (!hasNormals)
{
m.RecalculateNormals();
}
m.RecalculateBounds();
;
MeshFilter mf = subObject.AddComponent<MeshFilter>();
MeshRenderer mr = subObject.AddComponent<MeshRenderer>();
Material[] processedMaterials = new Material[meshMaterialNames.Count];
for (int i = 0; i < meshMaterialNames.Count; i++)
{
if (materialCache == null)
{
processedMaterials[i] = new Material(Shader.Find("Standard (Specular setup)"));
}
else
{
Material mfn = Array.Find(materialCache, x => x.name == meshMaterialNames[i]); ;
if (mfn == null)
{
processedMaterials[i] = new Material(Shader.Find("Standard (Specular setup)"));
}
else
{
processedMaterials[i] = mfn;
}
}
processedMaterials[i].name = meshMaterialNames[i];
}
mr.materials = processedMaterials;
mr.enabled = true;
mr.useLightProbes = false;
mf.mesh = m;
}
return parentObject;
}
}
}