Search Unity

Calculating position of projected object in reflection (reflection probes)

Discussion in 'Scripting' started by daterre, Jul 27, 2015.

  1. daterre

    daterre

    Joined:
    Jul 30, 2012
    Posts:
    41
    (Cross-posting this from Unity Answers, as this might be more suitable for discussion)

    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.

    Code (CSharp):
    1.  
    2. public Transform window;
    3. public Transform moon;
    4.  
    5. float moonWindowAngle()
    6. {
    7.     Vector3 reflectionVec = Vector3.Reflect(window.position - moon.position, window.forward);
    8.     Vector3 cameraVec = Camera.main.transform.position - window.position;
    9.     float angle = Vector3.Angle(reflectionVec, cameraVec);
    10.     return angle;
    11. }
    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!
     
    Last edited: Jul 27, 2015