zeux / meshoptimizer

Mesh optimization library that makes meshes smaller and faster to render
MIT License
5.49k stars 473 forks source link

gltfpack: hope compressed model reusing attributes and indices bin data as the same as the origin model #726

Closed rarest closed 1 month ago

rarest commented 1 month ago

As mentioned in 723, in the origin gltf model, mesh[0] and mesh[1] have the same attributes data(buffer index 1, 2, 4, 5) and the same indices data(buffer index 0, 3), only materials are different.

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

After 724 fix, the scenes informations of meshopt compress model are correct, but the meshes data are not reused, actually attributes and indices bin data of meshes[0] and meshes[1] in the compressed model are repeated. buffer index (0, 1, 2, 3, 4, 5) vs buffer index ( 6, 7, 8, 9, 10, 11) have the same data.

"meshes": [{
    "primitives": [{
        "attributes": {
            "POSITION": 0,
            "NORMAL": 1
        },
        "indices": 2,
        "material": 0
    }, {
        "attributes": {
            "POSITION": 3,
            "NORMAL": 4
        },
        "indices": 5,
        "material": 1
    }]
}, {
    "primitives": [{
        "attributes": {
            "POSITION": 6,
            "NORMAL": 7
        },
        "indices": 8,
        "material": 2
    }, {
        "attributes": {
            "POSITION": 9,
            "NORMAL": 10
        },
        "indices": 11,
        "material": 3
    }]
}]

If vertex attributes and index data can be reused, the model size can be further reduced. Thanks!

origin.zip originMeshopt2.zip

rarest commented 1 month ago

@zeux Could you give me some advice how to share the attributes data between different meshes?

zeux commented 1 month ago

gltfpack currently works with mesh attributes independently so the attribute data isn’t shared; doing that for an arbitrary example requires implementing deduplication on an attribute level.

Note that instances like this can be represented using KHR_materials_variants extension; if the input file contains it, gltfpack will correctly preserve it without geometry duplication. For this to work the application would need to use variant switching instead of scene switching as there’s no way to encode “active” variant per scene.

rarest commented 1 month ago

gltfpack currently works with mesh attributes independently so the attribute data isn’t shared; doing that for an arbitrary example requires implementing deduplication on an attribute level.

Note that instances like this can be represented using KHR_materials_variants extension; if the input file contains it, gltfpack will correctly preserve it without geometry duplication. For this to work the application would need to use variant switching instead of scene switching as there’s no way to encode “active” variant per scene.

Thanks for the valuable advice, I'll look into the KHR_materials_variants extension first。 By the way, is there any plan to support attribute data sharing?

zeux commented 1 month ago

By the way, is there any plan to support attribute data sharing?

Not in the short term but it can probably happen in the future.