Is there ANY way to have the three.js camera lookat being rendered off-center?

杀马特。学长 韩版系。学妹 提交于 2019-12-24 12:06:43

问题


Is there a way to setup the Three.js renderer in such a way that the lookat point of the camera is not in the center of the rendered image?

To clarify: image a scene with just one 1x1x1m cube at ( 0, 0, 0 ). The camera is located at ( 0, 0, 10 ) and the lookat point is at the origin, coinciding with the center of the cube. If I render this scene as is, I might end up with something like this:

normal render

However I'd like to be able to render this scene in such a way that the lookat point is in the upper left corner, giving me something like this:

desired render

If the normal image is 800x600, then the result I envision would be as if I rendered a 1600x1200 image with the lookat in the center and then cropped that normal image so that only the lower right part remains.

Of course, I can change the lookat to make the cube go to the upper left corner, but then I view the cube under an angle, giving me an undesired result like this:

test.moobels.com/temp/cube_angle.jpg

I could also actually render the full 1600x1200 image and hide 3/4 of the image, but one would hope there is a more elegant solution. Does anybody know it?


回答1:


If you want your perspective camera to have an off-center view, the pattern you need to use is:

camera = new THREE.PerspectiveCamera( for, aspect, near, far );
camera.setViewOffset( fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight );

See the docs: https://threejs.org/docs/#api/cameras/PerspectiveCamera

You can find examples of this usage in this example and this example.

three.js r.73




回答2:


Here's a simple solution:

Assuming your cube is 4 x 4 x 4, at position 0, 0, 0:

var geometry = new THREE.BoxGeometry( 4, 4, 4 );
var material = new THREE.MeshBasicMaterial( { color: 0x777777 } );
var cube = new THREE.Mesh( geometry, material );
cube.position.set( 0, 0, 0 );

Get cube's position:

var Vx = cube.position.x, 
    Vy = cube.position.y, 
    Vz = cube.position.z;

Then deduct by 2 from x position, then add 2 to y and z position, and use the values to create a new Vector3:

var newVx = Vx - 2,
    newVy = Vy + 2;
    newVz = Vz + 2;

var xyz = new THREE.Vector3(newVx, newVy, newVz)

Then camera lookAt:

camera.lookAt(xyz);   

Using console log, it would show that the camera is now looking at -2, 2, 2, which is the upper-left of your cube.

console.log(xyz);


来源:https://stackoverflow.com/questions/35092430/is-there-any-way-to-have-the-three-js-camera-lookat-being-rendered-off-center

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!