-
Notifications
You must be signed in to change notification settings - Fork 3
/
Gltf.cs
352 lines (296 loc) · 9.84 KB
/
Gltf.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
using Newtonsoft.Json;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RevitGltfExporter
{
public class Gltf
{
private ElementsSet<Node> _nodes;
private ElementsMap<int, Mesh> _meshes;
private ElementsMap<string, Accessor> _accessors;
private ElementsMap<string, BufferView> _bufferViews;
private ElementsMap<int, RenderingMaterial> _materials;
private ElementsMap<string, Image> _images;
private ConcurrentDictionary <int, List<Primitive>> _primitives;
public int scene = 0;
public Dictionary<string, string> asset
{
get
{
return new Dictionary<string, string> { { "generator", "gltf generator" }, { "version", "2.0" }, { "copyright", "2019 (p) jason" } };
}
}
public string uri { set; private get; }
private long byteLength;
public Buffer[] buffers => new Buffer[1] { new Buffer(uri, byteLength) };
public Dictionary<string, object>[] scenes
{
get
{
return new Dictionary<string, object>[] { new Dictionary<string, object> { { "nodes", new int[] { 0 } }, { "name", "Scene" } } };
}
}
public int add(Image img)
{
return _images.add(img.source, img);
}
public int add(Node node)
{
return _nodes.add(node);
}
public void add(List<Node> nodes)
{
_nodes.add(nodes);
return;
}
public int add(Accessor accessor)
{
return _accessors.add(accessor.id, accessor);
}
public int indexOfAccessor(string id)
{
return _accessors.indexOf(id);
}
public bool existAccessor(string id)
{
return _accessors.exists(id);
}
public int add(BufferView bufferView)
{
return _bufferViews.add(bufferView.id, bufferView);
}
public BufferView getBufferView(int index)
{
return _bufferViews.at(index);
}
public int indexOfBufferView(string id)
{
return _bufferViews.indexOf(id);
}
public int add(Mesh mesh)
{
return _meshes.add(mesh.id, mesh);
}
public int indexOfMesh(int id)
{
return _meshes.indexOf(id);
}
public int indexOfTexture(string id)
{
return _images.indexOf(id);
}
public Mesh meshAt(int index)
{
return _meshes.at(index);
}
public int add(RenderingMaterial material)
{
return _materials.add(material.id, material);
}
public RenderingMaterial getMaterial(int index)
{
return _materials.at(index);
}
public int getMaterialIndex(int id)
{
return _materials.indexOf(id);
}
public void add(Primitive primitive)
{
if (!_primitives.ContainsKey(primitive.meshId))
_primitives.TryAdd(primitive.meshId, new List<Primitive>());
_primitives[primitive.meshId].Add(primitive);
}
public List<Primitive> primitivesOf(int meshId)
{
if(_primitives.ContainsKey(meshId))
return _primitives[meshId];
return null;
}
private Gltf()
{
_nodes = new ElementsSet<Node>();
_meshes = new ElementsMap<int, Mesh>();
_accessors = new ElementsMap<string, Accessor>();
_bufferViews = new ElementsMap<string, BufferView>();
_materials = new ElementsMap<int, RenderingMaterial>();
_images = new ElementsMap<string, Image>();
_primitives = new ConcurrentDictionary<int, List<Primitive>>();
}
static private Gltf _instance;
static public Gltf Instance
{
get
{
return _instance;
}
}
static public void reset()
{
_instance = new Gltf();
}
private void generateModel()
{
//foreach (Mesh mesh in _meshes.toList())
//{
// mesh.generateMesh();
//}
//foreach(Node node in nodes.toList())
//{
// node.filterEmptyChildren();
//}
//filteredNodes = nodes.toList().Where((node) => !(node as Node).isEmpty).ToList();
//filteredNodes = _nodes.toList();
//int i = 0;
//foreach (Node node in filteredNodes)
//{
// node.index = i;
// i++;
//}
//Debug.WriteLine("nodes count: " + filteredNodes.Count);
Debug.WriteLine("accessors count: " + _accessors.toList().Count);
foreach(Accessor accessor in _accessors.toList())
{
accessor.saveBinary();
}
//foreach (BufferView bufferView in bufferViews.toList())
//{
// bufferView.saveToBuffer(bufferStream);
//}
//byteLength = bufferStream.Length;
}
public void CopyImages(string targetPath)
{
if (_images.toList().Count == 0) return;
Directory.CreateDirectory(Path.Combine(targetPath, "textures"));
List<string> missingImages = new List<string>();
foreach (Image image in _images.toList())
{
string dest = Path.Combine(targetPath, image.uri);
try
{
File.Copy(image.source, dest, true);
}
catch (IOException e)
{
Debug.WriteLine(e.Message);
}
}
}
public void save(string path, string name)
{
uri = name + ".bin";
generateModel();
CopyImages(path);
saveBin(path);
//saveBuffers(path, name);
saveGltf(path, name);
dispose();
}
public void dispose()
{
_nodes.dispose();
_nodes = null;
_meshes.dispose();
_meshes = null;
_accessors.dispose();
_accessors = null;
_bufferViews.dispose();
_bufferViews = null;
_materials.dispose();
_materials = null;
//images.dispose();
_images = null;
_instance = null;
}
private void saveBin(string path)
{
string file = Path.Combine(path, uri);
foreach (BufferView bufferView in _bufferViews.toList())
{
bufferView.byteOffset = byteLength;
byteLength += bufferView.byteLength;
}
using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(file, FileMode.Create, "null", byteLength))
{
foreach (Accessor accessor in _accessors.toList())
{
using (MemoryMappedViewStream stream = mmf.CreateViewStream(accessor.offset, accessor.byteLength))
{
stream.Write(accessor.bytes, 0, accessor.bytes.Length);
}
}
}
}
public List<Accessor> accessors => _accessors.toList();
public List<Mesh> meshes => _meshes.toList();
public List<BufferView> bufferViews => _bufferViews.toList();
public List<RenderingMaterial> materials => _materials.toList();
public List<Node> nodes
{
get
{
List<Node> ret = _nodes.toList();
int i = 0;
foreach (Node node in ret)
{
node.index = i;
i++;
}
return ret;
}
}
public List<Image> images
{
get
{
if (0 < _images.toList().Count) return _images.toList();
return null;
}
}
public List<Dictionary<string, int>> textures
{
get
{
if (_images.toList().Count == 0) return null;
List<Dictionary<string, int>> ret = new List<Dictionary<string, int>>();
for (int i = 0; i < _images.toList().Count; i++)
{
ret.Add(new Dictionary<string, int> { { "source", i } });
}
return ret;
}
}
public List<Camera> cameras => new List<Camera>() { new Camera() };
private void saveGltf(string path, string name)
{
Debug.WriteLine("meshes count: " + this.meshes.Count);
string f = Path.Combine(path, name + ".gltf");
using (StreamWriter file = File.CreateText(f))
{
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.NullValueHandling = NullValueHandling.Ignore;
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
settings.ContractResolver = new InterfaceContractResolver(typeof(IPbrMaterial));
//settings.Formatting = Formatting.Indented;
file.Write(JsonConvert.SerializeObject(this, settings));
}
}
}
public struct Buffer
{
public string uri;
public long byteLength;
public Buffer(string uri, long byteLength)
{
this.uri = uri;
this.byteLength = byteLength;
}
}
}