How to get the inverse of a transformation matrix that has a scale of zero?

Hello every one, I’m working on a project where I use a transformation matrix (translation, rotation and scale) to edit the vertices of a mesh. I also group vertices into parent child relationships. There are times where I need to go from a local space to a parent space so I usually use the inverse of my matrix to do that. This works most of the time except when I set the scale to 0 for x, y, or z then the inverse is always a zero matrix. Here is what I mean:

parentScale = Vector3.one;

// Create a transformation matrix for parent
Matrix4x4 M = Matrix4x4.TRS(parentTranslation, parentRotation, parentScale);

// Convert local point to parent point
Vector3 parentPoint = M.MultiplyPoint3x4(localPoint);

// Convert parent point back to local point
Vector3 originalPoint = M.inverse.MultiplyPoint3x4(parentPoint);

Does not work:

parentScale = new Vector3(1,1,0);

// Create a transformation matrix for parent
Matrix4x4 M = Matrix4x4.TRS(parentTranslation, parentRotation, parentScale);

// Convert local point to parent point
Vector3 parentPoint = M.MultiplyPoint3x4(localPoint);

// Convert parent point back to local point
Vector3 originalPoint = M.inverse.MultiplyPoint3x4(parentPoint);

// originalPoint is always Vector3.Zero

I notice that in unity the Transform.worldToLocalMatrix and Transform.localToWorldMatrix don’t have that issue. If you have a gameobjet on the scene, that is not a child of another object, then Transform.worldToLocalMatrix is the inverse of Transform.localToWorldMatrix when you have a non-zero scale. If you set the scale of your game object to zero then Transform.worldToLocalMatrix is not the inverse of Transform.localToWorldMatrix but is not a zero matrix either.

Does anyone know how unity generates Transform.worldToLocalMatrix or Transform.localToWorldMatrix or how to generate an inverse matrix that has zero scale?
Thanks in advance.

Hmm, I was just wondering what the fourth entry is, time? Either way I can only answer part of the question.

A matrix is invertible if the determinant is non zero. Which is usually not the case. But to get a zero scale you would have to multiply a [4 X 4] matrix by a [1 X 4] matrix filled with zero, which will give you a matrix with a scale of zero. I don’t think this is the problem.

I think the problem occurs when you try and invert a matrix with a zero scale because you probably end up with a scaled [4 X 4] matrix with a determinant that is equal to zero. So when computing the inverse of the matrix you end up trying to divide by zero. Which is why you don’t get an inverted matrix or a zero matrix.

I can’t help when it comes to figuring out how Unity calculates matrices.