glTFBuffer.cs
7.19 KB
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
using System;
using System.Linq;
using UniJSON;
namespace UniGLTF
{
[Serializable]
public class glTFBuffer : JsonSerializableBase
{
IBytesBuffer Storage;
public void OpenStorage(IStorage storage)
{
Storage = new ArraySegmentByteBuffer(storage.Get(uri));
/*
if (string.IsNullOrEmpty(uri))
{
Storage = (glbDataBytes);
}
else
{
Storage = new UriByteBuffer(baseDir, uri);
}
*/
}
public glTFBuffer()
{
}
public glTFBuffer(IBytesBuffer storage)
{
Storage = storage;
}
public string uri;
[JsonSchema(Required = true, Minimum = 1)]
public int byteLength;
// empty schemas
public object extensions;
public object extras;
public string name;
public glTFBufferView Append<T>(T[] array, glBufferTarget target) where T : struct
{
return Append(new ArraySegment<T>(array), target);
}
public glTFBufferView Append<T>(ArraySegment<T> segment, glBufferTarget target) where T : struct
{
var view = Storage.Extend(segment, target);
byteLength = Storage.GetBytes().Count;
return view;
}
public ArraySegment<Byte> GetBytes()
{
return Storage.GetBytes();
}
protected override void SerializeMembers(GLTFJsonFormatter f)
{
if (!string.IsNullOrEmpty(uri))
{
f.KeyValue(() => uri);
}
f.KeyValue(() => byteLength);
}
}
[Serializable]
public class glTFBufferView : JsonSerializableBase
{
[JsonSchema(Required = true, Minimum = 0)]
public int buffer;
[JsonSchema(Minimum = 0)]
public int byteOffset;
[JsonSchema(Required = true, Minimum = 1)]
public int byteLength;
[JsonSchema(Minimum = 4, Maximum = 252, MultipleOf = 4)]
public int byteStride;
[JsonSchema(EnumSerializationType = EnumSerializationType.AsInt, EnumExcludes = new object[] { glBufferTarget.NONE })]
public glBufferTarget target;
// empty schemas
public object extensions;
public object extras;
public string name;
protected override void SerializeMembers(GLTFJsonFormatter f)
{
f.KeyValue(() => buffer);
f.KeyValue(() => byteOffset);
f.KeyValue(() => byteLength);
if (target != glBufferTarget.NONE)
{
f.Key("target"); f.Value((int)target);
}
/* When this is not defined, data is tightly packed. When two or more accessors use the same bufferView, this field must be defined.
if (byteStride >= 4)
{
f.KeyValue(() => byteStride);
}
*/
}
}
[Serializable]
public class glTFSparseIndices : JsonSerializableBase
{
[JsonSchema(Required = true, Minimum = 0)]
public int bufferView = -1;
[JsonSchema(Minimum = 0)]
public int byteOffset;
[JsonSchema(Required = true, EnumValues = new object[] { 5121, 5123, 5125 })]
public glComponentType componentType;
// empty schemas
public object extensions;
public object extras;
protected override void SerializeMembers(GLTFJsonFormatter f)
{
f.KeyValue(() => bufferView);
f.KeyValue(() => byteOffset);
f.Key("componentType"); f.Value((int)componentType);
}
}
[Serializable]
public class glTFSparseValues : JsonSerializableBase
{
[JsonSchema(Required = true, Minimum = 0)]
public int bufferView = -1;
[JsonSchema(Minimum = 0)]
public int byteOffset;
// empty schemas
public object extensions;
public object extras;
protected override void SerializeMembers(GLTFJsonFormatter f)
{
f.KeyValue(() => bufferView);
f.KeyValue(() => byteOffset);
}
}
[Serializable]
public class glTFSparse : JsonSerializableBase
{
[JsonSchema(Required = true, Minimum = 1)]
public int count;
[JsonSchema(Required = true)]
public glTFSparseIndices indices;
[JsonSchema(Required = true)]
public glTFSparseValues values;
// empty schemas
public object extensions;
public object extras;
protected override void SerializeMembers(GLTFJsonFormatter f)
{
f.KeyValue(() => count);
f.Key("indices"); f.GLTFValue(indices);
f.Key("values"); f.GLTFValue(values);
}
}
[Serializable]
public class glTFAccessor : JsonSerializableBase
{
[JsonSchema(Minimum = 0)]
public int bufferView = -1;
[JsonSchema(Minimum = 0, Dependencies = new string[] { "bufferView" })]
public int byteOffset;
[JsonSchema(Required = true, EnumValues = new object[] { "SCALAR", "VEC2", "VEC3", "VEC4", "MAT2", "MAT3", "MAT4" }, EnumSerializationType = EnumSerializationType.AsString)]
public string type;
public int TypeCount
{
get
{
switch (type)
{
case "SCALAR":
return 1;
case "VEC2":
return 2;
case "VEC3":
return 3;
case "VEC4":
case "MAT2":
return 4;
case "MAT3":
return 9;
case "MAT4":
return 16;
default:
throw new NotImplementedException();
}
}
}
[JsonSchema(Required = true, EnumSerializationType = EnumSerializationType.AsInt)]
public glComponentType componentType;
[JsonSchema(Required = true, Minimum = 1)]
public int count;
[JsonSchema(MinItems = 1, MaxItems = 16)]
public float[] max;
[JsonSchema(MinItems = 1, MaxItems = 16)]
public float[] min;
public bool normalized;
public glTFSparse sparse;
// empty schemas
public string name;
public object extensions;
public object extras;
protected override void SerializeMembers(GLTFJsonFormatter f)
{
f.KeyValue(() => bufferView);
f.KeyValue(() => byteOffset);
f.KeyValue(() => type);
f.Key("componentType"); f.Value((int)componentType);
f.KeyValue(() => count);
if (max != null && max.Any())
{
f.KeyValue(() => max);
}
if (min != null && min.Any())
{
f.KeyValue(() => min);
}
if (sparse != null && sparse.count > 0)
{
f.Key("sparse"); f.GLTFValue(sparse);
}
f.KeyValue(() => normalized);
f.KeyValue(() => name);
}
}
}