• 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
1
Question by daterre · Jul 27, 2015 at 07:44 AM · unity 5reflectionskyboxcubemapprobes

Calculating position of projected object in reflection

For a puzzle game I'm working on, I need to track the position of the reflection of an object on a plane. I have a window in which the reflection of the moon is visible, and I would like to find the world space coordinates of the moon's projection on the reflective plane. My scene setup:

  • Window: an upright plane with a renderer that receives reflection probe information (Standard Shader)

  • Camera: perspective camera facing the window

  • Moon: a sphere behind the camera, facing the window

  • Reflection probe: realtime, refreshes every frame, at same position as camera

What I've been trying to do so far is to get the angle between the reflection vector and the camera. This gives me a rough indication of whether the moon is visible, but not a precise position which is what I need.

 public Transform window;
 public Transform moon;

 // Used in Update
 float moonWindowAngle()
 {
     Vector3 reflectionVec = Vector3.Reflect(window.position - moon.position, window.forward);
     Vector3 cameraVec = Camera.main.transform.position - window.position;
     float angle = Vector3.Angle(reflectionVec, cameraVec);
     return angle;
 }

I'm sure I need to somehow take into account the projection system (box/dome) and compare against a plane and not a single point, but I have no idea how to do this. Anything to set me on the right track greatly 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 daterre · Jul 29, 2015 at 10:30 AM 0
Share

How are reflection probes calculating the projection? In shaders? $$anonymous$$aybe I can do a similiar calculation for a limited angle.

avatar image TechSupportIncoming1 · Jul 29, 2015 at 10:31 AM 0
Share

Sounds quite interesting, I would love to know how this would be possible.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by zach-r-d · Jul 30, 2015 at 11:05 AM

It requires a some vector math but nothing too intense:

 public Transform window;
 public Transform moon;
 public Transform cam;
 
 Vector3? moonInWindowPos() {
     // Vector perpendicular to window, facing outward (change if necessary)
     Vector3 planeNormal = -window.forward;
 
     // Project moon position into plane the window lies in
     Vector3 projectedPos = window.position + Vector3.ProjectOnPlane(moon.position, planeNormal);
 
     // Subtract moon position from projection, to get the point of the moon "inside" the reflection
     Vector3 insideReflectionPos = projectedPos - moon.position;
 
     // Create ray from camera to "inside" point (it goes through the reflection point)
     Ray ray = new Ray(cam.position, (insideReflectionPos - cam.position).normalized);
 
     // Do ray-plane intersection test to see where the ray hits the plane
     float rayDist;
     if (new Plane(planeNormal, window.position).Raycast(ray, out rayDist)) {
         // The point along the ray that intersects the plane is the reflection position
         return ray.GetPoint(rayDist);
     } else {
         // If the intersection test fails, the camera is behind the window or something, and the point is undefined
         return null;
     }
 }

Note that you may also want to check if the point is actually visible to the camera.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

how to make Panorama hotspot creation between 2 panorama? 0 Answers

Why are my Smooth Textures Reflecting Blank Cubemap 0 Answers

Reflection probes and legacy shaders? 0 Answers

Reflection probes not blending smoothly 0 Answers

Bake Reflection Probe with Shuriken Particles? 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