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

Get error when save as gltf #45

Closed zbml closed 2 years ago

zbml commented 2 years ago
        doc := gltf.NewDocument()
    positionAccessor := modeler.WritePosition(doc, [][3]float32{{43, 43, 0}, {83, 43, 0}, {63, 63, 40}, {43, 83, 0}, {83, 83, 0}})
    indicesAccessor := modeler.WriteIndices(doc, []uint16{0, 1, 2, 3, 1, 0, 0, 2, 3, 1, 4, 2, 4, 3, 2, 4, 1, 3})
    colorIndices := modeler.WriteColor(doc, [][3]uint8{{50, 155, 255}, {0, 100, 200}, {255, 155, 50}, {155, 155, 155}, {0, 0, 0}})
    doc.Meshes = []*gltf.Mesh{{
        Name: "Pyramid",
        Primitives: []*gltf.Primitive{
            {
                Indices: gltf.Index(indicesAccessor),
                Attributes: map[string]uint32{
                    gltf.POSITION: positionAccessor,
                    gltf.COLOR_0:  colorIndices,
                },
            },
        },
    }}
    doc.Nodes = []*gltf.Node{{Name: "Root", Mesh: gltf.Index(0)}}
    doc.Scenes[0].Nodes = append(doc.Scenes[0].Nodes, 0)

    //if err := gltf.SaveBinary(doc, "./output/example.glb"); err != nil {
    //  panic(err)
    //}

    if err := gltf.Save(doc, "./output/example.gltf"); err != nil {
        panic(err)
    }

I got this code from here and use this code for learning how to use this package.

When I save this doc as a glb, it works fine. But not for saving as a gltf. When use saving as gltf code, I got "panic: gltf: Invalid buffer.uri value '' .

So I am wondering do I have to set a buffer for the document when save as gltf?

If I do, then why glb was ok?

qmuntal commented 2 years ago

When you write a non-binary glTF the buffers shall either be embedded, i.e. calling doc.Buffers[0].EmbeddedResource(), or have a valid URI, e.g. setting doc.Buffers[0].URI = "./foo.bin". If those invariants are not met, then gltf.Save returns an error as it doesn't know in which file to store each buffer.

This limitation does no apply to the first buffer of a GLB file because -following the glTF spec- it is written into the same GLB file unless it is embedded or has a URI.

zbml commented 2 years ago

@qmuntal thanks. That's what I need.