3d - Making an object fit exactly inside the camera frustum in Three.Js -
i trying make object fit inside camera frustum, , went through trigonometry that, , code used.
var setupcamera = function() { aspectratio = window.innerwidth / window.innerheight camera = new three.perspectivecamera( 45, aspectratio, 1, 10000 ); scene.add(camera); } var updatecamera = function() { var height = mesh1_bb.max.y; var width = mesh1_bb.max.x - mesh1_bb.min.x; var vertical_fov = camera.fov * (math.pi/ 180); var max_z = mesh1_bb.max.z; var horizontal_fov = 2 * math.atan (math.tan (vertical_fov/2) * aspectratio); var distance_vertical = height / (2 * math.tan(vertical_fov/2)); // alert ('vertical' + distance_vertical); var distance_horizontal = width / (2 * math.tan(horizontal_fov/2)); // alert ('horizontal' + distance_horizontal); var z_distance = distance_vertical >= distance_horizontal? distance_vertical : distance_horizontal; camera.position.z = z_distance + max_z; camera.position.y = 0 ; camera.position.x = 0; }
while think calculation camera distance correct, result get:
i thought issue changing y position of camera, , put camera.position.y = height; get:
the result want following (which obtained panning right-click button of mouse , dragging until fitted whole canvas frame):
i hope can because has been driving me crazy day long ;-)
thanks lot!
i have not checked distance calculations, looking straight down z-axis, while object isn't vertically centered around 0. putting camera.y
@ mesh1_bb.max.y / 2
should fix that.
if don't want move camera, @ least point towards actual center of object. in case, using (axis aligned) bounding box isn't 100% correct anymore.
Comments
Post a Comment