简体   繁体   English

根据宽度调整Three.js相机视场

[英]Adjust Three.js camera FOV based on width

The below standard Three.js resize code scales the scene in relationship to the DOM element's height. 下面的标准Three.js调整大小代码根据DOM元素的高度缩放场景。 If the element height decreases then the scene scales proportionally to keep the model in view. 如果元素高度减小,则场景将按比例缩放以保持模型可见。 However, when the element's width decreases the scene does not scale and the model gets cropped by the element. 但是,当元素的宽度减小时,场景将无法缩放,并且模型会被元素裁剪。 How can I have the scene scale with the element's width as it does with height? 如何使场景的缩放比例与元素的宽度以及高度一样?

window.onresize = function(event) {
    camera.aspect = $(threeContainer).width() / $(threeContainer).height();
    camera.updateProjectionMatrix();
    renderer.setSize($(threeContainer).width(), $(threeContainer).height());
};

The easiest way is to fake it. 最简单的方法是伪造它。

You'll want to set your canvas size based on the aspect ratio of your container. 您需要根据容器的纵横比设置画布大小。 Consider these aspect ratios: 考虑以下纵横比:

  • W = 800, H = 600, AR = 4/3 = 1.333 W = 800,H = 600,AR = 4/3 = 1.333
  • W = 500, H = 500, AR = 1 W = 500,H = 500,AR = 1
  • W = 600, H = 800, AR = 3/4 = 0.75 W = 600,H = 800,AR = 3/4 = 0.75

So if your aspect ratio is less than 1, you need to adjust the height of the canvas to compensate for the fact that it's a vertical FOV. 因此,如果长宽比小于1,则需要调整画布的高度以补偿它是垂直FOV的事实。

if(w/h < 1){
    renderer.setSize($(threeContainer).width(), $(threeContainer).height() * (w/h));
}

You'll also need to apply styling to renderer.domElement to center it vertically in threeContainer . 您还需要将样式应用于renderer.domElement ,使其在threeContainer垂直threeContainer This centering won't be noticeable when the container is wider, but will give it the appearance of scaling correctly when the container is taller. 当容器较宽时,此居中不会明显,但是当容器较高时,它将使缩放中心看起来正确缩放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM