qmuntal / gltf

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

KhronosGroup/glTF-Validator reports errors on glb file #40

Closed orzel7 closed 2 years ago

orzel7 commented 3 years ago

KhronosGroup/glTF-Validator reports errors on glb file(see attachment test.glb.gz) generated by this package:

Error Message Pointer
GLB_CHUNK_LENGTH_UNALIGNED Length of 0x00000002 chunk is not aligned to 4-byte boundaries.  
GLB_CHUNK_TOO_BIG Chunk (0x00000002) length (1179937895) does not fit total GLB length.  
GLB_UNEXPECTED_END_OF_CHUNK_DATA Unexpected end of chunk data.  
Warning Message Pointer
GLB_UNKNOWN_CHUNK_TYPE Unknown GLB chunk type: 0x00000002.
qmuntal commented 3 years ago

I would need some more context to investigate this issue. Ideally a code snippet showing how this glb was created.

swarmt commented 2 years ago

Hey great library! I'm unable to create a simple gltf that parses for some reason.

Below example taken from the README.

Looks like the vertices and indices are missing?

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}}),
        },
    }},
}}

gltf.Save(doc, "./foo.gltf")
{
  "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
        }
      ]
    }
  ],
  "scene": 0,
  "scenes": [
    {
      "name": "Root Scene"
    }
  ]
}
orzel7 commented 2 years ago

You forget to wrap Mesh into the Node and add this Node to the Scene.

orzel7 commented 2 years ago

Something has changed in the meantime, i can't reproduce this problem again.

swarmt commented 2 years ago

For anyone else that ends up here, here is an example of adding the node to a scene.

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}}),
        },
    }},
}}
doc.Nodes = []*gltf.Node{{Name: "Root", Mesh: gltf.Index(0)}}
doc.Scenes[0].Nodes = append(doc.Scenes[0].Nodes, 0)

gltf.SaveBinary(doc, "output/test.glb")