Skip to content

Commit

Permalink
Merge pull request #64 from qmuntal/embed
Browse files Browse the repository at this point in the history
Support binary glTF files where first buffer is not binary
  • Loading branch information
qmuntal committed Oct 6, 2022
2 parents 6914b8d + ccd6768 commit 7a8f8bb
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 2 deletions.
2 changes: 1 addition & 1 deletion decoder.go
Expand Up @@ -78,7 +78,7 @@ func (d *Decoder) Decode(doc *Document) error {
}

var externalBufferIndex = 0
if isBinary && len(doc.Buffers) > 0 {
if isBinary && len(doc.Buffers) > 0 && doc.Buffers[0].URI == "" {
externalBufferIndex = 1
if err := d.decodeBinaryBuffer(doc.Buffers[0]); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion encode.go
Expand Up @@ -111,7 +111,7 @@ func (e *Encoder) Encode(doc *Document) error {

for i := externalBufferIndex; i < len(doc.Buffers); i++ {
buf := doc.Buffers[i]
if len(buf.Data) == 0 || buf.URI == "" {
if len(buf.Data) == 0 || buf.URI == "" || buf.IsEmbeddedResource() {
continue
}
if err = e.encodeBuffer(buf); err != nil {
Expand Down
17 changes: 17 additions & 0 deletions gltf_test.go
@@ -1,6 +1,7 @@
package gltf

import (
"path/filepath"
"reflect"
"testing"
)
Expand Down Expand Up @@ -325,3 +326,19 @@ func TestSizeOfElement(t *testing.T) {
})
}
}

func TestIssue63(t *testing.T) {
srcDoc, err := Open("testdata/issue63/simple.gltf")
if nil != err {
t.Fatal(err)
}
outputGlb := filepath.Join(t.TempDir(), "out.glb")
err = SaveBinary(srcDoc, outputGlb)
if nil != err {
t.Fatal(err)
}
_, err = Open(outputGlb)
if err != nil {
t.Fatal(err)
}
}
102 changes: 102 additions & 0 deletions testdata/issue63/simple.gltf
@@ -0,0 +1,102 @@
{
"scenes": [
{
"nodes": [0, 1]
}
],
"nodes": [
{
"mesh": 0
},
{
"mesh": 0,
"name": "guid",
"matrix": [
2.0,
0.0,
0.0,
0.0,
0.0,
0.866,
0.5,
0.0,
0.0,
-0.25,
0.433,
0.0,
10.0,
20.0,
30.0,
1.0
]
}
],

"meshes": [
{
"primitives": [
{
"attributes": {
"POSITION": 1,
"NORMAL": 2
},
"indices": 0
}
]
}
],

"buffers": [
{
"uri": "data:application/octet-stream;base64,AAABAAIAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8=",
"byteLength": 80
}
],
"bufferViews": [
{
"buffer": 0,
"byteOffset": 0,
"byteLength": 6,
"target": 34963
},
{
"buffer": 0,
"byteOffset": 8,
"byteLength": 72,
"target": 34962
}
],
"accessors": [
{
"bufferView": 0,
"byteOffset": 0,
"componentType": 5123,
"count": 3,
"type": "SCALAR",
"max": [2],
"min": [0]
},
{
"bufferView": 1,
"byteOffset": 0,
"componentType": 5126,
"count": 3,
"type": "VEC3",
"max": [1.0, 1.0, 0.0],
"min": [0.0, 0.0, 0.0]
},
{
"bufferView": 1,
"byteOffset": 36,
"componentType": 5126,
"count": 3,
"type": "VEC3",
"max": [0.0, 0.0, 1.0],
"min": [0.0, 0.0, 1.0]
}
],

"asset": {
"version": "2.0"
}
}

0 comments on commit 7a8f8bb

Please sign in to comment.