-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser-solution.cs
More file actions
61 lines (48 loc) · 1.63 KB
/
Copy pathparser-solution.cs
File metadata and controls
61 lines (48 loc) · 1.63 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Globalization;
[RequireComponent(typeof(MeshFilter))]
public class ObjFileParser : MonoBehaviour
{
Mesh mesh;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
ReadFile("/path/to/wherever/your/file/is.obj");
}
void ReadFile(string filePath) {
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
StreamReader reader = new StreamReader(filePath);
while (!reader.EndOfStream)
{
string line = reader.ReadLine().Trim();
if (line == "") continue; // skip empty lines
string[] values = line.Split(' ');
if (values[0] == "v")
{
float x = float.Parse(values[1], CultureInfo.InvariantCulture);
float y = float.Parse(values[2], CultureInfo.InvariantCulture);
float z = float.Parse(values[3], CultureInfo.InvariantCulture);
vertices.Add(new Vector3(x, y, z));
}
else if (values[0] == "f")
{
int v1 = int.Parse(values[1].Split('/')[0]) - 1;
int v2 = int.Parse(values[2].Split('/')[0]) - 1;
int v3 = int.Parse(values[3].Split('/')[0]) - 1;
triangles.Add(v1);
triangles.Add(v2);
triangles.Add(v3);
}
}
reader.Close();
mesh.Clear();
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.RecalculateNormals();
}
}