Answers for "Matrix Transformation from Local Space to Screen Space"
http://answers.unity.com/questions/1719006/matrix-transformation-from-local-space-to-screen-s.html
The latest answers for the question "Matrix Transformation from Local Space to Screen Space"Answer by Namey5
http://answers.unity.com/answers/1719054/view.html
You're almost there with the matrices. There is one additional step when it comes to rendering, and that is the projection matrix (which will be either perspective or orthographic). This is what actually maps things to screen, and so if we multiply by that we will end up with coordinates in viewport space. Then, we can remap those to screen pixel coordinates;
//Cache this so we don't need to find the main camera every time
Camera cam = Camera.main;
Matrix4x4 localToWorld = mapObject.transform.localToWorldMatrix;
Matrix4x4 worldToView = cam.worldToCameraMatrix;
Matrix4x4 projection = cam.projectionMatrix;
//This transforms from object space to clip space, also known as the MVP matrix
Matrix4x4 localToViewport = projection * worldToView * localToWorld;
//This remaps from a range of [-1,1] to a range of [0,resolution] (i.e. screen space)
Matrix4x4 remap = Matrix4x4.TRS (new Vector3 (0.5f * cam.pixelWidth, 0.5f * cam.pixelHeight, 0f), Quaternion.identity, new Vector3 (0.5f * cam.pixelWidth, 0.5f * cam.pixelHeight, 1f));
//Finally, combine the matrices and transform the vertex
Matrix4x4 localToScreen = remap * localToViewport;
Vector2 screenVertex = localToScreen.MultiplyPoint (localVertex);
If you want this to be even faster, then rather than checking each vertex on the mesh, you can instead use the corners of it's bounding box;
//Get the (local space) mesh bounds and its minimum and maximum extents
Bounds bounds = mesh.bounds;
Vector3 max = bounds.max;
Vector3 min = bounds.min;
//The corners are simply all combinations of min and max
Vector3[] localCorners = new Vector3[8];
localCorners[0] = new Vector3 (max.x, max.y, max.z);
localCorners[1] = new Vector3 (min.x, max.y, max.z);
localCorners[2] = new Vector3 (max.x, min.y, max.z);
localCorners[3] = new Vector3 (min.x, min.y, max.z);
localCorners[4] = new Vector3 (max.x, max.y, min.z);
localCorners[5] = new Vector3 (min.x, max.y, min.z);
localCorners[6] = new Vector3 (max.x, min.y, min.z);
localCorners[7] = new Vector3 (min.x, min.y, min.z);
//Then do the same transformation, but with the corners instead
Vector2 screenVectex0 = localToScreen.MultiplyPoint (localCorners[0]);
//etc...Thu, 16 Apr 2020 09:30:11 GMTNamey5