Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
2
Question by mathusummut · Nov 18, 2016 at 09:28 PM · camera viewportmultithreading

How do I use WorldToViewportPoint() and ViewportToWorldPoint() on separate thread?

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.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by LucaHofmann · Dec 03, 2017 at 12:42 PM

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 :)

Comment
Add comment · 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
0

Answer by BarShiftGames · Sep 27, 2020 at 01:20 AM

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

Comment
Add comment · 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 BarShiftGames · Nov 09, 2020 at 09:01 PM 0
Share

For future purposes, the correct code looks like this, but I have forgotten where I got it from, and this is only screen to worldToViewportPoint, I forget what you would do to invert this process. Coding format on this website doesn't work for me ever, so I have no idea what this will look like when I paste it. You can pull out bits and pieces to multithread, like pre-calculating the m4 matrix, since you can't pass camera references through threads.

 $$anonymous$$atrix4x4 m4 = cameraReference.projection$$anonymous$$atrix * cameraReference.transform.worldToLocal$$anonymous$$atrix;
 Vector4 p4 = <input vector3 point here>;
 p4.w = 1;
 Vector4 result4 = m4 * p4;
 Vector3 result = result4;
 result /= -result4.w;
 result.x = result.x / 2 + 0.5f;
 result.y = result.y / 2 + 0.5f;
 result.z = -result4.w;

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.

Follow this Question

Answers Answers and Comments

79 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

Related Questions

Multithreading - threads using main thread 0 Answers

Player transparency with FP/TP system 0 Answers

Resizing camera based on device 0 Answers

Suggestion Needed to Move Several Objects ( Logic only) 0 Answers

How to remove black bars on different game resolutions? 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