Skip to content

Commit

Permalink
Merge pull request #94 from qmuntal/nil-buffer
Browse files Browse the repository at this point in the history
Return non-nil slice in ReadAccessor when bufferView and sparse are nil
  • Loading branch information
qmuntal authored Nov 12, 2024
2 parents aed1fc5 + aa9bbe9 commit 248c8f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
7 changes: 3 additions & 4 deletions modeler/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ var uint32Pool = sync.Pool{
// ReadAccessor is safe to use even with malformed documents.
// If that happens it will return an error instead of panic.
func ReadAccessor(doc *gltf.Document, acr *gltf.Accessor, buffer []byte) (any, error) {
if acr.BufferView == nil && acr.Sparse == nil {
return nil, nil
}
data, err := binary.MakeSliceBuffer(acr.ComponentType, acr.Type, acr.Count, buffer)
if err != nil {
return nil, err
Expand Down Expand Up @@ -119,8 +116,10 @@ var bufPool = sync.Pool{
func makeBufferOf[T any](count int, buffer []T) []T {
if len(buffer) < count {
buffer = append(buffer, make([]T, count-len(buffer))...)
} else {
} else if count != 0 {
buffer = buffer[:count]
} else if buffer == nil {
buffer = make([]T, 0)
}
return buffer
}
Expand Down
16 changes: 10 additions & 6 deletions modeler/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestReadAccessor(t *testing.T) {
want any
wantErr bool
}{
{"nodata", args{&gltf.Document{}, &gltf.Accessor{}}, nil, false},
{"nodata", args{&gltf.Document{}, &gltf.Accessor{}}, []float32{}, false},
{"base", args{&gltf.Document{Buffers: []*gltf.Buffer{
{ByteLength: 9, Data: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}},
}, BufferViews: []*gltf.BufferView{{
Expand Down Expand Up @@ -542,6 +542,9 @@ func TestReadPosition(t *testing.T) {
want [][3]float32
wantErr bool
}{
{"nil-bufferView", args{nil, &gltf.Accessor{
Type: gltf.AccessorVec3, ComponentType: gltf.ComponentFloat,
}, nil}, [][3]float32{}, false},
{"float32", args{[]byte{0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64}, &gltf.Accessor{
BufferView: gltf.Index(0), Count: 1, Type: gltf.AccessorVec3, ComponentType: gltf.ComponentFloat,
}, nil}, [][3]float32{{1, 2, 3}}, false},
Expand All @@ -557,13 +560,14 @@ func TestReadPosition(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
doc := &gltf.Document{
BufferViews: []*gltf.BufferView{
doc := new(gltf.Document)
if tt.args.data != nil {
doc.BufferViews = []*gltf.BufferView{
{Buffer: 0, ByteLength: len(tt.args.data)},
},
Buffers: []*gltf.Buffer{
}
doc.Buffers = []*gltf.Buffer{
{Data: tt.args.data, ByteLength: len(tt.args.data)},
},
}
}
got, err := modeler.ReadPosition(doc, tt.args.acr, tt.args.buffer)
if (err != nil) != tt.wantErr {
Expand Down

0 comments on commit 248c8f3

Please sign in to comment.