-
Notifications
You must be signed in to change notification settings - Fork 26
/
glTF.cs
326 lines (281 loc) · 8.48 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RevitExportGltf
{
#region glTF格式组成
/// <summary>
/// The json serializable glTF file format.
/// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0
/// </summary>
public struct glTF
{
public glTFVersion asset;
public List<glTFScene> scenes;
public List<glTFNode> nodes;
public List<glTFMesh> meshes;
public List<glTFBuffer> buffers;
public List<glTFBufferView> bufferViews;
public List<glTFAccessor> accessors;
public List<glTFMaterial> materials;
public List<glTFTexture> textures;
public List<glTFImage> images;
public List<glTFSampler> samplers;
}
/// <summary>
/// 版本
/// </summary>
public class glTFVersion
{
public string version = "2.0";
}
/// <summary>
/// 场景
/// </summary>
public class glTFScene
{
public List<int> nodes = new List<int>();
}
/// <summary>
///场景中的节点
/// </summary>
public class glTFNode
{
public string name { get; set; }
/// <summary>
/// mesh个数
/// </summary>
public int? mesh { get; set; } = null;
/// <summary>
/// 矩阵
/// </summary>
public List<double> matrix { get; set; }
/// <summary>
/// 旋转
/// </summary>
public List<double> rotation { get; set; }
/// <summary>
/// 平移
/// </summary>
public List<double> translation { get; set; }
/// <summary>
///
/// </summary>
public List<int> children { get; set; }
/// <summary>
///附加属性
/// </summary>
// public glTFExtras extras { get; set; }
}
/// <summary>
/// 网格
/// </summary>
public class glTFMesh
{
public List<glTFMeshPrimitive> primitives { get; set; }
}
/// <summary>
/// 属性定义GPU应该在哪里寻找网格和材质数据。
/// </summary>
public class glTFMeshPrimitive
{
public glTFAttribute attributes { get; set; } = new glTFAttribute();
public int indices { get; set; }
public int? material { get; set; } = null;
public int mode { get; set; } = 4; // 4 is triangles
}
public class glTFAttribute
{
/// <summary>
///位置数据访问器的索引。
/// </summary>
public int POSITION { get; set; }
/// <summary>
/// 第一组UV坐标
/// </summary>
public int TEXCOORD_0 { get; set; }
//public int NORMAL { get; set; }
}
/// <summary>
///对二进制数据的位置和大小的引用。
/// </summary>
public class glTFBuffer
{
/// <summary>
/// The uri of the buffer.
/// </summary>
public string uri { get; set; }
/// <summary>
/// The total byte length of the buffer.
/// </summary>
public int byteLength { get; set; }
}
/// <summary>
/// 对包含矢量或标量数据的缓冲区的分段的引用。
/// </summary>
public class glTFBufferView
{
/// <summary>
/// 缓冲区的索引。
/// </summary>
public int buffer { get; set; }
/// <summary>
/// 缓冲区的偏移量(以字节为单位)。
/// </summary>
public int byteOffset { get; set; }
/// <summary>
/// bufferView的长度,以字节为单位。
/// </summary>
public int byteLength { get; set; }
/// <summary>
/// GPU缓冲区应该绑定到的目标。
/// </summary>
public Targets target { get; set; }
/// <summary>
/// A user defined name for this view.
/// </summary>
public string name { get; set; }
}
/// <summary>
/// 逻辑数字来区分标量和矢量数组buff。
/// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#buffers-and-buffer-views
/// </summary>
public enum Targets
{
ARRAY_BUFFER = 34962, // signals vertex data
ELEMENT_ARRAY_BUFFER = 34963 // signals index or face data
}
/// <summary>
/// 逻辑数值以区分数组buff组件类型。
/// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#accessor-element-size
/// </summary>
public enum ComponentType
{
BYTE = 5120,
UNSIGNED_BYTE = 5121,
SHORT = 5122,
UNSIGNED_SHORT = 5123,
UNSIGNED_INT = 5125,
FLOAT = 5126
}
/// <summary>
/// 对包含特定数据类型的BufferView分段的引用。
/// </summary>
public class glTFAccessor
{
/// <summary>
///缓冲视图的索引
/// </summary>
public int bufferView { get; set; }
/// <summary>
/// 相对于bufferView开始的偏移量(以字节为单位)。
/// </summary>
public int byteOffset { get; set; }
/// <summary>
/// 属性中组件的数据类型
/// </summary>
public ComponentType componentType { get; set; }
/// <summary>
/// 此访问器引用的属性数量。
/// </summary>
public int count { get; set; }
/// <summary>
/// 指定属性是scala、向量还是矩阵
/// </summary>
public string type { get; set; }
/// <summary>
/// 此属性中每个组件的最大值。
/// </summary>
public List<float> max { get; set; }
/// <summary>
/// 此属性中每个组件的最小值。
/// </summary>
public List<float> min { get; set; }
/// <summary>
/// 此访问器的用户定义名称。
/// </summary>
public string name { get; set; }
}
/// <summary>
/// 材质
/// </summary>
public class glTFMaterial
{
public string name { get; set; }
public glTFPBR pbrMetallicRoughness { get; set; }
public string alphaMode { get; set; }
public string doubleSided { get; set; }
}
public class glTFPBR
{
//材质贴图
public glTFbaseColorTexture baseColorTexture { get; set; }
//材质颜色索引
public List<float> baseColorFactor { get; set; }
//材质金属性
public float metallicFactor { get; set; }
//材质粗糙度
public float roughnessFactor { get; set; }
}
public class glTFbaseColorTexture
{
//贴图索引
public int? index { get; set; } = null;
}
/// <summary>
/// 每个texture对象可以用于多个材质对象
/// </summary>
public class glTFTexture
{
/// <summary>
/// glTFImage的索引号
/// </summary>
public int? source { get; set; } = null;
/// <summary>
/// glTFSampler的索引号
/// </summary>
public int? sampler { get; set; } = null;
}
public class glTFImage
{
public string uri { get; set; }
}
public class glTFSampler
{
public float magFilter { get; set; }
public float minFilter { get; set; }
public float wrapS { get; set; }
public float wrapT { get; set; }
}
#endregion
#region bin文件
/// <summary>
/// A binary data store serialized to a *.bin file
/// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#binary-data-storage
/// </summary>
public class GeometryData
{
public List<string> vertices = new List<string>();
public List<double> normals = new List<double>();
public List<string> uvs = new List<string>();
public List<int> index = new List<int>();
}
/// <summary>
/// *.bin文件
/// </summary>
public class glTFBinaryData
{
public List<float> vertexBuffer { get; set; } = new List<float>();
public List<int> indexBuffer { get; set; } = new List<int>();
//public List<float> normalBuffer { get; set; } = new List<float>();
public List<float> uvBuffer { get; set; } = new List<float>();
public int vertexAccessorIndex { get; set; }
public int indexAccessorIndex { get; set; }
public int uvAccessorIndex { get; set; }
//public int normalsAccessorIndex { get; set; }
public string name { get; set; }
}
#endregion
}