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 PeterJK · Jul 29, 2013 at 12:08 PM · cameraray cast

Ray casting in Unity, matching Unity camera parameters

I'm trying to do some ray casting in a pixel shader.

I'm doing this by drawing a screen aligned quad using an orthographic projection, but I would like the ray casting to match the camera parameters of my main Unity camera - a perspective camera with a 60 degree field of view.

I'm having trouble calculating my Ray Direction to match this.

Here is the pixel shader code I'm using.

 // fragment.hPos is output from the vertex shader - is the model-view-projection of the 
 // vertex coordinate
 float4 p1 = fragment.hPos / fragment.hPos.w;
 float screen_x = (p1.x + 1) * (_ScreenParams.x/2);
 float screen_y = (p1.y + 1) * (_ScreenParams.y/2);            
 // fragcoord is supposed to be the same as openGL's gl_FragCoord
 float2 fragcoord = float2(screen_x, screen_y);
 
 float i =fragcoord.x;
 float j = fragcoord.y;
 
 float fovx, fovy;
 fovx = 1.04719755; //60 degrees horizontal field of view
 fovy =  _ScreenParams.y /  _ScreenParams.x * fovx;
             
 //holds the x and y coords of the viewplane point
 x = 0;
 y = 0;
 x = (((2* i) - _ScreenParams.x) / _ScreenParams.x) * tan(fovx);
 y = (((2*j) - _ScreenParams.y) / _ScreenParams.y) * tan(fovy);
 
 // ro = ray origin, at the camera position
 ro = cameraPosition;
 float3 rd = float3(x,y,1.0) - ro; // rd = ray direction viewPlane is at x, y, 1.0
 rd = normalize(rd);

When I go on to use this ray origin (ro) and ray direction (rd) to ray cast the scene, the result doesn't match the scene view from a regular unity camera. Objects aren't in quite the same position and look 'stretched' vertically.

Anyone know how to do this correctly? I would greatly appreciate any guidance. Thank you!

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

0 Replies

· Add your reply
  • Sort: 

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

15 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

Related Questions

is it possible to have raycasts to cover the whole screen in one Update() time? 0 Answers

How to make camera position relative to a specific target. 1 Answer

Accessing all GameObjects with a certain tag 1 Answer

nullifying targetObject after a Smooth LookAt Transition 2 Answers

How to display a camera on the screen while displaying the Oculus Rift in direct mode ? 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