-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathColladaLite.cs
186 lines (121 loc) · 6.74 KB
/
ColladaLite.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using UnityEngine;
/// **********************************************************************************************************************************************************************
/// in order to get materials working you will need to duplicate verts until they match the number of normals/uvs, since collada allows multiple normals and uvs per vert
/// **********************************************************************************************************************************************************************
public class ColladaLite {
private struct DaeInput {
public string semantic;
public string source;
public int offset;
}
public List<Mesh> meshes;
public List<string> textureNames;
public ColladaLite(string content) {
var doc = new XmlDocument();
doc.LoadXml(content);
XmlNode colladaNode = null;
foreach (XmlNode childNode in doc.ChildNodes) {
if (childNode.Name == "COLLADA") {
colladaNode = childNode;
break;
}
}
foreach (XmlNode childNode in colladaNode.ChildNodes) {
if (childNode.Name == "library_images") {
foreach (XmlNode imageNode in childNode.ChildNodes) {
if (imageNode.Name == "image" && imageNode.HasChildNodes) {
if (imageNode.FirstChild.Name == "init_from") {
(textureNames ?? (textureNames = new List<string>())).Add(imageNode.FirstChild.InnerText);
}
}
}
} else if (childNode.Name == "library_geometries") {
if (childNode.HasChildNodes) {
var fc = URDFLoader.GetXmlNodeChildByName(childNode, "geometry");
foreach (XmlNode mesh in fc.ChildNodes) {
if (mesh.Name != "mesh") {
continue;
}
var sources = new Dictionary<string, float[]>();
var vertsSource = "null";
Vector3[] triangles = null;
Vector3[] normals = null;
Vector2[] uvs = null;
int[] indices = null;
var inputs = new List<DaeInput>();
var triCount = 0;
foreach (XmlNode node in mesh.ChildNodes) {
if (node.Name == "source") {
var fa = URDFLoader.GetXmlNodeChildByName(node, "float_array");
if (fa != null) {
sources.Add(node.Attributes["id"].Value,
fa.InnerText.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries)
.Select(f => float.Parse(f))
.ToArray());
}
} else if (node.Name == "vertices") {
var vs = URDFLoader.GetXmlNodeChildByName(node, "input");
if (vs != null) {
vertsSource = vs.Attributes["source"].Value.Replace("#", "");
}
} else if (node.Name == "triangles") {
triCount = int.Parse(node.Attributes["count"].Value) * 3;
inputs.AddRange(URDFLoader.GetXmlNodeChildrenByName(node, "input")
.Select(inputNode => new DaeInput {
semantic = inputNode.Attributes["semantic"].Value,
source = inputNode.Attributes["source"].Value,
offset = int.Parse(inputNode.Attributes["offset"].Value)
}));
indices = URDFLoader.GetXmlNodeChildByName(node, "p").InnerText
.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries).Select(i => int.Parse(i))
.ToArray();
}
}
foreach (DaeInput input in inputs) {
var source = input.source.Replace("#", "");
if (sources.ContainsKey(source)) {
if (input.semantic == "TEXCOORD") {
var temp = new List<Vector2>();
for (int i = 0; i < sources[source].Length; i += 2) {
temp.Add(new Vector2(sources[source][i], sources[source][i + 1]));
}
uvs = temp.ToArray();
} else if (input.semantic == "NORMAL") {
//not actually dealing with normals right now
}
} else if (input.semantic == "VERTEX") {
var temp = new List<Vector3>();
for (int i = 0; i < sources[vertsSource].Length; i += 3) {
temp.Add(URDFLoader.URDFToUnityPos(new Vector3(sources[vertsSource][i],
sources[vertsSource][i + 1],
sources[vertsSource][i + 2])));
}
triangles = temp.ToArray();
}
}
if (triangles != null && triangles.Length > 2) {
var sb = new StringBuilder();
var tris = new int[triCount];
var uvsActual = new Vector2[triangles.Length];
var uvOffset = inputs.First(u => u.semantic == "TEXCOORD").offset;
for (int i = 0; i < tris.Length; i++) {
tris[i] = indices[i * 3];
}
Mesh temp = new Mesh();
temp.vertices = triangles;
temp.triangles = tris;
temp.uv = uvsActual.ToArray();
temp.RecalculateNormals();
(meshes ?? (meshes = new List<Mesh>())).Add(temp);
}
}
}
}
}
}
}