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 /
avatar image
0
Question by Semni · Aug 29, 2015 at 05:29 PM · compute shaderscreenpointtoray

How do I implement ScreenPointToRay in a compute shader?

I've been scratching my head for a while trying to figure this one out. The nearest I can figure is that you take the XY of the screen position, feed it to the projection matrix and the worldToCameraMatrix and then magically you get the right direction of the ray, then you can derive the ray's origin from the camera position. Seems simple right? I cannot get that to work.

Nearest I have is something along the lines of:

 Matrix4x4 V = Camera.main.worldToCameraMatrix;
 Matrix4x4 P = Camera.main.projectionMatrix;
 Matrix4x4 InvVP = (P*V).inverse;

which then gets fed into the compute shader to a function like this:

 float3 ComputeWorldPosition(float3 screenPos)
  {
      // Transform pixel space to clipping space
      screenPos.x = 2.0f*screenPos.x - 1.0f;
      screenPos.y =  1.0f - 2.0f * screenPos.y;
      
      // Do inverse VP multiplication
      float4 worldPos = mul(float4(screenPos, 1), InvVP;
      
      // Calculate final world position
      return worldPos.xyz * worldPos.w;
  }

Screenpos x and y are always between 0 and 1 and z is always 0, this results in nonsensical output, I know I'm doing something wrong but I cannot figure out what.

Comment
Add comment · Show 2
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 cjdev · Aug 29, 2015 at 05:41 PM 0
Share

Why use projection matrices? Aren't you looking for the world coordinates from a ray going through an x,y screen coordinate? All you need is the output of Camera.ScreenPointToRay and use the point field from the RaycastHit data.

avatar image Semni · Aug 29, 2015 at 06:18 PM 0
Share

@cjdev: As far as I know within compute shaders there's no such thing as Camera.ScreenPointToRay.

In my application I am attempting to generate a LOT of rays, I need the paralelism of compute shaders.

That is exactly what I am looking for and I wish I could do that within Compute Shaders but I need to find a different solution.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by anasainea · Feb 11, 2021 at 09:18 PM

You can check this article, it goes through using the matrices available on the Camera of Unity to create rays for all pixles inside a compute shader

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

ScreenPointToRay gives unexpected results? 2 Answers

Get Vector3 of Position Clicked on Plane 1 Answer

ScreenToRayPoint not working in VR, how to substitute? 0 Answers

Can't get Math Functions to Work in Compute Shader 0 Answers

Compute shaders not working in editor but working on device 0 Answers


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