forked from qmuntal/gltf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
217 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package gltf | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestPBRSpecularGlossiness_UnmarshalJSON(t *testing.T) { | ||
type args struct { | ||
data []byte | ||
} | ||
tests := []struct { | ||
name string | ||
p *PBRSpecularGlossiness | ||
args args | ||
want *PBRSpecularGlossiness | ||
wantErr bool | ||
}{ | ||
{"default", new(PBRSpecularGlossiness), args{[]byte("{}")}, &PBRSpecularGlossiness{DiffuseFactor: [4]float64{1, 1, 1, 1}, SpecularFactor: [3]float64{1, 1, 1}, GlossinessFactor: 1}, false}, | ||
{"nodefault", new(PBRSpecularGlossiness), args{[]byte(`{"diffuseFactor": [0.1,0.2,0.3,0.4],"specularFactor":[0.5,0.6,0.7],"glossinessFactor":0.5}`)}, &PBRSpecularGlossiness{ | ||
DiffuseFactor: [4]float64{0.1, 0.2, 0.3, 0.4}, SpecularFactor: [3]float64{0.5, 0.6, 0.7}, GlossinessFactor: 0.5, | ||
}, false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := tt.p.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr { | ||
t.Errorf("PBRSpecularGlossiness.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(tt.p, tt.want) { | ||
t.Errorf("PBRSpecularGlossiness.UnmarshalJSON() = %v, want %v", tt.p, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestPBRSpecularGlossiness_MarshalJSON(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
p *PBRSpecularGlossiness | ||
want []byte | ||
wantErr bool | ||
}{ | ||
{"default", &PBRSpecularGlossiness{GlossinessFactor: 1, DiffuseFactor: [4]float64{1, 1, 1, 1}, SpecularFactor: [3]float64{1, 1, 1}}, []byte(`{}`), false}, | ||
{"empty", &PBRSpecularGlossiness{GlossinessFactor: 0, DiffuseFactor: [4]float64{0, 0, 0, 0}, SpecularFactor: [3]float64{0, 0, 0}}, []byte(`{"diffuseFactor":[0,0,0,0],"specularFactor":[0,0,0],"glossinessFactor":0}`), false}, | ||
{"nodefault", &PBRSpecularGlossiness{GlossinessFactor: 0.5, DiffuseFactor: [4]float64{1, 0.5, 1, 1}, SpecularFactor: [3]float64{1, 1, 0.5}}, []byte(`{"diffuseFactor":[1,0.5,1,1],"specularFactor":[1,1,0.5],"glossinessFactor":0.5}`), false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := tt.p.MarshalJSON() | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("PBRSpecularGlossiness.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("PBRSpecularGlossiness.MarshalJSON() = %v, want %v", string(got), string(tt.want)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestExtensions_UnmarshalJSON(t *testing.T) { | ||
type args struct { | ||
data []byte | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *Extensions | ||
wantErr bool | ||
}{ | ||
{"specularGlossiness", args{[]byte(`{"KHR_materials_pbrSpecularGlossiness": {"diffuseFactor":[1,0.5,1,1],"specularFactor":[1,1,0.5],"glossinessFactor":0.5}}`)}, &Extensions{ | ||
ExtPBRSpecularGlossiness: &PBRSpecularGlossiness{GlossinessFactor: 0.5, DiffuseFactor: [4]float64{1, 0.5, 1, 1}, SpecularFactor: [3]float64{1, 1, 0.5}}, | ||
}, false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ext := Extensions{} | ||
if err := ext.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr { | ||
t.Errorf("Extensions.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(&ext, tt.want) { | ||
t.Errorf("PBRSpecularGlossiness.MarshalJSON() = %v, want %v", &ext, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters