三.js 正交相机:放大所有具有透视的立方体 [英] three.js orthographic camera: zoom all for a cube with perspective
问题描述
我开发了一个简单的 three.js
应用程序来渲染一个立方体.我创建了三个文件:index.html
、viewer_style.css
和 viewer.js
.
I have developed a simple three.js
application that renders a cube. I have created three files: index.html
, viewer_style.css
and viewer.js
.
index.html
的内容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>My first three.js app</title>
<link rel='stylesheet' type='text/css' href='viewer_style.css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="js/three.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script src="js/renderers/Projector.js"></script>
</head>
<body>
<script src="viewer.js"></script>
</body>
</html>
viewer.js
的内容如下:
// SCENE
var scene = new THREE.Scene();
// CAMERA
var frustumHeight;
var aspect = window.innerWidth / window.innerHeight;
var camera = new THREE.OrthographicCamera(- frustumHeight * aspect / 2, frustumHeight * aspect / 2, frustumHeight / 2, - frustumHeight / 2, 1, 2000 );
camera.position.x = 0;
camera.position.y = 0;
camera.position.z = 100;
// CUBE
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
var material = new THREE.MeshPhongMaterial({color: 0xbaf5e8, flatShading: true});
var cube = new THREE.Mesh( geometry, material );
cube.receiveShadow = true;
scene.add(cube);
// BOUNDING BOX
var helper_bbox = new THREE.BoxHelper(cube);
helper_bbox.update();
scene.add(helper_bbox);
// AXES
var helper_axes = new THREE.AxisHelper();
scene.add(helper_axes);
// FIT ALL:
var bbox_radius = helper_bbox.geometry.boundingSphere.radius;
if(aspect < 1){
frustumHeight = 2 * bbox_radius;
}
else{
frustumHeight = 2 * bbox_radius / aspect;
}
camera.left = - frustumHeight * aspect / 2;
camera.right = frustumHeight * aspect / 2;
camera.top = frustumHeight / 2;
camera.bottom = - frustumHeight / 2;
camera.updateProjectionMatrix();
// RENDERER
var renderer = new THREE.WebGLRenderer({alpha: true});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFShadowMap;
// LIGHTS
var ambientLight = new THREE.AmbientLight(0x101010, 1.0);
scene.add(ambientLight);
directionalLight = new THREE.DirectionalLight(0xffffff, 1.0);
directionalLight.position.set(1.0, 1.0, 1.0).normalize();
scene.add(directionalLight);
directionalLight_2 = new THREE.DirectionalLight(0xffffff, 1.0);
directionalLight_2.position.set(-1.0, -1.0, 1.0).normalize();
scene.add(directionalLight_2);
// CONTROLS
document.body.appendChild(renderer.domElement);
var controls = new THREE.OrbitControls(camera);
window.addEventListener( 'resize', onWindowResize, false );
function animate(){
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
function onWindowResize(){
var aspect = window.innerWidth / window.innerHeight;
camera.left = - frustumHeight * aspect / 2;
camera.right = frustumHeight * aspect / 2;
camera.top = frustumHeight / 2;
camera.bottom = - frustumHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
camera.lookAt(scene.position);
}
animate();
我打算使用立方体的边界球体的半径以一定的边距将立方体拟合到场景中.它似乎工作正常,如下图所示:
I intend to fit the cube to the scene with a certain margin using the radius of the bounding sphere of the cube. It seems to work correctly, as it can be seen in the following image:
但是,我将 viewer.js
中的相机位置更改为以下内容:
However, I changed the camera position in viewer.js
to the following:
camera.position.x = 100;
camera.position.y = 100;
camera.position.z = 100;
在这种情况下,我得到了这样的信息:
In this situation, I get something like this:
在这种情况下,立方体不适合屏幕.我认为这是因为我在错误的参考系统中测量边界球的半径.但是,我一直无法找到解决此问题的方法.
In this case, the cube is not fitted to the screen. I think this is due to the fact that I am measuring the radius of the bounding sphere in the wrong reference system. However, I have not been able to find a solution to this issue.
有谁知道我做错了什么?我是否使用了正确的方法?提前致谢.
Does anyone know what I am doing wrong? Am I using the right approach? Thanks in advance.
推荐答案
如果你改变 aspect < 你的代码是正确的1
到方面>1
,如建议的那样.这是您的代码的 jsfiddle 演示了这一点:https://jsfiddle.net/holgerl/kk5h39qa/一个>
Your code is correct if you change aspect < 1
to aspect > 1
, as suggested. Here is a jsfiddle with your code that demonstrates this: https://jsfiddle.net/holgerl/kk5h39qa/
这篇关于三.js 正交相机:放大所有具有透视的立方体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!