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 KirschiX · Jan 01, 2017 at 08:19 AM · c#shaderraycast

Ray picking objects with a 'Curved World' Shader

Hi

I am creating a game with a curved world and use the shader from https://alastaira.wordpress.com/2013/10/25/animal-crossing-curved-world-shader which works really fine. My problem is that Raycasts do not hit objects as their real position is a bit higher than their visual appearance (as they are warped down depending on distance).

How can I modify the mouse picking to account for this effect? Maybe a custom Projection Matrix for the Camera, but math like this is beyond my knowledge.

Any hints are appreciated.

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 tanoshimi · Jan 02, 2017 at 07:35 PM 0
Share

Hi. Yes, this is going to be a problem: the shader is an entirely visual effect - it has no effect on collider boundaries or raycasts, say - and it's performed in camera space. Adjusting for arbitrary linear raycasts in the world to match that would be very hard indeed.

What's your use case exactly? If you could reduce the problem domain slightly (say, you were only using raycasts based on the mouse cursor position, and hence already aligned to camera-view) it might be possible to come up with a suggestion.

avatar image KirschiX tanoshimi · Jan 03, 2017 at 08:47 AM 0
Share

Hi tanoshimi. I have a world with objects and units on it (basically a strategy game). The entire game area should be a planet, hence the curved world shader (I tried with a sphere object as a world before, but that is even more complicated with my use cases). Units are selected with the mouse and that is my problem at the moment. Is it possible to write a custom pick function where the ray is going in the inverse-curved direction as the shader? I know the curve function at least.

See "Populous The Beginning" images for the game that inspired me.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KirschiX · Jan 03, 2017 at 07:34 PM

After some thinking I came up with this solution by adjusting the original RaycastHit by the inverted curvature factor. Two downsides that are yet to be worked out are that it is not perfectly accurate as I had to tweak the curvature factor from 0.023f in shader to 0.03f in c# code. And clicking the "sky" is still picking terrain that is far off in the distance. But other than that it is working pretty good.

 private RaycastHit? adjustHit(RaycastHit hit)
 {
     var pt = hit.point;
     var dist = hit.distance;

     Vector4 vv = new Vector4(pt.x, pt.y, pt.z, 1.0f);

     vv.x -= Camera.main.transform.position.x;
     vv.y -= Camera.main.transform.position.y;
     vv.z -= Camera.main.transform.position.z;

     // Increase y by curvature factor (originally -m_Curvature)
     vv = new Vector4(0.0f, ((vv.z * vv.z) + (vv.x * vv.x)) * m_Curvature, 0.0f, 0.0f);

     var finalVec = new Vector3(pt.x, pt.y + vv.y, pt.z);

     // Send raycast again by finding screen pos of world position hit
     RaycastHit hit2;
     var screenPos = Camera.main.WorldToScreenPoint(finalVec);
     var ray = Camera.main.ScreenPointToRay(screenPos);
     if (Physics.Raycast(ray, out hit2, 100f))
     {
         return hit2;
     }

     return null;
 }
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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

getting center of rotated object 0 Answers

Reverse object position order 1 Answer

Changing material in code doesn't update shader 3 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