Graphics Reference
In-Depth Information
THREE.BoundingBoxHelper(cube);
helper.update();
// get the bounding sphere
var boundingSphere =
helper.box.getBoundingSphere();
// calculate the distance from the
center of the sphere
// and subtract the radius to get the
real distance.
var center = boundingSphere.center;
var radius = boundingSphere.radius;
var distance =
center.distanceTo(camera.position) -
radius;
var realHeight =
Math.abs(helper.box.max.y -
helper.box.min.y);
In the previous code snippet, we used
THREE.BoundingBoxHelper
to de-
termine the
realHeight
function of cube and its distance to the camera.
3. With this information, we can determine the field of view (
fov
) for the camera
so that it only shows the cube:
var fov = 2 * Math.atan(realHeight *
control.correctForDepth / (2 * distance))
* (180 / Math.PI);
What you can see in this code fragment is that we use one additional value,
which is
control.correctForDepth
, to calculate the field of view. This
value, which is set in the menu in the top-right section in the example, in-
creases the resulting field of view slightly. We do this because in this calcula-
tion, we assume that the camera is facing the object straight on. If the cam-
era isn't looking straight at the object, we need to compensate for this offset.
4. Now that we've got the field of view for the camera, we can assign this value
to the
camera.fov
property: