Skip to content

Commit

Permalink
fix default material values
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Feb 21, 2019
1 parent e1aa843 commit 4b60c7b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
22 changes: 11 additions & 11 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,6 @@ func (n *NormalTexture) MarshalJSON() ([]byte, error) {
return out, err
}

// NewOcclusionTexture returns a default OcclusionTexture.
func NewOcclusionTexture(index int32) *OcclusionTexture {
return &OcclusionTexture{Index: index, Strength: 1}
}

// An OcclusionTexture references to an occlusion texture
type OcclusionTexture struct {
Extensions Extensions `json:"extensions,omitempty"`
Expand All @@ -518,6 +513,11 @@ type OcclusionTexture struct {
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
Expand All @@ -537,8 +537,8 @@ func (o *OcclusionTexture) MarshalJSON() ([]byte, error) {
if o.Index == -1 {
out = removeProperty([]byte(`"index":-1`), out)
}
if o.Strength == -1 {
out = removeProperty([]byte(`"strength":-1`), out)
if o.Strength == 1 {
out = removeProperty([]byte(`"strength":1`), out)
}
out = sanitizeJSON(out)
}
Expand Down Expand Up @@ -577,11 +577,11 @@ 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.MetallicFactor == 1 {
out = removeProperty([]byte(`"metallicFactor":1`), out)
}
if p.RoughnessFactor == -1 {
out = removeProperty([]byte(`"roughnessFactor":-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)
Expand Down
8 changes: 4 additions & 4 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,9 @@ func TestOcclusionTexture_MarshalJSON(t *testing.T) {
want []byte
wantErr bool
}{
{"default", &OcclusionTexture{Index: -1, Strength: -1}, []byte(`{}`), false},
{"default", &OcclusionTexture{Index: -1, Strength: 1}, []byte(`{}`), false},
{"empty", &OcclusionTexture{Index: 0, Strength: 0}, []byte(`{"index":0,"strength":0}`), false},
{"nodefault", &OcclusionTexture{Index: 1, Strength: 1}, []byte(`{"index":1,"strength":1}`), false},
{"nodefault", &OcclusionTexture{Index: 1, Strength: 0.5}, []byte(`{"index":1,"strength":0.5}`), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -810,9 +810,9 @@ func TestPBRMetallicRoughness_MarshalJSON(t *testing.T) {
want []byte
wantErr bool
}{
{"default", &PBRMetallicRoughness{MetallicFactor: -1, RoughnessFactor: -1, BaseColorFactor: [4]float64{1, 1, 1, 1}}, []byte(`{}`), false},
{"default", &PBRMetallicRoughness{MetallicFactor: 1, RoughnessFactor: 1, BaseColorFactor: [4]float64{1, 1, 1, 1}}, []byte(`{}`), false},
{"empty", &PBRMetallicRoughness{MetallicFactor: 0, RoughnessFactor: 0, BaseColorFactor: [4]float64{0, 0, 0, 0}}, []byte(`{"baseColorFactor":[0,0,0,0],"metallicFactor":0,"roughnessFactor":0}`), false},
{"nodefault", &PBRMetallicRoughness{MetallicFactor: 1, RoughnessFactor: 1, BaseColorFactor: [4]float64{1, 0.5, 1, 1}}, []byte(`{"baseColorFactor":[1,0.5,1,1],"metallicFactor":1,"roughnessFactor":1}`), false},
{"nodefault", &PBRMetallicRoughness{MetallicFactor: 0.5, RoughnessFactor: 0.5, BaseColorFactor: [4]float64{1, 0.5, 1, 1}}, []byte(`{"baseColorFactor":[1,0.5,1,1],"metallicFactor":0.5,"roughnessFactor":0.5}`), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 4b60c7b

Please sign in to comment.