- Home /
Calculating InverseTransformPoint outside unity
Hi,
I need to calculate InverseTransformPoint of some points, but without using unity classes (multithreading issue). Do you know a code piece or a good straightforward reference that I can implement this function quickly?
In short:
GameObject g= new GameObject(); var result = g.transform.InverseTransformPoint(new Vector(1,2,3)); // Now, imagine doing this outside Unity.
What I want is the code that does InverseTransformPoint. Thank you.
I think it's called a map, to be facetious :P
Really though, I think you need to explain.
What is this multithreading issue you have? It'd be good to have the information in the QA
It's the fact that no unity object can be manipulated nor read from other threads. And whoever claims that they do, they really don't. They just dispatch it to the main thread, hence not really running on the other thread.
I'm guessing this also apply to static variables? It pretty much has to inject the value into the main thread in Async, right?
Answer by fafase · Oct 30, 2013 at 10:31 AM
You are trying to convert a world space into local space.
You need to define your local space matrix and multiply your point by that matrix.
Your local matrix is (if I remember right) the composite matrix of your new origin.
Yes but the main part is to fill up the values of the matrix. Depending if you are in 2D or 3D the matrix is different. Then you multiply scale rotation translation of the origin object and you get your composite matrix. $$anonymous$$ultiply your object composite matrix by the origin matrix and you get the object in local space.
So, I need to get g.transform.worldToLocal$$anonymous$$atrix and then multiply my point by that? (using meat5000's link)
Yes, all you need is the worldToLocal$$anonymous$$atrix, but keep in $$anonymous$$d to read the value synchronised and save a copy for your processing thread. Also the link above is about the $$anonymous$$atrix class of the .NET framework which is just a 3x3 matrix and only used for 2d transformations. That has nothing to do with Unity.
While it's true that most unity "objects" are not useable from other threads, most structs can be used without any problem besides the usual syncronising.
$$anonymous$$eep in $$anonymous$$d that even reading a variable from another thread can cause the content of that variable be corrupted due to caching.
So you need to read your worldToLocal$$anonymous$$atrix inside the Unity main thread and pass it syncronised to your thread.
Without more information about the actual task of your thread we can't suggest any alternatives. Hopefully you can decide if it's worth to do the calculations in a seperate thread. If it's something "quite small", the performance would actually decrease.
Thanks for the comment. It is not small. It is big, it is very very big. I know it sounds funny but I'm talking tens of thousands of geometry manipulations. $$anonymous$$ay be I'm better off creating them outside unity and bringing them back in once they're done.