forked from qmuntal/gltf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextensions.go
82 lines (72 loc) · 2.72 KB
/
extensions.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
package gltf
import "encoding/json"
const (
ExtPBRSpecularGlossiness = "KHR_materials_pbrSpecularGlossiness"
)
// Extension is map where the keys are the extension identifiers and the values are the extensions payloads.
// If a key matches with one of the supported extensions the value will be marshalled as a pointer to the extension struct.
// If a key does not match with any of the supported extensions the value will be a json.RawMessage so its decoding can be delayed.
type Extensions map[string]interface{}
type envelope map[string]json.RawMessage
// UnmarshalJSON unmarshal the extensions with the supported extensions initialized.
func (ext *Extensions) UnmarshalJSON(data []byte) error {
if len(*ext) == 0 {
*ext = make(Extensions)
}
var raw envelope
err := json.Unmarshal(data, &raw)
if err == nil {
for key, value := range raw {
switch key {
case ExtPBRSpecularGlossiness:
n := &PBRSpecularGlossiness{}
if err := json.Unmarshal(value, n); err != nil {
return err
}
(*ext)[ExtPBRSpecularGlossiness] = n
default:
(*ext)[key] = value
}
}
}
return err
}
type PBRSpecularGlossiness struct {
DiffuseFactor [4]float64 `json:"diffuseFactor" validate:"dive,gte=0,lte=1"`
DiffuseTexture *TextureInfo `json:"diffuseTexture,omitempty"`
SpecularFactor [3]float64 `json:"specularFactor" validate:"dive,gte=0,lte=1"`
GlossinessFactor float64 `json:"glossinessFactor" validate:"gte=0,lte=1"`
SpecularGlossinessTexture *TextureInfo `json:"specularGlossinessTexture,omitempty"`
}
// PBRSpecularGlossiness returns a default PBRSpecularGlossiness.
func NewPBRSpecularGlossiness() *PBRSpecularGlossiness {
return &PBRSpecularGlossiness{DiffuseFactor: [4]float64{1, 1, 1, 1}, SpecularFactor: [3]float64{1, 1, 1}, GlossinessFactor: 1}
}
// UnmarshalJSON unmarshal the pbr with the correct default values.
func (p *PBRSpecularGlossiness) UnmarshalJSON(data []byte) error {
type alias PBRSpecularGlossiness
tmp := alias(*NewPBRSpecularGlossiness())
err := json.Unmarshal(data, &tmp)
if err == nil {
*p = PBRSpecularGlossiness(tmp)
}
return err
}
// MarshalJSON marshal the pbr with the correct default values.
func (p *PBRSpecularGlossiness) MarshalJSON() ([]byte, error) {
type alias PBRSpecularGlossiness
out, err := json.Marshal(&struct{ *alias }{alias: (*alias)(p)})
if err == nil {
if p.GlossinessFactor == 1 {
out = removeProperty([]byte(`"glossinessFactor":1`), out)
}
if p.DiffuseFactor == [4]float64{1, 1, 1, 1} {
out = removeProperty([]byte(`"diffuseFactor":[1,1,1,1]`), out)
}
if p.SpecularFactor == [3]float64{1, 1, 1} {
out = removeProperty([]byte(`"specularFactor":[1,1,1]`), out)
}
out = sanitizeJSON(out)
}
return out, err
}