-
Notifications
You must be signed in to change notification settings - Fork 199
/
CSV2ObjLOLMobile.cs
168 lines (147 loc) · 4.89 KB
/
CSV2ObjLOLMobile.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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Csv2ObjLolMobile
{
struct Vector3
{
public float x, y, z;
public Vector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
}
struct Vector2
{
public float x, y;
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
}
struct Color
{
public float r, g, b, a;
public Color(float r, float g, float b, float a)
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
}
class MeshData
{
public List<int> VTX;
public List<int> IDX;
public List<Vector3> Position;
public List<Vector3> Normal;
public List<Color> VertexColor;
public List<Vector2> Texcoord0;
public List<Vector2> Texcoord1;
public MeshData()
{
this.VTX = new List<int>();
this.IDX = new List<int>();
this.Position = new List<Vector3>();
this.Normal = new List<Vector3>();
this.VertexColor = new List<Color>();
this.Texcoord0 = new List<Vector2>();
this.Texcoord1 = new List<Vector2>();
}
}
class Csv2Obj
{
public Csv2Obj(string[] args)
{
foreach (string arg in args)
{
//Console.WriteLine("Begin");
Console.WriteLine("Begin Parse:" + arg);
if (Directory.Exists(arg))
{
string[] files = Directory.GetFiles(arg);
ParseFolder(files);
}
else if (File.Exists(arg) && arg.EndsWith(".csv"))
{
ParseFile(arg);
}
}
Console.ReadKey();
}
public void ParseFolder(string[] files)
{
foreach (var file in files)
{
ParseFile(file);
}
}
public void ParseFile(string path)
{
Console.WriteLine("Parse: " + path);
string[] lines = File.ReadAllLines(path);
MeshData meshData = new MeshData();
for (int i = 1; i < lines.Length; i++)
{
var line = lines[i];
string[] words = line.Split(',');
int index = 0;
meshData.VTX.Add(int.Parse(words[index++]));
meshData.IDX.Add(int.Parse(words[index++]));
meshData.Position.Add(new Vector3(float.Parse(words[index++]), float.Parse(words[index++]), float.Parse(words[index++])));
meshData.Texcoord0.Add(new Vector2(float.Parse(words[index++]), 1f - float.Parse(words[index++]))); //uv颠倒了
meshData.Texcoord1.Add(new Vector2(float.Parse(words[index++]), 1f - float.Parse(words[index++])));//uv颠倒了
meshData.Normal.Add(new Vector3(float.Parse(words[index++]), float.Parse(words[index++]), float.Parse(words[index++])));
meshData.VertexColor.Add(new Color(float.Parse(words[index++]), float.Parse(words[index++]), float.Parse(words[index++]), float.Parse(words[index++])));
}
string objPath = path.Replace(".csv", ".obj");
string objText = "";
if (File.Exists(objPath))
{
File.Delete(objPath);
}
foreach (Vector3 v in meshData.Position)
{
objText += "v " + v.x + " " + v.y + " " + v.z + System.Environment.NewLine;
}
foreach (Vector2 v in meshData.Texcoord0)
{
objText += "vt " + v.x + " " + v.y + System.Environment.NewLine;
}
foreach (Vector3 v in meshData.Normal)
{
objText += "vn " + v.x + " " + v.y + " " + v.z + System.Environment.NewLine;
}
objText += "usemtl None" + System.Environment.NewLine;
objText += "s off" + System.Environment.NewLine;
for (int i = 0; i < meshData.VTX.Count; i += 3)
{
objText += "f ";
int index = i;
index++;
objText += (index + "/" + index + "/" + index) + " ";
index++;
objText += (index + "/" + index + "/" + index) + " ";
index++;
objText += (index + "/" + index + "/" + index) + " ";
objText += System.Environment.NewLine;
}
File.WriteAllText(objPath, objText);
Console.WriteLine("Done: " + path);
}
}
class Program
{
static void Main(string[] args)
{
new Csv2Obj(args);
}
}
}