Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ttielu · Sep 28, 2010 at 02:17 PM · rayprojectionviewporttoworldpoint

Projection of viewport on plane

I am trying to determine the ray-projection of a screen position on a plane. The goal is to use this projection to make sure the camera's viewport stays inside a cube's scale (but that really isnt important).

I managed to determine the point where the ray touches the plane using line-plane intersection math. This is my code:

Ray ray = Camera.main.ViewportPointToRay(new Vector3(1,0,0)); // bottom right corner float distance = Vector3.Dot((plane.position - ray.origin),plane.up)/Vector3.Dot(ray.direction, plane.up); // distance = (p0 - l0) . n / l*n

Vector3 test = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distance)); // world point in bottom right corner, distance away from camera Vector3 test2 = ray.origin + distance*ray.direction; // ray projection with distance

In my opinion test should equal test2, right? But it isnt .. test2 is correct, test isnt. Why is this?

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
Best Answer

Answer by Loius · Sep 28, 2010 at 02:58 PM

I believe this is the same issue I had a few days ago, and I eventually stumbled across the fact that adding (distance) in to a Screen-To-Ray calculation actually chooses from a sphere of points around the camera, rather than a plane like I thought it should.

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
1

Answer by skovacs1 · Sep 28, 2010 at 04:49 PM

All Screen and Viewport functions perform camera projection according to the camera projection settings. If you want an orthographic projection, you would need to use an orthographic camera. By default cameras are set to perspective projection which will therefore result in a curvature around the camera. It's not entirely as Vicenti describes about choosing points on a sphere.

The ScreenToWorldPoint and ViewportToWorldPoint don't work as you expect. The z component defines a plane in world space away from the camera and the x and y coordinates are then projected as a ray and the intersection point is returned.

For example, create a camera at 0,0,-10 and a plane at 0,0,0 with a rotation of 270,0,0. If you were to create a ray and intersect it as you have done, the intersection point returned would be 10.9,-5.8,0. If you were to create a screen point at the same viewport x and y point 1,0 with a z component of 10 (the distance from the camera to the plane we created along camera's forward, not the distance to the intersection point), the point will be the same.

//The plane is 10 units away on camera.forward, parallel to the camera's view plane. Vector3 test = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, 10));

Ray ray = Camera.main.ViewportPointToRay(new Vector3(1,0,0)); // bottom right corner float distance = Vector3.Dot((plane.position - ray.origin),plane.up)/Vector3.Dot(ray.direction, plane.up); // distance = (p0 - l0) . n / l*n Vector3 test2 = ray.GetPoint(distance); // ray projection with distance

//test should = test2

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

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

No one has followed this question yet.

Related Questions

Ray direction, projection matrix 0 Answers

Help with shooting using raycast !!!!!! 2 Answers

How to get information from raycast? 2 Answers

Make tagged objects explode with ray cast? 1 Answer

Shooting at mousePosition 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