Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

javascript - calculate camera fov distance for sphere

I trying to calculate the max distance the camera can zoom in on a sphere while still displaying all of the sphere on screen.

var dist =  diameter / (2 * Math.tan( camera.fov * (Math.PI/180) / 2 ));

this zooms in to much. The top and bottom of the sphere are clipped. What am i doing wrong?

fiddle

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The sphere is being clipped for the same reason that if you stand close to a large sphere you can't see its "north pole".

You need to do the calculation for a plane that passes through the front of the sphere, not its center.

As a result, the quantity you calculated is the minimum distance to the front of the sphere. The minimum distance to its center is the quantity you calculated plus the sphere's radius.

(Note: this will yield a conservative approximation, due to the curvature of the sphere.)

EDIT: OK, here is the exact answer.

var dist =  radius / ( Math.sin( camera.fov * ( Math.PI / 180 ) / 2 ) );

fiddle: http://jsfiddle.net/x98Fk/3/

The three.js camera field-of-view is the vertical FOV, so this this the result for the vertical direction. If the window is narrower than it is wide, then you have to deal with that issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...