-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathRenderDocDataParserForUnity.cs
124 lines (107 loc) · 4.26 KB
/
RenderDocDataParserForUnity.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
#if UNITY_EDITOR
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
namespace Windsmoon.Tools
{
public static class RenderDocDataParserForUnity
{
#region methods
[MenuItem("Windsmoon/Tools/Parse Mesh Data")]
public static void ParseMeshData()
{
StreamReader sr;
string path = LoadCSV(out sr);
sr.ReadLine(); // pass the title
List<string> stringDataList = new List<string>();
while (!sr.EndOfStream)
{
string tempData = sr.ReadLine();
tempData = tempData.Replace(" ", "");
tempData.Replace("\r", "");
tempData.Replace("\n", "");
stringDataList.Add(tempData);
}
List<VertexData> vertexDataList = new List<VertexData>();
// VTX, IDX, POSITION.x, POSITION.y, POSITION.z, NORMAL.x, NORMAL.y, NORMAL.z, TEXCOORD0.x, TEXCOORD0.y
foreach (var stringData in stringDataList)
{
string[] datas = stringData.Split(',');
VertexData vertexData = new VertexData();
vertexData.index = int.Parse(datas[1]);
vertexData.Position = new Vector3(float.Parse(datas[2]), float.Parse(datas[3]), float.Parse(datas[4]));
vertexData.Normal = new Vector3(float.Parse(datas[5]), float.Parse(datas[6]), float.Parse(datas[7]));
vertexData.UV = new Vector2(float.Parse(datas[8]), float.Parse(datas[9]));
vertexDataList.Add(vertexData);
}
// construct mesh
int maxIndex = FindMaxIndex(vertexDataList);
int vertexArrayCount = maxIndex + 1;
Vector3[] vertices = new Vector3[vertexArrayCount];
Vector3[] normals = new Vector3[vertexArrayCount];
int[] triangles = new int[vertexDataList.Count];
Vector2[] uvs = new Vector2[vertexArrayCount];
// fill mesh data
// ?? why hash set has not the capcity property
Dictionary<int, int> flagDict = new Dictionary<int, int>(vertexArrayCount);;
for (int i = 0; i < vertexDataList.Count; ++i)
{
VertexData vertexData = vertexDataList[i];
int index = vertexData.index;
triangles[i] = index;
if (flagDict.ContainsKey(index))
{
continue;
}
flagDict.Add(index, 1);
vertices[index] = vertexData.Position;
normals[index] = vertexData.Normal;
uvs[index] = vertexData.UV;
}
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.normals = normals;
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateBounds();
mesh.RecalculateTangents();
AssetDatabase.CreateAsset(mesh, "Assets/" + System.IO.Path.GetFileNameWithoutExtension(path) + "_" + System.DateTime.Now.Ticks + ".mesh");
AssetDatabase.SaveAssets();
}
private static int FindMaxIndex(List<VertexData> vertexDataList)
{
int maxIndex = 0;
foreach (VertexData vertexData in vertexDataList)
{
int currentIndex = vertexData.index;
if (currentIndex > maxIndex)
{
maxIndex = currentIndex;
}
}
return maxIndex;
}
private static string LoadCSV(out StreamReader sr)
{
string csvPath = EditorUtility.OpenFilePanel("select mesh data in csv", String.Empty, "csv");
sr = new StreamReader(new FileStream(csvPath, FileMode.Open));
return csvPath;
}
#endregion
#region structs
struct VertexData
{
#region fields
public int index;
public Vector3 Position;
public Vector3 Normal;
public Vector2 UV;
#endregion
}
#endregion
}
}
#endif