Skip to content

Commit

Permalink
Merge pull request #76 from qmuntal/float64
Browse files Browse the repository at this point in the history
Use float64 for all json numeric properties
  • Loading branch information
qmuntal committed Jan 16, 2024
2 parents 21d46fa + 1cec3d6 commit 9e2616e
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 173 deletions.
14 changes: 7 additions & 7 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (

var (
// DefaultMatrix defines an identity matrix.
DefaultMatrix = [16]float32{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
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]float32{0, 0, 0, 1}
DefaultRotation = [4]float64{0, 0, 0, 1}
// DefaultScale defines a scaling that does not modify the size of the object.
DefaultScale = [3]float32{1, 1, 1}
DefaultScale = [3]float64{1, 1, 1}
// DefaultTranslation defines a translation that does not move the object.
DefaultTranslation = [3]float32{0, 0, 0}
DefaultTranslation = [3]float64{0, 0, 0}
)

// Attribute names defined in the spec.
Expand All @@ -31,9 +31,9 @@ const (
)

var (
emptyMatrix = [16]float32{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
emptyRotation = [4]float32{0, 0, 0, 0}
emptyScale = [3]float32{0, 0, 0}
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.
Expand Down
42 changes: 21 additions & 21 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ func TestOpen(t *testing.T) {
{args{"openError", ""}, nil, true},
{args{"testdata/Cube/glTF/Cube.gltf", ""}, &Document{
Accessors: []*Accessor{
{BufferView: Index(0), ByteOffset: 0, ComponentType: ComponentUshort, Count: 36, Max: []float32{35}, Min: []float32{0}, Type: AccessorScalar},
{BufferView: Index(1), ByteOffset: 0, ComponentType: ComponentFloat, Count: 36, Max: []float32{1, 1, 1}, Min: []float32{-1, -1, -1}, Type: AccessorVec3},
{BufferView: Index(2), ByteOffset: 0, ComponentType: ComponentFloat, Count: 36, Max: []float32{1, 1, 1}, Min: []float32{-1, -1, -1}, Type: AccessorVec3},
{BufferView: Index(3), ByteOffset: 0, ComponentType: ComponentFloat, Count: 36, Max: []float32{1, 0, 0, 1}, Min: []float32{0, 0, -1, -1}, Type: AccessorVec4},
{BufferView: Index(4), ByteOffset: 0, ComponentType: ComponentFloat, Count: 36, Max: []float32{1, 1}, Min: []float32{-1, -1}, Type: AccessorVec2}},
{BufferView: Index(0), ByteOffset: 0, ComponentType: ComponentUshort, Count: 36, Max: []float64{35}, Min: []float64{0}, Type: AccessorScalar},
{BufferView: Index(1), ByteOffset: 0, ComponentType: ComponentFloat, Count: 36, Max: []float64{1, 1, 1}, Min: []float64{-1, -1, -1}, Type: AccessorVec3},
{BufferView: Index(2), ByteOffset: 0, ComponentType: ComponentFloat, Count: 36, Max: []float64{1, 1, 1}, Min: []float64{-1, -1, -1}, Type: AccessorVec3},
{BufferView: Index(3), ByteOffset: 0, ComponentType: ComponentFloat, Count: 36, Max: []float64{1, 0, 0, 1}, Min: []float64{0, 0, -1, -1}, Type: AccessorVec4},
{BufferView: Index(4), ByteOffset: 0, ComponentType: ComponentFloat, Count: 36, Max: []float64{1, 1}, Min: []float64{-1, -1}, Type: AccessorVec2}},
Asset: Asset{Generator: "VKTS glTF 2.0 exporter", Version: "2.0"},
BufferViews: []*BufferView{
{Buffer: 0, ByteLength: 72, ByteOffset: 0, Target: TargetElementArrayBuffer},
Expand All @@ -45,9 +45,9 @@ func TestOpen(t *testing.T) {
},
Buffers: []*Buffer{{ByteLength: 1800, URI: "Cube.bin", Data: readFile("testdata/Cube/glTF/Cube.bin")}},
Images: []*Image{{URI: "Cube_BaseColor.png"}, {URI: "Cube_MetallicRoughness.png"}},
Materials: []*Material{{Name: "Cube", AlphaMode: AlphaOpaque, AlphaCutoff: Float(0.5), PBRMetallicRoughness: &PBRMetallicRoughness{BaseColorFactor: &[4]float32{1, 1, 1, 1}, MetallicFactor: Float(1), RoughnessFactor: Float(1), BaseColorTexture: &TextureInfo{Index: 0}, MetallicRoughnessTexture: &TextureInfo{Index: 1}}}},
Materials: []*Material{{Name: "Cube", AlphaMode: AlphaOpaque, AlphaCutoff: Float(0.5), PBRMetallicRoughness: &PBRMetallicRoughness{BaseColorFactor: &[4]float64{1, 1, 1, 1}, MetallicFactor: Float(1), RoughnessFactor: Float(1), BaseColorTexture: &TextureInfo{Index: 0}, MetallicRoughnessTexture: &TextureInfo{Index: 1}}}},
Meshes: []*Mesh{{Name: "Cube", Primitives: []*Primitive{{Indices: Index(0), Material: Index(0), Mode: PrimitiveTriangles, Attributes: map[string]uint32{NORMAL: 2, POSITION: 1, TANGENT: 3, TEXCOORD_0: 4}}}}},
Nodes: []*Node{{Mesh: Index(0), Name: "Cube", Matrix: [16]float32{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float32{0, 0, 0, 1}, Scale: [3]float32{1, 1, 1}}},
Nodes: []*Node{{Mesh: Index(0), Name: "Cube", 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}}},
Samplers: []*Sampler{{WrapS: WrapRepeat, WrapT: WrapRepeat}},
Scene: Index(0),
Scenes: []*Scene{{Nodes: []uint32{0}}},
Expand All @@ -57,8 +57,8 @@ func TestOpen(t *testing.T) {
}, false},
{args{"testdata/Cameras/glTF/Cameras.gltf", "testdata/Cameras/glTF-Embedded/Cameras.gltf"}, &Document{
Accessors: []*Accessor{
{BufferView: Index(0), ByteOffset: 0, ComponentType: ComponentUshort, Count: 6, Max: []float32{3}, Min: []float32{0}, Type: AccessorScalar},
{BufferView: Index(1), ByteOffset: 0, ComponentType: ComponentFloat, Count: 4, Max: []float32{1, 1, 0}, Min: []float32{0, 0, 0}, Type: AccessorVec3},
{BufferView: Index(0), ByteOffset: 0, ComponentType: ComponentUshort, Count: 6, Max: []float64{3}, Min: []float64{0}, Type: AccessorScalar},
{BufferView: Index(1), ByteOffset: 0, ComponentType: ComponentFloat, Count: 4, Max: []float64{1, 1, 0}, Min: []float64{0, 0, 0}, Type: AccessorVec3},
},
Asset: Asset{Version: "2.0"},
BufferViews: []*BufferView{
Expand All @@ -72,17 +72,17 @@ func TestOpen(t *testing.T) {
},
Meshes: []*Mesh{{Primitives: []*Primitive{{Indices: Index(0), Mode: PrimitiveTriangles, Attributes: map[string]uint32{POSITION: 1}}}}},
Nodes: []*Node{
{Mesh: Index(0), Matrix: [16]float32{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float32{-0.3, 0, 0, 0.9}, Scale: [3]float32{1, 1, 1}},
{Camera: Index(0), Matrix: [16]float32{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float32{0, 0, 0, 1}, Scale: [3]float32{1, 1, 1}, Translation: [3]float32{0.5, 0.5, 3.0}},
{Camera: Index(1), Matrix: [16]float32{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float32{0, 0, 0, 1}, Scale: [3]float32{1, 1, 1}, Translation: [3]float32{0.5, 0.5, 3.0}},
{Mesh: Index(0), Matrix: [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float64{-0.3, 0, 0, 0.9}, Scale: [3]float64{1, 1, 1}},
{Camera: Index(0), 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}, Translation: [3]float64{0.5, 0.5, 3.0}},
{Camera: Index(1), 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}, Translation: [3]float64{0.5, 0.5, 3.0}},
},
Scene: nil,
Scenes: []*Scene{{Nodes: []uint32{0, 1, 2}}},
}, false},
{args{"testdata/BoxVertexColors/glTF-Binary/BoxVertexColors.glb", ""}, &Document{
Accessors: []*Accessor{
{BufferView: Index(0), ByteOffset: 0, ComponentType: ComponentUshort, Count: 36, Type: AccessorScalar},
{BufferView: Index(1), ByteOffset: 0, ComponentType: ComponentFloat, Count: 24, Max: []float32{0.5, 0.5, 0.5}, Min: []float32{-0.5, -0.5, -0.5}, Type: AccessorVec3},
{BufferView: Index(1), ByteOffset: 0, ComponentType: ComponentFloat, Count: 24, Max: []float64{0.5, 0.5, 0.5}, Min: []float64{-0.5, -0.5, -0.5}, Type: AccessorVec3},
{BufferView: Index(2), ByteOffset: 0, ComponentType: ComponentFloat, Count: 24, Type: AccessorVec3},
{BufferView: Index(3), ByteOffset: 0, ComponentType: ComponentFloat, Count: 24, Type: AccessorVec4},
{BufferView: Index(4), ByteOffset: 0, ComponentType: ComponentFloat, Count: 24, Type: AccessorVec2},
Expand All @@ -96,21 +96,21 @@ func TestOpen(t *testing.T) {
{Buffer: 0, ByteLength: 192, ByteOffset: 1032, Target: TargetArrayBuffer},
},
Buffers: []*Buffer{{ByteLength: 1224, Data: readFile("testdata/BoxVertexColors/glTF-Binary/BoxVertexColors.glb")[1628+20+8:]}},
Materials: []*Material{{Name: "Default", AlphaMode: AlphaOpaque, AlphaCutoff: Float(0.5), PBRMetallicRoughness: &PBRMetallicRoughness{BaseColorFactor: &[4]float32{0.8, 0.8, 0.8, 1}, MetallicFactor: Float(0.1), RoughnessFactor: Float(0.99)}}},
Materials: []*Material{{Name: "Default", AlphaMode: AlphaOpaque, AlphaCutoff: Float(0.5), PBRMetallicRoughness: &PBRMetallicRoughness{BaseColorFactor: &[4]float64{0.8, 0.8, 0.8, 1}, MetallicFactor: Float(0.1), RoughnessFactor: Float(0.99)}}},
Meshes: []*Mesh{{Name: "Cube", Primitives: []*Primitive{{Indices: Index(0), Material: Index(0), Mode: PrimitiveTriangles, Attributes: map[string]uint32{POSITION: 1, COLOR_0: 3, NORMAL: 2, TEXCOORD_0: 4}}}}},
Nodes: []*Node{
{Name: "RootNode", Children: []uint32{1, 2, 3}, Matrix: [16]float32{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float32{0, 0, 0, 1}, Scale: [3]float32{1, 1, 1}},
{Name: "Mesh", Matrix: [16]float32{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float32{0, 0, 0, 1}, Scale: [3]float32{1, 1, 1}},
{Name: "Cube", Mesh: Index(0), Matrix: [16]float32{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float32{0, 0, 0, 1}, Scale: [3]float32{1, 1, 1}},
{Name: "Texture Group", Matrix: [16]float32{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float32{0, 0, 0, 1}, Scale: [3]float32{1, 1, 1}},
{Name: "RootNode", Children: []uint32{1, 2, 3}, 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}},
{Name: "Mesh", 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}},
{Name: "Cube", Mesh: Index(0), 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}},
{Name: "Texture Group", 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}},
},
Samplers: []*Sampler{{WrapS: WrapRepeat, WrapT: WrapRepeat}},
Scene: Index(0),
Scenes: []*Scene{{Name: "Root Scene", Nodes: []uint32{0}}},
}, false},
{args{"testdata/Box With Spaces/glTF/Box With Spaces.gltf", ""}, &Document{
Accessors: []*Accessor{
{BufferView: Index(0), ComponentType: ComponentFloat, Count: 24, Max: []float32{1, 1, 1}, Min: []float32{-1, -1, -1}, Type: AccessorVec3},
{BufferView: Index(0), ComponentType: ComponentFloat, Count: 24, Max: []float64{1, 1, 1}, Min: []float64{-1, -1, -1}, Type: AccessorVec3},
{BufferView: Index(1), ComponentType: ComponentFloat, Count: 24, Type: AccessorVec3},
{BufferView: Index(2), ComponentType: ComponentFloat, Count: 24, Type: AccessorVec2},
{BufferView: Index(3), ComponentType: ComponentUshort, Count: 36, Type: AccessorScalar},
Expand All @@ -130,10 +130,10 @@ func TestOpen(t *testing.T) {
},
Materials: []*Material{{
Name: "Material", AlphaMode: AlphaOpaque, AlphaCutoff: Float(0.5), NormalTexture: &NormalTexture{Index: Index(0), Scale: Float(1)}, PBRMetallicRoughness: &PBRMetallicRoughness{
BaseColorFactor: &[4]float32{1, 1, 1, 1}, MetallicFactor: Float(1), RoughnessFactor: Float(1), BaseColorTexture: &TextureInfo{Index: 1}, MetallicRoughnessTexture: &TextureInfo{Index: 2},
BaseColorFactor: &[4]float64{1, 1, 1, 1}, MetallicFactor: Float(1), RoughnessFactor: Float(1), BaseColorTexture: &TextureInfo{Index: 1}, MetallicRoughnessTexture: &TextureInfo{Index: 2},
}}},
Meshes: []*Mesh{{Name: "Cube", Primitives: []*Primitive{{Indices: Index(3), Material: Index(0), Mode: PrimitiveTriangles, Attributes: map[string]uint32{NORMAL: 1, POSITION: 0, TEXCOORD_0: 2}}}}},
Nodes: []*Node{{Mesh: Index(0), Name: "Cube", Matrix: [16]float32{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float32{0, 0, 0, 1}, Scale: [3]float32{1, 1, 1}}},
Nodes: []*Node{{Mesh: Index(0), Name: "Cube", 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}}},
Scene: Index(0),
Scenes: []*Scene{{Name: "Scene", Nodes: []uint32{0}}},
Textures: []*Texture{{Source: Index(0)}, {Source: Index(1)}, {Source: Index(2)}},
Expand Down
22 changes: 11 additions & 11 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ func (n *Node) UnmarshalJSON(data []byte) error {
func (n *Node) MarshalJSON() ([]byte, error) {
type alias Node
tmp := &struct {
Matrix *[16]float32 `json:"matrix,omitempty"` // A 4x4 transformation matrix stored in column-major order.
Rotation *[4]float32 `json:"rotation,omitempty" 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]float32 `json:"scale,omitempty"`
Translation *[3]float32 `json:"translation,omitempty"`
Matrix *[16]float64 `json:"matrix,omitempty"` // A 4x4 transformation matrix stored in column-major order.
Rotation *[4]float64 `json:"rotation,omitempty" 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,omitempty"`
Translation *[3]float64 `json:"translation,omitempty"`
*alias
}{
alias: (*alias)(n),
Expand Down Expand Up @@ -339,16 +339,16 @@ func (m *Material) UnmarshalJSON(data []byte) error {
func (m *Material) MarshalJSON() ([]byte, error) {
type alias Material
tmp := &struct {
EmissiveFactor *[3]float32 `json:"emissiveFactor,omitempty" validate:"dive,gte=0,lte=1"`
AlphaCutoff *float32 `json:"alphaCutoff,omitempty" validate:"omitempty,gte=0"`
EmissiveFactor *[3]float64 `json:"emissiveFactor,omitempty" validate:"dive,gte=0,lte=1"`
AlphaCutoff *float64 `json:"alphaCutoff,omitempty" validate:"omitempty,gte=0"`
*alias
}{
alias: (*alias)(m),
}
if m.AlphaCutoff != nil && *m.AlphaCutoff != 0.5 {
tmp.AlphaCutoff = m.AlphaCutoff
}
if m.EmissiveFactor != [3]float32{0, 0, 0} {
if m.EmissiveFactor != [3]float64{0, 0, 0} {
tmp.EmissiveFactor = &m.EmissiveFactor
}
return json.Marshal(tmp)
Expand All @@ -370,7 +370,7 @@ func (n *NormalTexture) MarshalJSON() ([]byte, error) {
type alias NormalTexture
if n.Scale != nil && *n.Scale == 1 {
return json.Marshal(&struct {
Scale float32 `json:"scale,omitempty"`
Scale float64 `json:"scale,omitempty"`
*alias
}{
Scale: 0,
Expand All @@ -396,7 +396,7 @@ func (o *OcclusionTexture) MarshalJSON() ([]byte, error) {
type alias OcclusionTexture
if o.Strength != nil && *o.Strength == 1 {
return json.Marshal(&struct {
Strength float32 `json:"strength,omitempty"`
Strength float64 `json:"strength,omitempty"`
*alias
}{
Strength: 0,
Expand All @@ -409,7 +409,7 @@ func (o *OcclusionTexture) MarshalJSON() ([]byte, error) {
// UnmarshalJSON unmarshal the pbr with the correct default values.
func (p *PBRMetallicRoughness) UnmarshalJSON(data []byte) error {
type alias PBRMetallicRoughness
tmp := alias(PBRMetallicRoughness{BaseColorFactor: &[4]float32{1, 1, 1, 1}, MetallicFactor: Float(1), RoughnessFactor: Float(1)})
tmp := alias(PBRMetallicRoughness{BaseColorFactor: &[4]float64{1, 1, 1, 1}, MetallicFactor: Float(1), RoughnessFactor: Float(1)})
err := json.Unmarshal(data, &tmp)
if err == nil {
*p = PBRMetallicRoughness(tmp)
Expand All @@ -431,7 +431,7 @@ func (p *PBRMetallicRoughness) MarshalJSON() ([]byte, error) {
if p.RoughnessFactor != nil && *p.RoughnessFactor == 1 {
tmp.RoughnessFactor = nil
}
if p.BaseColorFactor != nil && *p.BaseColorFactor == [4]float32{1, 1, 1, 1} {
if p.BaseColorFactor != nil && *p.BaseColorFactor == [4]float64{1, 1, 1, 1} {
tmp.BaseColorFactor = nil
}
return json.Marshal(tmp)
Expand Down
Loading

0 comments on commit 9e2616e

Please sign in to comment.