Joyetta
1
I draw a rectangle on the screen [32568-draw+rectangle.jpg|32568]. Is there anybody know how to make the camera zoom to the selected area or make the selected area full of screen? Thank you so much!!!
and I tried as this: math - Zoom to screen space selection box in 3D - Stack Overflow , but doesn’t work very well.
bhartu
2
hope this will help you…!!!
Here is a bit of code. My comments above still hold, so this is not a perfect solution. But it will rotate, translate, and zoom (by changing the FOV) to approximately the view specified by a screen rectangle. Note I’m assuming you are confining your selection rectangle to the aspect ratio of the screen. If you are not, there is more work to be done to figure out to figure out the correct zoom (since width rather than height can be a limiting factor for an arbitrary rectangle).
// cam - camera to use
// center - screen pixel center
// pixelHeight - height of the rectangle in pixels
// time - time to take zooming
function ZoomToDisplay(cam : Camera, center : Vector3, pixelHeight : float, time : float) {
var camTran = cam.transform;
var ray = cam.ScreenPointToRay(center);
var endRotation = Quaternion.LookRotation(ray.direction);
var endPosition = ProjectPointOnPlane(camTran.forward, camTran.position, ray.origin);
var opp = Mathf.Tan(cam.fieldOfView * 0.5 * Mathf.Deg2Rad);
opp *= pixelHeight / Screen.height;
var endFOV = Mathf.Atan(opp) * 2.0 * Mathf.Rad2Deg;
var timer = 0.0;
var startRotation = camTran.rotation;
var startFOV = cam.fieldOfView;
var startPosition = camTran.position;
while (timer <= 1.0) {
var t = Mathf.Sin(timer * Mathf.PI * 0.5);
camTran.rotation = Quaternion.Slerp(startRotation, endRotation, t);
camTran.position = Vector3.Lerp(startPosition, endPosition, t);
cam.fieldOfView = Mathf.Lerp(startFOV, endFOV, t);
timer += Time.deltaTime / time;
yield;
}
camTran.rotation = endRotation;
camTran.position = endPosition;
cam.fieldOfView = endFOV;
}
function ProjectPointOnPlane(planeNormal : Vector3 , planePoint : Vector3 , point : Vector3 ) : Vector3 {
planeNormal.Normalize();
var distance = -Vector3.Dot(planeNormal.normalized, (point - planePoint));
return point + planeNormal * distance;
}