vpenades / SharpGLTF

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

System.ArgumentException: M44 (Parameter 'M44 0.9999999 must be equal to 1.') #150

Closed CKiOne closed 1 year ago

CKiOne commented 1 year ago

Hi, i set worldMatrix of a child node: image then I get following error: System.ArgumentException: M44 (Parameter 'M44 0.9999999 must be equal to 1.')

My values are: image and image

That should give me a matrix with 0 0 0 1 last row.

vpenades commented 1 year ago

I assume you're setting the world matrix of a child node.

glTF nodes don't have a world matrix, only a local matrix.

The World Matrix property exposed by SharpGLTF is built from the local matrices of the node chain.

In order to set the world matrix of a child node, internally, the library calculates the INVERSE matrix of the parent node, and multiplies it by the matrix you're setting. This is how the local matrix of the child node is evaluated. Most probably, there's some precission lost in these calculations and, since glTF specification does not allow any value other than 1, it throws an error.

I might look at it to see if I can handle this use case; in the meantime, instead of setting the world matrix, do the math steps yourself with the local matrix, fix the m44 to 1, and set the local matrix of the child node.

Another possibility, that might be odd, but not impossible, is that your matrix truly has a value of 0.999999~ , and it just happens that visual studio is rounding it to 1 when it's displayed in the debugger... if you force M44 to 1 just before setting the world matrix and it works, that will be the case.

in any case, I tried to create a matrix with the values you posted, and I couldn't reproduce the problem, most probably because the floating error is below the values displayed in visual studio so I would need some sort of test project that I could run to debug it myself.

CKiOne commented 1 year ago

The only place i'm setting the worldmatrix is here image

CKiOne commented 1 year ago

Matrix4x4.Invert(parentMatrix, out Matrix4x4 invertedParentMatrix)

image resolves to: image

vpenades commented 1 year ago

Can you dump your (the non inverted) parent matrix to bytes?

you can use System.BitConverter to get the bytes from a float.

vpenades commented 1 year ago

It should be fixed in the Alpha 27 nuget packages.

CKiOne commented 1 year ago

Oh, thank you, I will check it. Sorry, I was in holliday.