I am creating a drag-selection box that selects an object once all of its vertices are within the selection box (just like in the Unity Scene Editor).
Currently, I am converting the vertices of the mesh to world space, then to camera space.
Vector3 worldVertex = mapObject.transform.TransformPoint(localVertex);
Vector2 screenVertex = Camera.main.WorldToScreenPoint(worldVertex);
But this is too slow when there are many objects on the screen.
I was hoping to create a transformation that can map local space coordinates to screen space directly.
My attempt:
//Calculate the transformation from local space to screen space
Matrix4x4 localToWorld = mapObject.transform.localToWorldMatrix;
Matrix4x4 worldToScreen = Camera.main.worldToCameraMatrix;
Matrix4x4 localToScreen = worldToScreen * localToWorld;
//Apply the transformation to the vertex
Vector2 screenVertex = localToScreen.MultiplyPoint(localVertex);
But from what I read, this doesn’t work due to how the worldToCamera matrix is implemented.
Is there a method of converting from local space to screen space more efficiently?
How can the camera perform these calculations so quickly when rendering?