Point line leaves viewport (view space?)?

Im not sure I understand the term viewport totally(thinking of it as the volume of the 4 sided pyramid shown in edit mode when a camera is selected). Or is it just a rectangle/slice parallel to that pyramid?

If I understand correctly then how would I get the world point where a line, from 10 units directly in front of the camera, to somewhere out of the viewport hits the edge of the screen or viewport?

Just to reassure you, Tester, I intend to get back with some code for this soon…

Which direction? To the left or right, or both? Or do you mean you want to be able to find the intersect point in the direction of any point outside the viewing space (not necessarily immediately to the right or left)?

Doesn’t matter. I have a solution for you, but too tired tonite to write it/post it. Tomorrow should be a light work day, so I’ll post the code then.

Basically, you’re going to be solving for two separate triangles to determine the distance of the intersection with the edge of the frustum from your position. Once you have that, you just add ± half of your view angle to your transform’s rotation around Y-axis, and use sin and cos of that angle times the distance, and that’ll give you your point…I think.

Like I said, I’ll write it out in code for you tomorrow, and try and test it before I post it, but I test in excel b/c I have no access to Unity during the day.

Something like this might work. I really need to be working in Unity to test and fix, but, until then…

var camera : Camera;
function FrustumIntersect(origin : Vector3,targetPos : Vector3) : Vector3
{	
  viewAngle = Mathf.Deg2Rad*camera.fieldOfView*camera.aspect;
  var angleA = Mathf.Deg2Rad*Vector3.Angle(transform.position – origin,targetpos – origin);
  var angleB = Mathf.PI() – (viewAngle/2) – angleA;
  var distToIntersect = angleB*Mathf.Sin(angleA)/(Vector3.Distance(origin, transform.position));
  var intersect : Vector3;
  intersect.x = distToIntersect * Mathf.Cos(transform.localEulerAngles.y  + viewAngle/2 );
  intersect.y=origin.y;
  intersect.z = distToIntersect * Mathf.Sin(transform.localEulerAngles.y  + viewAngle/2 );
  return intersect;
}

This should find an intersect point to the left. The origin parameter is a point that is x units in front of camera (in your OP, you said 10 units, so origin would = transform.position + Vector3.forward*10). If you want to find one to the right, just change:

intersect.x = distToIntersect * Mathf.Cos(transform.localEulerAngles.y  + viewAngle/2 );
intersect.y=origin.y;
intersect.z = distToIntersect * Mathf.Sin(transform.localEulerAngles.y  + viewAngle/2 );

To:

intersect.x = distToIntersect * Mathf.Cos(transform.localEulerAngles.y  - viewAngle/2 );
intersect.y=origin.y;
intersect.z = distToIntersect * Mathf.Sin(transform.localEulerAngles.y  - viewAngle/2 );

I have no doubt there is a mathematical way to figure out whether it should be left or right and be able to work that into the function, but until even this bit proves to work, I wouldn’t worry about it.

Try it out. Andeee, if you have a chance, test this out and let me know how it breaks. I won’t be able to test it myself until late, late tonite.

Edit: One thought. I don’t know much about aspect ratio, but I assume an aspect ration of 2:1 would mean that, if a camera is set to 60 degree FOV, then the horizontal FOV would be 120. It’d be nice if it was that simple, but now I’m suspecting it isn’t. If not, then the code won’t work for sure.