Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return non-nil slice in ReadAccessor when bufferView and sparse are nil #94

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading