Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/qmuntal/gltf
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Jun 27, 2024
2 parents 1c0eee1 + fc65349 commit 3810d29
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
45 changes: 45 additions & 0 deletions modeler/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package modeler_test

import (
"testing"

"github.com/qmuntal/gltf"
"github.com/qmuntal/gltf/modeler"
)

func BenchmarkReadAccessorSparse(b *testing.B) {
doc := &gltf.Document{
Buffers: []*gltf.Buffer{{ByteLength: 284, Data: []byte{
0, 0, 8, 0, 7, 0, 0, 0, 1, 0, 8, 0, 1, 0, 9, 0, 8, 0, 1, 0, 2, 0, 9, 0,
2, 0, 10, 0, 9, 0, 2, 0, 3, 0, 10, 0, 3, 0, 11, 0, 10, 0, 3, 0, 4, 0, 11,
0, 4, 0, 12, 0, 11, 0, 4, 0, 5, 0, 12, 0, 5, 0, 13, 0, 12, 0, 5, 0, 6, 0,
13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 128, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 160, 64, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 192, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 0,
0, 0, 0, 0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 128, 63,
0, 0, 0, 0, 0, 0, 64, 64, 0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 128, 64, 0, 0, 128,
63, 0, 0, 0, 0, 0, 0, 160, 64, 0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 192, 64, 0, 0,
128, 63, 0, 0, 0, 0, 8, 0, 10, 0, 12, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0,
0, 0, 0, 0, 64, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 160, 64, 0, 0, 128, 64, 0, 0, 0, 0}}},
BufferViews: []*gltf.BufferView{
{Buffer: 0, ByteOffset: 72, ByteLength: 168},
{Buffer: 0, ByteOffset: 240, ByteLength: 6},
{Buffer: 0, ByteOffset: 248, ByteLength: 36},
},
}
acr := &gltf.Accessor{
BufferView: gltf.Index(0), ComponentType: gltf.ComponentFloat, Type: gltf.AccessorVec3, Count: 14,
Sparse: &gltf.Sparse{
Count: 3,
Indices: gltf.SparseIndices{BufferView: 1, ComponentType: gltf.ComponentUshort},
Values: gltf.SparseValues{BufferView: 2},
},
}
for i := 0; i < b.N; i++ {
_, err := modeler.ReadAccessor(doc, acr, nil)
if err != nil {
b.Fatal(err)
}
}
}
19 changes: 17 additions & 2 deletions modeler/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,24 @@ func ReadAccessor(doc *gltf.Document, acr *gltf.Accessor, buffer any) (any, erro
s := reflect.ValueOf(buffer)
ind := reflect.ValueOf(indices)
vals := reflect.ValueOf(values)
tp := reflect.TypeOf((*int)(nil)).Elem()
for i := 0; i < int(acr.Sparse.Count); i++ {
id := ind.Index(i).Convert(tp).Interface().(int)
var id int
idx := ind.Index(i)
if idx.CanInt() {
v := idx.Int()
if v < 0 || v >= int64(acr.Count) {
return nil, fmt.Errorf("gltf: sparse accessor index '%d' out of range: %d", i, v)
}
id = int(v)
} else if idx.CanUint() {
v := idx.Uint()
if v >= uint64(acr.Count) {
return nil, fmt.Errorf("gltf: sparse accessor index '%d' out of range: %d", i, v)
}
id = int(v)
} else {
return nil, fmt.Errorf("gltf: sparse accessor index '%d' is not an integer", i)
}
s.Index(id).Set(vals.Index(i))
}
}
Expand Down

0 comments on commit 3810d29

Please sign in to comment.