• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by AlexForTheWin · Apr 16, 2020 at 06:06 AM · mathtransformslinearalgebra

Matrix Transformation from Local Space to Screen Space

I am creating a drag-selection box that selects an object once all of its vertices are wit$$anonymous$$n 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 t$$anonymous$$s 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, t$$anonymous$$s 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?

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Namey5 · Apr 16, 2020 at 09:30 AM

You're almost there with the matrices. There is one additional step when it comes to rendering, and that is the projection matrix (w$$anonymous$$ch will be either perspective or orthograp$$anonymous$$c). T$$anonymous$$s is what actually maps t$$anonymous$$ngs 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 t$$anonymous$$s 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;
 
 //T$$anonymous$$s transforms from object space to clip space, also known as the MVP matrix
 Matrix4x4 localToViewport = projection * worldToView * localToWorld;
 
 //T$$anonymous$$s 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 t$$anonymous$$s 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...
Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlexForTheWin · Apr 16, 2020 at 10:07 PM 0
Share

This works perfectly! And its much faster than what I had originally. Thank you. I like the idea of using the 3D bounding box of the object since its orders of magnitude faster. But the 2D bounding box it produces is too inaccurate for my purposes. Instead, I think I will construct my own bounding box from the transformed screen points. If I still have performance issues, I'll try compute shaders.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to project a point on to a sphere? 1 Answer

How do you make an algebra function for this pattern? 1 Answer

How to lerp with a changing "from" value? 2 Answers

Not so much a scripting problem - Just need smart peoples' help. 2 Answers

Looking for a way to solve a 4th degree polynomial equation with constraints at runtime 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges