I am writing a script that works on a massive amount of particles, so I needed to implement multithreading. Everything works, except that whenever I call WorldToViewportPoint() on the main camera I get: “INTERNAL_CALL_WorldToViewportPoint can only be called from the main thread”, and the same happens when I try to use ViewportToWorldPoint().
So I’m kind of stuck. How can I emulate the functions using my own thread-safe implementation? I’m sure it’s possible, even if some values have to be hardcoded.
Just testing it out, @LucaHofmann your answer does NOT provide the same result as worldToViewportPoint, and I verified it on the same set of coordinates and got different outputs, even if I used multiplypoint3x4
Let’s assume you want to convert the transform.position to screen coordinates: screenSpace is what you want:
Matrix4x4 world2Screen = Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix;
Matrix4x4 screen2World = world2Screen.inverse;
Vector3 screenSpace = world2Screen.MultiplyPoint(transform.position);
Vector3 worldSpace = screen2World.MultiplyPoint(screenSpace);
The last line is to convert screen coordinates to world space. In this example converting it back makes no sense… it is just to show how it works and to check if it’s right 