Skip to content

Commit

Permalink
add attribute name consts
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Mar 3, 2021
1 parent 0de79ce commit 8e88b2f
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 31 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ doc.Meshes = []*gltf.Mesh{{
Primitives: []*gltf.Primitive{{
Indices: gltf.Index(modeler.WriteIndices(doc, []uint16{0, 1, 2})),
Attributes: map[string]uint32{
"POSITION": modeler.WritePosition(doc, [][3]float32{{0, 0, 0}, {0, 10, 0}, {0, 0, 10}}),
"COLOR_0": modeler.WriteColor(doc, [][3]uint8{{255, 0, 0}, {0, 255, 0}, {0, 0, 255}}),
gltf.POSITION: modeler.WritePosition(doc, [][3]float32{{0, 0, 0}, {0, 10, 0}, {0, 0, 10}}),
gltf.COLOR_0: modeler.WriteColor(doc, [][3]uint8{{255, 0, 0}, {0, 255, 0}, {0, 0, 255}}),
},
}},
}}
Expand Down
12 changes: 12 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ var (
DefaultTranslation = [3]float32{0, 0, 0}
)

// Attribute names defined in the spec.
const (
POSITION = "POSITION"
NORMAL = "NORMAL"
TANGENT = "TANGENT"
TEXCOORD_0 = "TEXCOORD_0"
TEXCOORD_1 = "TEXCOORD_1"
WEIGHTS_0 = "WEIGHTS_0"
JOINTS_0 = "JOINTS_0"
COLOR_0 = "COLOR_0"
)

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}
Expand Down
6 changes: 3 additions & 3 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ 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}}}},
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}}}}},
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}}},
Samplers: []*Sampler{{WrapS: WrapRepeat, WrapT: WrapRepeat}},
Scene: Index(0),
Expand All @@ -69,7 +69,7 @@ func TestOpen(t *testing.T) {
{Perspective: &Perspective{AspectRatio: Float(1.0), Yfov: 0.7, Zfar: Float(100), Znear: 0.01}},
{Orthographic: &Orthographic{Xmag: 1.0, Ymag: 1.0, Zfar: 100, Znear: 0.01}},
},
Meshes: []*Mesh{{Primitives: []*Primitive{{Indices: Index(0), Mode: PrimitiveTriangles, Attributes: map[string]uint32{"POSITION": 1}}}}},
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}},
Expand All @@ -96,7 +96,7 @@ func TestOpen(t *testing.T) {
},
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)}}},
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}}}}},
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}},
Expand Down
4 changes: 2 additions & 2 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ func TestEncoder_Encode(t *testing.T) {
{"withMeshes", args{&Document{Meshes: []*Mesh{
{Extras: 8.0, Name: "mesh_1", Weights: []float32{1.2, 2}},
{Extras: 8.0, Name: "mesh_2", Primitives: []*Primitive{
{Extras: 8.0, Attributes: Attribute{"POSITION": 1}, Indices: Index(2), Material: Index(1), Mode: PrimitiveLines},
{Extras: 8.0, Targets: []Attribute{{"POSITION": 1, "THEN": 4}, {"OTHER": 2}}, Indices: Index(2), Material: Index(1), Mode: PrimitiveLines},
{Extras: 8.0, Attributes: Attribute{POSITION: 1}, Indices: Index(2), Material: Index(1), Mode: PrimitiveLines},
{Extras: 8.0, Targets: []Attribute{{POSITION: 1, "THEN": 4}, {"OTHER": 2}}, Indices: Index(2), Material: Index(1), Mode: PrimitiveLines},
}},
}}}, false},
{"withNodes", args{&Document{Nodes: []*Node{
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func ExampleSave() {
Name: "Default", AlphaMode: gltf.AlphaOpaque, AlphaCutoff: gltf.Float(0.5),
PBRMetallicRoughness: &gltf.PBRMetallicRoughness{BaseColorFactor: &[4]float32{0.8, 0.8, 0.8, 1}, MetallicFactor: gltf.Float(0.1), RoughnessFactor: gltf.Float(0.99)},
}},
Meshes: []*gltf.Mesh{{Name: "Cube", Primitives: []*gltf.Primitive{{Indices: gltf.Index(0), Material: gltf.Index(0), Mode: gltf.PrimitiveTriangles, Attributes: map[string]uint32{"POSITION": 1, "COLOR_0": 3, "NORMAL": 2, "TEXCOORD_0": 4}}}}},
Meshes: []*gltf.Mesh{{Name: "Cube", Primitives: []*gltf.Primitive{{Indices: gltf.Index(0), Material: gltf.Index(0), Mode: gltf.PrimitiveTriangles, Attributes: map[string]uint32{gltf.POSITION: 1, gltf.COLOR_0: 3, gltf.NORMAL: 2, gltf.TEXCOORD_0: 4}}}}},
Nodes: []*gltf.Node{
{Name: "RootNode", Children: []uint32{1, 2, 3}},
{Name: "Mesh"},
Expand Down
8 changes: 4 additions & 4 deletions modeler/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func Example() {
{
Indices: gltf.Index(indicesAccessor),
Attributes: map[string]uint32{
"POSITION": positionAccessor,
"COLOR_0": colorIndices,
gltf.POSITION: positionAccessor,
gltf.COLOR_0: colorIndices,
},
},
},
Expand All @@ -42,8 +42,8 @@ func ExampleWriteAccessorsInterleaved() {
{
Indices: gltf.Index(indicesAccessor),
Attributes: map[string]uint32{
"POSITION": indices[0],
"COLOR_0": indices[1],
gltf.POSITION: indices[0],
gltf.COLOR_0: indices[1],
},
},
},
Expand Down
18 changes: 9 additions & 9 deletions modeler/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,40 +254,40 @@ func WriteAttributesInterleaved(doc *gltf.Document, v Attributes) (map[string]ui
data []interface{}
)
if len(v.Position) != 0 {
props = append(props, attrProps{Name: "POSITION"})
props = append(props, attrProps{Name: gltf.POSITION})
data = append(data, v.Position)
}
if len(v.Normal) != 0 {
props = append(props, attrProps{Name: "NORMAL"})
props = append(props, attrProps{Name: gltf.NORMAL})
data = append(data, v.Normal)
}
if len(v.Tangent) != 0 {
props = append(props, attrProps{Name: "TANGENT"})
props = append(props, attrProps{Name: gltf.TANGENT})
data = append(data, v.Tangent)
}
if sliceLength(v.TextureCoord_0) != 0 {
normalized := checkTextureCoord(v.TextureCoord_0)
props = append(props, attrProps{Name: "TEXCOORD_0", Normalized: normalized})
props = append(props, attrProps{Name: gltf.TEXCOORD_0, Normalized: normalized})
data = append(data, v.TextureCoord_0)
}
if sliceLength(v.TextureCoord_1) != 0 {
normalized := checkTextureCoord(v.TextureCoord_1)
props = append(props, attrProps{Name: "TEXCOORD_1", Normalized: normalized})
props = append(props, attrProps{Name: gltf.TEXCOORD_1, Normalized: normalized})
data = append(data, v.TextureCoord_1)
}
if sliceLength(v.Weights) != 0 {
normalized := checkWeights(v.Weights)
props = append(props, attrProps{Name: "WEIGHTS_0", Normalized: normalized})
props = append(props, attrProps{Name: gltf.WEIGHTS_0, Normalized: normalized})
data = append(data, v.Weights)
}
if sliceLength(v.Joints) != 0 {
checkJoints(v.Joints)
props = append(props, attrProps{Name: "JOINTS_0"})
props = append(props, attrProps{Name: gltf.JOINTS_0})
data = append(data, v.Joints)
}
if sliceLength(v.Color) != 0 {
normalized := checkColor(v.Color)
props = append(props, attrProps{Name: "COLOR_0", Normalized: normalized})
props = append(props, attrProps{Name: gltf.COLOR_0, Normalized: normalized})
data = append(data, v.Color)
}
for _, c := range v.CustomAttributes {
Expand All @@ -306,7 +306,7 @@ func WriteAttributesInterleaved(doc *gltf.Document, v Attributes) (map[string]ui
attrs[prop.Name] = index
doc.Accessors[index].Normalized = prop.Normalized
}
if pos, ok := attrs["POSITION"]; ok {
if pos, ok := attrs[gltf.POSITION]; ok {
min, max := minMaxFloat32(v.Position)
doc.Accessors[pos].Min = min[:]
doc.Accessors[pos].Max = max[:]
Expand Down
20 changes: 10 additions & 10 deletions modeler/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ func TestWriteAttributesInterleaved(t *testing.T) {
t.Errorf("TestWriteAttributesInterleaved() accessors size = %v, want 10", len(doc.Accessors))
}
want := map[string]uint32{
"POSITION": 0,
"NORMAL": 1,
"TANGENT": 2,
"TEXCOORD_0": 3,
"TEXCOORD_1": 4,
"WEIGHTS_0": 5,
"JOINTS_0": 6,
"COLOR_0": 7,
"COLOR_1": 8,
"COLOR_2": 9,
gltf.POSITION: 0,
gltf.NORMAL: 1,
gltf.TANGENT: 2,
gltf.TEXCOORD_0: 3,
gltf.TEXCOORD_1: 4,
gltf.WEIGHTS_0: 5,
gltf.JOINTS_0: 6,
gltf.COLOR_0: 7,
"gltf.COLOR_1": 8,
"gltf.COLOR_2": 9,
}
if diff := deep.Equal(attrs, want); diff != nil {
t.Errorf("TestWriteAttributesInterleaved() = %v", diff)
Expand Down

0 comments on commit 8e88b2f

Please sign in to comment.