- Home /
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!
How are reflection probes calculating the projection? In shaders? $$anonymous$$aybe I can do a similiar calculation for a limited angle.
Sounds quite interesting, I would love to know how this would be possible.
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.
Your answer
Follow this Question
Related Questions
Having static objects in scene disables skybox reflections. 0 Answers
How to change default filter mode of cubemaps generated from reflection probes? 0 Answers
Different reflection cubemaps for different materials? 0 Answers
How to change the reflection according to skybox-material? 2 Answers
how to make Panorama hotspot creation between 2 panorama? 0 Answers