Math behind Transform.TransformPoint?

So I need the functionality of Transform.TransformPoint and Transform.InverseTransformPoint inside a thread. I can’t call those functions directly since they are not thread safe.

I do know safely that the transform won’t change while the thread is running so I figured I could achieve the same results if I can figure out the math behind those two functions and do some magic with the transform.worldToLocal and transform.localToWorld matrix.

Unfortunately I’m not a magician, especially when it comes to complex vector and matrix math. Can anybody help me with this? I tried something like transform.localToWorldMatrix * p
but that doesn’t seem to produce the same results as transform.TransformPoint(p)

edit: actually that was not thread safe too, but that keeps me from just copying the Matrix4x4 values into the threads memory


Long story short, I’m trying to figure the formula behind the two transform functions and rebuild them in a thread safe method that I can use for my heavy calculations.

1 Answer

1

Well you almost solved your problem ^^. The problem here is that you most likely pass in a Vector3 position. However a 4x4 matrix represents a homogeneous coordinate space. A position / point / direction in homogeneous coordinates has 4 elements and not 3. Unity has implicit conversion operators for it’s Vector2 / 3 / 4 structs. So it implicitly converts your Vector3 into a Vector4. However the “w” coordinate is initialized to 0 in this case. This would turn your vector into a “direction” vector. Directions are not affected by the translation that the matrix might apply. So it only scales and rotates the vector.

To fix this you have to pass in a Vector4 with w set to “1”.

Vector3 pos;

Vector4 v = pos;
v.w = 1;
Vector4 wPos = localToWorld * v;

Keep in mind that you have to read the localToWorld property on the main thread. Once you have the matrix you can use it in another thread.

Another way is to use MultiplyPoint3x4. This would be probably the better solution ^^. It does the direct multiplication with the matrix and assumes that the matrix is a 3 by 4 matrix.

That's exactly what I've been looking for, thanks a lot for the great explanation! Made me read a little more about matrices in your crash course and some other sources and I think I understand the math behind it now. You are right, it's no black magic it makes sence! :D I always expected that matrices are not 'human readable' so I never made the effort to figure that out (and I back in school slacked on that topic I guess), I feel a lot smarter now :) codingskill+1 Code in the thread works as expected now, my problem is solved, I'm happy. cheers!

–