qmuntal / gltf

Go library for encoding glTF 2.0 files
https://www.khronos.org/gltf/
BSD 2-Clause "Simplified" License
241 stars 32 forks source link

gltf.Save() doesn't write buffer data #53

Closed paulmiller closed 2 years ago

paulmiller commented 2 years ago

I'm trying to create a mesh from scratch and save it. I started with the example code in the readme:

doc := gltf.NewDocument()
doc.Meshes = []*gltf.Mesh{{
    Name: "Pyramid",
    Primitives: []*gltf.Primitive{{
        Indices: gltf.Index(modeler.WriteIndices(doc, []uint16{0, 1, 2})),
        Attributes: map[string]uint32{
          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}}),
        },
    }},
}}

I added:

doc.Nodes = []*gltf.Node{{Name: "Pyramid", Mesh: gltf.Index(0)}}
gltf.Save(doc, "./test.gltf")

This output test.gltf is:

{"accessors":[{"bufferView":0,"componentType":5123,"count":3,"type":"SCALAR"},{"bufferView":1,"componentType":5126,"count":3,"type":"VEC3","max":[0,10,10],"min":[0,0,0]},{"bufferView":2,"componentType":5121,"normalized":true,"count":3,"type":"VEC3"}],"asset":{"generator":"qmuntal/gltf","version":"2.0"},"buffers":[{"byteLength":56}],"bufferViews":[{"buffer":0,"byteLength":6,"target":34963},{"buffer":0,"byteOffset":8,"byteLength":36,"target":34962},{"buffer":0,"byteOffset":44,"byteLength":12,"byteStride":4,"target":34962}],"meshes":[{"name":"Pyramid","primitives":[{"attributes":{"COLOR_0":2,"POSITION":1},"indices":0}]}],"nodes":[{"name":"Pyramid","mesh":0}],"scene":0,"scenes":[{"name":"Root Scene"}]}

The buffer here has "byteLength":56 but no "uri".

If instead of Save() I use SaveBinary(), the output test.glb does have data.

I'm using gltf v0.21.1 and go 1.17.

qmuntal commented 2 years ago

This is the same issue as #45 and #49. Clearly user expectations does not match with how this library encodes external buffers which are not base64-encoded nor have the URI property set, it's time for doing a better job here.

paulmiller commented 2 years ago

So I was using it wrong. Thanks!