vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
454 stars 72 forks source link

Asset equality #169

Closed famda closed 1 month ago

famda commented 1 year ago

Hi, I'm trying to identity if 2 assets are exactly the same within an assembly. To explain a little better, I have an asset that contains a lot of smaller components in it and I'm trying to identify repeated components (the only thing that is different is the positioning). Apparently some components are being considered different (through file hashing) because some of the assessors have different bounds. As an example, I have 2 parts that are exactly the same in everything and they present differences between them:

image

image

What is the best approach to bypass this? Thanks in advance.

vpenades commented 1 year ago

Comparing assets is very tricky to say the least.

Regarding the mesh bounds:

My bet is that you're importing a glTF that was originally written using double precission. SharpGLTF uses single precission, so there might be rounding errors. This inconsistency comes from the fact that vertex positions are written as binary singles, meanwhile the bounds are writtten as json text which is usually double precission. So in this case, if you want to use bounds for comparison, at the very least you have to round them to single precission, or recalculate the bounds on your own from the vertices and discard the bounds information.

Personally, I believe the glTF specification of these bounds is ill designed because the inconsistencies between encoding the vertices in binary, and the bounds in text.

Regarding the Accessor count, again it all depends on how the original glTF was written, maybe it has more vertices than what's actually needed. or duplicated vertices. If you're using MeshBuilder, it removes unused vertices and degenerated triangles, and welds duplicated vertices.

Now, it all boils down to what do you undestand as "equality between assets", if you wast to check the actual data, you might find lots of inconsistencies, because writers mess up a lot even if the model apparently looks fine.

If I would be me I would do this to see if an asset is equal to another asset:

For every triangle in source mesh, check if the triangle exists in target mesh. And the triangle exists test must check all three triangle variations that are visually equivalent (A,B,C,) (B,C,A) (C,A,B)

It's a very expensive loop, but it's the only way to guarantee that both assets are visually equivalent, regardless of how the internal data is structured.