forked from qmuntal/gltf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct.go
852 lines (772 loc) · 31.8 KB
/
struct.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
package gltf
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"strings"
)
// An Asset is metadata about the glTF asset.
type Asset struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Copyright string `json:"copyright,omitempty"` // A copyright message suitable for display to credit the content creator.
Generator string `json:"generator,omitempty"` // Tool that generated this glTF model. Useful for debugging.
Version string `json:"version" validate:"required"` // The glTF version that this asset targets.
MinVersion string `json:"minVersion,omitempty"` // The minimum glTF version that this asset targets.
}
// Document defines the root object for a glTF asset.
type Document struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
ExtensionsUsed []string `json:"extensionsUsed,omitempty"`
ExtensionsRequired []string `json:"extensionsRequired,omitempty"`
Accessors []Accessor `json:"accessors,omitempty" validate:"dive"`
Animations []Animation `json:"animations,omitempty" validate:"dive"`
Asset Asset `json:"asset"`
Buffers []Buffer `json:"buffers,omitempty" validate:"dive"`
BufferViews []BufferView `json:"bufferViews,omitempty" validate:"dive"`
Cameras []Camera `json:"cameras,omitempty" validate:"dive"`
Images []Image `json:"images,omitempty" validate:"dive"`
Materials []Material `json:"materials,omitempty" validate:"dive"`
Meshes []Mesh `json:"meshes,omitempty" validate:"dive"`
Nodes []Node `json:"nodes,omitempty" validate:"dive"`
Samplers []Sampler `json:"samplers,omitempty" validate:"dive"`
Scene int32 `json:"scene" validate:"gte=-1"`
Scenes []Scene `json:"scenes,omitempty" validate:"dive"`
Skins []Skin `json:"skins,omitempty" validate:"dive"`
Textures []Texture `json:"textures,omitempty" validate:"dive"`
}
// UnmarshalJSON unmarshal the document with the correct default values.
func (d *Document) UnmarshalJSON(data []byte) error {
type alias Document
tmp := &alias{Scene: -1}
err := json.Unmarshal(data, tmp)
if err == nil {
*d = Document(*tmp)
}
return err
}
// MarshalJSON marshal the document with the correct default values.
func (d *Document) MarshalJSON() ([]byte, error) {
type alias Document
if d.Scene == -1 {
return json.Marshal(&struct {
Scene int32 `json:"scene,omitempty"`
*alias
}{
Scene: 0,
alias: (*alias)(d),
})
}
return json.Marshal(&struct{ *alias }{alias: (*alias)(d)})
}
// An Accessor is a typed view into a bufferView.
// An accessor provides a typed view into a bufferView or a subset of a bufferView
// similar to how WebGL's vertexAttribPointer() defines an attribute in a buffer.
type Accessor struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Name string `json:"name,omitempty"`
BufferView int32 `json:"bufferView" validate:"gte=-1"`
ByteOffset uint32 `json:"byteOffset,omitempty"`
ComponentType ComponentType `json:"componentType" validate:"oneof=5120 5121 5122 5123 5125 5126"`
Normalized bool `json:"normalized,omitempty"` // Specifies whether integer data values should be normalized.
Count uint32 `json:"count" validate:"required"` // The number of attributes referenced by this accessor.
Type AccessorType `json:"type" validate:"oneof=SCALAR VEC2 VEC3 VEC4 MAT2 MAT3 MAT4"`
Max []float64 `json:"max,omitempty" validate:"omitempty,lte=16"` // Maximum value of each component in this attribute.
Min []float64 `json:"min,omitempty" validate:"omitempty,lte=16"` // Minimum value of each component in this attribute.
Sparse *Sparse `json:"sparse,omitempty"` // Sparse storage of attributes that deviate from their initialization value.
}
// NewAccessor returns a default accessor.
func NewAccessor() *Accessor {
return &Accessor{BufferView: -1}
}
// UnmarshalJSON unmarshal the accessor with the correct default values.
func (a *Accessor) UnmarshalJSON(data []byte) error {
type alias Accessor
tmp := alias(*NewAccessor())
err := json.Unmarshal(data, &tmp)
if err == nil {
*a = Accessor(tmp)
}
return err
}
// MarshalJSON marshal the accessor with the correct default values.
func (a *Accessor) MarshalJSON() ([]byte, error) {
type alias Accessor
if a.BufferView == -1 {
return json.Marshal(&struct {
BufferView int32 `json:"bufferView,omitempty"`
*alias
}{
BufferView: 0,
alias: (*alias)(a),
})
}
return json.Marshal(&struct{ *alias }{alias: (*alias)(a)})
}
// Sparse storage of attributes that deviate from their initialization value.
type Sparse struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Count uint32 `json:"count" validate:"gte=1"` // Number of entries stored in the sparse array.
Indices SparseIndices `json:"indices"` // Index array of size count that points to those accessor attributes that deviate from their initialization value.
Values SparseValues `json:"values"` // Array of size count times number of components, storing the displaced accessor attributes pointed by indices.
}
// SparseValues stores the displaced accessor attributes pointed by accessor.sparse.indices.
type SparseValues struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
BufferView uint32 `json:"bufferView"`
ByteOffset uint32 `json:"byteOffset,omitempty"`
}
// SparseIndices defines the indices of those attributes that deviate from their initialization value.
type SparseIndices struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
BufferView uint32 `json:"bufferView"`
ByteOffset uint32 `json:"byteOffset,omitempty"`
ComponentType ComponentType `json:"componentType" validate:"oneof=5121 5123 5125"`
}
// A Buffer points to binary geometry, animation, or skins.
type Buffer struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Name string `json:"name,omitempty"`
URI string `json:"uri,omitempty" validate:"omitempty"`
ByteLength uint32 `json:"byteLength" validate:"required"`
Data []uint8 `json:"-"`
}
// IsEmbeddedResource returns true if the buffer points to an embedded resource.
func (b *Buffer) IsEmbeddedResource() bool {
return strings.HasPrefix(b.URI, mimetypeApplicationOctet)
}
// EmbeddedResource defines the buffer as an embedded resource and encodes the URI so it points to the the resource.
func (b *Buffer) EmbeddedResource() {
b.URI = fmt.Sprintf("%s,%s", mimetypeApplicationOctet, base64.StdEncoding.EncodeToString(b.Data))
}
// marshalData decode the buffer from the URI. If the buffer is not en embedded resource the returned array will be empty.
func (b *Buffer) marshalData() ([]uint8, error) {
if !b.IsEmbeddedResource() {
return []uint8{}, nil
}
startPos := len(mimetypeApplicationOctet) + 1
return base64.StdEncoding.DecodeString(b.URI[startPos:])
}
// BufferView is a view into a buffer generally representing a subset of the buffer.
type BufferView struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Buffer int32 `json:"buffer" validate:"gte=-1"`
ByteOffset uint32 `json:"byteOffset,omitempty"`
ByteLength uint32 `json:"byteLength" validate:"required"`
ByteStride uint32 `json:"byteStride,omitempty" validate:"omitempty,gte=4,lte=252"`
Target Target `json:"target,omitempty" validate:"omitempty,oneof=34962 34963"`
}
// UnmarshalJSON unmarshal the buffer view with the correct default values.
func (b *BufferView) UnmarshalJSON(data []byte) error {
type alias BufferView
tmp := &alias{Buffer: -1}
err := json.Unmarshal(data, tmp)
if err == nil {
*b = BufferView(*tmp)
}
return err
}
// MarshalJSON marshal the buffer view with the correct default values.
func (b *BufferView) MarshalJSON() ([]byte, error) {
type alias BufferView
if b.Buffer == -1 {
return json.Marshal(&struct {
Buffer int32 `json:"buffer,omitempty"`
*alias
}{
Buffer: 0,
alias: (*alias)(b),
})
}
return json.Marshal(&struct{ *alias }{alias: (*alias)(b)})
}
// The Scene contains a list of root nodes.
type Scene struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Name string `json:"name,omitempty"`
Nodes []uint32 `json:"nodes,omitempty" validate:"omitempty,unique"`
}
// A Node in the node hierarchy.
// It can have either a matrix or any combination of translation/rotation/scale (TRS) properties.
type Node struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Name string `json:"name,omitempty"`
Camera int32 `json:"camera" validate:"gte=-1"`
Children []uint32 `json:"children,omitempty" validate:"omitempty,unique"`
Skin int32 `json:"skin" validate:"gte=-1"`
Matrix [16]float64 `json:"matrix"` // A 4x4 transformation matrix stored in column-major order.
Mesh int32 `json:"mesh" validate:"gte=-1"`
Rotation [4]float64 `json:"rotation" validate:"omitempty,dive,gte=-1,lte=1"` // The node's unit quaternion rotation in the order (x, y, z, w), where w is the scalar.
Scale [3]float64 `json:"scale"`
Translation [3]float64 `json:"translation"`
Weights []float64 `json:"weights,omitempty"` // The weights of the instantiated Morph Target.
}
// NewNode returns a default Node.
func NewNode() *Node {
return &Node{
Matrix: [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
Rotation: [4]float64{0, 0, 0, 1},
Scale: [3]float64{1, 1, 1},
Camera: -1,
Skin: -1,
Mesh: -1,
}
}
// UnmarshalJSON unmarshal the node with the correct default values.
func (n *Node) UnmarshalJSON(data []byte) error {
type alias Node
tmp := alias(*NewNode())
err := json.Unmarshal(data, &tmp)
if err == nil {
*n = Node(tmp)
}
return err
}
// MarshalJSON marshal the node with the correct default values.
func (n *Node) MarshalJSON() ([]byte, error) {
type alias Node
out, err := json.Marshal(&struct{ *alias }{alias: (*alias)(n)})
if err == nil {
if n.Matrix == [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1} {
out = removeProperty([]byte(`"matrix":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]`), out)
}
if n.Rotation == [4]float64{0, 0, 0, 1} {
out = removeProperty([]byte(`"rotation":[0,0,0,1]`), out)
}
if n.Scale == [3]float64{1, 1, 1} {
out = removeProperty([]byte(`"scale":[1,1,1]`), out)
}
if n.Translation == [3]float64{0, 0, 0} {
out = removeProperty([]byte(`"translation":[0,0,0]`), out)
}
if n.Camera == -1 {
out = removeProperty([]byte(`"camera":-1`), out)
}
if n.Skin == -1 {
out = removeProperty([]byte(`"skin":-1`), out)
}
if n.Mesh == -1 {
out = removeProperty([]byte(`"mesh":-1`), out)
}
out = sanitizeJSON(out)
}
return out, err
}
// Skin defines joints and matrices.
type Skin struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Name string `json:"name,omitempty"`
InverseBindMatrices int32 `json:"inverseBindMatrices" validate:"gte=-1"` // The index of the accessor containing the floating-point 4x4 inverse-bind matrices.
Skeleton int32 `json:"skeleton" validate:"gte=-1"` // The index of the node used as a skeleton root. When undefined, joints transforms resolve to scene root.
Joints []uint32 `json:"joints" validate:"omitempty,unique"` // Indices of skeleton nodes, used as joints in this skin.
}
// NewSkin create a default Skin.
func NewSkin() *Skin {
return &Skin{InverseBindMatrices: -1, Skeleton: -1}
}
// UnmarshalJSON unmarshal the skin with the correct default values.
func (s *Skin) UnmarshalJSON(data []byte) error {
type alias Skin
tmp := alias(*NewSkin())
err := json.Unmarshal(data, &tmp)
if err == nil {
*s = Skin(tmp)
}
return err
}
// MarshalJSON marshal the skin with the correct default values.
func (s *Skin) MarshalJSON() ([]byte, error) {
type alias Skin
out, err := json.Marshal(&struct{ *alias }{alias: (*alias)(s)})
if err == nil {
if s.InverseBindMatrices == -1 {
out = removeProperty([]byte(`"inverseBindMatrices":-1`), out)
}
if s.Skeleton == -1 {
out = removeProperty([]byte(`"skeleton":-1`), out)
}
out = sanitizeJSON(out)
}
return out, err
}
// A Camera projection. A node can reference a camera to apply a transform to place the camera in the scene.
type Camera struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Name string `json:"name,omitempty"`
Orthographic *Orthographic `json:"orthographic,omitempty"`
Perspective *Perspective `json:"perspective,omitempty"`
Type CameraType `json:"type" validate:"oneof=perspective orthographic"`
}
// Orthographic camera containing properties to create an orthographic projection matrix.
type Orthographic struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Xmag float64 `json:"xmag"` // The horizontal magnification of the view.
Ymag float64 `json:"ymag"` // The vertical magnification of the view.
Zfar float64 `json:"zfar" validate:"gt=0,gtfield=Znear"` // The distance to the far clipping plane.
Znear float64 `json:"znear" validate:"gte=0"` // The distance to the near clipping plane.
}
// Perspective camera containing properties to create a perspective projection matrix.
type Perspective struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
AspectRatio float64 `json:"aspectRatio,omitempty"`
Yfov float64 `json:"yfov"` // The vertical field of view in radians.
Zfar float64 `json:"zfar,omitempty"` // The distance to the far clipping plane.
Znear float64 `json:"znear"` // The distance to the near clipping plane.
}
// A Mesh is a set of primitives to be rendered. A node can contain one mesh. A node's transform places the mesh in the scene.
type Mesh struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Name string `json:"name,omitempty"`
Primitives []Primitive `json:"primitives" validate:"required,gt=0,dive"`
Weights []float64 `json:"weights,omitempty"`
}
// Primitive defines the geometry to be rendered with the given material.
type Primitive struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Attributes Attribute `json:"attributes"`
Indices int32 `json:"indices" validate:"gte=-1"` // The index of the accessor that contains the indices.
Material int32 `json:"material" validate:"gte=-1"`
Mode PrimitiveMode `json:"mode" validate:"lte=6"`
Targets []Attribute `json:"targets,omitempty" validate:"omitempty,dive,dive,keys,oneof=POSITION NORMAL TANGENT,endkeys"` // Only POSITION, NORMAL, and TANGENT supported.
}
// NewPrimitive create a default Primitive.
func NewPrimitive() *Primitive {
return &Primitive{Mode: Triangles, Indices: -1, Material: -1}
}
// UnmarshalJSON unmarshal the primitive with the correct default values.
func (p *Primitive) UnmarshalJSON(data []byte) error {
type alias Primitive
tmp := alias(*NewPrimitive())
err := json.Unmarshal(data, &tmp)
if err == nil {
*p = Primitive(tmp)
}
return err
}
// MarshalJSON marshal the primitive with the correct default values.
func (p *Primitive) MarshalJSON() ([]byte, error) {
type alias Primitive
out, err := json.Marshal(&struct{ *alias }{alias: (*alias)(p)})
if err == nil {
if p.Indices == -1 {
out = removeProperty([]byte(`"indices":-1`), out)
}
if p.Material == -1 {
out = removeProperty([]byte(`"material":-1`), out)
}
out = sanitizeJSON(out)
}
return out, err
}
// The Material appearance of a primitive.
type Material struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Name string `json:"name,omitempty"`
PBRMetallicRoughness *PBRMetallicRoughness `json:"pbrMetallicRoughness,omitempty"`
NormalTexture *NormalTexture `json:"normalTexture,omitempty"`
OcclusionTexture *OcclusionTexture `json:"occlusionTexture,omitempty"`
EmissiveTexture *TextureInfo `json:"emissiveTexture,omitempty"`
EmissiveFactor [3]float64 `json:"emissiveFactor,omitempty" validate:"dive,gte=0,lte=1"`
AlphaMode AlphaMode `json:"alphaMode,omitempty" validate:"oneof=OPAQUE MASK BLEND"`
AlphaCutoff float64 `json:"alphaCutoff" validate:"gte=0"`
DoubleSided bool `json:"doubleSided,omitempty"`
}
// NewMaterial create a default Material.
func NewMaterial() *Material {
return &Material{AlphaCutoff: 0.5, AlphaMode: Opaque}
}
// UnmarshalJSON unmarshal the material with the correct default values.
func (m *Material) UnmarshalJSON(data []byte) error {
type alias Material
tmp := alias(*NewMaterial())
err := json.Unmarshal(data, &tmp)
if err == nil {
*m = Material(tmp)
}
return err
}
// MarshalJSON marshal the material with the correct default values.
func (m *Material) MarshalJSON() ([]byte, error) {
type alias Material
out, err := json.Marshal(&struct{ *alias }{alias: (*alias)(m)})
if err == nil {
if m.AlphaCutoff == 0.5 {
out = removeProperty([]byte(`"alphaCutoff":0.5`), out)
}
if m.AlphaMode == Opaque {
out = removeProperty([]byte(`"alphaMode":"OPAQUE"`), out)
}
if m.EmissiveFactor == [3]float64{0, 0, 0} {
out = removeProperty([]byte(`"emissiveFactor":[0,0,0]`), out)
}
out = sanitizeJSON(out)
}
return out, err
}
// A NormalTexture references to a normal texture.
type NormalTexture struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Index int32 `json:"index" validate:"gte=-1"`
TexCoord uint32 `json:"texCoord,omitempty"` // The index of texture's TEXCOORD attribute used for texture coordinate mapping.
Scale float64 `json:"scale"`
}
// NewNormalTexture returns a default NormalTexture.
func NewNormalTexture(index int32) *NormalTexture {
return &NormalTexture{Index: index, Scale: 1}
}
// UnmarshalJSON unmarshal the texture info with the correct default values.
func (n *NormalTexture) UnmarshalJSON(data []byte) error {
type alias NormalTexture
tmp := alias(*NewNormalTexture(-1))
err := json.Unmarshal(data, &tmp)
if err == nil {
*n = NormalTexture(tmp)
}
return err
}
// MarshalJSON marshal the texture info with the correct default values.
func (n *NormalTexture) MarshalJSON() ([]byte, error) {
type alias NormalTexture
out, err := json.Marshal(&struct{ *alias }{alias: (*alias)(n)})
if err == nil {
if n.Index == -1 {
out = removeProperty([]byte(`"index":-1`), out)
}
if n.Scale == -1 {
out = removeProperty([]byte(`"scale":-1`), out)
}
out = sanitizeJSON(out)
}
return out, err
}
// An OcclusionTexture references to an occlusion texture
type OcclusionTexture struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Index int32 `json:"index" validate:"gte=-1"`
TexCoord uint32 `json:"texCoord,omitempty"` // The index of texture's TEXCOORD attribute used for texture coordinate mapping.
Strength float64 `json:"strength" validate:"gte=0,lte=1"`
}
// NewOcclusionTexture returns a default OcclusionTexture.
func NewOcclusionTexture(index int32) *OcclusionTexture {
return &OcclusionTexture{Index: index, Strength: 1}
}
// UnmarshalJSON unmarshal the texture info with the correct default values.
func (o *OcclusionTexture) UnmarshalJSON(data []byte) error {
type alias OcclusionTexture
tmp := alias(*NewOcclusionTexture(-1))
err := json.Unmarshal(data, &tmp)
if err == nil {
*o = OcclusionTexture(tmp)
}
return err
}
// MarshalJSON marshal the texture info with the correct default values.
func (o *OcclusionTexture) MarshalJSON() ([]byte, error) {
type alias OcclusionTexture
out, err := json.Marshal(&struct{ *alias }{alias: (*alias)(o)})
if err == nil {
if o.Index == -1 {
out = removeProperty([]byte(`"index":-1`), out)
}
if o.Strength == 1 {
out = removeProperty([]byte(`"strength":1`), out)
}
out = sanitizeJSON(out)
}
return out, err
}
// PBRMetallicRoughness defines a set of parameter values that are used to define the metallic-roughness material model from Physically-Based Rendering (PBR) methodology.
type PBRMetallicRoughness struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
BaseColorFactor [4]float64 `json:"baseColorFactor" validate:"dive,gte=0,lte=1"`
BaseColorTexture *TextureInfo `json:"baseColorTexture,omitempty"`
MetallicFactor float64 `json:"metallicFactor" validate:"gte=0,lte=1"`
RoughnessFactor float64 `json:"roughnessFactor" validate:"gte=0,lte=1"`
MetallicRoughnessTexture *TextureInfo `json:"metallicRoughnessTexture,omitempty"`
}
// NewPBRMetallicRoughness returns a default PBRMetallicRoughness.
func NewPBRMetallicRoughness() *PBRMetallicRoughness {
return &PBRMetallicRoughness{BaseColorFactor: [4]float64{1, 1, 1, 1}, MetallicFactor: 1, RoughnessFactor: 1}
}
// UnmarshalJSON unmarshal the pbr with the correct default values.
func (p *PBRMetallicRoughness) UnmarshalJSON(data []byte) error {
type alias PBRMetallicRoughness
tmp := alias(*NewPBRMetallicRoughness())
err := json.Unmarshal(data, &tmp)
if err == nil {
*p = PBRMetallicRoughness(tmp)
}
return err
}
// MarshalJSON marshal the pbr with the correct default values.
func (p *PBRMetallicRoughness) MarshalJSON() ([]byte, error) {
type alias PBRMetallicRoughness
out, err := json.Marshal(&struct{ *alias }{alias: (*alias)(p)})
if err == nil {
if p.MetallicFactor == 1 {
out = removeProperty([]byte(`"metallicFactor":1`), out)
}
if p.RoughnessFactor == 1 {
out = removeProperty([]byte(`"roughnessFactor":1`), out)
}
if p.BaseColorFactor == [4]float64{1, 1, 1, 1} {
out = removeProperty([]byte(`"baseColorFactor":[1,1,1,1]`), out)
}
out = sanitizeJSON(out)
}
return out, err
}
// TextureInfo references to a texture.
type TextureInfo struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Index int32 `json:"index" validate:"gte=-1"`
TexCoord uint32 `json:"texCoord,omitempty"` // The index of texture's TEXCOORD attribute used for texture coordinate mapping.
}
// NewTextureInfo returns a default TextureInfo.
func NewTextureInfo(index int32) *TextureInfo {
return &TextureInfo{Index: index}
}
// UnmarshalJSON unmarshal the texture info with the correct default values.
func (t *TextureInfo) UnmarshalJSON(data []byte) error {
type alias TextureInfo
tmp := alias(*NewTextureInfo(-1))
err := json.Unmarshal(data, &tmp)
if err == nil {
*t = TextureInfo(tmp)
}
return err
}
// MarshalJSON marshal the texture info with the correct default values.
func (t *TextureInfo) MarshalJSON() ([]byte, error) {
type alias TextureInfo
if t.Index == -1 {
return json.Marshal(&struct {
Index int32 `json:"index,omitempty"`
*alias
}{
Index: 0,
alias: (*alias)(t),
})
}
return json.Marshal(&struct{ *alias }{alias: (*alias)(t)})
}
// A Texture and its sampler.
type Texture struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Name string `json:"name,omitempty"`
Sampler int32 `json:"sampler" validate:"gte=-1"`
Source int32 `json:"source" validate:"gte=-1"`
}
// NewTexture returns a default Texture.
func NewTexture() *Texture {
return &Texture{Sampler: -1, Source: -1}
}
// UnmarshalJSON unmarshal the texture with the correct default values.
func (t *Texture) UnmarshalJSON(data []byte) error {
type alias Texture
tmp := alias(*NewTexture())
err := json.Unmarshal(data, &tmp)
if err == nil {
*t = Texture(tmp)
}
return err
}
// MarshalJSON marshal the texture with the correct default values.
func (t *Texture) MarshalJSON() ([]byte, error) {
type alias Texture
out, err := json.Marshal(&struct{ *alias }{alias: (*alias)(t)})
if err == nil {
if t.Sampler == -1 {
out = removeProperty([]byte(`"sampler":-1`), out)
}
if t.Source == -1 {
out = removeProperty([]byte(`"source":-1`), out)
}
out = sanitizeJSON(out)
}
return out, err
}
// Sampler of a texture for filtering and wrapping modes.
type Sampler struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Name string `json:"name,omitempty"`
MagFilter MagFilter `json:"magFilter,omitempty" validate:"omitempty,oneof=9728 9729"`
MinFilter MinFilter `json:"minFilter,omitempty" validate:"omitempty,oneof=9728 9729 9984 9985 9986 9987"`
WrapS WrappingMode `json:"wrapS,omitempty" validate:"omitempty,oneof=33071 33648 10497"`
WrapT WrappingMode `json:"wrapT,omitempty" validate:"omitempty,oneof=33071 33648 10497"`
}
// NewSampler returns a default Sampler.
func NewSampler() *Sampler {
return &Sampler{WrapS: Repeat, WrapT: Repeat}
}
// UnmarshalJSON unmarshal the sampler with the correct default values.
func (s *Sampler) UnmarshalJSON(data []byte) error {
type alias Sampler
tmp := alias(*NewSampler())
err := json.Unmarshal(data, &tmp)
if err == nil {
*s = Sampler(tmp)
}
return err
}
// Image data used to create a texture. Image can be referenced by URI or bufferView index.
// mimeType is required in the latter case.
type Image struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Name string `json:"name,omitempty"`
URI string `json:"uri,omitempty" validate:"omitempty"`
MimeType string `json:"mimeType,omitempty" validate:"omitempty,oneof=image/jpeg image/png"` // Manadatory if BufferView is defined.
BufferView uint32 `json:"bufferView,omitempty"` // Use this instead of the image's uri property.
}
// IsEmbeddedResource returns true if the buffer points to an embedded resource.
func (im *Image) IsEmbeddedResource() bool {
return strings.HasPrefix(im.URI, mimetypeImagePNG) || strings.HasPrefix(im.URI, mimetypeImageJPG)
}
// MarshalData decode the image from the URI. If the image is not en embedded resource the returned array will be empty.
func (im *Image) MarshalData() ([]uint8, error) {
if !im.IsEmbeddedResource() {
return []uint8{}, nil
}
mimetype := mimetypeImagePNG
if strings.HasPrefix(im.URI, mimetypeImageJPG) {
mimetype = mimetypeImageJPG
}
startPos := len(mimetype) + 1
return base64.StdEncoding.DecodeString(im.URI[startPos:])
}
// An Animation keyframe.
type Animation struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Name string `json:"name,omitempty"`
Channels []Channel `json:"channels" validate:"required,gt=0,dive"`
Samplers []AnimationSampler `json:"samplers" validate:"required,gt=0,dive"`
}
// AnimationSampler combines input and output accessors with an interpolation algorithm to define a keyframe graph (but not its target).
type AnimationSampler struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Input int32 `json:"input" validate:"gte=-1"` // The index of an accessor containing keyframe input values.
Interpolation Interpolation `json:"interpolation,omitempty" validate:"omitempty,oneof=LINEAR STEP CUBICSPLINE"`
Output int32 `json:"output" validate:"gte=-1"` // The index of an accessor containing keyframe output values.
}
// NewAnimationSampler returns a default AnimationSampler.
func NewAnimationSampler() *AnimationSampler {
return &AnimationSampler{Input: -1, Interpolation: Linear, Output: -1}
}
// UnmarshalJSON unmarshal the animation sampler with the correct default values.
func (as *AnimationSampler) UnmarshalJSON(data []byte) error {
type alias AnimationSampler
tmp := alias(*NewAnimationSampler())
err := json.Unmarshal(data, &tmp)
if err == nil {
*as = AnimationSampler(tmp)
}
return err
}
// The Channel targets an animation's sampler at a node's property.
type Channel struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Sampler int32 `json:"sampler" validate:"gte=-1"`
Target ChannelTarget `json:"target"`
}
// NewChannel returns a default Channel.
func NewChannel(sampler int32) *Channel {
return &Channel{Sampler: sampler}
}
// UnmarshalJSON unmarshal the channel with the correct default values.
func (ch *Channel) UnmarshalJSON(data []byte) error {
type alias Channel
tmp := alias(*NewChannel(-1))
err := json.Unmarshal(data, &tmp)
if err == nil {
*ch = Channel(tmp)
}
return err
}
// MarshalJSON marshal the channel with the correct default values.
func (ch *Channel) MarshalJSON() ([]byte, error) {
type alias Channel
if ch.Sampler == -1 {
return json.Marshal(&struct {
Sampler int32 `json:"sampler,omitempty"`
*alias
}{
Sampler: 0,
alias: (*alias)(ch),
})
}
return json.Marshal(&struct{ *alias }{alias: (*alias)(ch)})
}
// ChannelTarget describes the index of the node and TRS property that an animation channel targets.
// The Path represents the name of the node's TRS property to modify, or the "weights" of the Morph Targets it instantiates.
// For the "translation" property, the values that are provided by the sampler are the translation along the x, y, and z axes.
// For the "rotation" property, the values are a quaternion in the order (x, y, z, w), where w is the scalar.
// For the "scale" property, the values are the scaling factors along the x, y, and z axes.
type ChannelTarget struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Node int32 `json:"node" validate:"gte=-1"`
Path TRSProperty `json:"path" validate:"oneof=translation rotation scale weights"`
}
// NewChannelTarget returns a default ChannelTarget.
func NewChannelTarget(path TRSProperty) *ChannelTarget {
return &ChannelTarget{Node: -1, Path: path}
}
// UnmarshalJSON unmarshal the channel target with the correct default values.
func (ch *ChannelTarget) UnmarshalJSON(data []byte) error {
type alias ChannelTarget
tmp := alias(*NewChannelTarget(""))
err := json.Unmarshal(data, &tmp)
if err == nil {
*ch = ChannelTarget(tmp)
}
return err
}
// MarshalJSON marshal the channel target with the correct default values.
func (ch *ChannelTarget) MarshalJSON() ([]byte, error) {
type alias ChannelTarget
if ch.Node == -1 {
return json.Marshal(&struct {
Node int32 `json:"node,omitempty"`
*alias
}{
Node: 0,
alias: (*alias)(ch),
})
}
return json.Marshal(&struct{ *alias }{alias: (*alias)(ch)})
}
func removeProperty(str []byte, b []byte) []byte {
b = bytes.Replace(b, str, []byte(""), 1)
return bytes.Replace(b, []byte(`,,`), []byte(","), 1)
}
func sanitizeJSON(b []byte) []byte {
b = bytes.Replace(b, []byte(`{,`), []byte("{"), 1)
return bytes.Replace(b, []byte(`,}`), []byte("}"), 1)
}