-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
const.go
436 lines (386 loc) · 12 KB
/
const.go
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//go:generate stringer -linecomment -type ComponentType,AccessorType,PrimitiveMode,AlphaMode,MagFilter,MinFilter,WrappingMode,Interpolation,Target,TRSProperty -output const_strings.go
package gltf
import (
"encoding/json"
"fmt"
)
var (
// DefaultMatrix defines an identity matrix.
DefaultMatrix = [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
// DefaultRotation defines a quaternion without rotation.
DefaultRotation = [4]float64{0, 0, 0, 1}
// DefaultScale defines a scaling that does not modify the size of the object.
DefaultScale = [3]float64{1, 1, 1}
// DefaultTranslation defines a translation that does not move the object.
DefaultTranslation = [3]float64{0, 0, 0}
)
// Attribute names defined in the spec.
const (
POSITION = "POSITION"
NORMAL = "NORMAL"
TANGENT = "TANGENT"
TEXCOORD_0 = "TEXCOORD_0"
TEXCOORD_1 = "TEXCOORD_1"
WEIGHTS_0 = "WEIGHTS_0"
JOINTS_0 = "JOINTS_0"
COLOR_0 = "COLOR_0"
)
var (
emptyMatrix = [16]float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
emptyRotation = [4]float64{0, 0, 0, 0}
emptyScale = [3]float64{0, 0, 0}
)
// The ComponentType is the datatype of components in the attribute. All valid values correspond to WebGL enums.
// 5125 (UNSIGNED_INT) is only allowed when the accessor contains indices.
type ComponentType uint16
const (
ComponentFloat ComponentType = iota // FLOAT
ComponentByte // BYTE
ComponentUbyte // UNSIGNED_BYTE
ComponentShort // SHORT
ComponentUshort // UNSIGNED_SHORT
ComponentUint // UNSIGNED_INT
)
// ByteSize returns the size of a component in bytes.
func (c ComponentType) ByteSize() int {
return map[ComponentType]int{
ComponentByte: 1,
ComponentUbyte: 1,
ComponentShort: 2,
ComponentUshort: 2,
ComponentUint: 4,
ComponentFloat: 4,
}[c]
}
// UnmarshalJSON unmarshal the component type with the correct default values.
func (c *ComponentType) UnmarshalJSON(data []byte) error {
var tmp uint16
err := json.Unmarshal(data, &tmp)
if err == nil {
*c = map[uint16]ComponentType{
5120: ComponentByte,
5121: ComponentUbyte,
5122: ComponentShort,
5123: ComponentUshort,
5125: ComponentUint,
5126: ComponentFloat,
}[tmp]
}
return err
}
// MarshalJSON marshal the component type with the correct default values.
func (c *ComponentType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[ComponentType]uint16{
ComponentByte: 5120,
ComponentUbyte: 5121,
ComponentShort: 5122,
ComponentUshort: 5123,
ComponentUint: 5125,
ComponentFloat: 5126,
}[*c])
}
// AccessorType specifies if the attribute is a scalar, vector, or matrix.
type AccessorType uint8
const (
AccessorScalar AccessorType = iota // SCALAR
AccessorVec2 // VEC2
AccessorVec3 // VEC3
AccessorVec4 // VEC4
AccessorMat2 // MAT2
AccessorMat3 // MAT3
AccessorMat4 // MAT4
)
// Components returns the number of components of an accessor type.
func (a AccessorType) Components() int {
return map[AccessorType]int{
AccessorScalar: 1,
AccessorVec2: 2,
AccessorVec3: 3,
AccessorVec4: 4,
AccessorMat2: 4,
AccessorMat3: 9,
AccessorMat4: 16,
}[a]
}
// UnmarshalJSON unmarshal the accessor type with the correct default values.
func (a *AccessorType) UnmarshalJSON(data []byte) error {
var tmp string
err := json.Unmarshal(data, &tmp)
if err == nil {
accType, ok := map[string]AccessorType{
"SCALAR": AccessorScalar,
"VEC2": AccessorVec2,
"VEC3": AccessorVec3,
"VEC4": AccessorVec4,
"MAT2": AccessorMat2,
"MAT3": AccessorMat3,
"MAT4": AccessorMat4,
}[tmp]
if !ok {
return fmt.Errorf("gltf: unknown accessor's type: %s", tmp)
}
*a = accType
}
return err
}
// MarshalJSON marshal the accessor type with the correct default values.
func (a *AccessorType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[AccessorType]string{
AccessorScalar: "SCALAR",
AccessorVec2: "VEC2",
AccessorVec3: "VEC3",
AccessorVec4: "VEC4",
AccessorMat2: "MAT2",
AccessorMat3: "MAT3",
AccessorMat4: "MAT4",
}[*a])
}
// The Target that the GPU buffer should be bound to.
type Target uint16
const (
TargetNone Target = 0 // NONE
TargetArrayBuffer Target = 34962 // ARRAY_BUFFER
TargetElementArrayBuffer Target = 34963 // ELEMENT_ARRAY_BUFFER
)
// PrimitiveAttributes is a map that each key corresponds to mesh attribute semantic and each value is the index of the accessor containing attribute's data.
type PrimitiveAttributes = map[string]int
// PrimitiveMode defines the type of primitives to render. All valid values correspond to WebGL enums.
type PrimitiveMode uint8
const (
PrimitiveTriangles PrimitiveMode = iota // TRIANGLES
PrimitivePoints // POINTS
PrimitiveLines // LINES
PrimitiveLineLoop // LINE_LOOP
PrimitiveLineStrip // LINE_STRIP
PrimitiveTriangleStrip // TRIANGLE_STRIP
PrimitiveTriangleFan // TRIANGLE_FAN
)
// UnmarshalJSON unmarshal the primitive mode with the correct default values.
func (p *PrimitiveMode) UnmarshalJSON(data []byte) error {
var tmp uint8
err := json.Unmarshal(data, &tmp)
if err == nil {
*p = map[uint8]PrimitiveMode{
0: PrimitivePoints,
1: PrimitiveLines,
2: PrimitiveLineLoop,
3: PrimitiveLineStrip,
4: PrimitiveTriangles,
5: PrimitiveTriangleStrip,
6: PrimitiveTriangleFan,
}[tmp]
}
return err
}
// MarshalJSON marshal the primitive mode with the correct default values.
func (p *PrimitiveMode) MarshalJSON() ([]byte, error) {
return json.Marshal(map[PrimitiveMode]uint8{
PrimitivePoints: 0,
PrimitiveLines: 1,
PrimitiveLineLoop: 2,
PrimitiveLineStrip: 3,
PrimitiveTriangles: 4,
PrimitiveTriangleStrip: 5,
PrimitiveTriangleFan: 6,
}[*p])
}
// The AlphaMode enumeration specifying the interpretation of the alpha value of the main factor and texture.
type AlphaMode uint8
const (
AlphaOpaque AlphaMode = iota // OPAQUE
AlphaMask // MASK
AlphaBlend // BLEND
)
// UnmarshalJSON unmarshal the alpha mode with the correct default values.
func (a *AlphaMode) UnmarshalJSON(data []byte) error {
var tmp string
err := json.Unmarshal(data, &tmp)
if err == nil {
*a = map[string]AlphaMode{
"OPAQUE": AlphaOpaque,
"MASK": AlphaMask,
"BLEND": AlphaBlend,
}[tmp]
}
return err
}
// MarshalJSON marshal the alpha mode with the correct default values.
func (a *AlphaMode) MarshalJSON() ([]byte, error) {
return json.Marshal(map[AlphaMode]string{
AlphaOpaque: "OPAQUE",
AlphaMask: "MASK",
AlphaBlend: "BLEND",
}[*a])
}
// MagFilter is the magnification filter.
type MagFilter uint16
const (
MagUndefined MagFilter = iota // UNDEFINED
MagLinear // LINEAR
MagNearest // NEAREST
)
// UnmarshalJSON unmarshal the mag filter with the correct default values.
func (m *MagFilter) UnmarshalJSON(data []byte) error {
var tmp uint16
err := json.Unmarshal(data, &tmp)
if err == nil {
*m = map[uint16]MagFilter{
9728: MagNearest,
9729: MagLinear,
}[tmp]
}
return err
}
// MarshalJSON marshal the alpha mode with the correct default values.
func (m *MagFilter) MarshalJSON() ([]byte, error) {
return json.Marshal(map[MagFilter]uint16{
MagNearest: 9728,
MagLinear: 9729,
}[*m])
}
// MinFilter is the minification filter.
type MinFilter uint16
const (
MinUndefined MinFilter = iota // UNDEFINED
MinLinear // LINEAR
MinNearestMipMapLinear // NEAREST_MIPMAP_LINEAR
MinNearest // NEAREST
MinNearestMipMapNearest // NEAREST_MIPMAP_NEAREST
MinLinearMipMapNearest // LINEAR_MIPMAP_NEAREST
MinLinearMipMapLinear // LINEAR_MIPMAP_LINEAR
)
// UnmarshalJSON unmarshal the min filter with the correct default values.
func (m *MinFilter) UnmarshalJSON(data []byte) error {
var tmp uint16
err := json.Unmarshal(data, &tmp)
if err == nil {
*m = map[uint16]MinFilter{
9728: MinNearest,
9729: MinLinear,
9984: MinNearestMipMapNearest,
9985: MinLinearMipMapNearest,
9986: MinNearestMipMapLinear,
9987: MinLinearMipMapLinear,
}[tmp]
}
return err
}
// MarshalJSON marshal the min filter with the correct default values.
func (m *MinFilter) MarshalJSON() ([]byte, error) {
return json.Marshal(map[MinFilter]uint16{
MinNearest: 9728,
MinLinear: 9729,
MinNearestMipMapNearest: 9984,
MinLinearMipMapNearest: 9985,
MinNearestMipMapLinear: 9986,
MinLinearMipMapLinear: 9987,
}[*m])
}
// WrappingMode is the wrapping mode of a texture.
type WrappingMode uint16
const (
WrapRepeat WrappingMode = iota // REPEAT
WrapClampToEdge // CLAMP_TO_EDGE
WrapMirroredRepeat // MIRRORED_REPEAT
)
// UnmarshalJSON unmarshal the wrapping mode with the correct default values.
func (w *WrappingMode) UnmarshalJSON(data []byte) error {
var tmp uint16
err := json.Unmarshal(data, &tmp)
if err == nil {
*w = map[uint16]WrappingMode{
33071: WrapClampToEdge,
33648: WrapMirroredRepeat,
10497: WrapRepeat,
}[tmp]
}
return err
}
// MarshalJSON marshal the wrapping mode with the correct default values.
func (w *WrappingMode) MarshalJSON() ([]byte, error) {
return json.Marshal(map[WrappingMode]uint16{
WrapClampToEdge: 33071,
WrapMirroredRepeat: 33648,
WrapRepeat: 10497,
}[*w])
}
// Interpolation algorithm.
type Interpolation uint8
const (
InterpolationLinear Interpolation = iota // LINEAR
InterpolationStep // STEP
InterpolationCubicSpline // CUBICSPLINE
)
// UnmarshalJSON unmarshal the interpolation with the correct default values.
func (i *Interpolation) UnmarshalJSON(data []byte) error {
var tmp string
err := json.Unmarshal(data, &tmp)
if err == nil {
*i = map[string]Interpolation{
"LINEAR": InterpolationLinear,
"STEP": InterpolationStep,
"CUBICSPLINE": InterpolationCubicSpline,
}[tmp]
}
return err
}
// MarshalJSON marshal the interpolation with the correct default values.
func (i *Interpolation) MarshalJSON() ([]byte, error) {
return json.Marshal(map[Interpolation]string{
InterpolationLinear: "LINEAR",
InterpolationStep: "STEP",
InterpolationCubicSpline: "CUBICSPLINE",
}[*i])
}
// TRSProperty defines a local space transformation.
// TRSproperties are converted to matrices and postmultiplied in the T * R * S order to compose the transformation matrix.
type TRSProperty uint8
const (
TRSTranslation TRSProperty = iota // translation
TRSRotation // rotation
TRSScale // scale
TRSWeights // weights
)
// UnmarshalJSON unmarshal the TRSProperty with the correct default values.
func (t *TRSProperty) UnmarshalJSON(data []byte) error {
var tmp string
err := json.Unmarshal(data, &tmp)
if err == nil {
*t = map[string]TRSProperty{
"translation": TRSTranslation,
"rotation": TRSRotation,
"scale": TRSScale,
"weights": TRSWeights,
}[tmp]
}
return err
}
// MarshalJSON marshal the TRSProperty with the correct default values.
func (t *TRSProperty) MarshalJSON() ([]byte, error) {
return json.Marshal(map[TRSProperty]string{
TRSTranslation: "translation",
TRSRotation: "rotation",
TRSScale: "scale",
TRSWeights: "weights",
}[*t])
}
const (
glbHeaderMagic = 0x46546c67
glbChunkJSON = 0x4e4f534a
glbChunkBIN = 0x004e4942
)
type chunkHeader struct {
Length uint32
Type uint32
}
type glbHeader struct {
Magic uint32
Version uint32
Length uint32
JSONHeader chunkHeader
}
const (
mimetypeApplicationOctet = "data:application/octet-stream;base64"
mimetypeImagePNG = "data:image/png;base64"
mimetypeImageJPG = "data:image/jpeg;base64"
)